-
Notifications
You must be signed in to change notification settings - Fork 799
feat(trace): implement span start/end metrics #4880
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
anuraaga
wants to merge
19
commits into
open-telemetry:main
Choose a base branch
from
anuraaga:spanstartmetrics
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.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f6fc79c
feat(trace): implement span start/end metrics
anuraaga 95f1854
changelog
anuraaga 47cfdcc
Filter in TestBase
anuraaga d7d1a7b
Format
anuraaga 87b25ae
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 46a32e1
Fix scope check
anuraaga 93324fe
Cleanup
anuraaga 4d151a1
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga f0dde3a
Fix
anuraaga f0197d8
Temporarily switch contrib tests to PR branch
anuraaga 6f28794
Bump
anuraaga 698560f
Public symbols
anuraaga ed623f8
Restore CI
anuraaga 4dc770d
Merge branch 'main' into spanstartmetrics
xrmx 6ccccfa
Cleanup
anuraaga 3422eec
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga a97e94f
Merge branch 'main' into spanstartmetrics
xrmx 46948ad
Fix mismerge
xrmx d383e91
Fix merge
xrmx 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
85 changes: 85 additions & 0 deletions
85
opentelemetry-sdk/src/opentelemetry/sdk/trace/_tracer_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,85 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| from opentelemetry import metrics as metrics_api | ||
| from opentelemetry.sdk.trace.sampling import Decision | ||
| from opentelemetry.semconv._incubating.attributes.otel_attributes import ( | ||
| OTEL_SPAN_PARENT_ORIGIN, | ||
| OTEL_SPAN_SAMPLING_RESULT, | ||
| OtelSpanSamplingResultValues, | ||
| ) | ||
| from opentelemetry.semconv._incubating.metrics.otel_metrics import ( | ||
| create_otel_sdk_span_live, | ||
| create_otel_sdk_span_started, | ||
| ) | ||
| from opentelemetry.trace.span import SpanContext | ||
|
|
||
|
|
||
| class TracerMetrics: | ||
| def __init__(self, meter_provider: metrics_api.MeterProvider) -> None: | ||
| meter = meter_provider.get_meter("opentelemetry-sdk") | ||
|
|
||
| self._started_spans = create_otel_sdk_span_started(meter) | ||
| self._live_spans = create_otel_sdk_span_live(meter) | ||
|
|
||
| def start_span( | ||
| self, | ||
| parent_span_context: SpanContext | None, | ||
| sampling_decision: Decision, | ||
| ) -> Callable[[], None]: | ||
| sampling_result_value = sampling_result(sampling_decision) | ||
| self._started_spans.add( | ||
| 1, | ||
| { | ||
| OTEL_SPAN_PARENT_ORIGIN: parent_origin(parent_span_context), | ||
| OTEL_SPAN_SAMPLING_RESULT: sampling_result_value, | ||
| }, | ||
| ) | ||
|
|
||
| if not sampling_decision.is_recording(): | ||
| return noop | ||
|
|
||
| live_span_attrs = { | ||
| OTEL_SPAN_SAMPLING_RESULT: sampling_result_value, | ||
| } | ||
| self._live_spans.add(1, live_span_attrs) | ||
|
|
||
| def end_span() -> None: | ||
| self._live_spans.add(-1, live_span_attrs) | ||
|
|
||
| return end_span | ||
|
|
||
|
|
||
| def noop() -> None: | ||
| pass | ||
|
|
||
|
|
||
| def parent_origin(span_ctx: SpanContext | None) -> str: | ||
| if span_ctx is None: | ||
| return "none" | ||
| if span_ctx.is_remote: | ||
| return "remote" | ||
| return "local" | ||
|
|
||
|
|
||
| def sampling_result(decision: Decision) -> str: | ||
| if decision == Decision.RECORD_AND_SAMPLE: | ||
| return OtelSpanSamplingResultValues.RECORD_AND_SAMPLE.value | ||
| if decision == Decision.RECORD_ONLY: | ||
| return OtelSpanSamplingResultValues.RECORD_ONLY.value | ||
| return OtelSpanSamplingResultValues.DROP.value |
Oops, something went wrong.
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.