Skip to content

Commit

Permalink
add extended tracer module
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 23, 2023
1 parent 04a2a48 commit d9b8f6c
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/scripts/draft-change-log-entries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ component_names["jmx-metrics/"]="JMX metrics"
component_names["maven-extension/"]="Maven extension"
component_names["micrometer-meter-provider/"]="Micrometer MeterProvider"
component_names["noop-api/"]="No-op API"
component_names["extended-tracer/"]="Extended Tracer"
component_names["prometheus-client-bridge/"]="Prometheus client bridge"
component_names["runtime-attach/"]="Runtime attach"
component_names["resource-providers/"]="Resource providers"
Expand Down
11 changes: 11 additions & 0 deletions extended-tracer/build.gradle.kts
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")
}
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();
}
}
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));
}
}
Loading

0 comments on commit d9b8f6c

Please sign in to comment.