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

Register and schedule Micrometer MeterRegistry when Meters are registered #2255

Merged
merged 3 commits into from
Nov 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ updates:
- dependency-name: "com.squareup.okhttp3:okhttp"
- dependency-name: "com.datastax.cassandra:cassandra-driver-core"
- dependency-name: "com.datastax.oss:java-driver-core"
- dependency-name: "io.micrometer:*"
ignore:
- dependency-name: "net.bytebuddy:byte-buddy-agent"
# We deliberately want to keep this older version of Byte Buddy for our runtime attach test
Expand Down
9 changes: 3 additions & 6 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ endif::[]

=== Unreleased

[[release-notes-1.28.0]]
==== 1.28.0 - YYYY/MM/DD

[float]
===== Features
* Add support to Jakarta EE for JSF - {pull}2254[#2254]
[[release-notes-1.27.1]]
==== 1.27.1 - YYYY/MM/DD

[float]
===== Bug fixes
* Fixing missing Micrometer metrics in Spring boot due to premature initialization - {pull}2255[#2255]

[[release-notes-1.x]]
=== Java Agent version 1.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
*/
@javax.annotation.Generated(
value = "by gRPC proto compiler (version 1.42.0)",
value = "by gRPC proto compiler (version 1.42.1)",
comments = "Source: rpc.proto")
@io.grpc.stub.annotations.GrpcGenerated
public final class HelloGrpc {
Expand Down
7 changes: 6 additions & 1 deletion apm-agent-plugins/apm-micrometer-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.0.1</version>
<version>1.8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,27 @@
import co.elastic.apm.agent.impl.GlobalTracer;
import io.micrometer.core.instrument.MeterRegistry;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Collection;
import java.util.Collections;

import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.nameContains;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;

public class MicrometerInstrumentation extends TracerAwareInstrumentation {

private static final MicrometerMetricsReporter reporter = new MicrometerMetricsReporter(GlobalTracer.requireTracerImpl());

@Override
public ElementMatcher<? super NamedElement> getTypeMatcherPreFilter() {
return nameContains("Meter").or(nameContains("Registry"));
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
return hasSuperType(named("io.micrometer.core.instrument.MeterRegistry"));
return named("io.micrometer.core.instrument.MeterRegistry");
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return isPublic().and(not(isStatic()));
return named("registerMeterIfNecessary");
eyalkoren marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,47 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import net.bytebuddy.agent.ByteBuddyAgent;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.stagemonitor.configuration.ConfigurationRegistry;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;

class MicrometerInstrumentationTest {
public class MicrometerInstrumentationTest {

private MockReporter reporter;
private ConfigurationRegistry config;
private int lastMeasuredMetricSetNumber;
private int lastFooSamples;

@BeforeEach
void setUp() {
@Before
public void setUp() {
config = SpyConfiguration.createSpyConfig();
doReturn(50L).when(config.getConfig(ReporterConfiguration.class)).getMetricsIntervalMs();
reporter = new MockReporter();
lastMeasuredMetricSetNumber = 0;
lastFooSamples = 0;
}

@AfterEach
void tearDown() {
@After
public void tearDown() {
ElasticApmAgent.reset();
}

@Test
void testRegisterMeterRegistry() {
public void testRegisterMeterRegistry() {
ElasticApmAgent.initInstrumentation(MockTracer.createRealTracer(reporter, config), ByteBuddyAgent.install());
SimpleMeterRegistry registry = new SimpleMeterRegistry();
registry.counter("foo").increment();
reporter.awaitUntilAsserted(() -> assertThat(countFooSamples()).isGreaterThanOrEqualTo(1));
}

@Test
void testReportedWhenInstrumentConfigDisabled() {
public void testReportedWhenInstrumentConfigDisabled() {
doReturn(false).when(config.getConfig(CoreConfiguration.class)).isInstrument();
ElasticApmAgent.initInstrumentation(MockTracer.createRealTracer(reporter, config), ByteBuddyAgent.install());
SimpleMeterRegistry registry = new SimpleMeterRegistry();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.agent.micrometer;

import co.elastic.apm.agent.TestClassWithDependencyRunner;
import org.junit.Test;

public class MicrometerInstrumentationVersionsIT {

private final TestClassWithDependencyRunner runner;

public MicrometerInstrumentationVersionsIT() throws Exception {
this.runner = new TestClassWithDependencyRunner("io.micrometer", "micrometer-core", "1.0.1",
MicrometerInstrumentationTest.class
);
}

@Test
public void testOldVersion() {
runner.run();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ void testExclusionOfFailedGauge_singleGauge() {
.isEmpty();

getMetricSets();
assertThat(metricsReporter.getFailedMeters().iterator().next().getId().getName()).isEqualTo("gauge1");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] Was this change required due to upgrading micrometer that have different behavior between 1.0.1 and 1.8.0 or due to the change of instrumented method ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a different behavior - in the new version the abstract meters capture exceptions quietly, so we don't get to see that they failed

}

@Test
Expand All @@ -386,7 +385,6 @@ void testExclusionOfFailedGauge_firstFails() {

// serialization should handle ignoring the 1st value
assertThat(metricSet.get("metricset").get("samples").get("gauge2").get("value").doubleValue()).isEqualTo(42D);
assertThat(metricsReporter.getFailedMeters().iterator().next().getId().getName()).isEqualTo("gauge1");
}

@Test
Expand All @@ -405,8 +403,6 @@ void testExclusionOfFailedGauge_secondFails() {
assertThat(metricSet.get("metricset").get("samples").get("gauge2"))
.describedAs("value of %s is not expected to be written to json", "gauge1")
.isNull();

assertThat(metricsReporter.getFailedMeters().iterator().next().getId().getName()).isEqualTo("gauge2");
}

@Test
Expand Down Expand Up @@ -441,7 +437,7 @@ void testNotReportingEmptySamples() {

@Test
void tryToSerializeInvalidTimerValues() {
for (Double invalidValue : Arrays.asList(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN)) {
for (Double invalidValue : Arrays.asList(Double.POSITIVE_INFINITY, Double.NaN)) {
List<Tag> tags = List.of(Tag.of("foo", "bar"));
meterRegistry.more().timer("custom-timer-1", tags, 42, v -> 42L, v -> invalidValue, TimeUnit.MICROSECONDS);
meterRegistry.more().timer("custom-timer-2", tags, 42, v -> 42L, v -> 42D, TimeUnit.MICROSECONDS);
Expand Down