-
Notifications
You must be signed in to change notification settings - Fork 966
Implement SDK metrics for trace #7895
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
Changes from 7 commits
968ca7b
6dc848c
a8e12ab
0d7c4dc
abd98df
3d60231
5c5e6ea
af0de7a
052587d
caf64d0
0012d26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| Comparing source compatibility of opentelemetry-sdk-trace-1.58.0-SNAPSHOT.jar against opentelemetry-sdk-trace-1.57.0.jar | ||
| No changes. | ||
| *** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.trace.export.BatchSpanProcessorBuilder (not serializable) | ||
| === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 | ||
| +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.trace.export.BatchSpanProcessorBuilder setInternalTelemetryVersion(io.opentelemetry.sdk.common.InternalTelemetryVersion) | ||
| +++ NEW INTERFACE: PUBLIC(+) ABSTRACT(+) STATIC(+) io.opentelemetry.sdk.trace.export.SpanProcessorMetrics$LongCallable (not serializable) | ||
| +++ CLASS FILE FORMAT VERSION: 52.0 <- n.a. | ||
| +++ NEW SUPERCLASS: java.lang.Object | ||
| +++ NEW METHOD: PUBLIC(+) ABSTRACT(+) long get() | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,85 @@ | ||||||
| /* | ||||||
| * Copyright The OpenTelemetry Authors | ||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||
| */ | ||||||
|
|
||||||
| package io.opentelemetry.sdk.trace; | ||||||
|
|
||||||
| import static io.opentelemetry.api.common.AttributeKey.stringKey; | ||||||
|
|
||||||
| import io.opentelemetry.api.common.AttributeKey; | ||||||
| import io.opentelemetry.api.common.Attributes; | ||||||
| import io.opentelemetry.api.metrics.LongCounter; | ||||||
| import io.opentelemetry.api.metrics.LongUpDownCounter; | ||||||
| import io.opentelemetry.api.metrics.Meter; | ||||||
| import io.opentelemetry.api.metrics.MeterProvider; | ||||||
| import io.opentelemetry.api.trace.SpanContext; | ||||||
| import io.opentelemetry.sdk.trace.samplers.SamplingDecision; | ||||||
|
|
||||||
| /** | ||||||
| * SDK metrics exported for started and ended spans as defined in the <a | ||||||
| * href="https://opentelemetry.io/docs/specs/semconv/otel/sdk-metrics/#span-metrics">semantic | ||||||
| * conventions</a>. | ||||||
| */ | ||||||
| final class SdkTracerMetrics { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #nit For alignment with
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's possible, I'd like to suggest the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fine. Personally, I think any code that records spans, metrics, or logs is instrumentation. With that said, for the foreseeable future, these do only record metrics so |
||||||
|
|
||||||
| // Visible for testing | ||||||
| static final AttributeKey<String> OTEL_SPAN_PARENT_ORIGIN = stringKey("otel.span.parent.origin"); | ||||||
| // Visible for testing | ||||||
| static final AttributeKey<String> OTEL_SPAN_SAMPLING_RESULT = | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably move those into SemConvAttributes with the corresponding tests for up-to-date-ness. |
||||||
| stringKey("otel.span.sampling_result"); | ||||||
|
|
||||||
| private final LongCounter startedSpans; | ||||||
| private final LongUpDownCounter liveSpans; | ||||||
|
|
||||||
| SdkTracerMetrics(MeterProvider meterProvider) { | ||||||
| Meter meter = meterProvider.get("io.opentelemetry.sdk.trace"); | ||||||
|
|
||||||
| startedSpans = | ||||||
| meter | ||||||
| .counterBuilder("otel.sdk.span.started") | ||||||
| .setUnit("{span}") | ||||||
| .setDescription("The number of created spans.") | ||||||
| .build(); | ||||||
| liveSpans = | ||||||
| meter | ||||||
| .upDownCounterBuilder("otel.sdk.span.live") | ||||||
| .setUnit("{span}") | ||||||
| .setDescription( | ||||||
| "The number of created spans with recording=true for which the end operation has not been called yet.") | ||||||
| .build(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Records metrics for when a span starts and returns a {@link Runnable} to execute when ending | ||||||
| * the span. | ||||||
| */ | ||||||
| Runnable startSpan(SpanContext parentSpanContext, SamplingDecision samplingDecision) { | ||||||
| startedSpans.add( | ||||||
| 1, | ||||||
| Attributes.of( | ||||||
| OTEL_SPAN_PARENT_ORIGIN, | ||||||
| parentOrigin(parentSpanContext), | ||||||
| OTEL_SPAN_SAMPLING_RESULT, | ||||||
| samplingDecision.name())); | ||||||
|
|
||||||
| if (samplingDecision == SamplingDecision.DROP) { | ||||||
| return () -> {}; | ||||||
| } | ||||||
|
|
||||||
| Attributes liveSpansAttributes = | ||||||
| Attributes.of(OTEL_SPAN_SAMPLING_RESULT, samplingDecision.name()); | ||||||
| liveSpans.add(1, liveSpansAttributes); | ||||||
| return () -> liveSpans.add(-1, liveSpansAttributes); | ||||||
|
jack-berg marked this conversation as resolved.
Outdated
|
||||||
| } | ||||||
|
|
||||||
| private static String parentOrigin(SpanContext parentSpanContext) { | ||||||
| if (!parentSpanContext.isValid()) { | ||||||
| return "none"; | ||||||
| } | ||||||
| if (parentSpanContext.isRemote()) { | ||||||
| return "remote"; | ||||||
| } | ||||||
| return "local"; | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.