-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04a2a48
commit d9b8f6c
Showing
6 changed files
with
471 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,11 @@ | ||
plugins { | ||
id("otel.java-conventions") | ||
id("otel.publish-conventions") | ||
} | ||
|
||
description = "Tracing Utilities" | ||
otelJava.moduleName.set("io.opentelemetry.contrib.extended-tracer") | ||
|
||
dependencies { | ||
api("io.opentelemetry:opentelemetry-api") | ||
} |
126 changes: 126 additions & 0 deletions
126
extended-tracer/src/main/java/io/opentelemetry/contrib/tracer/ExtendedSpanBuilder.java
This file contains 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,126 @@ | ||
package io.opentelemetry.contrib.tracer;/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
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 java.time.Instant; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ExtendedSpanBuilder implements SpanBuilder { | ||
|
||
private final SpanBuilder delegate; | ||
|
||
ExtendedSpanBuilder(SpanBuilder delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
/** Run the provided {@link Runnable} and wrap with a {@link Span} with the provided name. */ | ||
public void run(Runnable runnable) { | ||
Tracing.run(startSpan(), runnable); | ||
} | ||
|
||
/** Call the provided {@link Callable} and wrap with a {@link Span} with the provided name. */ | ||
public <T> T call(Callable<T> callable) { | ||
return Tracing.call(startSpan(), callable); | ||
} | ||
|
||
/** | ||
* Trace a block of code using a server span. | ||
* | ||
* <p>The span context will be extracted from the <code>transport</code>, which you usually get | ||
* from HTTP headers of the metadata of an event you're processing. | ||
*/ | ||
public <T> T traceServerSpan(Map<String, String> transport, Callable<T> callable) { | ||
return Tracing.traceServerSpan(transport, this, callable); | ||
} | ||
|
||
/** | ||
* Trace a block of code using a consumer span. | ||
* | ||
* <p>The span context will be extracted from the <code>transport</code>, which you usually get | ||
* from HTTP headers of the metadata of an event you're processing. | ||
*/ | ||
public <T> T traceConsumerSpan( | ||
Map<String, String> transport, SpanBuilder spanBuilder, Callable<T> callable) { | ||
return Tracing.traceConsumerSpan(transport, this, callable); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setParent(Context context) { | ||
return delegate.setParent(context); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setNoParent() { | ||
return delegate.setNoParent(); | ||
} | ||
|
||
@Override | ||
public SpanBuilder addLink(SpanContext spanContext) { | ||
return delegate.addLink(spanContext); | ||
} | ||
|
||
@Override | ||
public SpanBuilder addLink(SpanContext spanContext, Attributes attributes) { | ||
return delegate.addLink(spanContext, attributes); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setAttribute(String key, String value) { | ||
return delegate.setAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setAttribute(String key, long value) { | ||
return delegate.setAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setAttribute(String key, double value) { | ||
return delegate.setAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setAttribute(String key, boolean value) { | ||
return delegate.setAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public <T> SpanBuilder setAttribute(AttributeKey<T> key, T value) { | ||
return delegate.setAttribute(key, value); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setAllAttributes(Attributes attributes) { | ||
return delegate.setAllAttributes(attributes); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setSpanKind(SpanKind spanKind) { | ||
return delegate.setSpanKind(spanKind); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { | ||
return delegate.setStartTimestamp(startTimestamp, unit); | ||
} | ||
|
||
@Override | ||
public SpanBuilder setStartTimestamp(Instant startTimestamp) { | ||
return delegate.setStartTimestamp(startTimestamp); | ||
} | ||
|
||
@Override | ||
public Span startSpan() { | ||
return delegate.startSpan(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
extended-tracer/src/main/java/io/opentelemetry/contrib/tracer/ExtendedTracer.java
This file contains 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,61 @@ | ||
package io.opentelemetry.contrib.tracer;/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.context.Context; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
|
||
/** Provides easy mechanisms for wrapping standard Java constructs with an OpenTelemetry Span. */ | ||
public final class ExtendedTracer implements Tracer { | ||
|
||
private final Tracer delegate; | ||
|
||
/** Create a new {@link ExtendedTracer} that wraps the provided Tracer. */ | ||
public static ExtendedTracer create(Tracer delegate) { | ||
return new ExtendedTracer(delegate); | ||
} | ||
|
||
private ExtendedTracer(Tracer delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
/** Run the provided {@link Runnable} and wrap with a {@link Span} with the provided name. */ | ||
public void run(String spanName, Runnable runnable) { | ||
spanBuilder(spanName).run(runnable); | ||
} | ||
|
||
/** Call the provided {@link Callable} and wrap with a {@link Span} with the provided name. */ | ||
public <T> T call(String spanName, Callable<T> callable) { | ||
return spanBuilder(spanName).call(callable); | ||
} | ||
|
||
/** | ||
* Injects the current context into a string map, which can then be added to HTTP headers or the | ||
* metadata of an event. | ||
*/ | ||
public Map<String, String> injectContext() { | ||
return Tracing.injectContext(); | ||
} | ||
|
||
/** | ||
* Extract the context from a string map, which you get from HTTP headers of the metadata of an | ||
* event you're processing. | ||
*/ | ||
public static Context extractContext(Map<String, String> transport) { | ||
return Tracing.extractContext(transport); | ||
} | ||
|
||
/** Sets baggage items which are active in given block. */ | ||
public static <T> T setBaggage(Map<String, String> baggage, Callable<T> callable) { | ||
return Tracing.setBaggage(baggage, callable); | ||
} | ||
|
||
@Override | ||
public ExtendedSpanBuilder spanBuilder(String spanName) { | ||
return new ExtendedSpanBuilder(delegate.spanBuilder(spanName)); | ||
} | ||
} |
Oops, something went wrong.