Skip to content

Commit

Permalink
Use micrometer library instrumentation in agent (#6560)
Browse files Browse the repository at this point in the history
* Use micrometer library instrumentation in agent

* PR feedback
  • Loading branch information
jack-berg authored Sep 8, 2022
1 parent 13cb1f6 commit 6d22758
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 1,880 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ muzzle {
dependencies {
library("io.micrometer:micrometer-core:1.5.0")

implementation("io.opentelemetry:opentelemetry-micrometer1-shim") {
// just get the instrumentation, without micrometer itself
exclude("io.micrometer", "micrometer-core")
}
implementation(project(":instrumentation:micrometer:micrometer-1.5:library"))

testImplementation(project(":instrumentation:micrometer:micrometer-1.5:testing"))
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ 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,8 +7,8 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import io.opentelemetry.micrometer1shim.OpenTelemetryMeterRegistry;

public final class MicrometerSingletons {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

import io.opentelemetry.micrometer1shim.OpenTelemetryMeterRegistry;
import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,25 @@

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

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractCounterTest;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.assertj.core.api.AbstractIterableAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.RegisterExtension;

class CounterTest {

static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim";
class CounterTest extends AbstractCounterTest {

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

@BeforeEach
void cleanupMeters() {
@AfterEach
public void cleanup() {
Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove);
}

@Test
void testCounter() throws Exception {
// 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")
.hasDoubleSumSatisfying(
sum ->
sum.isMonotonic()
.hasPointsSatisfying(
point ->
point
.hasValue(3)
.hasAttributesSatisfying(
equalTo(
AttributeKey.stringKey("tag"),
"value"))))));
testing.clearData();

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

// then
Thread.sleep(100); // interval of the test metrics exporter
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME, "testCounter", AbstractIterableAssert::isEmpty);
@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,214 +5,25 @@

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

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;

import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Metrics;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractDistributionSummaryTest;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.assertj.core.api.AbstractIterableAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.RegisterExtension;

class DistributionSummaryTest {

static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer1shim";
class DistributionSummaryTest extends AbstractDistributionSummaryTest {

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

@BeforeEach
void cleanupMeters() {
@AfterEach
public void cleanup() {
Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove);
}

@Test
void testDistributionSummary() throws Exception {
// given
DistributionSummary summary =
DistributionSummary.builder("testSummary")
.description("This is a test distribution summary")
.baseUnit("things")
.scale(2.0)
.tags("tag", "value")
.register(Metrics.globalRegistry);

// when
summary.record(21);

// then
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME,
"testSummary",
metrics ->
metrics.anySatisfy(
metric ->
assertThat(metric)
.hasDescription("This is a test distribution summary")
.hasUnit("things")
.hasHistogramSatisfying(
histogram ->
histogram.hasPointsSatisfying(
point ->
point
.hasSum(42)
.hasCount(1)
.hasAttributesSatisfying(
equalTo(
AttributeKey.stringKey("tag"), "value"))))));
testing.clearData();

// when
Metrics.globalRegistry.remove(summary);
summary.record(6);

// then
Thread.sleep(100); // interval of the test metrics exporter
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME, "testSummary", AbstractIterableAssert::isEmpty);
}

@Test
void testMicrometerHistogram() {
// given
DistributionSummary summary =
DistributionSummary.builder("testSummaryHistogram")
.description("This is a test distribution summary")
.baseUnit("things")
.tags("tag", "value")
.serviceLevelObjectives(1, 10, 100, 1000)
.distributionStatisticBufferLength(10)
.register(Metrics.globalRegistry);

// when
summary.record(0.5);
summary.record(5);
summary.record(50);
summary.record(500);

// then
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME,
"testSummaryHistogram.histogram",
metrics ->
metrics.anySatisfy(
metric ->
assertThat(metric)
.hasDoubleGaugeSatisfying(
gauge ->
gauge.hasPointsSatisfying(
point ->
point
.hasValue(1)
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("tag"), "value"),
equalTo(AttributeKey.stringKey("le"), "1")),
point ->
point
.hasValue(2)
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("tag"), "value"),
equalTo(AttributeKey.stringKey("le"), "10")),
point ->
point
.hasValue(3)
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("tag"), "value"),
equalTo(AttributeKey.stringKey("le"), "100")),
point ->
point
.hasValue(4)
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("tag"), "value"),
equalTo(AttributeKey.stringKey("le"), "1000"))))));
}

@Test
void testMicrometerPercentiles() {
// given
DistributionSummary summary =
DistributionSummary.builder("testSummaryPercentiles")
.description("This is a test distribution summary")
.baseUnit("things")
.tags("tag", "value")
.publishPercentiles(0.5, 0.95, 0.99)
.register(Metrics.globalRegistry);

// when
summary.record(50);
summary.record(100);

// then
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME,
"testSummaryPercentiles.percentile",
metrics ->
metrics.anySatisfy(
metric ->
assertThat(metric)
.hasDoubleGaugeSatisfying(
gauge ->
gauge.hasPointsSatisfying(
point ->
point.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("tag"), "value"),
equalTo(AttributeKey.stringKey("phi"), "0.5")),
point ->
point.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("tag"), "value"),
equalTo(AttributeKey.stringKey("phi"), "0.95")),
point ->
point.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("tag"), "value"),
equalTo(AttributeKey.stringKey("phi"), "0.99"))))));
}

@Test
void testMicrometerMax() throws InterruptedException {
// given
DistributionSummary summary =
DistributionSummary.builder("testSummaryMax")
.description("This is a test distribution summary")
.baseUnit("things")
.tags("tag", "value")
.register(Metrics.globalRegistry);

// when
summary.record(1);
summary.record(2);
summary.record(4);

// then
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME,
"testSummaryMax.max",
metrics ->
metrics.anySatisfy(
metric ->
assertThat(metric)
.hasDescription("This is a test distribution summary")
.hasDoubleGaugeSatisfying(
gauge ->
gauge.hasPointsSatisfying(
point ->
point
.hasValue(4)
.hasAttributesSatisfying(
equalTo(
AttributeKey.stringKey("tag"), "value"))))));

// when
Metrics.globalRegistry.remove(summary);
Thread.sleep(100); // give time for any inflight metric export to be received
testing.clearData();

// then
Thread.sleep(100); // interval of the test metrics exporter
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME, "testSummaryMax.max", AbstractIterableAssert::isEmpty);
@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Loading

0 comments on commit 6d22758

Please sign in to comment.