Skip to content

Commit 96dea08

Browse files
authored
[File based config] UT improvements (#2594)
* Tests added * Cleanup of test yaml strings * Cleanup
1 parent 16b39d9 commit 96dea08

File tree

5 files changed

+89
-28
lines changed

5 files changed

+89
-28
lines changed

profiler/src/main/java/com/splunk/opentelemetry/profiler/snapshot/StackTraceExporterActivator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
3030
import io.opentelemetry.sdk.resources.Resource;
3131

32-
// TODO: Improve tests for this class to cover declarative config
3332
@AutoService(AgentListener.class)
3433
public class StackTraceExporterActivator implements AgentListener {
3534
private static final java.util.logging.Logger logger =

profiler/src/test/java/com/splunk/opentelemetry/profiler/JfrActivatorTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.splunk.opentelemetry.profiler;
1818

1919
import static com.splunk.opentelemetry.testing.declarativeconfig.DeclarativeConfigTestUtil.createAutoConfiguredSdk;
20-
import static com.splunk.opentelemetry.testing.declarativeconfig.DeclarativeConfigTestUtil.toYamlString;
2120
import static org.mockito.ArgumentMatchers.any;
2221
import static org.mockito.Mockito.mock;
2322
import static org.mockito.Mockito.mockStatic;
@@ -140,10 +139,23 @@ void shouldNotActivateJfrRecording_profilerDisabled(String yaml, @TempDir Path t
140139
private List<Arguments> generateNoProfilerYamlStrings() {
141140
return List.of(
142141
Arguments.of("file_format: \"1.0-rc.3\""),
143-
Arguments.of(toYamlString("file_format: \"1.0-rc.3\"", "distribution:")),
144-
Arguments.of(toYamlString("file_format: \"1.0-rc.3\"", "distribution:", " splunk:")),
145142
Arguments.of(
146-
toYamlString(
147-
"file_format: \"1.0-rc.3\"", "distribution:", " splunk:", " something:")));
143+
"""
144+
file_format: "1.0-rc.3"
145+
distribution:
146+
"""),
147+
Arguments.of(
148+
"""
149+
file_format: "1.0-rc.3"
150+
distribution:
151+
splunk:
152+
"""),
153+
Arguments.of(
154+
"""
155+
file_format: "1.0-rc.3"
156+
distribution:
157+
splunk:
158+
something:
159+
"""));
148160
}
149161
}

profiler/src/test/java/com/splunk/opentelemetry/profiler/snapshot/SnapshotProfilingConfigurationCustomizerProviderTest.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.splunk.opentelemetry.profiler.snapshot;
1818

19-
import static com.splunk.opentelemetry.testing.declarativeconfig.DeclarativeConfigTestUtil.toYamlString;
2019
import static org.assertj.core.api.Assertions.assertThat;
2120
import static org.mockito.Mockito.mock;
2221
import static org.mockito.Mockito.verify;
@@ -47,7 +46,11 @@ void resetSuppliers() {
4746
void shouldDoNothingIfProfilerIsNotEnabled(@TempDir Path tempDir) throws IOException {
4847
// given
4948
String yaml =
50-
toYamlString("file_format: \"1.0-rc.3\"", "instrumentation/development:", " java:");
49+
"""
50+
file_format: "1.0-rc.3"
51+
instrumentation/development:
52+
java:
53+
""";
5154

5255
// when
5356
OpenTelemetryConfigurationModel model = getCustomizedModel(yaml);
@@ -61,12 +64,13 @@ void shouldDoNothingIfProfilerIsNotEnabled(@TempDir Path tempDir) throws IOExcep
6164
void shouldAddShutdownHookSpanProcessor() {
6265
// given
6366
String yaml =
64-
toYamlString(
65-
"file_format: \"1.0-rc.3\"",
66-
"distribution:",
67-
" splunk:",
68-
" profiling:",
69-
" callgraphs:");
67+
"""
68+
file_format: "1.0-rc.3"
69+
distribution:
70+
splunk:
71+
profiling:
72+
callgraphs:
73+
""";
7074

7175
// when
7276
OpenTelemetryConfigurationModel model = getCustomizedModel(yaml);
@@ -85,11 +89,13 @@ void shouldInitialize() {
8589
// given
8690
OpenTelemetryConfigurationModel model =
8791
DeclarativeConfigTestUtil.parse(
88-
"file_format: \"1.0-rc.3\"",
89-
"distribution:",
90-
" splunk:",
91-
" profiling:",
92-
" callgraphs:");
92+
"""
93+
file_format: "1.0-rc.3"
94+
distribution:
95+
splunk:
96+
profiling:
97+
callgraphs:
98+
""");
9399

94100
TraceRegistry traceRegistryMock = mock(TraceRegistry.class);
95101
ContextStorageWrapper contextStorageWrapperMock = mock(ContextStorageWrapper.class);

profiler/src/test/java/com/splunk/opentelemetry/profiler/snapshot/StackTraceExporterActivatorTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,28 @@
1616

1717
package com.splunk.opentelemetry.profiler.snapshot;
1818

19+
import static com.splunk.opentelemetry.testing.declarativeconfig.DeclarativeConfigTestUtil.createAutoConfiguredSdk;
1920
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2021
import static org.junit.jupiter.api.Assertions.assertNotSame;
2122
import static org.junit.jupiter.api.Assertions.assertSame;
2223

24+
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
2325
import io.opentelemetry.sdk.autoconfigure.OpenTelemetrySdkExtension;
26+
import java.io.IOException;
27+
import java.nio.file.Path;
2428
import org.junit.jupiter.api.AfterEach;
2529
import org.junit.jupiter.api.Nested;
2630
import org.junit.jupiter.api.Test;
2731
import org.junit.jupiter.api.extension.RegisterExtension;
32+
import org.junit.jupiter.api.io.TempDir;
2833

2934
class StackTraceExporterActivatorTest {
3035
@AfterEach
3136
void tearDown() {
37+
SpanTracker.SUPPLIER.reset();
38+
StackTraceSampler.SUPPLIER.reset();
39+
StagingArea.SUPPLIER.reset();
40+
SnapshotProfilingDeclarativeConfiguration.SUPPLIER.reset();
3241
StackTraceExporter.SUPPLIER.reset();
3342
}
3443

@@ -64,4 +73,48 @@ void doNotConfigureStackTraceExporterProvider() {
6473
assertSame(StackTraceExporter.NOOP, exporter);
6574
}
6675
}
76+
77+
@Nested
78+
class DeclarativeConfig {
79+
@RegisterExtension final AutoCleanupExtension autoCleanup = AutoCleanupExtension.create();
80+
81+
@Test
82+
void configureStackTraceExporterProvider(@TempDir Path tempDir) throws IOException {
83+
String yaml =
84+
"""
85+
file_format: "1.0-rc.3"
86+
distribution:
87+
splunk:
88+
profiling:
89+
exporter:
90+
otlp_log_http:
91+
callgraphs:
92+
""";
93+
var sdk = createAutoConfiguredSdk(yaml, tempDir, autoCleanup);
94+
95+
new StackTraceExporterActivator().afterAgent(sdk);
96+
97+
var exporter = StackTraceExporter.SUPPLIER.get();
98+
assertNotSame(StackTraceExporter.NOOP, exporter);
99+
assertInstanceOf(AsyncStackTraceExporter.class, exporter);
100+
}
101+
102+
@Test
103+
void doNotConfigureStackTraceExporterProviderWhenNoCallgraphs(@TempDir Path tempDir)
104+
throws IOException {
105+
String yaml =
106+
"""
107+
file_format: "1.0-rc.3"
108+
distribution:
109+
splunk:
110+
profiling:
111+
""";
112+
var sdk = createAutoConfiguredSdk(yaml, tempDir, autoCleanup);
113+
114+
new StackTraceExporterActivator().afterAgent(sdk);
115+
116+
var exporter = StackTraceExporter.SUPPLIER.get();
117+
assertSame(StackTraceExporter.NOOP, exporter);
118+
}
119+
}
67120
}

testing/common/src/main/java/com/splunk/opentelemetry/testing/declarativeconfig/DeclarativeConfigTestUtil.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,11 @@
3737
import java.nio.charset.StandardCharsets;
3838
import java.nio.file.Files;
3939
import java.nio.file.Path;
40-
import java.util.Arrays;
4140
import java.util.Collections;
4241

4342
public class DeclarativeConfigTestUtil {
4443
private DeclarativeConfigTestUtil() {}
4544

46-
public static String toYamlString(String... lines) {
47-
return String.join("\n", Arrays.asList(lines));
48-
}
49-
50-
public static OpenTelemetryConfigurationModel parse(String... lines) {
51-
return parse(toYamlString(lines));
52-
}
53-
5445
public static OpenTelemetryConfigurationModel parse(String yaml) {
5546
try (InputStream yamlStream = new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8))) {
5647
return DeclarativeConfiguration.parse(yamlStream);

0 commit comments

Comments
 (0)