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

Bridge more incubating api #12230

Merged
merged 1 commit into from
Sep 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 @@ -5,26 +5,20 @@

package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace;

import static io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging.toAgentOrNull;

import application.io.opentelemetry.api.common.AttributeKey;
import application.io.opentelemetry.api.common.Attributes;
import application.io.opentelemetry.api.trace.Span;
import application.io.opentelemetry.api.trace.SpanBuilder;
import application.io.opentelemetry.api.trace.SpanContext;
import application.io.opentelemetry.api.trace.SpanKind;
import application.io.opentelemetry.api.trace.StatusCode;
import application.io.opentelemetry.context.Context;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

class ApplicationSpan implements Span {
public class ApplicationSpan implements Span {

private final io.opentelemetry.api.trace.Span agentSpan;

ApplicationSpan(io.opentelemetry.api.trace.Span agentSpan) {
public ApplicationSpan(io.opentelemetry.api.trace.Span agentSpan) {
this.agentSpan = agentSpan;
}

Expand Down Expand Up @@ -190,103 +184,4 @@ public String toString() {
public int hashCode() {
return agentSpan.hashCode();
}

static class Builder implements SpanBuilder {

private final io.opentelemetry.api.trace.SpanBuilder agentBuilder;

Builder(io.opentelemetry.api.trace.SpanBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setParent(Context applicationContext) {
agentBuilder.setParent(AgentContextStorage.getAgentContext(applicationContext));
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setNoParent() {
agentBuilder.setNoParent();
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder addLink(SpanContext applicationSpanContext) {
agentBuilder.addLink(Bridging.toAgent(applicationSpanContext));
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder addLink(
SpanContext applicationSpanContext, Attributes applicationAttributes) {
agentBuilder.addLink(Bridging.toAgent(applicationSpanContext));
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, String value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, long value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, double value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, boolean value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public <T> SpanBuilder setAttribute(AttributeKey<T> applicationKey, T value) {
@SuppressWarnings("unchecked")
io.opentelemetry.api.common.AttributeKey<T> agentKey = Bridging.toAgent(applicationKey);
if (agentKey != null) {
agentBuilder.setAttribute(agentKey, value);
}
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setSpanKind(SpanKind applicationSpanKind) {
io.opentelemetry.api.trace.SpanKind agentSpanKind = toAgentOrNull(applicationSpanKind);
if (agentSpanKind != null) {
agentBuilder.setSpanKind(agentSpanKind);
}
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) {
agentBuilder.setStartTimestamp(startTimestamp, unit);
return this;
}

@Override
public Span startSpan() {
return new ApplicationSpan(agentBuilder.startSpan());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace;

import static io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging.toAgentOrNull;

import application.io.opentelemetry.api.common.AttributeKey;
import application.io.opentelemetry.api.common.Attributes;
import application.io.opentelemetry.api.trace.Span;
import application.io.opentelemetry.api.trace.SpanBuilder;
import application.io.opentelemetry.api.trace.SpanContext;
import application.io.opentelemetry.api.trace.SpanKind;
import application.io.opentelemetry.context.Context;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import java.util.concurrent.TimeUnit;

public class ApplicationSpanBuilder implements SpanBuilder {

private final io.opentelemetry.api.trace.SpanBuilder agentBuilder;

protected ApplicationSpanBuilder(io.opentelemetry.api.trace.SpanBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setParent(Context applicationContext) {
agentBuilder.setParent(AgentContextStorage.getAgentContext(applicationContext));
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setNoParent() {
agentBuilder.setNoParent();
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder addLink(SpanContext applicationSpanContext) {
agentBuilder.addLink(Bridging.toAgent(applicationSpanContext));
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder addLink(SpanContext applicationSpanContext, Attributes applicationAttributes) {
agentBuilder.addLink(Bridging.toAgent(applicationSpanContext));
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, String value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, long value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, double value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setAttribute(String key, boolean value) {
agentBuilder.setAttribute(key, value);
return this;
}

@Override
@CanIgnoreReturnValue
public <T> SpanBuilder setAttribute(AttributeKey<T> applicationKey, T value) {
@SuppressWarnings("unchecked")
io.opentelemetry.api.common.AttributeKey<T> agentKey = Bridging.toAgent(applicationKey);
if (agentKey != null) {
agentBuilder.setAttribute(agentKey, value);
}
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setSpanKind(SpanKind applicationSpanKind) {
io.opentelemetry.api.trace.SpanKind agentSpanKind = toAgentOrNull(applicationSpanKind);
if (agentSpanKind != null) {
agentBuilder.setSpanKind(agentSpanKind);
}
return this;
}

@Override
@CanIgnoreReturnValue
public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) {
agentBuilder.setStartTimestamp(startTimestamp, unit);
return this;
}

@Override
public Span startSpan() {
return new ApplicationSpan(agentBuilder.startSpan());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public ApplicationTracer(io.opentelemetry.api.trace.Tracer agentTracer) {

@Override
public SpanBuilder spanBuilder(String spanName) {
return new ApplicationSpan.Builder(agentTracer.spanBuilder(spanName));
return new ApplicationSpanBuilder(agentTracer.spanBuilder(spanName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace;

public interface ApplicationTracerFactory {

ApplicationTracer newTracer(io.opentelemetry.api.trace.Tracer agentTracer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import application.io.opentelemetry.api.trace.Tracer;
import application.io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.api.GlobalOpenTelemetry;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
Expand All @@ -16,10 +15,13 @@ public class ApplicationTracerProvider implements TracerProvider {

private static final MethodHandle TRACE_PROVIDER_14 = getApplicationTracerProvider14();

protected final ApplicationTracerFactory tracerFactory;
protected final io.opentelemetry.api.trace.TracerProvider agentTracerProvider;

protected ApplicationTracerProvider(
ApplicationTracerFactory tracerFactory,
io.opentelemetry.api.trace.TracerProvider agentTracerProvider) {
this.tracerFactory = tracerFactory;
this.agentTracerProvider = agentTracerProvider;
}

Expand Down Expand Up @@ -48,17 +50,17 @@ public static TracerProvider create(
}
}

return new ApplicationTracerProvider(agentTracerProvider);
return new ApplicationTracerProvider(ApplicationTracer::new, agentTracerProvider);
}

@Override
public Tracer get(String instrumentationName) {
return new ApplicationTracer(agentTracerProvider.get(instrumentationName));
return tracerFactory.newTracer(agentTracerProvider.get(instrumentationName));
}

@Override
public Tracer get(String instrumentationName, String instrumentationVersion) {
return new ApplicationTracer(
GlobalOpenTelemetry.getTracerProvider().get(instrumentationName, instrumentationVersion));
return tracerFactory.newTracer(
agentTracerProvider.get(instrumentationName, instrumentationVersion));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging;

class ApplicationDoubleCounter implements DoubleCounter {
public class ApplicationDoubleCounter implements DoubleCounter {

private final io.opentelemetry.api.metrics.DoubleCounter agentCounter;

ApplicationDoubleCounter(io.opentelemetry.api.metrics.DoubleCounter agentCounter) {
protected ApplicationDoubleCounter(io.opentelemetry.api.metrics.DoubleCounter agentCounter) {
this.agentCounter = agentCounter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging;

final class ApplicationDoubleHistogram implements DoubleHistogram {
public class ApplicationDoubleHistogram implements DoubleHistogram {

private final io.opentelemetry.api.metrics.DoubleHistogram agentHistogram;

ApplicationDoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram agentHistogram) {
protected ApplicationDoubleHistogram(
io.opentelemetry.api.metrics.DoubleHistogram agentHistogram) {
this.agentHistogram = agentHistogram;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging;

class ApplicationDoubleUpDownCounter implements DoubleUpDownCounter {
public class ApplicationDoubleUpDownCounter implements DoubleUpDownCounter {

private final io.opentelemetry.api.metrics.DoubleUpDownCounter agentCounter;

ApplicationDoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter agentCounter) {
protected ApplicationDoubleUpDownCounter(
io.opentelemetry.api.metrics.DoubleUpDownCounter agentCounter) {
this.agentCounter = agentCounter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging;

class ApplicationLongCounter implements LongCounter {
public class ApplicationLongCounter implements LongCounter {

private final io.opentelemetry.api.metrics.LongCounter agentCounter;

ApplicationLongCounter(io.opentelemetry.api.metrics.LongCounter agentCounter) {
protected ApplicationLongCounter(io.opentelemetry.api.metrics.LongCounter agentCounter) {
this.agentCounter = agentCounter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging;

final class ApplicationLongHistogram implements LongHistogram {
public class ApplicationLongHistogram implements LongHistogram {

private final io.opentelemetry.api.metrics.LongHistogram agentHistogram;

ApplicationLongHistogram(io.opentelemetry.api.metrics.LongHistogram agentHistogram) {
protected ApplicationLongHistogram(io.opentelemetry.api.metrics.LongHistogram agentHistogram) {
this.agentHistogram = agentHistogram;
}

Expand Down
Loading
Loading