> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gp.scale.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Helper Methods

> Access the currently active span and trace in your application's execution context.

The SGP Tracing SDK leverages Python's `contextvars` to automatically manage the active span and trace within your application's execution flow. This allows you to easily retrieve information about the current operation without explicitly passing objects around.

You can retrieve the currently active span or trace in the execution context using the following helper methods:

### `tracing.current_span()`

Returns the `Span` instance representing the operation currently active in the local execution context. This is typically the innermost span created by `tracing.create_span()`.

### `tracing.current_trace()`

Returns the `Trace` instance representing the overall workflow currently active in the local execution context. This will be the trace established by `tracing.create_trace()`.

### Example Usage

This example demonstrates how `current_span()` and `current_trace()` can be used to inspect the active context within nested operations:

```python {1,4-5} theme={null}
import scale_gp_beta.lib.tracing as tracing

def process_data_step():
    current_span_instance = tracing.current_span()
    current_trace_instance = tracing.current_trace()

    if current_span_instance:
        print(f"Current Span: {current_span_instance.name} (ID: {current_span_instance.span_id})")
    if current_trace_instance:
        print(f"Current Trace: (ID: {current_trace_instance.trace_id})")

    print("Processing data...")

def main_workflow_with_helpers():
    with tracing.create_trace("my_main_workflow"):
        print("Main workflow started.")
        with tracing.create_span("data_processing"):
            process_data_step()
```

### When are these methods useful?

* **Debugging:** Quickly inspect the current tracing context during development or when logging.
* **Contextual Logging:** Enrich log messages with `span_id` or `trace_id` without needing to pass them explicitly.
* **Conditional Logic:** Implement logic that depends on the existence or attributes of the current span/trace.

These methods are primarily for introspection and should not be used to create or manage the lifecycle of spans, which should be done via `create_span()` and `create_trace()`.
