-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply micrometer instrumentation to spring-boot-actuator apps (#5666)
* Apply micrometer instrumentation to spring-boot-actuator apps * fix a bug * code review comments
- Loading branch information
Mateusz Rzeszutek
authored
Mar 23, 2022
1 parent
990dad8
commit f2587ba
Showing
15 changed files
with
360 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
instrumentation/spring/spring-boot-actuator-autoconfigure-2.0/javaagent/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("org.springframework.boot") | ||
module.set("spring-boot-actuator-autoconfigure") | ||
versions.set("[2.0.0.RELEASE,)") | ||
extraDependency("io.micrometer:micrometer-core:1.5.0") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
library("org.springframework.boot:spring-boot-actuator-autoconfigure:2.0.0.RELEASE") | ||
library("io.micrometer:micrometer-core:1.5.0") | ||
|
||
implementation(project(":instrumentation:micrometer:micrometer-1.5:javaagent")) | ||
} |
49 changes: 49 additions & 0 deletions
49
...agent/instrumentation/spring/actuator/AutoConfigurationImportSelectorInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.actuator; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class AutoConfigurationImportSelectorInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.springframework.boot.autoconfigure.AutoConfigurationImportSelector"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("getCandidateConfigurations").and(returns(List.class)), | ||
getClass().getName() + "$GetCandidateConfigurationsAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class GetCandidateConfigurationsAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void onExit(@Advice.Return(readOnly = false) List<String> configurations) { | ||
if (configurations.contains( | ||
"org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration")) { | ||
List<String> configs = new ArrayList<>(configurations.size() + 1); | ||
configs.addAll(configurations); | ||
// using class reference here so that muzzle will consider it a dependency of this advice | ||
// and capture all references to spring & micrometer classes that it makes | ||
configs.add(OpenTelemetryMeterRegistryAutoConfiguration.class.getName()); | ||
configurations = configs; | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...avaagent/instrumentation/spring/actuator/OpenTelemetryMeterRegistryAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.actuator; | ||
|
||
import io.micrometer.core.instrument.Clock; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.opentelemetry.javaagent.instrumentation.micrometer.v1_5.MicrometerSingletons; | ||
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; | ||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
// CompositeMeterRegistryAutoConfiguration configures the "final" composite registry | ||
@AutoConfigureBefore(CompositeMeterRegistryAutoConfiguration.class) | ||
// configure after the SimpleMeterRegistry has initialized; it is normally the last MeterRegistry | ||
// implementation to be configured, as it's used as a fallback | ||
// the OTel registry should be added in addition to that fallback and not replace it | ||
@AutoConfigureAfter(SimpleMetricsExportAutoConfiguration.class) | ||
@ConditionalOnBean(Clock.class) | ||
@ConditionalOnClass(MeterRegistry.class) | ||
public class OpenTelemetryMeterRegistryAutoConfiguration { | ||
|
||
@Bean | ||
public MeterRegistry otelMeterRegistry() { | ||
return MicrometerSingletons.meterRegistry(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...y/javaagent/instrumentation/spring/actuator/SpringBootActuatorIgnoredTypesConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.actuator; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.instrumentation.api.config.Config; | ||
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesBuilder; | ||
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer; | ||
|
||
@AutoService(IgnoredTypesConfigurer.class) | ||
public class SpringBootActuatorIgnoredTypesConfigurer implements IgnoredTypesConfigurer { | ||
|
||
@Override | ||
public void configure(Config config, IgnoredTypesBuilder builder) { | ||
builder.allowClass("org.springframework.boot.autoconfigure.AutoConfigurationImportSelector"); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ry/javaagent/instrumentation/spring/actuator/SpringBootActuatorInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.actuator; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.HelperResourceBuilder; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class SpringBootActuatorInstrumentationModule extends InstrumentationModule { | ||
|
||
public SpringBootActuatorInstrumentationModule() { | ||
super("spring-boot-actuator-autoconfigure", "spring-boot-actuator-autoconfigure-2.0"); | ||
} | ||
|
||
@Override | ||
public void registerHelperResources(HelperResourceBuilder helperResourceBuilder) { | ||
// autoconfigure classes are loaded as resources using ClassPathResource | ||
// this line will make OpenTelemetryMeterRegistryAutoConfiguration available to all | ||
// classloaders, so that the bean classloader (different than the instrumented classloader) can | ||
// load it | ||
helperResourceBuilder.registerForAllClassLoaders( | ||
"io/opentelemetry/javaagent/instrumentation/spring/actuator/OpenTelemetryMeterRegistryAutoConfiguration.class"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new AutoConfigurationImportSelectorInstrumentation()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...rc/test/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/ActuatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.actuator; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; | ||
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.javaagent.instrumentation.spring.actuator.SpringApp.TestBean; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
|
||
class ActuatorTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); | ||
|
||
@Test | ||
void shouldInjectOtelMeterRegistry() { | ||
SpringApplication app = new SpringApplication(SpringApp.class); | ||
ConfigurableApplicationContext context = app.run(); | ||
cleanup.deferCleanup(context); | ||
|
||
TestBean testBean = context.getBean(TestBean.class); | ||
testBean.inc(); | ||
|
||
testing.waitAndAssertMetrics( | ||
"io.opentelemetry.micrometer-1.5", | ||
"test-counter", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metric -> | ||
assertThat(metric) | ||
.hasUnit("thingies") | ||
.hasDoubleSum() | ||
.isMonotonic() | ||
.points() | ||
.satisfiesExactly( | ||
point -> | ||
assertThat(point) | ||
.hasValue(1) | ||
.attributes() | ||
.containsOnly(attributeEntry("tag", "value"))))); | ||
|
||
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class); | ||
assertThat(meterRegistry).isNotNull().isInstanceOf(CompositeMeterRegistry.class); | ||
assertThat(((CompositeMeterRegistry) meterRegistry).getRegistries()) | ||
.anyMatch(r -> r.getClass().getSimpleName().equals("OpenTelemetryMeterRegistry")) | ||
.anyMatch(r -> r.getClass().getSimpleName().equals("SimpleMeterRegistry")); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...t/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/actuator/SpringApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.actuator; | ||
|
||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@SpringBootApplication | ||
public class SpringApp { | ||
|
||
@Bean | ||
public TestBean testBean(MeterRegistry meterRegistry) { | ||
return new TestBean(meterRegistry); | ||
} | ||
|
||
public static class TestBean { | ||
private final Counter counter; | ||
|
||
public TestBean(MeterRegistry registry) { | ||
this.counter = | ||
Counter.builder("test-counter") | ||
.baseUnit("thingies") | ||
.tags("tag", "value") | ||
.register(registry); | ||
} | ||
|
||
public void inc() { | ||
counter.increment(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.