1818 */
1919package co .elastic .apm .agent .otelmetricsdk ;
2020
21- import co .elastic .apm .agent .AbstractInstrumentationTest ;
21+ import co .elastic .apm .agent .MockReporter ;
22+ import co .elastic .apm .agent .MockTracer ;
23+ import co .elastic .apm .agent .bci .ElasticApmAgent ;
2224import co .elastic .apm .agent .common .util .WildcardMatcher ;
25+ import co .elastic .apm .agent .configuration .CoreConfiguration ;
2326import co .elastic .apm .agent .configuration .MetricsConfiguration ;
27+ import co .elastic .apm .agent .configuration .SpyConfiguration ;
28+ import co .elastic .apm .agent .impl .ElasticApmTracer ;
2429import co .elastic .apm .agent .report .ReporterConfiguration ;
2530import co .elastic .apm .agent .util .AtomicDouble ;
2631import io .opentelemetry .api .common .Attributes ;
4146import io .opentelemetry .api .metrics .ObservableLongGauge ;
4247import io .opentelemetry .api .metrics .ObservableLongMeasurement ;
4348import io .opentelemetry .api .metrics .ObservableLongUpDownCounter ;
49+ import net .bytebuddy .agent .ByteBuddyAgent ;
50+ import org .junit .AfterClass ;
51+ import org .junit .jupiter .api .AfterAll ;
52+ import org .junit .jupiter .api .BeforeAll ;
4453import org .junit .jupiter .api .BeforeEach ;
4554import org .junit .jupiter .api .Test ;
55+ import org .stagemonitor .configuration .ConfigurationRegistry ;
4656
4757import javax .annotation .Nullable ;
4858import java .time .Instant ;
6373import static io .opentelemetry .api .common .AttributeKey .stringKey ;
6474import static org .mockito .Mockito .doReturn ;
6575
66- public abstract class AbstractOtelMetricsTest extends AbstractInstrumentationTest {
76+ public abstract class AbstractOtelMetricsTest {
77+
78+ protected static ElasticApmTracer tracer ;
79+ protected static MockReporter reporter ;
80+ protected static ConfigurationRegistry config ;
81+
6782
6883 private ReporterConfiguration reporterConfig ;
6984
@@ -74,9 +89,32 @@ public abstract class AbstractOtelMetricsTest extends AbstractInstrumentationTes
7489 @ Nullable
7590 private MeterProvider meterProvider ;
7691
92+
93+ @ BeforeAll
94+ public static synchronized void beforeAll () {
95+ MockTracer .MockInstrumentationSetup mockInstrumentationSetup = MockTracer .createMockInstrumentationSetup ();
96+ config = mockInstrumentationSetup .getConfig ();
97+ tracer = mockInstrumentationSetup .getTracer ();
98+ reporter = mockInstrumentationSetup .getReporter ();
99+
100+ //Metrics export should work even with instrument=false
101+ CoreConfiguration coreConfig = config .getConfig (CoreConfiguration .class );
102+ doReturn (false ).when (coreConfig ).isInstrument ();
103+
104+ assertThat (tracer .isRunning ()).isTrue ();
105+ ElasticApmAgent .initInstrumentation (tracer , ByteBuddyAgent .install ());
106+ }
107+
108+ @ AfterAll
109+ @ AfterClass
110+ public static synchronized void afterAll () {
111+ ElasticApmAgent .reset ();
112+ }
113+
77114 @ BeforeEach
78115 public void setup () {
79- reporterConfig = tracer .getConfig (ReporterConfiguration .class );
116+ SpyConfiguration .reset (config );
117+ reporterConfig = config .getConfig (ReporterConfiguration .class );
80118 // we use explicit flush in tests instead of periodic reporting to prevent flakyness
81119 doReturn (1_000_000L ).when (reporterConfig ).getMetricsIntervalMs ();
82120 meterProvider = null ;
0 commit comments