Skip to content

Commit d80b63d

Browse files
authored
Merge pull request #218 from jeremydvoss/logging
2 parents 99faefb + 827e6e8 commit d80b63d

File tree

8 files changed

+463
-66
lines changed

8 files changed

+463
-66
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
## Unreleased
44

5-
- Added Status Logger
6-
([#217](https://github.com/microsoft/ApplicationInsights-Python/pull/217))
7-
- Added Diagnostic Logging for App Service
8-
([#212](https://github.com/microsoft/ApplicationInsights-Python/pull/212))
95
- Updated main and distro READMEs
106
([#205](https://github.com/microsoft/ApplicationInsights-Python/pull/205))
117
- Update CONTRIBUTING.md, support Py3.11
128
([#210](https://github.com/microsoft/ApplicationInsights-Python/pull/210))
9+
- Added Diagnostic Logging for App Service
10+
([#212](https://github.com/microsoft/ApplicationInsights-Python/pull/212))
1311
- Updated setup.py, directory structure
1412
([#214](https://github.com/microsoft/ApplicationInsights-Python/pull/214))
1513
- Introduce Distro API
1614
([#215](https://github.com/microsoft/ApplicationInsights-Python/pull/215))
1715
- Rename to `configure_azure_monitor`, add sampler to config
1816
([#216](https://github.com/microsoft/ApplicationInsights-Python/pull/216))
17+
- Added Status Logger
18+
([#217](https://github.com/microsoft/ApplicationInsights-Python/pull/217))
19+
- Add Logging configuration to Distro API
20+
([#218](https://github.com/microsoft/ApplicationInsights-Python/pull/218))
1921

2022
## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26
2123

azure-monitor-opentelemetry-distro/README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This distro automatically installs the following libraries:
1414

1515
### Key Concepts
1616

17-
This package is simply a collection of OpenTelemetry and Azure Monitor components bundled together to enable the collection and sending of telemetry to Azure Monitor. For MANUAL instrumentation, it is equivalent to installing the above packages individually. AUTOMATIC instrumentation is not yet supported.
17+
This package bundles a series of OpenTelemetry and Azure Monitor components to enable the collection and sending of telemetry to Azure Monitor. For MANUAL instrumentation, use the `configure_azure_monitor` function. AUTOMATIC instrumentation is not yet supported.
1818

1919
The [Azure Monitor OpenTelemetry exporters][azure_monitor_opentelemetry_exporters] are the main components in accomplishing this. You will be able to use the exporters and their APIs directly through this package. Please go the exporter documentation to understand how OpenTelemetry and Azure Monitor components work in enabling telemetry collection and exporting.
2020

@@ -28,6 +28,23 @@ Install the Azure Monitor Opentelemetry Distro with [pip][pip]:
2828
pip install azure-monitor-opentelemetry-distro --pre
2929
```
3030

31+
### Usage
32+
33+
You can use `configure_azure_monitor` to set up instrumentation for your app to Azure Monitor. `configure_azure_monitor` supports the following optional arguments:
34+
35+
* connection_string - The [connection string][connection_string_doc] for your Application Insights resource. The connection string will be automatically populated from the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable if not explicitly passed in.
36+
* service_name = Specifies the [service][service_semantic_convention_doc] name.
37+
* service_namespace = Specifies the [service][service_semantic_convention_doc] namespace.
38+
* service_instance_id = Specifies the [service][service_semantic_convention_doc] instance id.
39+
* disable_logging = If set to `True`, disables collection and export of logging telemetry.
40+
* logging_level = Specifies the [logging level][logging_level] of the Opentelemetry Logging Handler. Ex: logging.WARNING.
41+
* logging_export_interval_millis = Specifies the export interval of the logging exporter in milliseconds. Defaults to 30,000.
42+
* disable_tracing = If set to `True`, disables collection and export of distributed tracing telemetry.
43+
* sampling_ratio = Specifies the ratio of distributed tracing telemetry to be [sampled][application_insights_sampling]. Accepted values are in the range [0,1]. Defaults to 1.0, meaning no telemetry is sampled out.
44+
* tracing_export_interval_millis = Specifies the export interval of the distributed tracing exporter in milliseconds. Defaults to 30,000.
45+
46+
See additional [configuration related to exporting here][exporter_configuration_docs].
47+
3148
### Prerequisites:
3249
To use this package, you must have:
3350
* Azure subscription - [Create a free account][azure_sub]
@@ -44,12 +61,17 @@ To use this package, you must have:
4461
[azure_monitor_opentelemetry_exporters]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter#microsoft-opentelemetry-exporter-for-azure-monitor
4562
[azure_portal]: https://portal.azure.com
4663
[azure_sub]: https://azure.microsoft.com/free/
47-
[application_insights_namespace]: https://docs.microsoft.com/azure/azure-monitor/app/
48-
[pip]: https://pypi.org/project/pip/
64+
[application_insights_namespace]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview
65+
[application_insights_sampling]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling
66+
[connection_string_doc]: https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string
67+
[exporter_configuration_docs]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter#configuration
68+
[logging_level]: https://docs.python.org/3/library/logging.html#levels
4969
[ot_python_docs]: https://opentelemetry.io/docs/instrumentation/python/
5070
[ot_sdk_python]: https://github.com/open-telemetry/opentelemetry-python
5171
[opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests
5272
[opentelemetry_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django
5373
[opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask
5474
[opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2
5575
[python]: https://www.python.org/downloads/
76+
[pip]: https://pypi.org/project/pip/
77+
[service_semantic_convention_doc]: https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#service

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
# Licensed under the MIT License. See License in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
6+
from logging import NOTSET, getLogger
7+
68
from azure.monitor.opentelemetry.distro.util import get_configurations
79
from azure.monitor.opentelemetry.exporter import (
810
ApplicationInsightsSampler,
11+
AzureMonitorLogExporter,
912
AzureMonitorTraceExporter,
1013
)
11-
from opentelemetry import trace
14+
from opentelemetry.sdk._logs import (
15+
LoggerProvider,
16+
LoggingHandler,
17+
get_logger_provider,
18+
set_logger_provider,
19+
)
20+
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
1221
from opentelemetry.sdk.resources import Resource
1322
from opentelemetry.sdk.trace import TracerProvider
1423
from opentelemetry.sdk.trace.export import BatchSpanProcessor
1524
from opentelemetry.semconv.resource import ResourceAttributes
25+
from opentelemetry.trace import get_tracer_provider, set_tracer_provider
1626

1727

1828
def configure_azure_monitor(**kwargs):
@@ -23,31 +33,51 @@ def configure_azure_monitor(**kwargs):
2333
"""
2434

2535
configurations = get_configurations(**kwargs)
26-
disable_tracing = configurations.get("disable_tracing", False)
2736
service_name = configurations.get("service_name", "")
2837
service_namespace = configurations.get("service_namespace", "")
2938
service_instance_id = configurations.get("service_instance_id", "")
39+
disable_logging = configurations.get("disable_logging", False)
40+
logging_level = configurations.get("logging_level", NOTSET)
41+
logging_export_interval_millis = configurations.get(
42+
"logging_export_interval_millis", 30000
43+
)
44+
disable_tracing = configurations.get("disable_tracing", False)
3045
sampling_ratio = configurations.get("sampling_ratio", 1.0)
3146
tracing_export_interval_millis = configurations.get(
3247
"tracing_export_interval_millis", 30000
3348
)
3449

35-
if not disable_tracing:
50+
resource = None
51+
if not disable_logging or not disable_tracing:
3652
resource = Resource.create(
3753
{
3854
ResourceAttributes.SERVICE_NAME: service_name,
3955
ResourceAttributes.SERVICE_NAMESPACE: service_namespace,
4056
ResourceAttributes.SERVICE_INSTANCE_ID: service_instance_id,
4157
}
4258
)
59+
if not disable_logging:
60+
logger_provider = LoggerProvider(resource=resource)
61+
set_logger_provider(logger_provider)
62+
log_exporter = AzureMonitorLogExporter(**kwargs)
63+
log_record_processor = BatchLogRecordProcessor(
64+
log_exporter,
65+
export_timeout_millis=logging_export_interval_millis,
66+
)
67+
get_logger_provider().add_log_record_processor(log_record_processor)
68+
handler = LoggingHandler(
69+
level=logging_level, logger_provider=get_logger_provider()
70+
)
71+
getLogger().addHandler(handler)
72+
if not disable_tracing:
4373
tracer_provider = TracerProvider(
4474
sampler=ApplicationInsightsSampler(sampling_ratio=sampling_ratio),
4575
resource=resource,
4676
)
47-
trace.set_tracer_provider(tracer_provider)
48-
exporter = AzureMonitorTraceExporter(**kwargs)
77+
set_tracer_provider(tracer_provider)
78+
trace_exporter = AzureMonitorTraceExporter(**kwargs)
4979
span_processor = BatchSpanProcessor(
50-
exporter,
80+
trace_exporter,
5181
export_timeout_millis=tracing_export_interval_millis,
5282
)
53-
trace.get_tracer_provider().add_span_processor(span_processor)
83+
get_tracer_provider().add_span_processor(span_processor)

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/distro.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
AzureStatusLogger,
1414
)
1515
from opentelemetry.environment_variables import (
16+
OTEL_LOGS_EXPORTER,
1617
OTEL_METRICS_EXPORTER,
1718
OTEL_TRACES_EXPORTER,
1819
)
@@ -55,6 +56,9 @@ def _configure_auto_instrumentation() -> None:
5556
environ.setdefault(
5657
OTEL_TRACES_EXPORTER, "azure_monitor_opentelemetry_exporter"
5758
)
59+
environ.setdefault(
60+
OTEL_LOGS_EXPORTER, "azure_monitor_opentelemetry_exporter"
61+
)
5862
AzureStatusLogger.log_status(True)
5963
_logger.info(
6064
"Azure Monitor OpenTelemetry Distro configured successfully."
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 logging import WARN, getLogger
8+
9+
from azure.monitor.opentelemetry.distro import configure_azure_monitor
10+
from opentelemetry import trace
11+
12+
configure_azure_monitor(
13+
connection_string="<your-connection-string>",
14+
service_name="foo_service",
15+
logging_level=WARN,
16+
disable_tracing=True,
17+
)
18+
19+
logger = getLogger(__name__)
20+
tracer = trace.get_tracer(__name__)
21+
22+
logger.info("info log")
23+
logger.warning("warning log")
24+
logger.error("error log")
25+
26+
with tracer.start_as_current_span("hello"):
27+
print("Hello, World!")
28+
logger.info("Correlated info log")
29+
logger.warning("Correlated warning log")
30+
logger.error("Correlated error log")

azure-monitor-opentelemetry-distro/samples/tracing/simple.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
connection_string="<your-connection-string>",
1212
service_name="foo_service",
1313
tracing_export_interval_millis=15000,
14+
disable_logging=True,
1415
)
1516

1617
tracer = trace.get_tracer(__name__)

0 commit comments

Comments
 (0)