diff --git a/azure-monitor-opentelemetry/README.md b/azure-monitor-opentelemetry/README.md index f6f1cfa..1c62115 100644 --- a/azure-monitor-opentelemetry/README.md +++ b/azure-monitor-opentelemetry/README.md @@ -8,7 +8,7 @@ This distro automatically installs the following libraries: ## Officially supported instrumentations -The following OpenTelemetry instrumentations come bundled in with the Azure monitor distro. If you would like to add support for another OpenTelemetry instrumentation, please submit a feature [request][distro_feature_request]. In the meantime, you can use the OpenTelemetry instrumentation manually via it's own APIs (i.e. `instrument()`) in your code. +The following OpenTelemetry instrumentations come bundled in with the Azure monitor distro. If you would like to add support for another OpenTelemetry instrumentation, please submit a feature [request][distro_feature_request]. In the meantime, you can use the OpenTelemetry instrumentation manually via it's own APIs (i.e. `instrument()`) in your code. See [this][samples_manual] for an example. * [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django] * [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi] @@ -131,3 +131,4 @@ Samples are available [here][samples] to demonstrate how to utilize the above co [python]: https://www.python.org/downloads/ [pip]: https://pypi.org/project/pip/ [samples]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples +[samples_manual]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples/tracing/manual.py diff --git a/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py b/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py index e6aa9a4..72bfab9 100644 --- a/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py +++ b/azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py @@ -14,6 +14,7 @@ configure_azure_monitor( tracing_export_interval_millis=15000, ) +# Database calls using the psycopg2 library will be automatically captured cnx = psycopg2.connect(database="test", user="", password="") cursor = cnx.cursor() cursor.execute("INSERT INTO test_tables (test_field) VALUES (123)") diff --git a/azure-monitor-opentelemetry/samples/tracing/manual.py b/azure-monitor-opentelemetry/samples/tracing/manual.py new file mode 100644 index 0000000..285c1fc --- /dev/null +++ b/azure-monitor-opentelemetry/samples/tracing/manual.py @@ -0,0 +1,31 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- +from sqlalchemy import create_engine, text + +from azure.monitor.opentelemetry import configure_azure_monitor +from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor + +configure_azure_monitor( + connection_string="", + tracing_export_interval_millis=15000, + disable_logging=True, + disable_metrics=True, +) + +engine = create_engine("sqlite:///:memory:") +# SQLAlchemy instrumentation is not officially supported by this package +# However, you can use the OpenTelemetry instument method manually in +# conjunction with configure_azure_monitor +SQLAlchemyInstrumentor().instrument( + engine=engine, +) + +# Database calls using the SqlAlchemy library will be automatically captured +with engine.connect() as conn: + result = conn.execute(text("select 'hello world'")) + print(result.all()) + +input() diff --git a/azure-monitor-opentelemetry/samples/tracing/sampling.py b/azure-monitor-opentelemetry/samples/tracing/sampling.py new file mode 100644 index 0000000..ad0c8a7 --- /dev/null +++ b/azure-monitor-opentelemetry/samples/tracing/sampling.py @@ -0,0 +1,27 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from azure.monitor.opentelemetry import configure_azure_monitor +from opentelemetry import trace + +configure_azure_monitor( + connection_string="", + # Sampling ratio of between 0 and 1 inclusive + # 0.1 means approximately 10% of your traces are sent + sampling_ratio=0.1, + tracing_export_interval_millis=15000, + disable_logging=True, + disable_metrics=True, +) + +tracer = trace.get_tracer(__name__) + +for i in range(100): + # Approximately 90% of these spans should be sampled out + with tracer.start_as_current_span("hello"): + print("Hello, World!") + +input()