-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.autoconfigure.spi.internal; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.Ordered; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
public interface ResourceDetector<D> extends Ordered { | ||
/** Read the data for the resource attributes. */ | ||
Optional<D> readData(ConfigProperties config); | ||
|
||
/** Registers the attributes that this resource detector can provide. */ | ||
void registerAttributes(Builder<D> builder); | ||
|
||
/** Greater order means lower priority. The default order is 0. */ | ||
@Override | ||
int order(); | ||
|
||
/** Returns the name of this resource detector. */ | ||
String name(); | ||
|
||
/** | ||
* Returns whether this resource detector is enabled by default. If not, it will only be used if | ||
* explicitly enabled in the configuration. | ||
*/ | ||
default boolean defaultEnabled() { | ||
return true; | ||
Check warning on line 33 in sdk-extensions/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ResourceDetector.java Codecov / codecov/patchsdk-extensions/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ResourceDetector.java#L33
|
||
} | ||
|
||
/** A builder for registering attributes that a resource detector can provide. */ | ||
interface Builder<D> { | ||
/** | ||
* Adds an attribute to the resource. | ||
* | ||
* @param key the attribute key | ||
* @param getter a function that returns the value of the attribute from the data that is read | ||
* by {@link ResourceDetector#readData(ConfigProperties)} | ||
* @return this builder | ||
* @param <T> the type of the attribute | ||
*/ | ||
<T> Builder<D> add(AttributeKey<T> key, Function<D, Optional<T>> getter); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.autoconfigure.internal; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
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.internal.ResourceDetector; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public final class ResourceDetectorReader<D> { | ||
|
||
private final ResourceDetector<D> resourceDetector; | ||
|
||
public class AttributeBuilder implements ResourceDetector.Builder<D> { | ||
|
||
private AttributeBuilder() {} | ||
Check warning on line 27 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L27
|
||
|
||
@Override | ||
public <T> AttributeBuilder add(AttributeKey<T> key, Function<D, Optional<T>> getter) { | ||
attributeGetters.put((AttributeKey) key, Objects.requireNonNull((Function) getter)); | ||
return this; | ||
Check warning on line 32 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L31-L32
|
||
} | ||
} | ||
|
||
private final Map<AttributeKey<Object>, Function<D, Optional<?>>> attributeGetters = | ||
Check warning on line 36 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L36
|
||
new HashMap<>(); | ||
|
||
public ResourceDetectorReader(ResourceDetector<D> resourceDetector) { | ||
this.resourceDetector = resourceDetector; | ||
resourceDetector.registerAttributes(new AttributeBuilder()); | ||
} | ||
Check warning on line 42 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L39-L42
|
||
|
||
public boolean shouldApply(ConfigProperties config, Resource existing) { | ||
Map<String, String> resourceAttributes = getResourceAttributes(config); | ||
return attributeGetters.keySet().stream() | ||
.allMatch(key -> shouldUpdate(config, existing, key, resourceAttributes)); | ||
Check warning on line 47 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L45-L47
|
||
} | ||
|
||
public Resource createResource(ConfigProperties config, Resource existing) { | ||
return resourceDetector | ||
.readData(config) | ||
.map( | ||
Check warning on line 53 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L51-L53
|
||
data -> { | ||
Map<String, String> resourceAttributes = getResourceAttributes(config); | ||
AttributesBuilder builder = Attributes.builder(); | ||
attributeGetters.entrySet().stream() | ||
.filter(e -> shouldUpdate(config, existing, e.getKey(), resourceAttributes)) | ||
.forEach( | ||
Check warning on line 59 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L55-L59
|
||
e -> | ||
e.getValue() | ||
.apply(data) | ||
.ifPresent(value -> putAttribute(builder, e.getKey(), value))); | ||
return Resource.create(builder.build()); | ||
Check warning on line 64 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L61-L64
|
||
}) | ||
.orElse(Resource.empty()); | ||
Check warning on line 66 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L66
|
||
} | ||
|
||
private static <T> void putAttribute(AttributesBuilder builder, AttributeKey<T> key, T value) { | ||
builder.put(key, value); | ||
} | ||
Check warning on line 71 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L70-L71
|
||
|
||
private static Map<String, String> getResourceAttributes(ConfigProperties config) { | ||
return config.getMap("otel.resource.attributes"); | ||
Check warning on line 74 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L74
|
||
} | ||
|
||
private static boolean shouldUpdate( | ||
ConfigProperties config, | ||
Resource existing, | ||
AttributeKey<?> key, | ||
Map<String, String> resourceAttributes) { | ||
if (resourceAttributes.containsKey(key.getKey())) { | ||
return false; | ||
Check warning on line 83 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L83
|
||
} | ||
|
||
Object value = existing.getAttribute(key); | ||
Check warning on line 86 in sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java Codecov / codecov/patchsdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java#L86
|
||
|
||
if (key.getKey().equals("service.name")) { | ||
return config.getString("otel.service.name") == null && "unknown_service:java".equals(value); | ||
} | ||
|
||
return value == null; | ||
} | ||
} |