Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.IntStream;

import io.opentelemetry.api.internal.PercentEscaper;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import org.springframework.mock.env.MockEnvironment;
Expand All @@ -39,23 +37,12 @@
*/
class OpenTelemetryResourceAttributesTests {

private static Random random;

private static final PercentEscaper escaper = PercentEscaper.create();

private final MockEnvironment environment = new MockEnvironment();

private final Map<String, String> environmentVariables = new LinkedHashMap<>();

private final Map<String, String> resourceAttributes = new LinkedHashMap<>();

@BeforeAll
static void beforeAll() {
long seed = new Random().nextLong();
System.out.println(OpenTelemetryResourceAttributesTests.class.getSimpleName() + " seed: " + seed);
random = new Random(seed);
}

@Test
void otelServiceNameShouldTakePrecedenceOverOtelResourceAttributes() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.name=ignored");
Expand All @@ -73,13 +60,13 @@ void otelServiceNameWhenEmptyShouldTakePrecedenceOverOtelResourceAttributes() {
@Test
void otelResourceAttributes() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES",
", ,,key1=value1,key2= value2, key3=value3,key4=,=value5,key6,=,key7=spring+boot,key8=ś");
", ,,key1=value1,key2= value2, key3=value3,key4=,=value5,key6,=,key7=%20spring+boot%20,key8=ś");
assertThat(getAttributes()).hasSize(7)
.containsEntry("key1", "value1")
.containsEntry("key2", "value2")
.containsEntry("key3", "value3")
.containsEntry("key4", "")
.containsEntry("key7", "spring+boot")
.containsEntry("key7", " spring+boot ")
.containsEntry("key8", "ś")
.containsEntry("service.name", "unknown_service");
}
Expand Down Expand Up @@ -120,12 +107,14 @@ void systemGetEnvShouldBeUsedAsDefaultEnvFunction() {

@Test
void otelResourceAttributeValuesShouldBePercentDecoded() {
Stream.generate(this::generateRandomString).limit(10000).forEach((value) -> {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=" + escaper.escape(value));
assertThat(getAttributes()).hasSize(2)
.containsEntry("service.name", "unknown_service")
.containsEntry("key", value);
});
PercentEscaper escaper = PercentEscaper.create();
String value = IntStream.range(32, 127)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "key=" + escaper.escape(value));
assertThat(getAttributes()).hasSize(2)
.containsEntry("service.name", "unknown_service")
.containsEntry("key", value);
}

@Test
Expand Down Expand Up @@ -206,10 +195,10 @@ void resourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName() {
@Test
void resourceAttributesShouldTakePrecedenceOverApplicationGroupNameForPopulatingServiceNamespace() {
this.resourceAttributes.put("service.namespace", "spring-boot-app");
this.environment.setProperty("spring.application.group", "overriden");
this.environment.setProperty("spring.application.group", "overridden");
assertThat(getAttributes()).hasSize(3)
.containsEntry("service.name", "unknown_service")
.containsEntry("service.group", "overriden")
.containsEntry("service.group", "overridden")
.containsEntry("service.namespace", "spring-boot-app");
}

Expand All @@ -226,9 +215,9 @@ void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupName()
@Test
void otelResourceAttributesShouldTakePrecedenceOverSpringApplicationGroupNameForServiceNamespace() {
this.environmentVariables.put("OTEL_RESOURCE_ATTRIBUTES", "service.namespace=spring-boot");
this.environment.setProperty("spring.application.group", "overriden");
this.environment.setProperty("spring.application.group", "overridden");
assertThat(getAttributes()).hasSize(3)
.containsEntry("service.group", "overriden")
.containsEntry("service.group", "overridden")
.containsEntry("service.namespace", "spring-boot");
}

Expand All @@ -250,11 +239,4 @@ private Map<String, String> getAttributes() {
return attributes;
}

private String generateRandomString() {
return random.ints(32, 127)
.limit(64)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}

}