-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Added custom log record processors configuration option #43713
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
04c3a95
Added custom log record processors configuration option
rads-1996 ada4731
Updated CHANGELOG
rads-1996 bc6141c
Update sdk/monitor/azure-monitor-opentelemetry/README.md
rads-1996 8bcc7ba
Update sdk/monitor/azure-monitor-opentelemetry/samples/logging/modify…
rads-1996 c9ee101
Update sdk/monitor/azure-monitor-opentelemetry/README.md
rads-1996 23f406b
Update README
rads-1996 71b2ea6
Fix cspell
rads-1996 4ba2671
Fix lint
rads-1996 3147965
Fix docstring
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
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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # 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 opentelemetry import trace | ||
| from opentelemetry.sdk._logs import LogRecordProcessor, LogData | ||
| from azure.monitor.opentelemetry.exporter._generated.models import ContextTagKeys | ||
|
|
||
| logger = getLogger(__name__) | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
|
|
||
|
|
||
| class LogRecordEnrichingProcessor(LogRecordProcessor): | ||
| """ | ||
| A log record processor that enriches log records with operation name | ||
|
rads-1996 marked this conversation as resolved.
Outdated
|
||
| from the current span context. | ||
| """ | ||
|
|
||
| def force_flush(self, timeout_millis: int = 30000) -> bool: | ||
| """Force flush any pending log records.""" | ||
| return True | ||
|
|
||
| def shutdown(self) -> None: | ||
| """Shutdown the processor.""" | ||
| pass | ||
|
|
||
| def on_emit(self, log_data: LogData) -> None: | ||
| """ | ||
| Enrich the log record with operation name from the current span. | ||
|
|
||
| Args: | ||
| log_data: The log data containing log record and context information | ||
| """ | ||
| # Get the current span from the current context (not from trace_context which might be None) | ||
| current_span = trace.get_current_span() | ||
|
|
||
| if current_span and hasattr(current_span, 'name') and current_span.name: | ||
| # Add the operation name to log record attributes | ||
| if log_data.log_record.attributes is None: | ||
| log_data.log_record.attributes = {} | ||
| log_data.log_record.attributes[ContextTagKeys.AI_OPERATION_NAME] = current_span.name | ||
|
|
||
|
|
||
| # 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] | ||
| ) | ||
|
|
||
| # Your application code here | ||
| 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() | ||
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
Oops, something went wrong.
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.