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

Micrometer bridge instrumentation #4919

Merged
merged 10 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,7 @@
plugins {
id("otel.javaagent-instrumentation")
Copy link
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+100

Copy link
Member Author

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.

}

dependencies {
library("io.micrometer:micrometer-core:1.5.0")
Copy link
Member

Choose a reason for hiding this comment

The 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 1.8.x (latest).

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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?
Currently the oldest version that we support is the 1.7.x line which will reach its EOL in May.

Copy link
Member Author

Choose a reason for hiding this comment

The 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,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BridgingUtil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that Util adds anything useful to the name; and we already have a couple of *Bridging classes in the codebase. I don't really have a strong opinion though, both names are fine.

Copy link
Member

Choose a reason for hiding this comment

The 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,70 @@
/*
* 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 io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
import java.util.Collections;
import java.util.function.ToDoubleFunction;

final class OpenTelemetryGauge<T> implements Gauge, RemovableMeter {

private final Id id;
private final T obj;
private final ToDoubleFunction<T> objMetric;
private final Attributes attributes;

volatile boolean removed = false;

OpenTelemetryGauge(Id id, T obj, ToDoubleFunction<T> objMetric, Meter otelMeter) {
this.id = id;
this.obj = obj;
this.objMetric = objMetric;
this.attributes = toAttributes(id.getTags());

otelMeter
.gaugeBuilder(id.getName())
.setDescription(description(id))
.setUnit(baseUnit(id))
.buildWithCallback(this::recordMeasurements);
}

private void recordMeasurements(ObservableDoubleMeasurement measurement) {
if (removed) {
return;
}
measurement.record(objMetric.applyAsDouble(obj), attributes);
}

@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() {
removed = true;
}
}
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.micrometer.v1_5;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.FunctionTimer;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.Measurement;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.HistogramGauges;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.opentelemetry.api.GlobalOpenTelemetry;
import java.util.concurrent.TimeUnit;
import java.util.function.ToDoubleFunction;
import java.util.function.ToLongFunction;

public final class OpenTelemetryMeterRegistry extends MeterRegistry {

private static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer-1.5";

public static MeterRegistry create() {
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
OpenTelemetryMeterRegistry openTelemetryMeterRegistry =
new OpenTelemetryMeterRegistry(
Clock.SYSTEM, GlobalOpenTelemetry.get().getMeterProvider().get(INSTRUMENTATION_NAME));
openTelemetryMeterRegistry.config().onMeterRemoved(OpenTelemetryMeterRegistry::onMeterRemoved);
return openTelemetryMeterRegistry;
}

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

private OpenTelemetryMeterRegistry(Clock clock, io.opentelemetry.api.metrics.Meter otelMeter) {
super(clock);
this.otelMeter = otelMeter;
}

@Override
protected <T> Gauge newGauge(Meter.Id id, T t, ToDoubleFunction<T> toDoubleFunction) {
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
return new OpenTelemetryGauge<>(id, t, toDoubleFunction, otelMeter);
}

@Override
protected Counter newCounter(Meter.Id id) {
return new OpenTelemetryCounter(id, otelMeter);
}

@Override
protected LongTaskTimer newLongTaskTimer(
Meter.Id id, DistributionStatisticConfig distributionStatisticConfig) {
throw new UnsupportedOperationException("Not implemented yet");
}

@Override
protected Timer newTimer(
Meter.Id id,
DistributionStatisticConfig distributionStatisticConfig,
PauseDetector pauseDetector) {
OpenTelemetryTimer timer =
new OpenTelemetryTimer(id, clock, distributionStatisticConfig, pauseDetector, otelMeter);
if (timer.isUsingMicrometerHistograms()) {
HistogramGauges.registerWithCommonFormat(timer, this);
}
return timer;
}

@Override
protected DistributionSummary newDistributionSummary(
Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, double v) {
throw new UnsupportedOperationException("Not implemented yet");
}

@Override
protected Meter newMeter(Meter.Id id, Meter.Type type, Iterable<Measurement> iterable) {
throw new UnsupportedOperationException("Not implemented yet");
}

@Override
protected <T> FunctionTimer newFunctionTimer(
Meter.Id id,
T t,
ToLongFunction<T> toLongFunction,
ToDoubleFunction<T> toDoubleFunction,
TimeUnit timeUnit) {
throw new UnsupportedOperationException("Not implemented yet");
}

@Override
protected <T> FunctionCounter newFunctionCounter(
Meter.Id id, T t, ToDoubleFunction<T> toDoubleFunction) {
throw new UnsupportedOperationException("Not implemented yet");
}

@Override
protected TimeUnit getBaseTimeUnit() {
return TimeUnit.MILLISECONDS;
}

@Override
protected DistributionStatisticConfig defaultHistogramConfig() {
return DistributionStatisticConfig.DEFAULT;
}

private static void onMeterRemoved(Meter meter) {
if (meter instanceof RemovableMeter) {
((RemovableMeter) meter).onRemove();
}
}
}
Loading