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

Use micrometer1-shim in micrometer javaagent instrumentation, depreca… #5820

Merged
merged 2 commits into from
Apr 15, 2022
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 @@ -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 {
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 had to change the instrumentation name, so I just inlined all the tests - the testing module will get removed after 1.13 anyway.


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