-
Notifications
You must be signed in to change notification settings - Fork 924
Add OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN support to record_exception #5323
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
Open
RKest
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
RKest:record-exception-signal-opt-in
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−1
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-sdk`: `Span.record_exception` now honors `OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN` (`logs`/`logs/dup`) to record exceptions as logs instead of (or in addition to) span events |
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
138 changes: 138 additions & 0 deletions
138
opentelemetry-sdk/tests/trace/test_record_exception_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,138 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Tests for OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN handling in record_exception.""" | ||
|
|
||
| import os | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from opentelemetry._logs import SeverityNumber | ||
| from opentelemetry.sdk.trace import ( | ||
| _OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN as OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN, | ||
| ) | ||
| from opentelemetry.sdk.trace import ( | ||
| TracerProvider, | ||
| ) | ||
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
| from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( | ||
| InMemorySpanExporter, | ||
| ) | ||
|
|
||
|
|
||
| class _CapturingLogger: | ||
| """A minimal Logger that records emitted LogRecords.""" | ||
|
|
||
| def __init__(self): | ||
| self.records = [] | ||
|
|
||
| def emit(self, record): | ||
| self.records.append(record) | ||
|
|
||
|
|
||
| class TestRecordExceptionSignalOptIn(unittest.TestCase): | ||
| def setUp(self): | ||
| self.span_exporter = InMemorySpanExporter() | ||
| provider = TracerProvider() | ||
| provider.add_span_processor(SimpleSpanProcessor(self.span_exporter)) | ||
| self.tracer = provider.get_tracer("test-scope", "1.0") | ||
| self.logger = _CapturingLogger() | ||
| patcher = mock.patch( | ||
| "opentelemetry.sdk.trace._logs.get_logger", | ||
| return_value=self.logger, | ||
| ) | ||
| self.mock_get_logger = patcher.start() | ||
| self.addCleanup(patcher.stop) | ||
|
|
||
| def _raise_in_span(self): | ||
| with self.assertRaises(ValueError): | ||
| with self.tracer.start_as_current_span("op"): | ||
| raise ValueError("boom") | ||
|
|
||
| def _exception_events(self): | ||
| finished_span = self.span_exporter.get_finished_spans()[0] | ||
| return [e for e in finished_span.events if e.name == "exception"] | ||
|
|
||
| def test_unset_records_span_event_only(self): | ||
| with mock.patch.dict(os.environ, {}, clear=True): | ||
| self._raise_in_span() | ||
| self.assertEqual(len(self._exception_events()), 1) | ||
| self.assertEqual(self.logger.records, []) | ||
|
|
||
| def test_unrecognized_value_records_span_event_only(self): | ||
| with mock.patch.dict( | ||
| os.environ, {OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN: "bogus"} | ||
| ): | ||
| self._raise_in_span() | ||
| self.assertEqual(len(self._exception_events()), 1) | ||
| self.assertEqual(self.logger.records, []) | ||
|
|
||
| def test_logs_records_log_only(self): | ||
| with mock.patch.dict( | ||
| os.environ, {OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN: "logs"} | ||
| ): | ||
| self._raise_in_span() | ||
| self.assertEqual(self._exception_events(), []) | ||
| self.assertEqual(len(self.logger.records), 1) | ||
| record = self.logger.records[0] | ||
| self.assertEqual(record.event_name, "exception") | ||
| self.assertEqual(record.severity_number, SeverityNumber.ERROR) | ||
| self.assertEqual(record.attributes["exception.type"], "ValueError") | ||
| self.assertEqual(record.attributes["exception.message"], "boom") | ||
| self.assertIn( | ||
| "ValueError: boom", record.attributes["exception.stacktrace"] | ||
| ) | ||
| # The deprecated exception.escaped attribute is not set on logs. | ||
| self.assertNotIn("exception.escaped", record.attributes) | ||
|
|
||
| def test_logs_dup_records_both(self): | ||
| with mock.patch.dict( | ||
| os.environ, {OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN: "logs/dup"} | ||
| ): | ||
| self._raise_in_span() | ||
| self.assertEqual(len(self._exception_events()), 1) | ||
| self.assertEqual(len(self.logger.records), 1) | ||
|
|
||
| def test_log_is_correlated_with_span(self): | ||
| with mock.patch.dict( | ||
| os.environ, {OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN: "logs"} | ||
| ): | ||
| self._raise_in_span() | ||
| finished_span = self.span_exporter.get_finished_spans()[0] | ||
| record = self.logger.records[0] | ||
| self.assertEqual(record.trace_id, finished_span.context.trace_id) | ||
| self.assertEqual(record.span_id, finished_span.context.span_id) | ||
|
|
||
| def test_logs_uses_instrumentation_scope_for_logger(self): | ||
| with mock.patch.dict( | ||
| os.environ, {OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN: "logs"} | ||
| ): | ||
| self._raise_in_span() | ||
| # The logger is obtained using the span's instrumentation scope. | ||
| args, _ = self.mock_get_logger.call_args | ||
| self.assertEqual(args[0], "test-scope") | ||
|
|
||
| def test_directly_recorded_exception_is_logged(self): | ||
| with mock.patch.dict( | ||
| os.environ, {OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN: "logs"} | ||
| ): | ||
| with self.tracer.start_as_current_span("op") as span: | ||
| try: | ||
| raise ValueError("handled") | ||
| except ValueError as err: | ||
| span.record_exception(err) | ||
| record = self.logger.records[0] | ||
| self.assertEqual(record.event_name, "exception") | ||
| self.assertEqual(record.severity_number, SeverityNumber.ERROR) | ||
| self.assertEqual(record.attributes["exception.type"], "ValueError") | ||
|
|
||
| def test_extra_attributes_forwarded_to_log(self): | ||
| with mock.patch.dict( | ||
| os.environ, {OTEL_SEMCONV_EXCEPTION_SIGNAL_OPT_IN: "logs"} | ||
| ): | ||
| with self.tracer.start_as_current_span("op") as span: | ||
| span.record_exception( | ||
| ValueError("boom"), attributes={"custom.key": "v"} | ||
| ) | ||
| record = self.logger.records[0] | ||
| self.assertEqual(record.attributes["custom.key"], "v") |
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.