forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceConfiguration.java
91 lines (75 loc) · 3.42 KB
/
ResourceConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* 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.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceBuilder;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiFunction;
/** Auto-configuration for the OpenTelemetry {@link Resource}. */
final class ResourceConfiguration {
// Visible for testing
static final String ATTRIBUTE_PROPERTY = "otel.resource.attributes";
static final String SERVICE_NAME_PROPERTY = "otel.service.name";
static final String DISABLED_ATTRIBUTE_KEYS = "otel.experimental.resource.disabled.keys";
static Resource configureResource(
ConfigProperties config,
ClassLoader serviceClassLoader,
BiFunction<? super Resource, ConfigProperties, ? extends Resource> resourceCustomizer) {
Resource result = Resource.getDefault();
Set<String> enabledProviders =
new HashSet<>(config.getList("otel.java.enabled.resource.providers"));
// TODO(anuraaga): We use a hyphen only once in this artifact, for
// otel.java.disabled.resource-providers. But fetching by the dot version is the simplest way
// to implement it for now.
Set<String> disabledProviders =
new HashSet<>(config.getList("otel.java.disabled.resource.providers"));
for (ResourceProvider resourceProvider :
SpiUtil.loadOrdered(ResourceProvider.class, serviceClassLoader)) {
if (!enabledProviders.isEmpty()
&& !enabledProviders.contains(resourceProvider.getClass().getName())) {
continue;
}
if (disabledProviders.contains(resourceProvider.getClass().getName())) {
continue;
}
if (resourceProvider.shouldApply(config, result)) {
result = result.merge(resourceProvider.createResource(config));
}
}
result = result.merge(createEnvironmentResource(config));
result = filterAttributes(result, config);
return resourceCustomizer.apply(result, config);
}
private static Resource createEnvironmentResource(ConfigProperties config) {
return Resource.create(getAttributes(config), ResourceAttributes.SCHEMA_URL);
}
// visible for testing
static Attributes getAttributes(ConfigProperties configProperties) {
AttributesBuilder resourceAttributes = Attributes.builder();
configProperties.getMap(ATTRIBUTE_PROPERTY).forEach(resourceAttributes::put);
String serviceName = configProperties.getString(SERVICE_NAME_PROPERTY);
if (serviceName != null) {
resourceAttributes.put(ResourceAttributes.SERVICE_NAME, serviceName);
}
return resourceAttributes.build();
}
// visible for testing
static Resource filterAttributes(Resource resource, ConfigProperties configProperties) {
Set<String> disabledKeys = new HashSet<>(configProperties.getList(DISABLED_ATTRIBUTE_KEYS));
ResourceBuilder builder =
resource.toBuilder().removeIf(attributeKey -> disabledKeys.contains(attributeKey.getKey()));
if (resource.getSchemaUrl() != null) {
builder.setSchemaUrl(resource.getSchemaUrl());
}
return builder.build();
}
private ResourceConfiguration() {}
}