Skip to content

Commit

Permalink
Fix Spring Boot null resource attributes for Open Telemetry (#177)
Browse files Browse the repository at this point in the history
* Fix Sonar issue on Spring Boot property

* Fix Sonar issue on Spring Boot property

* Fix Sonar issue on Spring Boot property
  • Loading branch information
loicgreffier authored Mar 6, 2024
1 parent 0e04810 commit 04b6d57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class OpenTelemetryConfig {
/**
* The OpenTelemetry resource attributes.
*/
@Value("${otel.resource.attributes:}")
@Value("${otel.resource.attributes:#{null}}")
private String otelResourceAttributes;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.michelin.kstreamplify.opentelemetry;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -18,6 +19,32 @@
class OpenTelemetryConfigTest {
private final OpenTelemetryConfig openTelemetryConfig = new OpenTelemetryConfig();

@Test
void shouldNotAddTagsToMetricsWhenNull() {
openTelemetryConfig.setOtelResourceAttributes(null);
MeterRegistryCustomizer<MeterRegistry> customizer = openTelemetryConfig.addTagsOnMetrics();

MeterRegistry meterRegistry = new OpenTelemetryMeterRegistry();
customizer.customize(meterRegistry);
meterRegistry.counter("fakeCounterMetric");

assertEquals("fakeCounterMetric", meterRegistry.getMeters().get(0).getId().getName());
assertTrue(meterRegistry.getMeters().get(0).getId().getTags().isEmpty());
}

@Test
void shouldNotAddTagsToMetricsWhenEmpty() {
openTelemetryConfig.setOtelResourceAttributes(EMPTY);
MeterRegistryCustomizer<MeterRegistry> customizer = openTelemetryConfig.addTagsOnMetrics();

MeterRegistry meterRegistry = new OpenTelemetryMeterRegistry();
customizer.customize(meterRegistry);
meterRegistry.counter("fakeCounterMetric");

assertEquals("fakeCounterMetric", meterRegistry.getMeters().get(0).getId().getName());
assertTrue(meterRegistry.getMeters().get(0).getId().getTags().isEmpty());
}

@Test
void shouldAddTagsToMetricsWhenOpenTelemetryRegistry() {
openTelemetryConfig.setOtelResourceAttributes("tagName=tagValue,tagName2=tagValue2");
Expand Down

0 comments on commit 04b6d57

Please sign in to comment.