### Install Dependencies Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/metrics/reader/README.rst Installs the necessary dependencies for the examples. ```sh pip install -r requirements.txt ``` -------------------------------- ### Install SQLCommenter Example Dependencies Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/sqlcommenter/README.rst Installs the necessary Python packages for the sqlcommenter example, including OpenTelemetry SDK, OTLP exporter, MySQL instrumentation, and the MySQL connector. ```sh pip install opentelemetry-sdk \ opentelemetry-exporter-otlp-proto-grpc \ opentelemetry-instrumentation-mysql \ mysql-connector-python ``` -------------------------------- ### Start Redis Server Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opentracing/README.rst Starts the Redis server. Ensure Redis is installed on your system. ```sh redis-server ``` -------------------------------- ### Setup Virtual Environment Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/error_handler/README.rst Create and activate a new virtual environment for the global error handler example. This isolates dependencies. ```sh mkdir global_error_handler cd global_error_handler virtualenv global_error_handler source global_error_handler/bin/activate ``` -------------------------------- ### Run Flask Application Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opencensus-shim/README.rst Starts the Flask development server for the OpenCensus shim example application. ```sh flask --app app run -h 0.0.0.0 ``` -------------------------------- ### Install opentelemetry-proto-json Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-proto-json/README.rst Install the library using pip. ```bash pip install opentelemetry-proto-json ``` -------------------------------- ### Install pre-commit Hooks Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Install pre-commit and then run 'pre-commit install' to automatically run ruff and rstcheck before each commit. ```bash pip install pre-commit -c dev-requirements.txt pre-commit install ``` -------------------------------- ### Prometheus Exporter Usage Example Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/exporter/prometheus/prometheus.rst Example demonstrating how to initialize and use the Prometheus exporter to expose metrics. Ensure the 'prometheus_client' library is installed. ```python from prometheus_client import start_http_server from opentelemetry import metrics from opentelemetry.exporter.prometheus import PrometheusMetricReader from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.resources import SERVICE_NAME, Resource # Service name is required for most backends resource = Resource.create(attributes={ SERVICE_NAME: "your-service-name" }) # Start Prometheus client start_http_server(port=9464, addr="localhost") # Initialize PrometheusMetricReader which pulls metrics from the SDK # on-demand to respond to scrape requests reader = PrometheusMetricReader() provider = MeterProvider(resource=resource, metric_readers=[reader]) metrics.set_meter_provider(provider) ``` -------------------------------- ### Run Django with Auto Instrumentation and uWSGI Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst Starts the Django application using uWSGI and the `opentelemetry-instrument` command for auto-instrumentation. This example demonstrates integrating OpenTelemetry with a common production deployment setup. ```bash opentelemetry-instrument uwsgi --http :8000 --module instrumentation_example.wsgi ``` -------------------------------- ### Install Dependencies and Error Handlers Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/error_handler/README.rst Install the OpenTelemetry SDK and custom error handler packages. This prepares the environment for running the example. ```sh pip install opentelemetry-sdk git clone https://github.com/open-telemetry/opentelemetry-python.git pip install -e opentelemetry-python/docs/examples/error_handler/error_handler_0 pip install -e opentelemetry-python/docs/examples/error_handler/error_handler_1 ``` -------------------------------- ### Install All Packages and Dev Tools Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/AGENTS.md Installs all packages and development tools within the monorepo using uv. Ensure uv is installed and configured for workspaces. ```sh uv sync --frozen --all-packages ``` -------------------------------- ### Install Python Dependencies Separately Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opencensus-shim/README.rst Installs the necessary Python packages individually for the OpenCensus shim example. ```sh pip install \ opentelemetry-api \ opentelemetry-sdk \ opentelemetry-exporter-otlp \ opentelemetry-opencensus-shim \ opentelemetry-instrumentation-sqlite3 \ opencensus \ opencensus-ext-flask \ Flask ``` -------------------------------- ### Install OTLP HTTP Exporter Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-otlp-proto-http/README.rst Install the opentelemetry-exporter-otlp-proto-http package using pip. ```bash pip install opentelemetry-exporter-otlp-proto-http ``` -------------------------------- ### Install Zipkin Protobuf Exporter Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-zipkin-proto-http/README.rst Install the opentelemetry-exporter-zipkin-proto-http package using pip. ```bash pip install opentelemetry-exporter-zipkin-proto-http ``` -------------------------------- ### Install Dependencies Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/fork-process-model/flask-gunicorn/README.rst Installs project dependencies from a requirements file. ```sh pip install -rrequirements.txt ``` -------------------------------- ### Set up Docker Compose for Collector and Jaeger Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opencensus-exporter-tracer/README.rst Installs docker-compose and starts the OpenTelemetry collector and Jaeger services using a docker-compose configuration. ```sh pip install docker-compose cd docker docker-compose up ``` -------------------------------- ### Run an OpenTelemetry Example Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/basic_context/README.rst Executes a Python example script for OpenTelemetry context propagation. ```sh python .py ``` -------------------------------- ### Install tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Install the tox automation tool. This is a prerequisite for running development tasks. ```bash pip install tox ``` -------------------------------- ### Install OpenTracing Shim Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/shim/opentelemetry-opentracing-shim/README.rst Install the opentelemetry-opentracing-shim package using pip. ```bash pip install opentelemetry-opentracing-shim ``` -------------------------------- ### Install uWSGI Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst Installs the uWSGI server, which can be used to run Django applications in a more production-like environment with OpenTelemetry auto-instrumentation. ```bash pip install uwsgi ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst Sets up a dedicated virtual environment for the Django instrumentation example. This ensures dependencies are isolated. ```bash mkdir django_auto_instrumentation cd django_auto_instrumentation virtualenv django_auto_instrumentation source django_auto_instrumentation/bin/activate ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/metrics/prometheus-grafana/README.rst Prepare a dedicated virtual environment for the Prometheus instrumentation example. This ensures dependencies are isolated. ```bash mkdir prometheus_auto_instrumentation cd prometheus_auto_instrumentation virtualenv prometheus_auto_instrumentation source prometheus_auto_instrumentation/bin/activate ``` -------------------------------- ### Install opentelemetry-exporter-otlp-proto-grpc Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-otlp-proto-grpc/README.rst Install the OTLP gRPC exporter for Python using pip. This command installs the necessary package for exporting telemetry data. ```bash pip install opentelemetry-exporter-otlp-proto-grpc ``` -------------------------------- ### Run the OpenTracing Shim Example Application Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opentracing/README.rst Executes the main Python script for the example. This script calculates Fibonacci numbers, stores them in Redis, and generates traces that can be viewed in Jaeger. ```sh python main.py ``` -------------------------------- ### Sync Dependencies with uv Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Use the 'uv sync' command to create a virtual environment in the .venv directory and install all necessary dependencies. ```sh uv sync ``` -------------------------------- ### Install OpenTelemetry Python SDK and Exporters Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/multi-destination-exporting/README.rst Installs the necessary OpenTelemetry packages for API, SDK, and OTLP exporters (gRPC and HTTP). Also installs the logging instrumentation for the LoggingHandler. ```sh pip install opentelemetry-api pip install opentelemetry-sdk pip install opentelemetry-exporter-otlp-proto-grpc pip install opentelemetry-exporter-otlp-proto-http pip install opentelemetry-instrumentation-logging # For LoggingHandler ``` -------------------------------- ### Install tox-uv Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Install the tox-uv plugin for enhanced tox environment management. This is optional but recommended for improved performance. ```bash pip install tox-uv ``` -------------------------------- ### Install opentelemetry-propagator-b3 Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/propagator/opentelemetry-propagator-b3/README.rst Install the B3 propagator package using pip. ```bash pip install opentelemetry-propagator-b3 ``` -------------------------------- ### Install OpenTelemetry Python API Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/README.rst Install the opentelemetry-api package using pip. ```bash pip install opentelemetry-api ``` -------------------------------- ### Install OpenCensus Exporter Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-opencensus/README.rst Installs the opentelemetry-exporter-opencensus package using pip. ```bash pip install opentelemetry-exporter-opencensus ``` -------------------------------- ### Install OpenCensus Shim Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/shim/opentelemetry-opencensus-shim/README.rst Install the OpenCensus Shim for OpenTelemetry using pip. ```bash pip install opentelemetry-opencensus-shim ``` -------------------------------- ### Execute Python Metrics Example Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/metrics/instruments/README.rst Command to run the Python example script that generates and exports metrics. ```sh $ python example.py ``` -------------------------------- ### Clone and Setup OpenTelemetry Python Repository Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Clone the opentelemetry-python repository and set up the development environment with tox and tox-uv. ```bash git clone https://github.com/open-telemetry/opentelemetry-python.git cd opentelemetry-python ``` ```bash pip install tox tox-uv ``` -------------------------------- ### Install opentelemetry-exporter-otlp-proto-common Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-otlp-proto-common/README.rst Install the library using pip. This package provides Protobuf encoding capabilities for OpenTelemetry. ```bash pip install opentelemetry-exporter-otlp-proto-common ``` -------------------------------- ### Install OpenTelemetry API and SDK Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/basic_context/README.rst Installs the necessary OpenTelemetry API and SDK packages using pip. ```sh pip install opentelemetry-api pip install opentelemetry-sdk ``` -------------------------------- ### Install OpenTelemetry Zipkin Exporter Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-zipkin/README.rst Install the opentelemetry-exporter-zipkin package using pip. This command installs the core exporter, which may include JSON and Proto HTTP support. ```bash pip install opentelemetry-exporter-zipkin ``` -------------------------------- ### Install Zipkin JSON Exporter Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-zipkin-json/README.rst Install the opentelemetry-exporter-zipkin-json package using pip. ```bash pip install opentelemetry-exporter-zipkin-json ``` -------------------------------- ### Run the OpenCensus Exporter Example Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opencensus-exporter-tracer/README.rst Executes the Python script that utilizes the OpenCensus exporter to send traces. ```sh python collector.py ``` -------------------------------- ### Example Execution Output Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/error_handler/README.rst Illustrates the expected output when running the error handler example script, showing various exceptions being handled. This output demonstrates the behavior of custom and default error handlers. ```pytb ErrorHandler0 handling a ZeroDivisionError Traceback (most recent call last): File "test.py", line 5, in 1 / 0 ZeroDivisionError: division by zero ErrorHandler1 handling an IndexError Traceback (most recent call last): File "test.py", line 11, in [1][2] IndexError: list index out of range ErrorHandler1 handling a KeyError Traceback (most recent call last): File "test.py", line 17, in {1: 2}[2] KeyError: 2 Error handled by default error handler: Traceback (most recent call last): File "test.py", line 23, in assert False AssertionError No error raised ``` -------------------------------- ### Install OpenTelemetry and Django Dependencies Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst Installs the necessary OpenTelemetry SDK, Django instrumentation package, and the requests library for making client calls. ```bash pip install opentelemetry-sdk pip install opentelemetry-instrumentation-django pip install requests ``` -------------------------------- ### Install Jaeger Propagator Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/propagator/opentelemetry-propagator-jaeger/README.rst Install the opentelemetry-propagator-jaeger package using pip. ```bash pip install opentelemetry-propagator-jaeger ``` -------------------------------- ### Install opentelemetry-proto Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-proto/README.rst Install the opentelemetry-proto library using pip. This command is used to add the generated protobuf code to your Python project. ```bash pip install opentelemetry-proto ``` -------------------------------- ### Install Prometheus Exporter Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/exporter/prometheus/prometheus.rst Install the OpenTelemetry Prometheus Exporter package using pip. ```bash pip install opentelemetry-exporter-prometheus ``` -------------------------------- ### Run Multi-Destination Exporting Examples Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/multi-destination-exporting/README.rst Executes the example scripts for multi-destination exporting of traces, metrics, and logs. The output will appear in the console for console exporters, and OTLP destinations require a running collector. ```sh python multi_destination_traces.py python multi_destination_metrics.py python multi_destination_logs.py ``` -------------------------------- ### Example MySQL Log Entry with SQLComment Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/sqlcommenter/README.rst Shows a sample log entry from the MySQL general log, demonstrating how sqlcommenter appends contextual information to SQL queries. ```sql 2025-09-02T18:49:06.981980Z 186 Query SELECT * FROM authors WHERE id = 1 /*db_driver='mysql.connector%%3A9.4.0',dbapi_level='2.0',dbapi_threadsafety=1,driver_paramstyle='pyformat',mysql_client_version='9.4.0',traceparent='00-2c45248f2beefdd9688b0a94eb4ac9ee-4f3af9a825aae9b1-01'*/ ``` -------------------------------- ### Install Python Dependencies (Separately) Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opentracing/README.rst Installs the necessary Python packages individually. Useful if you prefer to manage dependencies one by one or if requirements.txt is not available. ```sh pip install \ opentelemetry-api \ opentelemetry-sdk \ opentelemetry-exporter-otlp \ opentelemetry-opentracing-shim \ redis \ redis_opentracing ``` -------------------------------- ### Install OpenTelemetry Collector Exporters Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-otlp/README.rst Use this command to install the main OpenTelemetry Collector Exporters package, which includes support for proto-grpc and proto-http. ```bash pip install opentelemetry-exporter-otlp ``` -------------------------------- ### Install opentelemetry-exporter-otlp-json-common Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/exporter/opentelemetry-exporter-otlp-json-common/README.rst Install the library using pip. This command adds the necessary package to your Python environment for OTLP JSON encoding. ```bash pip install opentelemetry-exporter-otlp-json-common ``` -------------------------------- ### Install OpenCensus Exporter Dependencies Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opencensus-exporter-tracer/README.rst Installs the necessary OpenTelemetry API, SDK, and OpenCensus exporter packages using pip. ```sh pip install opentelemetry-api pip install opentelemetry-sdk pip install opentelemetry-exporter-opencensus ``` -------------------------------- ### Install OpenTelemetry Python SDK Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/README.rst Install the opentelemetry-sdk package using pip. This command is used to add the SDK to your Python project. ```bash pip install opentelemetry-sdk ``` -------------------------------- ### Install OpenTelemetry Propagator Package Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/README.md Installs a specific OpenTelemetry propagator package. Replace '{propagator}' with the desired propagator name (e.g., 'b3', 'tracecontext'). ```sh pip install opentelemetry-propagator-{propagator} ``` -------------------------------- ### Changelog Entry Example Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Write a concise, imperative, one-line description for the changelog entry, prefixed with the affected package name if applicable. ```markdown `opentelemetry-sdk`: add support for new feature ``` -------------------------------- ### Run Instrumented SQLCommenter Example Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/sqlcommenter/README.rst Executes the Python script that instruments mysql-connector calls with sqlcommenter features enabled. ```sh python instrumented_query.py ``` -------------------------------- ### Install OpenTelemetry Exporter Package Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/README.md Installs a specific OpenTelemetry exporter package. Replace '{exporter}' with the desired exporter name (e.g., 'otlp', 'jaeger'). ```sh pip install opentelemetry-exporter-{exporter} ``` -------------------------------- ### Install OpenTelemetry Semantic Conventions Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-semantic-conventions/README.rst Install the library using pip. This command is used to add the semantic conventions package to your Python project. ```bash pip install opentelemetry-semantic-conventions ``` -------------------------------- ### Editable Install for Development Versions Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/README.md Installs development versions of OpenTelemetry API, SDK, and semantic conventions packages in editable mode. This is useful for contributing to the project. ```sh pip install -e ./opentelemetry-api -e ./opentelemetry-sdk -e ./opentelemetry-semantic-conventions ``` -------------------------------- ### Example Collector Output for Logs Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/logs/README.rst Sample output from the OpenTelemetry Collector showing processed logs with resource and scope information. ```sh ResourceLog #0 Resource SchemaURL: Resource attributes: -> telemetry.sdk.language: Str(python) -> telemetry.sdk.name: Str(opentelemetry) -> telemetry.sdk.version: Str(1.33.0.dev0) -> service.name: Str(shoppingcart) -> service.instance.id: Str(instance-12) ScopeLogs #0 ScopeLogs SchemaURL: InstrumentationScope myapp.area2 LogRecord #0 ObservedTimestamp: 2025-04-22 12:16:57.315179 +0000 UTC Timestamp: 2025-04-22 12:16:57.315152896 +0000 UTC SeverityText: WARN SeverityNumber: Warn(13) Body: Str(Jail zesty vixen who grabbed pay from quack.) Attributes: -> code.filepath: Str(/Users/jayclifford/Repos/opentelemetry-python/docs/examples/logs/example.py) -> code.function: Str() -> code.lineno: Int(47) Trace ID: Span ID: Flags: 0 LogRecord #1 ObservedTimestamp: 2025-04-22 12:16:57.31522 +0000 UTC Timestamp: 2025-04-22 12:16:57.315213056 +0000 UTC SeverityText: ERROR SeverityNumber: Error(17) Body: Str(The five boxing wizards jump quickly.) Attributes: -> code.filepath: Str(/Users/jayclifford/Repos/opentelemetry-python/docs/examples/logs/example.py) -> code.function: Str() -> code.lineno: Int(48) Trace ID: Span ID: Flags: 0 LogRecord #2 ObservedTimestamp: 2025-04-22 12:16:57.315445 +0000 UTC Timestamp: 2025-04-22 12:16:57.31543808 +0000 UTC SeverityText: ERROR SeverityNumber: Error(17) Body: Str(Hyderabad, we have a major problem.) Attributes: -> code.filepath: Str(/Users/jayclifford/Repos/opentelemetry-python/docs/examples/logs/example.py) -> code.function: Str() -> code.lineno: Int(61) Trace ID: 8a6739fffce895e694700944e2faf23e Span ID: a45337020100cb63 Flags: 1 ScopeLogs #1 ScopeLogs SchemaURL: InstrumentationScope myapp.area1 LogRecord #0 ObservedTimestamp: 2025-04-22 12:16:57.315242 +0000 UTC Timestamp: 2025-04-22 12:16:57.315234048 +0000 UTC SeverityText: ERROR SeverityNumber: Error(17) Body: Str(I have custom attributes.) Attributes: -> user_id: Str(user-123) -> code.filepath: Str(/Users/jayclifford/Repos/opentelemetry-python/docs/examples/logs/example.py) -> code.function: Str() -> code.lineno: Int(53) Trace ID: Span ID: Flags: 0 ``` -------------------------------- ### Prometheus Metrics Output Example Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/metrics/prometheus-grafana/README.rst An example of the Prometheus metrics output generated by the application. This includes standard Python garbage collection metrics and a custom application counter. ```text # HELP python_gc_objects_collected_total Objects collected during gc # TYPE python_gc_objects_collected_total counter python_gc_objects_collected_total{generation="0"} 320.0 python_gc_objects_collected_total{generation="1"} 58.0 python_gc_objects_collected_total{generation="2"} 0.0 # HELP python_gc_objects_uncollectable_total Uncollectable objects found during GC # TYPE python_gc_objects_uncollectable_total counter python_gc_objects_uncollectable_total{generation="0"} 0.0 python_gc_objects_uncollectable_total{generation="1"} 0.0 python_gc_objects_uncollectable_total{generation="2"} 0.0 # HELP python_gc_collections_total Number of times this generation was collected # TYPE python_gc_collections_total counter python_gc_collections_total{generation="0"} 61.0 python_gc_collections_total{generation="1"} 5.0 python_gc_collections_total{generation="2"} 0.0 # HELP python_info Python platform information # TYPE python_info gauge python_info{implementation="CPython",major="3",minor="8",patchlevel="5",version="3.8.5"} 1.0 # HELP MyAppPrefix_my_counter_total # TYPE MyAppPrefix_my_counter_total counter MyAppPrefix_my_counter_total 964.0 ``` -------------------------------- ### Start Jaeger Docker Container Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opencensus-shim/README.rst Starts a Jaeger all-in-one Docker container, exposing necessary ports for tracing data collection and UI access. ```sh docker run --rm \ -p 4317:4317 \ -p 4318:4318 \ -p 16686:16686 \ jaegertracing/all-in-one:latest \ --log-level=debug ``` -------------------------------- ### Run OpenTelemetry Collector Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/sqlcommenter/README.rst Starts the OpenTelemetry Collector using a provided configuration file. This collector will process and display telemetry data, including enriched SQL comments. ```sh docker run \ -p 4317:4317 \ -v $(pwd)/collector-config.yaml:/etc/otel/config.yaml \ otel/opentelemetry-collector-contrib:latest ``` -------------------------------- ### Sample Collector Output for Metrics Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/metrics/instruments/README.rst Example output from the OpenTelemetry Collector showing received metrics, including counter, updown_counter, and histogram. ```sh ScopeMetrics #0 ScopeMetrics SchemaURL: InstrumentationScope getting-started 0.1.2 Metric #0 Descriptor: -> Name: counter -> Description: -> Unit: -> DataType: Sum -> IsMonotonic: true -> AggregationTemporality: Cumulative NumberDataPoints #0 StartTimestamp: 2024-08-09 11:21:42.145179 +0000 UTC Timestamp: 2024-08-09 11:21:42.145325 +0000 UTC Value: 1 Metric #1 Descriptor: -> Name: updown_counter -> Description: -> Unit: -> DataType: Sum -> IsMonotonic: false -> AggregationTemporality: Cumulative NumberDataPoints #0 StartTimestamp: 2024-08-09 11:21:42.145202 +0000 UTC Timestamp: 2024-08-09 11:21:42.145325 +0000 UTC Value: -4 Metric #2 Descriptor: -> Name: histogram -> Description: -> Unit: -> DataType: Histogram -> AggregationTemporality: Cumulative HistogramDataPoints #0 StartTimestamp: 2024-08-09 11:21:42.145221 +0000 UTC Timestamp: 2024-08-09 11:21:42.145325 +0000 UTC Count: 1 ``` -------------------------------- ### Run Django Server with Manual Instrumentation Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst Starts the Django development server with OpenTelemetry instrumentation enabled. The --noreload flag is crucial to prevent double execution of the main entry point. ```bash export DJANGO_SETTINGS_MODULE=instrumentation_example.settings python manage.py runserver --noreload ``` -------------------------------- ### Install Contrib Packages with Specific SHA Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Set the CONTRIB_REPO_SHA environment variable to a specific git commit hash before running tox to install packages from that commit in the OpenTelemetry Python Contrib Repository. ```bash CONTRIB_REPO_SHA=dde62cebffe519c35875af6d06fae053b3be65ec tox ``` -------------------------------- ### Run Flask Application with Gunicorn Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/fork-process-model/flask-gunicorn/README.rst Starts a Flask application using Gunicorn with a specified configuration file. This command assumes the application is named 'app' and the configuration is in 'gunicorn.conf.py'. ```sh gunicorn app -c gunicorn.conf.py ``` -------------------------------- ### Run Flask Application with uWSGI Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/fork-process-model/flask-uwsgi/README.rst Starts a Flask application using uWSGI. This command binds to port 8000, specifies the WSGI file and callable, and enables threading for better performance. ```sh uwsgi --http :8000 --wsgi-file app.py --callable application --master --enable-threads ``` -------------------------------- ### Execute Prometheus Monitor Script Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/metrics/prometheus-grafana/README.rst Run the Python script that exposes Prometheus metrics. The script starts a server that can be accessed to retrieve metrics. ```bash python ./prometheus-monitor.py Server is running at http://localhost:8000 ``` -------------------------------- ### Example OpenTelemetry Span with SQLCommenter Attributes Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/sqlcommenter/README.rst Illustrates an OpenTelemetry span generated by the MySQL instrumentation. The 'db.statement' attribute includes the sqlcomment, and other attributes provide database context. ```text ScopeSpans #0 ScopeSpans SchemaURL: https://opentelemetry.io/schemas/1.11.0 InstrumentationScope opentelemetry.instrumentation.mysql 0.57b0 Span #0 Trace ID : 2c45248f2beefdd9688b0a94eb4ac9ee Parent ID : ID : 4f3af9a825aae9b1 Name : SELECT Kind : Client Start time : 2025-09-02 18:49:06.982341 +0000 UTC End time : 2025-09-02 18:49:06.98463 +0000 UTC Status code : Unset Status message : Attributes: -> db.system: Str(mysql) -> db.name: Str(books) -> db.statement: Str(SELECT * FROM authors WHERE id = %s /*db_driver='mysql.connector%%3A9.4.0',dbapi_level='2.0',dbapi_threadsafety=1,driver_paramstyle='pyformat',mysql_client_version='9.4.0',traceparent='00-2c45248f2beefdd9688b0a94eb4ac9ee-4f3af9a825aae9b1-01'*) -> db.user: Str(books) -> net.peer.name: Str(localhost) -> net.peer.port: Int(3366) ``` -------------------------------- ### Registering Custom Error Handler via Entry Point Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/error_handler/README.rst Illustrates how to register a custom error handler using the 'opentelemetry_error_handler' entry point in a package's setup. This makes the custom handler discoverable by the GlobalErrorHandler. ```ini [options.entry_points] opentelemetry_error_handler = error_handler_0 = error_handler_0:ErrorHandler0 ``` -------------------------------- ### Commit Message Trailer for AI Assistance Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/AGENTS.md Example of how to disclose the use of AI tools in commit messages using the 'Assisted-by:' trailer. This is required when a significant part of a commit is taken from a tool without changes. ```git Assisted-by: ChatGPT 5.2 ``` ```git Assisted-by: Claude Opus 4.6 ``` -------------------------------- ### Testbed Directory Structure for New Patterns Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/shim/opentelemetry-opentracing-shim/tests/testbed/README.rst Illustrates the required directory and file structure for adding a new testing pattern to the testbed. ```text testbed/ test_new_pattern/ test_threads.py test_asyncio.py ``` -------------------------------- ### Build and Upload Distributions to PyPI Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/RELEASING.md Manual steps to build package distributions and upload them to PyPI using twine. Ensure you are on the release branch before executing. ```bash ./scripts/build.sh ``` ```bash twine upload --skip-existing --verbose dist/* ``` -------------------------------- ### Create a Changelog Fragment Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Create a changelog fragment file in the .changelog/ directory. The filename should follow the format ., where TYPE is one of added, changed, deprecated, removed, or fixed. ```bash .changelog/1234.added ``` -------------------------------- ### Trigger Request with curl Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/opencensus-shim/README.rst Sends an HTTP GET request to the running Flask application to generate trace data. ```sh curl http://127.0.0.1:5000 ``` -------------------------------- ### Build and Run MySQL Docker Container Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/sqlcommenter/README.rst Builds a Docker image for the MySQL database and runs it as a container. Ensure the general log is enabled for this container. ```sh cd books_database docker build -t books-db . docker run -d --name books-db -p 3366:3306 books-db cd .. ``` -------------------------------- ### Run pre-commit actions with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Execute all pre-commit hooks and actions defined in the project using tox. This ensures code quality and consistency before committing. ```bash tox -e precommit ``` -------------------------------- ### Preview Changelog Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Use the towncrier build command with the --draft and --version Unreleased flags to preview the generated changelog. ```console towncrier build --draft --version Unreleased ``` -------------------------------- ### Build and Test with Tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/shim/opentelemetry-opentracing-shim/tests/testbed/README.rst Run the test suite for the OpenTracing shim using tox for Python 3.11. ```sh tox -e py311-test-opentracing-shim ``` -------------------------------- ### Example Span Output Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst This JSON output represents a trace span generated by the OpenTelemetry Django instrumentation for an incoming HTTP request. It details the request, response, and context information. ```json { "name": "home_page_view", "context": { "trace_id": "0xed88755c56d95d05a506f5f70e7849b9", "span_id": "0x0a94c7a60e0650d5", "trace_state": "{}" }, "kind": "SpanKind.SERVER", "parent_id": "0x3096ef92e621c22d", "start_time": "2020-04-26T01:49:57.205833Z", "end_time": "2020-04-26T01:49:57.206214Z", "status": { "status_code": "OK" }, "attributes": { "http.request.method": "GET", "server.address": "localhost", "url.scheme": "http", "server.port": 8000, "url.full": "http://localhost:8000/?param=hello", "server.socket.address": "127.0.0.1", "network.protocol.version": "1.1", "http.response.status_code": 200 }, "events": [], "links": [] } ``` -------------------------------- ### Run Django Client Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst Executes the client script to send a request to the instrumented Django application. Ensure the virtual environment is activated and the script is run from the correct directory. ```bash python client.py hello ``` -------------------------------- ### RequestHandler Span Management Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/README.rst This snippet shows how to start a new span as a child of a provided SpanContext, ignoring any active span. This is useful in middleware scenarios where explicit parent context management is required. ```python def before_request(self, request, request_context): # If we should ignore the active Span, use any passed SpanContext # as the parent. Else, use the active one. span = self.tracer.start_span("send", child_of=self.context, ignore_active_span=True) ``` -------------------------------- ### Python RPC Client Span with ResponseListener Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/README.rst Shows how to create a client-side span, submit a task to an executor that will process a response, and use a ResponseListener to finish the span upon receiving the response. ```python def _task(self, message, listener): res = "%s::response" % message listener.on_response(res) return res def send_sync(self, message): span = self.tracer.start_span("send") span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_CLIENT) listener = ResponseListener(span) return self.executor.submit(self._task, message, listener).result() ``` -------------------------------- ### Create and Push Feature Branch Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Create a new branch for your feature, make modifications, and push the branch to your fork. ```bash git checkout -b feature ``` ```bash git add . git commit git push fork feature ``` -------------------------------- ### Lint Code with Ruff via Pre-commit Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/AGENTS.md Runs the linter, specifically ruff, using the pre-commit framework. This command checks for code style and potential issues across the repository. ```sh uv run tox -e precommit ``` -------------------------------- ### Run API or SDK unit tests with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Execute unit tests for either the opentelemetry-api or opentelemetry-sdk packages using tox. This allows for targeted testing. ```bash tox -e opentelemetry-api ``` ```bash tox -e opentelemetry-sdk ``` -------------------------------- ### SPDX License Header Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md All Python files must include the SPDX license header as the first two lines. This is enforced by CI. ```python # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 ``` -------------------------------- ### Regenerate API docs with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Use tox to regenerate the API documentation. This command is specific to the documentation generation process. ```bash tox -e docs ``` -------------------------------- ### uWSGI Postfork Decorator for OpenTelemetry Tracing Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/fork-process-model/README.rst This snippet shows how to use the uWSGI `@postfork` decorator to re-initialize OpenTelemetry tracing in worker processes. This approach addresses the fork-safety issues of `BatchSpanProcessor` by ensuring tracing is correctly set up in each child process after a fork. ```python from uwsgidecorators import postfork from opentelemetry import trace from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor @postfork def init_tracing(): resource = Resource.create(attributes={ "service.name": "api-service" }) trace.set_tracer_provider(TracerProvider(resource=resource)) span_processor = BatchSpanProcessor( OTLPSpanExporter(endpoint="http://localhost:4317") ) trace.get_tracer_provider().add_span_processor(span_processor) ``` -------------------------------- ### Update Version String References Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/README.md Search recursively for old version strings within the opentelemetry-sdk directory to ensure all references are updated after a schema change. ```sh grep -r "OLD_VERSION" opentelemetry-sdk/ ``` -------------------------------- ### Run Django Server with Auto Instrumentation Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/django/README.rst Executes the Django app using the `opentelemetry-instrument` command for automatic instrumentation. This method requires commenting out manual instrumentation calls in `manage.py`. ```bash opentelemetry-instrument python manage.py runserver --noreload ``` -------------------------------- ### Run tracecontext integration tests with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Execute integration tests specifically for tracecontext using tox. This verifies the correct implementation of trace context propagation. ```bash tox -e tracecontext ``` -------------------------------- ### Run public symbols check with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Execute the public_symbols_checker.py script using tox. This verifies the public API surface of the library. ```bash tox -e public-symbols-check ``` -------------------------------- ### Manage Git Tags for Release Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/RELEASING.md Commands to delete the old 'stable' tag, create a new one, and push it to the origin. This is a manual step to update the stable release tag. ```bash git tag -d stable git tag stable git push --delete origin stable git push origin tag stable ``` -------------------------------- ### Run docker tests with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Execute tests within Docker containers for specified exporters (e.g., otlpexporter, opencensus) using tox. This ensures compatibility in containerized environments. ```bash tox -e docker-tests-{otlpexporter,opencensus} ``` -------------------------------- ### Gunicorn Post-Fork Hook for OpenTelemetry Tracing Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/fork-process-model/README.rst This snippet demonstrates how to use Gunicorn's `post_fork` hook to re-initialize OpenTelemetry tracing in worker processes after a fork. This is necessary because the `BatchSpanProcessor` is not fork-safe due to potential deadlocks with its background export thread. ```python from opentelemetry import trace from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor def post_fork(server, worker): server.log.info("Worker spawned (pid: %s)", worker.pid) resource = Resource.create(attributes={ "service.name": "api-service" }) trace.set_tracer_provider(TracerProvider(resource=resource)) span_processor = BatchSpanProcessor( OTLPSpanExporter(endpoint="http://localhost:4317") ) trace.get_tracer_provider().add_span_processor(span_processor) ``` -------------------------------- ### Run specific Python version tests with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Run unit tests for a specific package (e.g., opentelemetry-api) against a particular Python version (e.g., py314) using tox. ```bash tox -e py314-opentelemetry-api ``` -------------------------------- ### Tail MySQL General Log Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/sqlcommenter/README.rst Executes a command within the running MySQL Docker container to view the general query log in real-time. ```sh docker exec -it books-db tail -f /var/log/general.log ``` -------------------------------- ### Run type checking with tox Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md Perform static type checking on the entire codebase using pyright via tox. This helps catch type-related errors early. ```bash tox -e typecheck ``` -------------------------------- ### Configure OpenTelemetry Collector for Logs Source: https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/logs/README.rst YAML configuration for the OpenTelemetry Collector to receive OTLP logs and export them to a debug exporter. ```yaml # otel-collector-config.yaml receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 exporters: debug: verbosity: detailed service: pipelines: logs: receivers: [otlp] exporters: [debug] traces: receivers: [otlp] exporters: [debug] ```