Skip to content

Commit d49268f

Browse files
committed
samplES
1 parent 95cf300 commit d49268f

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

azure-monitor-opentelemetry/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This distro automatically installs the following libraries:
88

99
## Officially supported instrumentations
1010

11-
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.
11+
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.
1212

1313
* [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django]
1414
* [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi]
@@ -131,3 +131,4 @@ Samples are available [here][samples] to demonstrate how to utilize the above co
131131
[python]: https://www.python.org/downloads/
132132
[pip]: https://pypi.org/project/pip/
133133
[samples]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples
134+
[samples_manual]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples/tracing/manual.py

azure-monitor-opentelemetry/samples/tracing/db_psycopg2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
tracing_export_interval_millis=15000,
1515
)
1616

17+
# Database calls using the psycopg2 library will be automatically captured
1718
cnx = psycopg2.connect(database="test", user="<user>", password="<password>")
1819
cursor = cnx.cursor()
1920
cursor.execute("INSERT INTO test_tables (test_field) VALUES (123)")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
from sqlalchemy import create_engine, text
7+
8+
from azure.monitor.opentelemetry import configure_azure_monitor
9+
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
10+
11+
configure_azure_monitor(
12+
connection_string="<your-connection-string>",
13+
tracing_export_interval_millis=15000,
14+
disable_logging=True,
15+
disable_metrics=True,
16+
)
17+
18+
engine = create_engine("sqlite:///:memory:")
19+
# SQLAlchemy instrumentation is not officially supported by this package
20+
# However, you can use the OpenTelemetry instument method manually in
21+
# conjunction with configure_azure_monitor
22+
SQLAlchemyInstrumentor().instrument(
23+
engine=engine,
24+
)
25+
26+
# Database calls using the SqlAlchemy library will be automatically captured
27+
with engine.connect() as conn:
28+
result = conn.execute(text("select 'hello world'"))
29+
print(result.all())
30+
31+
input()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
from azure.monitor.opentelemetry import configure_azure_monitor
8+
from opentelemetry import trace
9+
10+
configure_azure_monitor(
11+
connection_string="<your-connection-string>",
12+
# Sampling ratio of between 0 and 1 inclusive
13+
# 0.1 means approximately 10% of your traces are sent
14+
sampling_ratio=0.1,
15+
tracing_export_interval_millis=15000,
16+
disable_logging=True,
17+
disable_metrics=True,
18+
)
19+
20+
tracer = trace.get_tracer(__name__)
21+
22+
for i in range(100):
23+
# Approximately 90% of these spans should be sampled out
24+
with tracer.start_as_current_span("hello"):
25+
print("Hello, World!")
26+
27+
input()

0 commit comments

Comments
 (0)