github.com/grafana/pyroscope@v1.18.0/docs/sources/configure-client/trace-span-profiles/python-span-profiles.md (about) 1 --- 2 title: Span profiles with Traces to profiles for Python 3 menuTitle: Span profiles with Traces to profiles (Python) 4 description: Learn about and configure Span profiles with Traces to profiles in Grafana for Python applications. 5 weight: 104 6 --- 7 8 # Span profiles with Traces to profiles for Python 9 10 Span Profiles represent a major shift in profiling methodology, enabling deeper analysis of both tracing and profiling data. 11 Traditional continuous profiling provides an application-wide view over fixed intervals. 12 In contrast, Span Profiles delivers focused, dynamic analysis on specific execution scopes within applications, such as individual requests or specific trace spans. 13 14 This shift enables a more granular view of performance, enhancing the utility of profiles by linking them directly with traces for a comprehensive understanding of application behavior. As a result, engineering teams can more efficiently identify and address performance bottlenecks. 15 16 To learn more about Span Profiles, refer to [Combining tracing and profiling for enhanced observability: Introducing Span Profiles](/blog/2024/02/06/combining-tracing-and-profiling-for-enhanced-observability-introducing-span-profiles/). 17 18  19 20 Pyroscope integrates with distributed tracing systems supporting the [**OpenTelemetry**](https://opentelemetry.io/docs/languages/python/getting-started/) standard. 21 This integration lets you link traces with the profiling data and find resource usage for specific lines of code for your trace spans. 22 23 {{< admonition type="note" >}} 24 * Only CPU profiling is supported at the moment. 25 * Because of how sampling profilers work, spans shorter than the sample interval may not be captured. 26 {{< /admonition >}} 27 28 To use Span Profiles, you need to: 29 30 * [Configure Pyroscope to send profiling data](../../) 31 * Configure a client-side package to link traces and profiles: [Python](https://github.com/grafana/otel-profiling-python) 32 * [Configure the Tempo data source in Grafana or Grafana Cloud to discover linked traces and profiles](/docs/grafana-cloud/connect-externally-hosted/data-sources/tempo/configure-tempo-data-source/) 33 34 ## Before you begin 35 36 Your applications must be instrumented for profiling and tracing before you can use span profiles. 37 38 * Profiling: Your application must be instrumented with Pyroscope's Python instrumentation library. Refer to the [Python](../../language-sdks/python/) guide for instructions. 39 * Tracing: Your application must be instrumented with OpenTelemetry traces. Refer to the [OpenTelemetry](https://opentelemetry.io/docs/languages/python/getting-started/) guide for isntructions. 40 41 ## Configure the `pyroscope-otel` package 42 43 To start collecting Span Profiles for your Python application, you need to include [pyroscope-otel](https://github.com/grafana/otel-profiling-python) in your code. 44 45 This package provides a [`SpanProcessor`](https://github.com/open-telemetry/opentelemetry-python/blob/d213e02941039d4383abc3608b75404ce84725b1/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py#L85) implementation, which connects the two telemetry signals (traces and profiles) together. 46 47 ```shell 48 pip install pyroscope-otel 49 ``` 50 51 Next, create and register the `PyroscopeSpanProcessor`: 52 ```python 53 # import span processor 54 from pyroscope.otel import PyroscopeSpanProcessor 55 56 # obtain a OpenTelemetry tracer provider 57 from opentelemetry import trace 58 from opentelemetry.sdk.trace import TracerProvider 59 provider = TracerProvider() 60 61 # register the span processor 62 provider.add_span_processor(PyroscopeSpanProcessor()) 63 64 # register the tracer provider 65 trace.set_tracer_provider(provider) 66 ``` 67 68 With the span processor registered, spans created automatically (for example, HTTP handlers) and manually will have profiling data associated with them. 69 70 ## View the span profiles in Grafana Tempo 71 72 To view the span profiles in Grafana Tempo, you need to have a Grafana instance running and a data source configured to link traces and profiles. 73 74 Refer to the [data source configuration documentation](https://grafana.com/docs/grafana/<GRAFANA_VERSION>/datasources/tempo/configure-tempo-data-source) to see how to configure the visualization to link traces with profiles. 75 76 ## Examples 77 78 Check out these demo applications for span profiles: 79 - [Python example](https://github.com/grafana/pyroscope/tree/main/examples/tracing/python) 80 - [Other examples](https://github.com/grafana/pyroscope/tree/main/examples/tracing/tempo) in multiple languages