-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d4a2f4
commit d2d4324
Showing
3 changed files
with
161 additions
and
12 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...e-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/internal/ResourceDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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 { | ||
Optional<D> readData(ConfigProperties config); | ||
|
||
void registerAttributes(Builder<D> builder); | ||
|
||
/** | ||
* Greater order means lower priority. The default order is 0. | ||
*/ | ||
@Override | ||
int order(); | ||
|
||
String name(); | ||
|
||
default boolean defaultEnabled() { | ||
return true; | ||
} | ||
|
||
interface Builder<D> { | ||
<T> Builder<D> add(AttributeKey<T> key, Function<D, Optional<T>> getter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ResourceDetectorReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
|
||
@Override | ||
public <T> AttributeBuilder add(AttributeKey<T> key, Function<D, Optional<T>> getter) { | ||
attributeGetters.put((AttributeKey) key, Objects.requireNonNull((Function) getter)); | ||
return this; | ||
} | ||
} | ||
|
||
private final Map<AttributeKey<Object>, Function<D, Optional<?>>> attributeGetters = | ||
new HashMap<>(); | ||
|
||
public ResourceDetectorReader(ResourceDetector<D> resourceDetector) { | ||
this.resourceDetector = resourceDetector; | ||
resourceDetector.registerAttributes(new AttributeBuilder()); | ||
} | ||
|
||
public boolean shouldApply(ConfigProperties config, Resource existing) { | ||
Map<String, String> resourceAttributes = getResourceAttributes(config); | ||
return attributeGetters.keySet().stream() | ||
.allMatch(key -> shouldUpdate(config, existing, key, resourceAttributes)); | ||
} | ||
|
||
public Resource createResource(ConfigProperties config, Resource existing) { | ||
return resourceDetector | ||
.readData(config) | ||
.map( | ||
data -> { | ||
Map<String, String> resourceAttributes = getResourceAttributes(config); | ||
AttributesBuilder builder = Attributes.builder(); | ||
attributeGetters.entrySet().stream() | ||
.filter(e -> shouldUpdate(config, existing, e.getKey(), resourceAttributes)) | ||
.forEach( | ||
e -> | ||
e.getValue() | ||
.apply(data) | ||
.ifPresent(value -> putAttribute(builder, e.getKey(), value))); | ||
return Resource.create(builder.build()); | ||
}) | ||
.orElse(Resource.empty()); | ||
} | ||
|
||
private static <T> void putAttribute(AttributesBuilder builder, AttributeKey<T> key, T value) { | ||
builder.put(key, value); | ||
} | ||
|
||
private static Map<String, String> getResourceAttributes(ConfigProperties config) { | ||
return config.getMap("otel.resource.attributes"); | ||
} | ||
|
||
private static boolean shouldUpdate( | ||
ConfigProperties config, | ||
Resource existing, | ||
AttributeKey<?> key, | ||
Map<String, String> resourceAttributes) { | ||
if (resourceAttributes.containsKey(key.getKey())) { | ||
return false; | ||
} | ||
|
||
Object value = existing.getAttribute(key); | ||
|
||
if (key.getKey().equals("service.name")) { | ||
return config.getString("otel.service.name") == null && "unknown_service:java".equals(value); | ||
} | ||
|
||
return value == null; | ||
} | ||
} |