Skip to content

Commit

Permalink
Use micrometer1-shim in micrometer javaagent instrumentation, depreca… (
Browse files Browse the repository at this point in the history
#5820)

* Use micrometer1-shim in micrometer javaagent instrumentation, deprecate micrometer library instrumentation

* fix actuator instrumentation
  • Loading branch information
Mateusz Rzeszutek authored Apr 15, 2022
1 parent 4879ced commit b19e95b
Show file tree
Hide file tree
Showing 37 changed files with 1,971 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ muzzle {
dependencies {
library("io.micrometer:micrometer-core:1.5.0")

implementation(project(":instrumentation:micrometer:micrometer-1.5:library"))

testImplementation(project(":instrumentation:micrometer:micrometer-1.5:testing"))
implementation("io.opentelemetry:opentelemetry-micrometer1-shim")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("io.micrometer.core.instrument.config.validate.Validated");
}

@Override
public boolean isHelperClass(String className) {
return className.startsWith("io.opentelemetry.micrometer1shim.");
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new MetricsInstrumentation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.micrometer1shim.OpenTelemetryMeterRegistry;

public final class MicrometerSingletons {

private static final MeterRegistry METER_REGISTRY =
OpenTelemetryMeterRegistry.create(GlobalOpenTelemetry.get());
private static final MeterRegistry METER_REGISTRY;

static {
Config config = Config.get();
METER_REGISTRY =
OpenTelemetryMeterRegistry.builder(GlobalOpenTelemetry.get())
.setPrometheusMode(
config.getBoolean("otel.instrumentation.micrometer.prometheus-mode.enabled", false))
.setBaseTimeUnit(
TimeUnitParser.parseConfigValue(
config.getString("otel.instrumentation.micrometer.base-time-unit")))
.build();
}

public static MeterRegistry meterRegistry() {
return METER_REGISTRY;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5;

import static java.util.logging.Level.WARNING;

import io.opentelemetry.micrometer1shim.OpenTelemetryMeterRegistry;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.Nullable;

final class TimeUnitParser {

private static final Logger logger = Logger.getLogger(OpenTelemetryMeterRegistry.class.getName());

static TimeUnit parseConfigValue(@Nullable String value) {
if (value == null) {
return TimeUnit.MILLISECONDS;
}
// short names are UCUM names
// long names are just TimeUnit values lowercased
switch (value.toLowerCase(Locale.ROOT)) {
case "ns":
case "nanoseconds":
return TimeUnit.NANOSECONDS;
case "us":
case "microseconds":
return TimeUnit.MICROSECONDS;
case "ms":
case "milliseconds":
return TimeUnit.MILLISECONDS;
case "s":
case "seconds":
return TimeUnit.SECONDS;
case "min":
case "minutes":
return TimeUnit.MINUTES;
case "h":
case "hours":
return TimeUnit.HOURS;
case "d":
case "days":
return TimeUnit.DAYS;
default:
if (logger.isLoggable(WARNING)) {
logger.log(
WARNING,
"Invalid base time unit: '{0}'; using 'ms' as the base time unit instead",
value);
}
return TimeUnit.MILLISECONDS;
}
}

private TimeUnitParser() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,78 @@

package io.opentelemetry.javaagent.instrumentation.micrometer.v1_5;

import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractCounterTest;
import static io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class CounterTest extends AbstractCounterTest {
class CounterTest {

static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometershim";

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@Override
protected InstrumentationExtension testing() {
return testing;
@BeforeEach
void cleanupMeters() {
Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove);
}

@Test
void testCounter() {
// given
Counter counter =
Counter.builder("testCounter")
.description("This is a test counter")
.tags("tag", "value")
.baseUnit("items")
.register(Metrics.globalRegistry);

// when
counter.increment();
counter.increment(2);

// then
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME,
"testCounter",
metrics ->
metrics.anySatisfy(
metric ->
assertThat(metric)
.hasDescription("This is a test counter")
.hasUnit("items")
.hasDoubleSum()
.isMonotonic()
.points()
.satisfiesExactly(
point ->
assertThat(point)
.hasValue(3)
.attributes()
.containsOnly(attributeEntry("tag", "value")))));
testing.clearData();

// when
Metrics.globalRegistry.remove(counter);
counter.increment();

// then
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME,
"testCounter",
metrics ->
metrics.allSatisfy(
metric ->
assertThat(metric)
.hasDoubleSum()
.points()
.noneSatisfy(point -> assertThat(point).hasValue(4))));
}
}
Loading

0 comments on commit b19e95b

Please sign in to comment.