Skip to content

Commit

Permalink
Bridge incubator metrics apis (#9884)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Nov 17, 2023
1 parent 7400025 commit 9fb5b3f
Show file tree
Hide file tree
Showing 29 changed files with 1,074 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import application.io.opentelemetry.api.trace.StatusCode;
import application.io.opentelemetry.api.trace.TraceState;
import application.io.opentelemetry.api.trace.TraceStateBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -143,6 +145,16 @@ public static io.opentelemetry.api.common.AttributeKey toAgent(AttributeKey appl
return null;
}

public static List<io.opentelemetry.api.common.AttributeKey<?>> toAgent(
List<AttributeKey<?>> attributeKeys) {
List<io.opentelemetry.api.common.AttributeKey<?>> result =
new ArrayList<>(attributeKeys.size());
for (AttributeKey<?> attributeKey : attributeKeys) {
result.add(toAgent(attributeKey));
}
return result;
}

public static io.opentelemetry.api.trace.StatusCode toAgent(StatusCode applicationStatus) {
io.opentelemetry.api.trace.StatusCode agentCanonicalCode;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.function.Consumer;

final class ApplicationDoubleCounterBuilder implements DoubleCounterBuilder {
public class ApplicationDoubleCounterBuilder implements DoubleCounterBuilder {

private final io.opentelemetry.api.metrics.DoubleCounterBuilder agentBuilder;

ApplicationDoubleCounterBuilder(io.opentelemetry.api.metrics.DoubleCounterBuilder agentBuilder) {
protected ApplicationDoubleCounterBuilder(
io.opentelemetry.api.metrics.DoubleCounterBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.function.Consumer;

final class ApplicationDoubleGaugeBuilder implements DoubleGaugeBuilder {
public class ApplicationDoubleGaugeBuilder implements DoubleGaugeBuilder {

private final io.opentelemetry.api.metrics.DoubleGaugeBuilder agentBuilder;

ApplicationDoubleGaugeBuilder(io.opentelemetry.api.metrics.DoubleGaugeBuilder agentBuilder) {
protected ApplicationDoubleGaugeBuilder(
io.opentelemetry.api.metrics.DoubleGaugeBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import application.io.opentelemetry.api.metrics.LongHistogramBuilder;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

final class ApplicationDoubleHistogramBuilder implements DoubleHistogramBuilder {
public class ApplicationDoubleHistogramBuilder implements DoubleHistogramBuilder {

private final io.opentelemetry.api.metrics.DoubleHistogramBuilder agentBuilder;

ApplicationDoubleHistogramBuilder(
protected ApplicationDoubleHistogramBuilder(
io.opentelemetry.api.metrics.DoubleHistogramBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.function.Consumer;

final class ApplicationDoubleUpDownCounterBuilder implements DoubleUpDownCounterBuilder {
public class ApplicationDoubleUpDownCounterBuilder implements DoubleUpDownCounterBuilder {

private final io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder agentBuilder;

ApplicationDoubleUpDownCounterBuilder(
protected ApplicationDoubleUpDownCounterBuilder(
io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.function.Consumer;

final class ApplicationLongCounterBuilder implements LongCounterBuilder {
public class ApplicationLongCounterBuilder implements LongCounterBuilder {

private final io.opentelemetry.api.metrics.LongCounterBuilder agentBuilder;

ApplicationLongCounterBuilder(io.opentelemetry.api.metrics.LongCounterBuilder agentBuilder) {
protected ApplicationLongCounterBuilder(
io.opentelemetry.api.metrics.LongCounterBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.function.Consumer;

final class ApplicationLongGaugeBuilder implements LongGaugeBuilder {
public class ApplicationLongGaugeBuilder implements LongGaugeBuilder {

private final io.opentelemetry.api.metrics.LongGaugeBuilder agentBuilder;

ApplicationLongGaugeBuilder(io.opentelemetry.api.metrics.LongGaugeBuilder agentBuilder) {
protected ApplicationLongGaugeBuilder(
io.opentelemetry.api.metrics.LongGaugeBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import application.io.opentelemetry.api.metrics.LongHistogramBuilder;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

final class ApplicationLongHistogramBuilder implements LongHistogramBuilder {
public class ApplicationLongHistogramBuilder implements LongHistogramBuilder {

private final io.opentelemetry.api.metrics.LongHistogramBuilder agentBuilder;

ApplicationLongHistogramBuilder(io.opentelemetry.api.metrics.LongHistogramBuilder agentBuilder) {
protected ApplicationLongHistogramBuilder(
io.opentelemetry.api.metrics.LongHistogramBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.function.Consumer;

final class ApplicationLongUpDownCounterBuilder implements LongUpDownCounterBuilder {
public class ApplicationLongUpDownCounterBuilder implements LongUpDownCounterBuilder {

private final io.opentelemetry.api.metrics.LongUpDownCounterBuilder agentBuilder;

ApplicationLongUpDownCounterBuilder(
protected ApplicationLongUpDownCounterBuilder(
io.opentelemetry.api.metrics.LongUpDownCounterBuilder agentBuilder) {
this.agentBuilder = agentBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_10.metrics.ApplicationMeter;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_10.metrics.ObservableMeasurementWrapper;

class ApplicationMeter115 extends ApplicationMeter {
public class ApplicationMeter115 extends ApplicationMeter {

private final io.opentelemetry.api.metrics.Meter agentMeter;

ApplicationMeter115(io.opentelemetry.api.metrics.Meter agentMeter) {
protected ApplicationMeter115(io.opentelemetry.api.metrics.Meter agentMeter) {
super(agentMeter);
this.agentMeter = agentMeter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import application.io.opentelemetry.api.trace.TracerProvider;
import application.io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.propagation.ApplicationContextPropagators;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_10.metrics.ApplicationMeterFactory;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_10.metrics.ApplicationMeterProvider;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_15.metrics.ApplicationMeterFactory115;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_27.logs.ApplicationLoggerProvider;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_4.trace.ApplicationTracerProvider14;
import java.lang.reflect.InvocationTargetException;

public final class ApplicationOpenTelemetry127 implements OpenTelemetry {

Expand All @@ -36,8 +38,7 @@ private ApplicationOpenTelemetry127() {
applicationContextPropagators =
new ApplicationContextPropagators(agentOpenTelemetry.getPropagators());
applicationMeterProvider =
new ApplicationMeterProvider(
new ApplicationMeterFactory115(), agentOpenTelemetry.getMeterProvider());
new ApplicationMeterProvider(getMeterFactory(), agentOpenTelemetry.getMeterProvider());
applicationLoggerProvider = new ApplicationLoggerProvider(agentOpenTelemetry.getLogsBridge());
}

Expand All @@ -60,4 +61,20 @@ public LoggerProvider getLogsBridge() {
public ContextPropagators getPropagators() {
return applicationContextPropagators;
}

private static ApplicationMeterFactory getMeterFactory() {
try {
// this class is defined in opentelemetry-api-1.31
Class<?> clazz =
Class.forName(
"io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_31.metrics.ApplicationMeterFactory131");
return (ApplicationMeterFactory) clazz.getConstructor().newInstance();
} catch (ClassNotFoundException
| NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException exception) {
return new ApplicationMeterFactory115();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("otel.javaagent-instrumentation")
}

dependencies {
compileOnly(project(":opentelemetry-api-shaded-for-instrumenting", configuration = "v1_31"))
compileOnly("io.opentelemetry:opentelemetry-extension-incubator")

implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent"))
implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.4:javaagent"))
implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.10:javaagent"))
implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.15:javaagent"))
implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.27:javaagent"))

testImplementation("io.opentelemetry:opentelemetry-extension-incubator")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import static java.util.Collections.singletonList;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class OpenTelemetryApiInstrumentationModule extends InstrumentationModule {
public OpenTelemetryApiInstrumentationModule() {
super("opentelemetry-api", "opentelemetry-api-1.31");
}

@Override
public boolean isIndyModule() {
return false;
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new OpenTelemetryInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.none;

import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_31.metrics.ApplicationMeterFactory131;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class OpenTelemetryInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("application.io.opentelemetry.api.GlobalOpenTelemetry");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
none(), OpenTelemetryInstrumentation.class.getName() + "$InitAdvice");
}

@SuppressWarnings({"ReturnValueIgnored", "unused"})
public static class InitAdvice {
@Advice.OnMethodEnter
public static void init() {
// the sole purpose of this advice is to ensure that ApplicationMeterFactory131 is recognized
// as helper class and injected into class loader
ApplicationMeterFactory131.class.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_31.metrics;

import application.io.opentelemetry.api.common.AttributeKey;
import application.io.opentelemetry.extension.incubator.metrics.ExtendedDoubleCounterBuilder;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_10.metrics.ApplicationDoubleCounterBuilder;
import java.util.List;

final class ApplicationDoubleCounterBuilder131 extends ApplicationDoubleCounterBuilder
implements ExtendedDoubleCounterBuilder {

private final io.opentelemetry.api.metrics.DoubleCounterBuilder agentBuilder;

ApplicationDoubleCounterBuilder131(
io.opentelemetry.api.metrics.DoubleCounterBuilder agentBuilder) {
super(agentBuilder);
this.agentBuilder = agentBuilder;
}

@Override
public ExtendedDoubleCounterBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
((io.opentelemetry.extension.incubator.metrics.ExtendedDoubleCounterBuilder) agentBuilder)
.setAttributesAdvice(Bridging.toAgent(attributes));
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_31.metrics;

import application.io.opentelemetry.api.common.AttributeKey;
import application.io.opentelemetry.api.common.Attributes;
import application.io.opentelemetry.api.metrics.LongGaugeBuilder;
import application.io.opentelemetry.extension.incubator.metrics.DoubleGauge;
import application.io.opentelemetry.extension.incubator.metrics.ExtendedDoubleGaugeBuilder;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging;
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_10.metrics.ApplicationDoubleGaugeBuilder;
import java.util.List;

final class ApplicationDoubleGaugeBuilder131 extends ApplicationDoubleGaugeBuilder
implements ExtendedDoubleGaugeBuilder {

private final io.opentelemetry.api.metrics.DoubleGaugeBuilder agentBuilder;

ApplicationDoubleGaugeBuilder131(io.opentelemetry.api.metrics.DoubleGaugeBuilder agentBuilder) {
super(agentBuilder);
this.agentBuilder = agentBuilder;
}

@Override
public LongGaugeBuilder ofLongs() {
return new ApplicationLongGaugeBuilder131(agentBuilder.ofLongs());
}

@Override
public DoubleGauge build() {
io.opentelemetry.extension.incubator.metrics.DoubleGauge agentDoubleGauge =
((io.opentelemetry.extension.incubator.metrics.ExtendedDoubleGaugeBuilder) agentBuilder)
.build();
return new DoubleGauge() {

@Override
public void set(double value) {
agentDoubleGauge.set(value);
}

@Override
public void set(double value, Attributes attributes) {
agentDoubleGauge.set(value, Bridging.toAgent(attributes));
}
};
}

@Override
public ExtendedDoubleGaugeBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
((io.opentelemetry.extension.incubator.metrics.ExtendedDoubleGaugeBuilder) agentBuilder)
.setAttributesAdvice(Bridging.toAgent(attributes));
return this;
}
}
Loading

0 comments on commit 9fb5b3f

Please sign in to comment.