-
Notifications
You must be signed in to change notification settings - Fork 3.2k
add log exporter and metric reader params to configure azure monitor #44367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
967b17b
add log exporter and metric reader params
eavanvalkenburg 5a7f642
consistent ordering
eavanvalkenburg 3055a73
added changelog
eavanvalkenburg fe40e05
add some test configs
eavanvalkenburg 99798ea
test fixes
eavanvalkenburg 20a2867
fix pylint errors
eavanvalkenburg 50ea193
Add more tests and rename
rads-1996 8e18d8a
Remove duplicate metric reader
rads-1996 ac9ea18
Add samples
rads-1996 1778e17
Fix black
rads-1996 d2e1bad
Fix format using tox
rads-1996 e0ef54c
update test
rads-1996 52aba7d
Merge branch 'main' into add_exporters
rads-1996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,8 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to | |
| | `resource` | Specifies the OpenTelemetry [Resource][ot_spec_resource] associated with your application. Passed in [Resource Attributes][ot_spec_resource_attributes] take priority over default attributes and those from [Resource Detectors][ot_python_resource_detectors]. | [OTEL_SERVICE_NAME][ot_spec_service_name], [OTEL_RESOURCE_ATTRIBUTES][ot_spec_resource_attributes], [OTEL_EXPERIMENTAL_RESOURCE_DETECTORS][ot_python_resource_detectors] | | ||
| | `span_processors` | A list of [span processors][ot_span_processor] that will perform processing on each of your spans before they are exported. Useful for filtering/modifying telemetry. | `N/A` | | ||
| | `views` | A list of [views][ot_view] that will be used to customize metrics exported by the SDK. | `N/A` | | ||
| | `log_record_processors` | A list of [log record processors][ot_log_record_processor] that will process log records before they are exported. | `N/A` | | ||
| | `metric_readers` | A list of [metric reader][ot_metric_reader] that will process metric readers before they are exported | `N/A` | | ||
| | `traces_per_second` | Configures the Rate Limited sampler by specifying the maximum number of traces to sample per second. When set, this automatically enables the rate-limited sampler. Alternatively, you can configure sampling using the `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG` environment variables as described in the table below. Please note that the sampling configuration via environment variables will have precedence over the sampling exporter/distro options. | `N/A` | ||
|
|
||
| You can configure further with [OpenTelemetry environment variables][ot_env_vars]. | ||
|
|
@@ -231,6 +233,7 @@ contact [[email protected]](mailto:[email protected]) with any additio | |
| [ot_sdk_python]: https://github.com/open-telemetry/opentelemetry-python | ||
| [ot_sdk_python_metric_reader]: https://opentelemetry-python.readthedocs.io/en/latest/sdk/metrics.export.html#opentelemetry.sdk.metrics.export.MetricReader | ||
| [ot_span_processor]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#span-processor | ||
| [ot_log_record_processor]: https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/logs/sdk.md#log-record-processor | ||
| [ot_view]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view | ||
| [ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views | ||
| [ot_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
sdk/monitor/azure-monitor-opentelemetry/samples/logging/modify_logs.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
|
|
||
| import logging | ||
| from logging import getLogger | ||
| from azure.monitor.opentelemetry import configure_azure_monitor | ||
| from azure.monitor.opentelemetry.exporter._generated.models import ContextTagKeys | ||
| from opentelemetry import trace | ||
| from opentelemetry.sdk._logs import LogRecordProcessor, ReadableLogRecord | ||
|
|
||
| logger = getLogger(__name__) | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
|
|
||
| class LogRecordEnrichingProcessor(LogRecordProcessor): | ||
| """Enriches log records with operation name from the current span context.""" | ||
|
|
||
| def on_emit(self, readable_log_record: ReadableLogRecord) -> None: | ||
| current_span = trace.get_current_span() | ||
| if current_span and getattr(current_span, "name", None): | ||
| if readable_log_record.log_record.attributes is None: | ||
| readable_log_record.log_record.attributes = {} | ||
| readable_log_record.log_record.attributes[ContextTagKeys.AI_OPERATION_NAME] = current_span.name | ||
|
|
||
| def shutdown(self) -> None: | ||
| pass | ||
|
|
||
| def force_flush(self, timeout_millis: int = 30000) -> bool: | ||
| return True | ||
|
|
||
|
|
||
| # Create the log record enriching processor | ||
| log_enriching_processor = LogRecordEnrichingProcessor() | ||
|
|
||
| # Configure Azure Monitor with the custom log record processor | ||
| configure_azure_monitor(log_record_processors=[log_enriching_processor]) | ||
|
|
||
| tracer = trace.get_tracer(__name__) | ||
|
|
||
| with tracer.start_as_current_span("span-name-here"): | ||
| logger.info("This log will be enriched with operation name") | ||
|
|
||
| input() |
50 changes: 50 additions & 0 deletions
50
sdk/monitor/azure-monitor-opentelemetry/samples/metrics/modify_metrics.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
|
|
||
| from time import sleep | ||
|
|
||
| from azure.monitor.opentelemetry import configure_azure_monitor | ||
| from opentelemetry import metrics | ||
| from opentelemetry.sdk.metrics.export import ( | ||
| MetricExportResult, | ||
| MetricExporter, | ||
| MetricsData, | ||
| PeriodicExportingMetricReader, | ||
| ) | ||
|
|
||
|
|
||
| class PrintMetricExporter(MetricExporter): | ||
| """Minimal exporter that prints metric data.""" | ||
|
|
||
| def export(self, metrics_data: MetricsData, **kwargs) -> MetricExportResult: # type: ignore[override] | ||
| # In a real exporter, send metrics_data to your backend | ||
| print(f"exported metrics: {metrics_data}") | ||
| return MetricExportResult.SUCCESS | ||
|
|
||
| def shutdown(self, timeout_millis: float = 30000, **kwargs) -> None: # type: ignore[override] | ||
| return None | ||
|
|
||
| def force_flush(self, timeout_millis: float = 30000, **kwargs) -> bool: # type: ignore[override] | ||
| return True | ||
|
|
||
|
|
||
| # Add a custom reader; the SDK will append its own Azure Monitor reader | ||
| custom_reader = PeriodicExportingMetricReader( | ||
| PrintMetricExporter(), | ||
| export_interval_millis=5000, | ||
| ) | ||
|
|
||
| configure_azure_monitor( | ||
| enable_performance_counters=False, | ||
| metric_readers=[custom_reader], | ||
| ) | ||
|
|
||
| meter = metrics.get_meter_provider().get_meter("metric-readers-sample") | ||
| counter = meter.create_counter("example.counter") | ||
|
|
||
| for _ in range(3): | ||
| counter.add(1) | ||
| sleep(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.