-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add configuration for custom span processors #34326
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
6 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to | |
| | `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. | `APPLICATIONINSIGHTS_CONNECTION_STRING` | | ||
| | `logger_name` | The name of the [Python logger][python_logger] under which telemetry is collected. | `N/A` | | ||
| | `instrumentation_options` | A nested dictionary that determines which instrumentations to enable or disable. Instrumentations are referred to by their [Library Names](#officially-supported-instrumentations). For example, `{"azure_sdk": {"enabled": False}, "flask": {"enabled": False}, "django": {"enabled": True}}` will disable Azure Core Tracing and the Flask instrumentation but leave Django and the other default instrumentations enabled. The `OTEL_PYTHON_DISABLED_INSTRUMENTATIONS` environment variable explained below can also be used to disable instrumentations. | `N/A` | | ||
|
|
||
| | `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` | | ||
|
|
||
| You can configure further with [OpenTelemetry environment variables][ot_env_vars] such as: | ||
| | Environment Variable | Description | | ||
|
|
@@ -217,6 +217,7 @@ contact [[email protected]](mailto:[email protected]) with any additio | |
| [ot_python_docs]: https://opentelemetry.io/docs/instrumentation/python/ | ||
| [ot_sdk_python]: https://github.com/open-telemetry/opentelemetry-python | ||
| [ot_sdk_python_metric_reader]: https://opentelemetry-python.readthedocs.io/en/stable/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_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 | ||
| [ot_instrumentation_django_version]: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/package.py#L16 | ||
|
|
@@ -249,4 +250,4 @@ contact [[email protected]](mailto:[email protected]) with any additio | |
| [python_logger]: https://docs.python.org/3/library/logging.html#logger-objects | ||
| [python_logging_level]: https://docs.python.org/3/library/logging.html#levels | ||
| [samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry/samples | ||
| [samples_manual]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/manual.py | ||
| [samples_manual]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/manually_instrumented.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
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
49 changes: 49 additions & 0 deletions
49
sdk/monitor/azure-monitor-opentelemetry/samples/tracing/filter_spans.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,49 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
|
|
||
| import requests | ||
| from azure.monitor.opentelemetry import configure_azure_monitor | ||
| from opentelemetry.sdk.trace import SpanProcessor | ||
| from opentelemetry.trace import get_tracer, SpanContext, SpanKind, TraceFlags | ||
|
|
||
| # Define a custom processor to filter your spans | ||
| class SpanFilteringProcessor(SpanProcessor): | ||
|
|
||
| # Prevents exporting spans that are of kind INTERNAL | ||
| def on_start(self, span, parent_context): | ||
| # Check if the span is an internal activity. | ||
| if span._kind is SpanKind.INTERNAL: | ||
| # Create a new span context with the following properties: | ||
| # * The trace ID is the same as the trace ID of the original span. | ||
| # * The span ID is the same as the span ID of the original span. | ||
| # * The is_remote property is set to `False`. | ||
| # * The trace flags are set to `DEFAULT`. | ||
| # * The trace state is the same as the trace state of the original span. | ||
| span._context = SpanContext( | ||
| span.context.trace_id, | ||
| span.context.span_id, | ||
| span.context.is_remote, | ||
| TraceFlags(TraceFlags.DEFAULT), | ||
| span.context.trace_state, | ||
| ) | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Create a SpanFilteringProcessor instance. | ||
| span_filter_processor = SpanFilteringProcessor() | ||
|
|
||
| # Pass in your processor to configuration options | ||
| configure_azure_monitor( | ||
| span_processors=[span_filter_processor] | ||
| ) | ||
|
|
||
| tracer = get_tracer(__name__) | ||
|
|
||
| with tracer.start_as_current_span("this_span_is_ignored"): | ||
| # Requests made using the requests library will be automatically captured | ||
| # The span generated from this request call will be tracked since it is not an INTERNAL span | ||
| response = requests.get("https://azure.microsoft.com/", timeout=5) | ||
| print("Hello, World!") | ||
|
|
||
| input() | ||
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
sdk/monitor/azure-monitor-opentelemetry/samples/tracing/modify_spans.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,35 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # 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 | ||
| from opentelemetry.sdk.trace import SpanProcessor | ||
|
|
||
| # Define a custom processor to modify your spans | ||
| class SpanEnrichingProcessor(SpanProcessor): | ||
|
|
||
| def on_end(self, span): | ||
| # Prefix the span name with the string "Updated-". | ||
| span._name = "Updated-" + span.name | ||
| # Add the custom dimension "CustomDimension1" with the value "Value1". | ||
| span._attributes["CustomDimension1"] = "Value1" | ||
| # Add the custom dimension "CustomDimension2" with the value "Value2". | ||
| span._attributes["CustomDimension2"] = "Value2" | ||
|
|
||
| # Create a SpanEnrichingProcessor instance. | ||
| span_enrich_processor = SpanEnrichingProcessor() | ||
|
|
||
| # Pass in your processor to configuration options | ||
| configure_azure_monitor( | ||
| span_processors=[span_enrich_processor] | ||
| ) | ||
|
|
||
| tracer = trace.get_tracer(__name__) | ||
|
|
||
| with tracer.start_as_current_span("this_span_will_be_modified"): | ||
| print("Hello, World!") | ||
|
|
||
| input() |
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.