-
Notifications
You must be signed in to change notification settings - Fork 828
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
Micrometer bridge instrumentation #4919
Changes from 3 commits
579ee6f
944b071
1f958f9
bb6278c
8419ea5
4b38619
7f8bbd3
1a9fe56
88457a0
aee1779
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
dependencies { | ||
library("io.micrometer:micrometer-core:1.5.0") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pretty old version, it is not supported anymore, I would go with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a compile dependency, users bring in the version that they want. Our standard practice is to target the lowest version we support as the compile dependency, while running tests on that and the latest version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get that but do you want to support a version that reached its end of life and nobody should use it in prod? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will support all versions from 1.5 upwards, 1.8 included. As Anuraag mentioned, we generally aim to support the lowest version possible - the javaagent is often attached to applications that haven't been updated for some time; and it is supposed to work with no code changes (like upgrading the micrometer dependency version) at all. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; | ||
import java.lang.ref.WeakReference; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Consumer; | ||
import java.util.function.ToDoubleFunction; | ||
|
||
final class AsyncInstrumentRegistry { | ||
|
||
private final Meter meter; | ||
private final ConcurrentMap<String, GaugeMeasurementsRecorder> gauges = new ConcurrentHashMap<>(); | ||
|
||
AsyncInstrumentRegistry(Meter meter) { | ||
this.meter = meter; | ||
} | ||
|
||
<T> void buildGauge( | ||
String name, | ||
String description, | ||
String baseUnit, | ||
Attributes attributes, | ||
T obj, | ||
ToDoubleFunction<T> objMetric) { | ||
gauges | ||
.computeIfAbsent( | ||
name, | ||
n -> { | ||
GaugeMeasurementsRecorder recorder = new GaugeMeasurementsRecorder(); | ||
meter | ||
.gaugeBuilder(name) | ||
.setDescription(description) | ||
.setUnit(baseUnit) | ||
.buildWithCallback(recorder); | ||
return recorder; | ||
}) | ||
.addGaugeMeasurement(attributes, obj, objMetric); | ||
} | ||
|
||
void removeGauge(String name, Attributes attributes) { | ||
GaugeMeasurementsRecorder recorder = gauges.get(name); | ||
if (recorder != null) { | ||
recorder.removeGaugeMeasurement(attributes); | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
private static final class GaugeMeasurementsRecorder | ||
implements Consumer<ObservableDoubleMeasurement> { | ||
|
||
private final ConcurrentMap<Attributes, GaugeInfo> measurements = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void accept(ObservableDoubleMeasurement measurement) { | ||
measurements.forEach( | ||
(attributes, gauge) -> { | ||
Object obj = gauge.objWeakRef.get(); | ||
if (obj != null) { | ||
measurement.record(gauge.metricFunction.applyAsDouble(obj), attributes); | ||
} | ||
}); | ||
} | ||
|
||
<T> void addGaugeMeasurement(Attributes attributes, T obj, ToDoubleFunction<T> objMetric) { | ||
measurements.put(attributes, new GaugeInfo(obj, (ToDoubleFunction<Object>) objMetric)); | ||
} | ||
|
||
void removeGaugeMeasurement(Attributes attributes) { | ||
measurements.remove(attributes); | ||
} | ||
} | ||
|
||
private static final class GaugeInfo { | ||
|
||
private final WeakReference<Object> objWeakRef; | ||
private final ToDoubleFunction<Object> metricFunction; | ||
|
||
private GaugeInfo(Object obj, ToDoubleFunction<Object> metricFunction) { | ||
this.objWeakRef = new WeakReference<>(obj); | ||
this.metricFunction = metricFunction; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; | ||
|
||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.instrumentation.api.cache.Cache; | ||
|
||
final class Bridging { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BridgingUtil? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel strongly. If there's precedent for "Bridging", let's keep it. |
||
|
||
private static final Cache<String, AttributeKey<String>> tagsCache = Cache.bounded(64); | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static Attributes toAttributes(Iterable<Tag> tags) { | ||
if (!tags.iterator().hasNext()) { | ||
return Attributes.empty(); | ||
} | ||
AttributesBuilder builder = Attributes.builder(); | ||
for (Tag tag : tags) { | ||
builder.put(tagsCache.computeIfAbsent(tag.getKey(), AttributeKey::stringKey), tag.getValue()); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
static String description(Meter.Id id) { | ||
String description = id.getDescription(); | ||
return description == null ? "" : description; | ||
} | ||
|
||
static String baseUnit(Meter.Id id) { | ||
String baseUnit = id.getBaseUnit(); | ||
return baseUnit == null ? "1" : baseUnit; | ||
} | ||
|
||
private Bridging() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isTypeInitializer; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.micrometer.core.instrument.Metrics; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class MetricsInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("io.micrometer.core.instrument.Metrics"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isTypeInitializer(), this.getClass().getName() + "$StaticInitializerAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class StaticInitializerAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onExit() { | ||
Metrics.addRegistry(OpenTelemetryMeterRegistry.create()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class MicrometerInstrumentationModule extends InstrumentationModule { | ||
|
||
public MicrometerInstrumentationModule() { | ||
super("micrometer", "micrometer-1.5"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
// added in 1.5 | ||
return hasClassesNamed("io.micrometer.core.instrument.config.validate.Validated"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Collections.singletonList(new MetricsInstrumentation()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.micrometer.v1_5.Bridging.baseUnit; | ||
import static io.opentelemetry.javaagent.instrumentation.micrometer.v1_5.Bridging.description; | ||
import static io.opentelemetry.javaagent.instrumentation.micrometer.v1_5.Bridging.toAttributes; | ||
|
||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.Measurement; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.DoubleCounter; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import java.util.Collections; | ||
|
||
final class OpenTelemetryCounter implements Counter, RemovableMeter { | ||
|
||
private final Id id; | ||
// TODO: use bound instruments when they're available | ||
private final DoubleCounter otelCounter; | ||
private final Attributes attributes; | ||
|
||
volatile boolean removed = false; | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
OpenTelemetryCounter(Id id, Meter otelMeter) { | ||
this.id = id; | ||
this.otelCounter = | ||
otelMeter | ||
.counterBuilder(id.getName()) | ||
.setDescription(description(id)) | ||
.setUnit(baseUnit(id)) | ||
.ofDoubles() | ||
.build(); | ||
this.attributes = toAttributes(id.getTags()); | ||
} | ||
|
||
@Override | ||
public void increment(double v) { | ||
if (removed) { | ||
return; | ||
} | ||
otelCounter.add(v, attributes); | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public double count() { | ||
// OpenTelemetry metrics bridge does not support reading measurements | ||
return Double.NaN; | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public Iterable<Measurement> measure() { | ||
// OpenTelemetry metrics bridge does not support reading measurements | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public Id getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public void onRemove() { | ||
removed = true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.micrometer.v1_5.Bridging.baseUnit; | ||
import static io.opentelemetry.javaagent.instrumentation.micrometer.v1_5.Bridging.description; | ||
import static io.opentelemetry.javaagent.instrumentation.micrometer.v1_5.Bridging.toAttributes; | ||
|
||
import io.micrometer.core.instrument.Gauge; | ||
import io.micrometer.core.instrument.Measurement; | ||
import io.opentelemetry.api.common.Attributes; | ||
import java.util.Collections; | ||
import java.util.function.ToDoubleFunction; | ||
|
||
final class OpenTelemetryGauge<T> implements Gauge, RemovableMeter { | ||
|
||
private final Id id; | ||
private final Attributes attributes; | ||
private final AsyncInstrumentRegistry asyncInstrumentRegistry; | ||
|
||
OpenTelemetryGauge( | ||
Id id, | ||
T obj, | ||
ToDoubleFunction<T> objMetric, | ||
AsyncInstrumentRegistry asyncInstrumentRegistry) { | ||
this.id = id; | ||
this.attributes = toAttributes(id.getTags()); | ||
this.asyncInstrumentRegistry = asyncInstrumentRegistry; | ||
|
||
asyncInstrumentRegistry.buildGauge( | ||
id.getName(), description(id), baseUnit(id), attributes, obj, objMetric); | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public double value() { | ||
// OpenTelemetry metrics bridge does not support reading measurements | ||
return Double.NaN; | ||
} | ||
|
||
@Override | ||
public Iterable<Measurement> measure() { | ||
// OpenTelemetry metrics bridge does not support reading measurements | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public Id getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public void onRemove() { | ||
asyncInstrumentRegistry.removeGauge(id.getName(), attributes); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this make sense as library instrumentation as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+100
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that this would make a very useful library instrumentation some time after opening this PR 😅
I'll split it out in one of the next PRs - once all meters/instruments are implemented.