Move the parsing of the otel.resource.attributes into an SPI impl (#2713)
This commit is contained in:
parent
9bc7a71b20
commit
e7fcbb7e46
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import io.opentelemetry.api.common.Attributes;
|
||||
import io.opentelemetry.api.common.AttributesBuilder;
|
||||
import io.opentelemetry.sdk.resources.ResourceProvider;
|
||||
|
||||
public class EnvironmentResource extends ResourceProvider {
|
||||
|
||||
static final String ATTRIBUTE_PROPERTY = "otel.resource.attributes";
|
||||
|
||||
@Override
|
||||
protected Attributes getAttributes() {
|
||||
return getAttributes(ConfigProperties.get());
|
||||
}
|
||||
|
||||
// visible for testing
|
||||
Attributes getAttributes(ConfigProperties configProperties) {
|
||||
AttributesBuilder resourceAttributes = Attributes.builder();
|
||||
configProperties.getCommaSeparatedMap(ATTRIBUTE_PROPERTY).forEach(resourceAttributes::put);
|
||||
return resourceAttributes.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,8 +5,6 @@
|
|||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import io.opentelemetry.api.common.Attributes;
|
||||
import io.opentelemetry.api.common.AttributesBuilder;
|
||||
import io.opentelemetry.context.propagation.ContextPropagators;
|
||||
import io.opentelemetry.sdk.OpenTelemetrySdk;
|
||||
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
|
||||
|
|
@ -28,7 +26,7 @@ public final class OpenTelemetrySdkAutoConfiguration {
|
|||
ConfigProperties config = ConfigProperties.get();
|
||||
ContextPropagators propagators = PropagatorConfiguration.configurePropagators(config);
|
||||
|
||||
Resource resource = configureResource(config);
|
||||
Resource resource = Resource.getDefault();
|
||||
|
||||
configureMeterProvider(resource, config);
|
||||
|
||||
|
|
@ -51,12 +49,5 @@ public final class OpenTelemetrySdkAutoConfiguration {
|
|||
}
|
||||
}
|
||||
|
||||
// Visible for testing
|
||||
static Resource configureResource(ConfigProperties config) {
|
||||
AttributesBuilder resourceAttributes = Attributes.builder();
|
||||
config.getCommaSeparatedMap("otel.resource.attributes").forEach(resourceAttributes::put);
|
||||
return Resource.getDefault().merge(Resource.create(resourceAttributes.build()));
|
||||
}
|
||||
|
||||
private OpenTelemetrySdkAutoConfiguration() {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
io.opentelemetry.sdk.autoconfigure.EnvironmentResource
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import io.opentelemetry.api.common.AttributeKey;
|
||||
import io.opentelemetry.api.common.Attributes;
|
||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class EnvironmentResourceTest {
|
||||
|
||||
@Test
|
||||
void resourceFromConfig_empty() {
|
||||
Attributes attributes =
|
||||
new EnvironmentResource().getAttributes(ConfigProperties.createForTest(emptyMap()));
|
||||
|
||||
assertThat(attributes).isEqualTo(Attributes.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceFromConfig() {
|
||||
Attributes attributes =
|
||||
new EnvironmentResource()
|
||||
.getAttributes(
|
||||
ConfigProperties.createForTest(
|
||||
singletonMap(
|
||||
EnvironmentResource.ATTRIBUTE_PROPERTY,
|
||||
"service.name=myService,appName=MyApp")));
|
||||
|
||||
assertThat(attributes)
|
||||
.isEqualTo(
|
||||
Attributes.of(
|
||||
ResourceAttributes.SERVICE_NAME,
|
||||
"myService",
|
||||
AttributeKey.stringKey("appName"),
|
||||
"MyApp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceFromConfig_emptyEnvVar() {
|
||||
Attributes attributes =
|
||||
new EnvironmentResource()
|
||||
.getAttributes(
|
||||
ConfigProperties.createForTest(
|
||||
singletonMap(EnvironmentResource.ATTRIBUTE_PROPERTY, "")));
|
||||
|
||||
assertThat(attributes).isEqualTo(Attributes.empty());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.sdk.autoconfigure;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import io.opentelemetry.sdk.resources.Resource;
|
||||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
|
||||
import java.util.Collections;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class OpenTelemetrySdkAutoConfigurationTest {
|
||||
|
||||
@Test
|
||||
void resourcePrioritizesUser() {
|
||||
Resource resource =
|
||||
OpenTelemetrySdkAutoConfiguration.configureResource(
|
||||
ConfigProperties.createForTest(
|
||||
Collections.singletonMap("otel.resource.attributes", "telemetry.sdk.name=test")));
|
||||
assertThat(resource.getAttributes().get(ResourceAttributes.TELEMETRY_SDK_NAME))
|
||||
.isEqualTo("test");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue