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

Narrow ExtendedSpanBuilder return types for chaining #6514

Merged
merged 7 commits into from
Jun 24, 2024
Merged
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 @@ -6,10 +6,17 @@
package io.opentelemetry.api.incubator.trace;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
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.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

/** Extended {@link SpanBuilder} with experimental APIs. */
Expand Down Expand Up @@ -90,4 +97,60 @@ <T, E extends Throwable> T startAndCall(
*/
<E extends Throwable> void startAndRun(
SpanRunnable<E> runnable, BiConsumer<Span, Throwable> handleException) throws E;

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setParent(Context context);
trask marked this conversation as resolved.
Show resolved Hide resolved

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setNoParent();

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder addLink(SpanContext spanContext);

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder addLink(SpanContext spanContext, Attributes attributes);

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setAttribute(String key, String value);

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setAttribute(String key, long value);

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setAttribute(String key, double value);

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setAttribute(String key, boolean value);

/** {@inheritDoc} */
@Override
<T> ExtendedSpanBuilder setAttribute(AttributeKey<T> key, T value);

/** {@inheritDoc} */
@Override
default ExtendedSpanBuilder setAllAttributes(Attributes attributes) {
return (ExtendedSpanBuilder) SpanBuilder.super.setAllAttributes(attributes);
}

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setSpanKind(SpanKind spanKind);

/** {@inheritDoc} */
@Override
ExtendedSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit);

/** {@inheritDoc} */
@Override
default ExtendedSpanBuilder setStartTimestamp(Instant startTimestamp) {
return (ExtendedSpanBuilder) SpanBuilder.super.setStartTimestamp(startTimestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void startAndCallOrRun() {
// 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 runnable variation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class SdkSpanBuilder implements ExtendedSpanBuilder {
}

@Override
public SpanBuilder setParent(Context context) {
public ExtendedSpanBuilder setParent(Context context) {
if (context == null) {
return this;
}
Expand All @@ -77,13 +77,13 @@ public SpanBuilder setParent(Context context) {
}

@Override
public SpanBuilder setNoParent() {
public ExtendedSpanBuilder setNoParent() {
this.parent = Context.root();
return this;
}

@Override
public SpanBuilder setSpanKind(SpanKind spanKind) {
public ExtendedSpanBuilder setSpanKind(SpanKind spanKind) {
if (spanKind == null) {
return this;
}
Expand All @@ -92,7 +92,7 @@ public SpanBuilder setSpanKind(SpanKind spanKind) {
}

@Override
public SpanBuilder addLink(SpanContext spanContext) {
public ExtendedSpanBuilder addLink(SpanContext spanContext) {
if (spanContext == null || !spanContext.isValid()) {
return this;
}
Expand All @@ -101,7 +101,7 @@ public SpanBuilder addLink(SpanContext spanContext) {
}

@Override
public SpanBuilder addLink(SpanContext spanContext, Attributes attributes) {
public ExtendedSpanBuilder addLink(SpanContext spanContext, Attributes attributes) {
if (spanContext == null || !spanContext.isValid()) {
return this;
}
Expand Down Expand Up @@ -135,27 +135,27 @@ private void addLink(LinkData link) {
}

@Override
public SpanBuilder setAttribute(String key, String value) {
public ExtendedSpanBuilder setAttribute(String key, String value) {
return setAttribute(stringKey(key), value);
}

@Override
public SpanBuilder setAttribute(String key, long value) {
public ExtendedSpanBuilder setAttribute(String key, long value) {
return setAttribute(longKey(key), value);
}

@Override
public SpanBuilder setAttribute(String key, double value) {
public ExtendedSpanBuilder setAttribute(String key, double value) {
return setAttribute(doubleKey(key), value);
}

@Override
public SpanBuilder setAttribute(String key, boolean value) {
public ExtendedSpanBuilder setAttribute(String key, boolean value) {
return setAttribute(booleanKey(key), value);
}

@Override
public <T> SpanBuilder setAttribute(AttributeKey<T> key, T value) {
public <T> ExtendedSpanBuilder setAttribute(AttributeKey<T> key, T value) {
if (key == null || key.getKey().isEmpty() || value == null) {
return this;
}
Expand All @@ -164,7 +164,7 @@ public <T> SpanBuilder setAttribute(AttributeKey<T> key, T value) {
}

@Override
public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) {
public ExtendedSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) {
if (startTimestamp < 0 || unit == null) {
return this;
}
Expand Down
Loading