|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.extension.incubator.fileconfig; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 10 | + |
| 11 | +import io.opentelemetry.api.common.AttributeKey; |
| 12 | +import io.opentelemetry.api.common.Attributes; |
| 13 | +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; |
| 14 | +import io.opentelemetry.sdk.resources.Resource; |
| 15 | +import java.util.Objects; |
| 16 | +import java.util.UUID; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junitpioneer.jupiter.ClearSystemProperty; |
| 19 | + |
| 20 | +class ServiceResourceDetectorTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + void getTypeAndName() { |
| 24 | + ServiceResourceDetector detector = new ServiceResourceDetector(); |
| 25 | + |
| 26 | + assertThat(detector.getType()).isEqualTo(Resource.class); |
| 27 | + assertThat(detector.getName()).isEqualTo("service"); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + @ClearSystemProperty(key = "otel.service.name") |
| 32 | + void create_SystemPropertySet() { |
| 33 | + System.setProperty("otel.service.name", "test"); |
| 34 | + |
| 35 | + assertThat(new ServiceResourceDetector().create(DeclarativeConfigProperties.empty())) |
| 36 | + .satisfies( |
| 37 | + resource -> { |
| 38 | + Attributes attributes = resource.getAttributes(); |
| 39 | + assertThat(attributes.get(AttributeKey.stringKey("service.name"))).isEqualTo("test"); |
| 40 | + assertThatCode( |
| 41 | + () -> |
| 42 | + UUID.fromString( |
| 43 | + Objects.requireNonNull( |
| 44 | + attributes.get(AttributeKey.stringKey("service.instance.id"))))) |
| 45 | + .doesNotThrowAnyException(); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void create_NoSystemProperty() { |
| 51 | + assertThat(new ServiceResourceDetector().create(DeclarativeConfigProperties.empty())) |
| 52 | + .satisfies( |
| 53 | + resource -> { |
| 54 | + Attributes attributes = resource.getAttributes(); |
| 55 | + assertThat(attributes.get(AttributeKey.stringKey("service.name"))).isNull(); |
| 56 | + assertThatCode( |
| 57 | + () -> |
| 58 | + UUID.fromString( |
| 59 | + Objects.requireNonNull( |
| 60 | + attributes.get(AttributeKey.stringKey("service.instance.id"))))) |
| 61 | + .doesNotThrowAnyException(); |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
0 commit comments