Skip to content

Commit

Permalink
Micrometer library instrumentation (open-telemetry#5063)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored and RashmiRam committed May 23, 2022
1 parent d53dea1 commit 0c61d4e
Show file tree
Hide file tree
Showing 45 changed files with 1,889 additions and 1,305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ muzzle {

dependencies {
library("io.micrometer:micrometer-core:1.5.0")
}

// TODO: disabled by default, since not all instruments are implemented
tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.micrometer.enabled=true")
implementation(project(":instrumentation:micrometer:micrometer-1.5:library"))

testImplementation(project(":instrumentation:micrometer:micrometer-1.5:testing"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("io.micrometer.core.instrument.config.validate.Validated");
}

@Override
protected boolean defaultEnabled() {
// TODO: disabled by default, since not all instruments are implemented
return false;
}

@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,6 +7,7 @@

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

public final class MicrometerSingletons {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,18 @@

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

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.micrometer.v1_5.AbstractCounterTest;
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 {

static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer-1.5";
class CounterTest extends AbstractCounterTest {

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

@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))));
@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,162 +5,18 @@

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

import static io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry;

import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Metrics;
import io.opentelemetry.instrumentation.micrometer.v1_5.AbstractDistributionSummaryTest;
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 DistributionSummaryTest {

static final String INSTRUMENTATION_NAME = "io.opentelemetry.micrometer-1.5";
class DistributionSummaryTest extends AbstractDistributionSummaryTest {

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

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

@Test
void testDistributionSummary() {
// 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")
.hasDoubleHistogram()
.points()
.satisfiesExactly(
point ->
assertThat(point)
.hasSum(42)
.hasCount(1)
.attributes()
.containsOnly(attributeEntry("tag", "value")))));
testing.clearData();

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

// then
testing.waitAndAssertMetrics(
INSTRUMENTATION_NAME,
"testSummary",
metrics ->
metrics.allSatisfy(
metric ->
assertThat(metric)
.hasDoubleHistogram()
.points()
.noneSatisfy(point -> assertThat(point).hasSum(54).hasCount(2))));
}

@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)
.hasDoubleGauge()
.points()
.anySatisfy(
point ->
assertThat(point).hasValue(1).attributes().containsEntry("le", "1"))
.anySatisfy(
point ->
assertThat(point)
.hasValue(2)
.attributes()
.containsEntry("le", "10"))
.anySatisfy(
point ->
assertThat(point)
.hasValue(3)
.attributes()
.containsEntry("le", "100"))
.anySatisfy(
point ->
assertThat(point)
.hasValue(4)
.attributes()
.containsEntry("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)
.hasDoubleGauge()
.points()
.anySatisfy(
point -> assertThat(point).attributes().containsEntry("phi", "0.5"))
.anySatisfy(
point -> assertThat(point).attributes().containsEntry("phi", "0.95"))
.anySatisfy(
point -> assertThat(point).attributes().containsEntry("phi", "0.99"))));
@Override
protected InstrumentationExtension testing() {
return testing;
}
}
Loading

0 comments on commit 0c61d4e

Please sign in to comment.