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

OpenTelemetry metrics exporter instrumentation breaks with instrument=false #3326

Merged
merged 3 commits into from
Sep 20, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
* Fix micrometer histogram serialization - {pull}3290[#3290], {pull}3304[#3304]
* Fix transactions not being correctly handled in certain edge cases - {pull}3294[#3294]
* Fixed JDBC instrumentation for DB2 - {pull}3313[#3313]
* Fixed OpenTelemetry metrics export breaking when `instrument=false` is configured - {pull}3326[#3326]

[[release-notes-1.x]]
=== Java Agent version 1.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
package co.elastic.apm.agent.otelmetricsdk;

import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.GlobalTracer;
import co.elastic.apm.agent.sdk.ElasticApmInstrumentation;
import co.elastic.apm.agent.sdk.internal.util.LoggerUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakSet;
import co.elastic.apm.agent.sdk.internal.util.LoggerUtils;
import co.elastic.apm.agent.tracer.GlobalTracer;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -55,6 +55,11 @@ public Collection<String> getInstrumentationGroupNames() {
return Arrays.asList("opentelemetry", "opentelemetry-metrics", "experimental");
}

@Override
public final boolean includeWhenInstrumentationIsDisabled() {
return true;
}

@Override
public String getAdviceClassName() {
return getClass().getName() + "$SdkMeterProviderBuilderAdvice";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
*/
package co.elastic.apm.agent.otelmetricsdk;

import co.elastic.apm.agent.AbstractInstrumentationTest;
import co.elastic.apm.agent.MockReporter;
import co.elastic.apm.agent.MockTracer;
import co.elastic.apm.agent.bci.ElasticApmAgent;
import co.elastic.apm.agent.common.util.WildcardMatcher;
import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.configuration.MetricsConfiguration;
import co.elastic.apm.agent.configuration.SpyConfiguration;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.report.ReporterConfiguration;
import co.elastic.apm.agent.util.AtomicDouble;
import io.opentelemetry.api.common.Attributes;
Expand All @@ -41,8 +46,13 @@
import io.opentelemetry.api.metrics.ObservableLongGauge;
import io.opentelemetry.api.metrics.ObservableLongMeasurement;
import io.opentelemetry.api.metrics.ObservableLongUpDownCounter;
import net.bytebuddy.agent.ByteBuddyAgent;
import org.junit.AfterClass;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.stagemonitor.configuration.ConfigurationRegistry;

import javax.annotation.Nullable;
import java.time.Instant;
Expand All @@ -63,7 +73,12 @@
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.mockito.Mockito.doReturn;

public abstract class AbstractOtelMetricsTest extends AbstractInstrumentationTest {
public abstract class AbstractOtelMetricsTest {

protected static ElasticApmTracer tracer;
protected static MockReporter reporter;
protected static ConfigurationRegistry config;


private ReporterConfiguration reporterConfig;

Expand All @@ -74,9 +89,32 @@ public abstract class AbstractOtelMetricsTest extends AbstractInstrumentationTes
@Nullable
private MeterProvider meterProvider;


@BeforeAll
public static synchronized void beforeAll() {
MockTracer.MockInstrumentationSetup mockInstrumentationSetup = MockTracer.createMockInstrumentationSetup();
config = mockInstrumentationSetup.getConfig();
tracer = mockInstrumentationSetup.getTracer();
reporter = mockInstrumentationSetup.getReporter();

//Metrics export should work even with instrument=false
CoreConfiguration coreConfig = config.getConfig(CoreConfiguration.class);
doReturn(false).when(coreConfig).isInstrument();

assertThat(tracer.isRunning()).isTrue();
ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install());
}

@AfterAll
@AfterClass
public static synchronized void afterAll() {
ElasticApmAgent.reset();
}

@BeforeEach
public void setup() {
reporterConfig = tracer.getConfig(ReporterConfiguration.class);
SpyConfiguration.reset(config);
reporterConfig = config.getConfig(ReporterConfiguration.class);
// we use explicit flush in tests instead of periodic reporting to prevent flakyness
doReturn(1_000_000L).when(reporterConfig).getMetricsIntervalMs();
meterProvider = null;
Expand Down
Loading