Skip to content

Commit 6f8f0f5

Browse files
committed
add isempty check for namespace
1 parent d05e488 commit 6f8f0f5

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/exporter/aws/metrics/AwsCloudWatchEmfExporterBuilder.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ public AwsCloudWatchEmfExporterBuilder setShouldAddApplicationSignalsDimensions(
6161
}
6262

6363
public AwsCloudWatchEmfExporter build() {
64-
if (this.namespace == null) {
65-
this.namespace = "default";
66-
}
6764

6865
if (this.emitter == null) {
6966
requireNonNull(logGroupName, "Must set logGroupName when emitter is not provided");

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/exporter/aws/metrics/ConsoleEmfExporterBuilder.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public ConsoleEmfExporterBuilder setShouldAddApplicationSignalsDimensions(
4141
}
4242

4343
public ConsoleEmfExporter build() {
44-
if (this.namespace == null) {
45-
this.namespace = "default";
46-
}
4744
if (this.emitter == null) {
4845
this.emitter = new ConsoleEmitter();
4946
}

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/exporter/aws/metrics/common/BaseEmfExporter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public abstract class BaseEmfExporter<T> implements MetricExporter {
6161
*/
6262
protected BaseEmfExporter(
6363
String namespace, LogEventEmitter<T> emitter, boolean shouldAddApplicationSignalsDimensions) {
64-
this.namespace = namespace != null ? namespace : "default";
64+
if (emitter == null) {
65+
throw new IllegalArgumentException("Given emitter must not be null");
66+
}
67+
this.namespace = namespace == null || namespace.isEmpty() ? "default" : namespace;
6568
this.emitter = emitter;
6669
this.shouldAddApplicationSignalsDimensions = shouldAddApplicationSignalsDimensions;
6770
}

awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/exporter/aws/metrics/AwsCloudWatchEmfExporterTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,20 @@ protected LogEventEmitter<CloudWatchLogsClient> createEmitter() {
6464
}
6565

6666
@Override
67-
protected MetricExporter buildExporter(boolean shouldAddAppSignals) {
67+
protected MetricExporter buildExporter(
68+
LogEventEmitter<CloudWatchLogsClient> emitter,
69+
String namespace,
70+
boolean shouldAddAppSignals) {
6871
return AwsCloudWatchEmfExporter.builder()
69-
.setNamespace(NAMESPACE)
70-
.setEmitter(this.mockEmitter)
72+
.setNamespace(namespace)
73+
.setEmitter(emitter)
7174
.setShouldAddApplicationSignalsDimensions(shouldAddAppSignals)
7275
.build();
7376
}
7477

7578
@Test
7679
void testShutdown() {
77-
MetricExporter spyExporter = spy(buildExporter(false));
80+
MetricExporter spyExporter = spy(buildExporter(this.mockEmitter, NAMESPACE, false));
7881
doNothing().when(this.mockEmitter).flushEvents();
7982

8083
CompletableResultCode result = spyExporter.shutdown();
@@ -246,14 +249,15 @@ void testBatchInactiveAfter24Hours() {
246249
@MethodSource("batchLimitScenarios")
247250
void testEventBatchLimits(
248251
Map<String, Object> logEvent, int eventCount, boolean shouldExceedLimit) {
252+
LogEventEmitter<CloudWatchLogsClient> testEmitter = createEmitter();
249253
for (int i = 0; i < eventCount; i++) {
250-
this.mockEmitter.emit(logEvent);
254+
testEmitter.emit(logEvent);
251255
}
252256

253257
if (shouldExceedLimit) {
254-
verify(this.mockEmitter, atLeast(2)).emit(logEvent);
258+
verify(testEmitter, atLeast(2)).emit(logEvent);
255259
} else {
256-
verify(this.mockEmitter, times(eventCount)).emit(logEvent);
260+
verify(testEmitter, times(eventCount)).emit(logEvent);
257261
}
258262
}
259263

awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/exporter/aws/metrics/BaseEmfExporterTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ public abstract class BaseEmfExporterTest<T> {
6363

6464
abstract LogEventEmitter<T> createEmitter();
6565

66-
abstract MetricExporter buildExporter(boolean shouldAddAppSignals);
66+
abstract MetricExporter buildExporter(
67+
LogEventEmitter<T> emitter, String namespace, boolean shouldAddAppSignals);
6768

6869
@BeforeEach
6970
void setup() {
7071
this.capturedLogEvents = new ArrayList<>();
7172
this.mockEmitter = this.createEmitter();
72-
this.exporter = this.buildExporter(false);
73+
this.exporter = this.buildExporter(mockEmitter, NAMESPACE, false);
7374
this.metricData = mock(MetricData.class);
7475

7576
doAnswer(
@@ -90,6 +91,18 @@ void testExportEmptyMetrics() {
9091
assertEquals(0, capturedLogEvents.size());
9192
}
9293

94+
@Test
95+
void testNullNamespaceDefaultsToDefault() {
96+
MetricExporter exporter = buildExporter(createEmitter(), null, false);
97+
assertNotNull(exporter);
98+
}
99+
100+
@Test
101+
void testEmptyNamespaceDefaultsToDefault() {
102+
MetricExporter exporter = buildExporter(createEmitter(), "", false);
103+
assertNotNull(exporter);
104+
}
105+
93106
@Test
94107
void testExportFailureHandling() {
95108
doThrow(new RuntimeException("Test exception")).when(mockEmitter).emit(any());
@@ -268,7 +281,7 @@ void testExponentialHistogramMetricProcessing() {
268281
@MethodSource("applicationSignalsDimensionsProvider")
269282
void testApplicationSignalsDimensions(
270283
Map<String, String> resourceAttrs, String expectedService, String expectedEnvironment) {
271-
MetricExporter exporterWithAppSignals = buildExporter(true);
284+
MetricExporter exporterWithAppSignals = buildExporter(mockEmitter, NAMESPACE, true);
272285

273286
Resource resource = Resource.empty();
274287
for (Map.Entry<String, String> entry : resourceAttrs.entrySet()) {

awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/exporter/aws/metrics/ConsoleEmfExporterTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ protected LogEventEmitter<PrintStream> createEmitter() {
5555
}
5656

5757
@Override
58-
protected MetricExporter buildExporter(boolean shouldAddAppSignals) {
58+
protected MetricExporter buildExporter(
59+
LogEventEmitter<PrintStream> emitter, String namespace, boolean shouldAddAppSignals) {
5960
return ConsoleEmfExporter.builder()
60-
.setNamespace(NAMESPACE)
61-
.setEmitter(mockEmitter)
61+
.setNamespace(namespace)
62+
.setEmitter(emitter)
6263
.setShouldAddApplicationSignalsDimensions(shouldAddAppSignals)
6364
.build();
6465
}

0 commit comments

Comments
 (0)