-
Notifications
You must be signed in to change notification settings - Fork 549
Discard open spans after 10 minutes #2801
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
9 commits
Select commit
Hold shift + click to select a range
99b1ad0
Discard open spans after 10 minutes
antonpirker 7239adf
Merge branch 'master' into antonpirker/otel-memory-leak
antonpirker f058dfb
Apply suggestions from code review
antonpirker da48aa1
Made function private
antonpirker 4ee9535
Clarifying comments
antonpirker d426e1a
Update sentry_sdk/integrations/opentelemetry/span_processor.py
antonpirker b5cc262
Fixed prune
antonpirker abc865c
Merge branch 'master' into antonpirker/otel-memory-leak
antonpirker 2aa4247
Linting
antonpirker 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 |
---|---|---|
|
@@ -531,3 +531,95 @@ def test_link_trace_context_to_error_event(): | |
assert "contexts" in event | ||
assert "trace" in event["contexts"] | ||
assert event["contexts"]["trace"] == fake_trace_context | ||
|
||
|
||
def test_pruning_old_spans_on_start(): | ||
otel_span = MagicMock() | ||
otel_span.name = "Sample OTel Span" | ||
otel_span.start_time = time.time_ns() | ||
span_context = SpanContext( | ||
trace_id=int("1234567890abcdef1234567890abcdef", 16), | ||
span_id=int("1234567890abcdef", 16), | ||
is_remote=True, | ||
) | ||
otel_span.get_span_context.return_value = span_context | ||
otel_span.parent = MagicMock() | ||
otel_span.parent.span_id = int("abcdef1234567890", 16) | ||
|
||
parent_context = {} | ||
fake_client = MagicMock() | ||
fake_client.options = {"instrumenter": "otel"} | ||
fake_client.dsn = "https://[email protected]/123456" | ||
|
||
current_hub = MagicMock() | ||
current_hub.client = fake_client | ||
|
||
fake_hub = MagicMock() | ||
fake_hub.current = current_hub | ||
|
||
with mock.patch( | ||
"sentry_sdk.integrations.opentelemetry.span_processor.Hub", fake_hub | ||
): | ||
span_processor = SentrySpanProcessor() | ||
|
||
span_processor.otel_span_map = { | ||
"111111111abcdef": MagicMock(), # should stay | ||
"2222222222abcdef": MagicMock(), # should go | ||
"3333333333abcdef": MagicMock(), # should go | ||
} | ||
current_time_minutes = int(time.time() / 60) | ||
span_processor.open_spans = { | ||
current_time_minutes - 3: {"111111111abcdef"}, # should stay | ||
current_time_minutes | ||
- 11: {"2222222222abcdef", "3333333333abcdef"}, # should go | ||
} | ||
|
||
span_processor.on_start(otel_span, parent_context) | ||
assert sorted(list(span_processor.otel_span_map.keys())) == [ | ||
"111111111abcdef", | ||
"1234567890abcdef", | ||
] | ||
assert sorted(list(span_processor.open_spans.values())) == [ | ||
{"111111111abcdef"}, | ||
{"1234567890abcdef"}, | ||
] | ||
|
||
|
||
def test_pruning_old_spans_on_end(): | ||
otel_span = MagicMock() | ||
otel_span.name = "Sample OTel Span" | ||
otel_span.start_time = time.time_ns() | ||
span_context = SpanContext( | ||
trace_id=int("1234567890abcdef1234567890abcdef", 16), | ||
span_id=int("1234567890abcdef", 16), | ||
is_remote=True, | ||
) | ||
otel_span.get_span_context.return_value = span_context | ||
otel_span.parent = MagicMock() | ||
otel_span.parent.span_id = int("abcdef1234567890", 16) | ||
|
||
fake_sentry_span = MagicMock(spec=Span) | ||
fake_sentry_span.set_context = MagicMock() | ||
fake_sentry_span.finish = MagicMock() | ||
|
||
span_processor = SentrySpanProcessor() | ||
span_processor._get_otel_context = MagicMock() | ||
span_processor._update_span_with_otel_data = MagicMock() | ||
|
||
span_processor.otel_span_map = { | ||
"111111111abcdef": MagicMock(), # should stay | ||
"2222222222abcdef": MagicMock(), # should go | ||
"3333333333abcdef": MagicMock(), # should go | ||
"1234567890abcdef": fake_sentry_span, # should go (because it is closed) | ||
} | ||
current_time_minutes = int(time.time() / 60) | ||
span_processor.open_spans = { | ||
current_time_minutes: {"1234567890abcdef"}, # should go (because it is closed) | ||
current_time_minutes - 3: {"111111111abcdef"}, # should stay | ||
current_time_minutes | ||
- 11: {"2222222222abcdef", "3333333333abcdef"}, # should go | ||
} | ||
|
||
span_processor.on_end(otel_span) | ||
assert sorted(list(span_processor.otel_span_map.keys())) == ["111111111abcdef"] | ||
assert sorted(list(span_processor.open_spans.values())) == [{"111111111abcdef"}] | ||
antonpirker marked this conversation as resolved.
Show resolved
Hide resolved
|
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.