Skip to content
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

make extended tracer easier to use #6943

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.opentelemetry.api.incubator.propagation.ExtendedContextPropagators;
import io.opentelemetry.api.internal.ApiUsageLogger;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
Expand All @@ -33,7 +32,7 @@ static Tracer getNoop() {
}

@Override
public SpanBuilder spanBuilder(String spanName) {
public ExtendedSpanBuilder spanBuilder(String spanName) {
return NoopSpanBuilder.create();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ public interface ExtendedTracer extends Tracer {
default boolean isEnabled() {
return true;
}

@Override
ExtendedSpanBuilder spanBuilder(String spanName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,27 +166,32 @@ void startAndCallOrRun() {

// Get a Tracer for a scope
Tracer tracer = tracerProvider.get("org.foo.my-scope");
ExtendedTracer extendedTracer = (ExtendedTracer) tracer;

// Wrap the resetCheckout method in a span
String cartId =
((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout_and_return"))
.setAttribute("key123", "val456")
.startAndCall(() -> resetCheckoutAndReturn("abc123", /* throwException= */ false));
assertThat(cartId).isEqualTo("abc123");
// ...or use ExtendedTracer instance
// ...or runnable variation
((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout"))
extendedTracer
.spanBuilder("reset_checkout")
.startAndRun(() -> resetCheckout("abc123", /* throwException= */ false));

// Wrap the resetCheckout method in a span; resetCheckout throws an exception
try {
((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout_and_return"))
extendedTracer
.spanBuilder("reset_checkout_and_return")
.startAndCall(() -> resetCheckoutAndReturn("def456", /* throwException= */ true));
} catch (Throwable e) {
// Ignore expected exception
}
// ...or runnable variation
try {
((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout"))
extendedTracer
.spanBuilder("reset_checkout")
.startAndRun(() -> resetCheckout("def456", /* throwException= */ true));
} catch (Throwable e) {
// Ignore expected exception
Expand All @@ -195,7 +200,8 @@ void startAndCallOrRun() {
// Wrap the resetCheckout method in a span; resetCheckout throws an exception; use custom error
// handler
try {
((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout_and_return"))
extendedTracer
.spanBuilder("reset_checkout_and_return")
.startAndCall(
() -> resetCheckoutAndReturn("ghi789", /* throwException= */ true),
(span, throwable) -> span.setAttribute("my-attribute", "error"));
Expand All @@ -204,7 +210,8 @@ void startAndCallOrRun() {
}
// ...or runnable variation
try {
((ExtendedSpanBuilder) tracer.spanBuilder("reset_checkout"))
extendedTracer
.spanBuilder("reset_checkout")
.startAndRun(
() -> resetCheckout("ghi789", /* throwException= */ true),
(span, throwable) -> span.setAttribute("my-attribute", "error"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.sdk.trace;

import io.opentelemetry.api.incubator.trace.ExtendedSpanBuilder;
import io.opentelemetry.api.incubator.trace.ExtendedTracer;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
Expand Down Expand Up @@ -34,16 +34,16 @@ final class SdkTracer implements ExtendedTracer {
}

@Override
public SpanBuilder spanBuilder(String spanName) {
public ExtendedSpanBuilder spanBuilder(String spanName) {
if (!tracerEnabled) {
return NOOP_TRACER.spanBuilder(spanName);
return (ExtendedSpanBuilder) NOOP_TRACER.spanBuilder(spanName);
}
if (spanName == null || spanName.trim().isEmpty()) {
spanName = FALLBACK_SPAN_NAME;
}
if (sharedState.hasBeenShutdown()) {
Tracer tracer = TracerProvider.noop().get(instrumentationScopeInfo.getName());
return tracer.spanBuilder(spanName);
return (ExtendedSpanBuilder) tracer.spanBuilder(spanName);
}
return new SdkSpanBuilder(
spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits());
Expand Down
Loading