|
5 | 5 |
|
6 | 6 | package io.opentelemetry.instrumentation.spring.autoconfigure.resources;
|
7 | 7 |
|
| 8 | +import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_NAME; |
| 9 | +import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_VERSION; |
8 | 10 | import static org.assertj.core.api.Assertions.assertThat;
|
9 | 11 | import static org.assertj.core.api.Assertions.entry;
|
10 | 12 |
|
|
13 | 15 | import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
|
14 | 16 | import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
|
15 | 17 | import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
|
| 18 | +import io.opentelemetry.sdk.resources.Resource; |
| 19 | +import java.util.Properties; |
16 | 20 | import org.junit.jupiter.api.DisplayName;
|
17 | 21 | import org.junit.jupiter.api.Test;
|
18 | 22 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
| 23 | +import org.springframework.boot.info.BuildProperties; |
19 | 24 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
20 | 25 |
|
21 | 26 | public class SpringResourceProviderTest {
|
@@ -54,4 +59,74 @@ void hasDefaultTypes() {
|
54 | 59 | context ->
|
55 | 60 | assertThat(context.getBean(OtelResourceProperties.class).getAttributes()).isEmpty());
|
56 | 61 | }
|
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName( |
| 65 | + "when spring.application.name is set value should be passed to service name attribute") |
| 66 | + void shouldDetermineServiceNameBySpringApplicationName() { |
| 67 | + this.contextRunner |
| 68 | + .withPropertyValues("spring.application.name=myapp-backend") |
| 69 | + .withConfiguration( |
| 70 | + AutoConfigurations.of( |
| 71 | + OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class)) |
| 72 | + .run( |
| 73 | + context -> { |
| 74 | + Resource otelResource = context.getBean("otelResource", Resource.class); |
| 75 | + |
| 76 | + assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("myapp-backend"); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @DisplayName( |
| 82 | + "when spring.application.name is set value should be passed to service name attribute") |
| 83 | + void shouldDetermineServiceNameAndVersionBySpringApplicationVersion() { |
| 84 | + Properties properties = new Properties(); |
| 85 | + properties.put("name", "demo"); |
| 86 | + properties.put("version", "0.3"); |
| 87 | + this.contextRunner |
| 88 | + .withBean("buildProperties", BuildProperties.class, () -> new BuildProperties(properties)) |
| 89 | + .withConfiguration( |
| 90 | + AutoConfigurations.of( |
| 91 | + OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class)) |
| 92 | + .run( |
| 93 | + context -> { |
| 94 | + Resource otelResource = context.getBean("otelResource", Resource.class); |
| 95 | + |
| 96 | + assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("demo"); |
| 97 | + assertThat(otelResource.getAttribute(SERVICE_VERSION)).isEqualTo("0.3"); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @DisplayName( |
| 103 | + "when spring application name and otel service name are not set service name should be default") |
| 104 | + void hasDefaultServiceName() { |
| 105 | + this.contextRunner |
| 106 | + .withConfiguration( |
| 107 | + AutoConfigurations.of( |
| 108 | + OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class)) |
| 109 | + .run( |
| 110 | + context -> { |
| 111 | + Resource otelResource = context.getBean("otelResource", Resource.class); |
| 112 | + |
| 113 | + assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("unknown_service:java"); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @DisplayName("when otel service name is set it should be set as service name attribute") |
| 119 | + void shouldDetermineServiceNameByOtelServiceName() { |
| 120 | + this.contextRunner |
| 121 | + .withConfiguration( |
| 122 | + AutoConfigurations.of( |
| 123 | + OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class)) |
| 124 | + .withPropertyValues("otel.resource.attributes.service.name=otel-name-backend") |
| 125 | + .run( |
| 126 | + context -> { |
| 127 | + Resource otelResource = context.getBean("otelResource", Resource.class); |
| 128 | + |
| 129 | + assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("otel-name-backend"); |
| 130 | + }); |
| 131 | + } |
57 | 132 | }
|
0 commit comments