-
Notifications
You must be signed in to change notification settings - Fork 845
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
Apply micrometer instrumentation to spring-boot-actuator apps #5666
Merged
trask
merged 3 commits into
open-telemetry:main
from
mateuszrzeszutek:spring-boot-micrometer
Mar 23, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used a bit different way of making that class visible to spring class loaders; maybe it'll be useful in other spring instrumentations too.
(Also, an interesting bit: no need to explicitly pass
.classdata
here, theAgentClassLoader
will add the suffix automatically when it's asked to get a.class
resource)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯