Skip to content

add missing declarative config resource providers #14222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions instrumentation/resources/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
compileOnly("io.opentelemetry:opentelemetry-api-incubator")
implementation("io.opentelemetry:opentelemetry-sdk-common")
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-incubator")
implementation("io.opentelemetry.semconv:opentelemetry-semconv")

annotationProcessor("com.google.auto.service:auto-service")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

/**
* An easier alternative to {@link io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider}, which
Expand All @@ -45,7 +46,6 @@ public <T> AttributeBuilder add(AttributeKey<T> key, Function<D, Optional<T>> ge
}

private Set<AttributeKey<?>> filteredKeys;

private final Map<AttributeKey<Object>, Function<D, Optional<?>>> attributeGetters =
new HashMap<>();

Expand All @@ -66,16 +66,25 @@ public final boolean shouldApply(ConfigProperties config, Resource existing) {

@Override
public final Resource createResource(ConfigProperties config) {
return create(filteredKeys);
}

@SuppressWarnings({"unchecked", "rawtypes"})
protected final Resource createUnconditional() {
return create((Set) attributeGetters.keySet());
}

private Resource create(@Nullable Set<AttributeKey<?>> keys) {
if (keys == null) {
throw new IllegalStateException("shouldApply should be called first");
}
return attributeProvider
.readData()
.map(
data -> {
if (filteredKeys == null) {
throw new IllegalStateException("shouldApply should be called first");
}
AttributesBuilder builder = Attributes.builder();
attributeGetters.entrySet().stream()
.filter(e -> filteredKeys.contains(e.getKey()))
.filter(e -> keys.contains(e.getKey()))
.forEach(
e ->
e.getValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.google.auto.service.AutoService;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
Expand Down Expand Up @@ -46,6 +47,15 @@ private JarServiceNameDetector(Supplier<Optional<Path>> jarPathSupplier) {

@Override
public Resource createResource(ConfigProperties config) {
return create();
}

@SuppressWarnings("unused")
public Resource createResource(DeclarativeConfigProperties config) {
return create();
}

private Resource create() {
return jarPathSupplier
.get()
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import static java.util.logging.Level.WARNING;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ServiceAttributes;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -69,6 +71,11 @@ public void registerAttributes(Builder<Manifest> builder) {
this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()), manifestReader);
}

@SuppressWarnings("unused")
public Resource createResource(DeclarativeConfigProperties config) {
return createUnconditional();
}

private static Optional<Manifest> readManifest(Path jarPath) {
try (JarFile jarFile = new JarFile(jarPath.toFile(), false)) {
return Optional.of(jarFile.getManifest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
@AutoService(ComponentProvider.class)
public class ContainerResourceComponentProvider extends ResourceComponentProvider {
public ContainerResourceComponentProvider() {
super("container", ContainerResource::get);
super("container", p -> ContainerResource.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
@AutoService(ComponentProvider.class)
public class HostResourceComponentProvider extends ResourceComponentProvider {
public HostResourceComponentProvider() {
super("host", () -> HostResource.get().merge(HostIdResource.get()).merge(OsResource.get()));
super("host", p -> HostResource.get().merge(HostIdResource.get()).merge(OsResource.get()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.resources.internal;

import com.google.auto.service.AutoService;
import io.opentelemetry.instrumentation.resources.JarServiceNameDetector;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;

/**
* Declarative config jar resource provider.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
@SuppressWarnings("rawtypes")
@AutoService(ComponentProvider.class)
public class JarResourceComponentProvider extends ResourceComponentProvider {
public JarResourceComponentProvider() {
super("jar", p -> new JarServiceNameDetector().createResource(p));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.resources.internal;

import com.google.auto.service.AutoService;
import io.opentelemetry.instrumentation.resources.ManifestResourceProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;

/**
* Declarative config manifest resource provider.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
@SuppressWarnings("rawtypes")
@AutoService(ComponentProvider.class)
public class ManifestResourceComponentProvider extends ResourceComponentProvider {
public ManifestResourceComponentProvider() {
super("manifest", p -> new ManifestResourceProvider().createResource(p));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
@AutoService(ComponentProvider.class)
public class ProcessResourceComponentProvider extends ResourceComponentProvider {
public ProcessResourceComponentProvider() {
super("process", () -> ProcessResource.get().merge(ProcessRuntimeResource.get()));
super("process", p -> ProcessResource.get().merge(ProcessRuntimeResource.get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.resources.Resource;
import java.util.function.Supplier;
import java.util.function.Function;

/** Abstract class to simply {@link Resource} {@link ComponentProvider} implementations. */
abstract class ResourceComponentProvider implements ComponentProvider<Resource> {

private final String name;
private final Supplier<Resource> supplier;
private final Function<DeclarativeConfigProperties, Resource> supplier;

ResourceComponentProvider(String name, Supplier<Resource> supplier) {
ResourceComponentProvider(String name, Function<DeclarativeConfigProperties, Resource> supplier) {
this.name = name;
this.supplier = supplier;
}
Expand All @@ -33,6 +33,6 @@ public String getName() {

@Override
public Resource create(DeclarativeConfigProperties declarativeConfigProperties) {
return supplier.get();
return supplier.apply(declarativeConfigProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ Collection<DynamicTest> createResource() {
assertThat(resource.getAttribute(SERVICE_NAME)).isEqualTo(t.expectedName);
assertThat(resource.getAttribute(SERVICE_VERSION))
.isEqualTo(t.expectedVersion);

if (t.existing.getAttributes().isEmpty()) {
// component provider does not consider existing resource
assertThat(provider.createUnconditional().getAttributes())
.isEqualTo(resource.getAttributes());
}
}))
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class DeclarativeConfigTest {
class ResourceDeclarativeConfigTest {

// just to ensure that the test exporters are registered
@RegisterExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ void mapObjectHeaders(String key) {

public static Stream<Arguments> listProperties() {
return Stream.of(
Arguments.of("otel.experimental.metrics.view.config", Arrays.asList("a", "b")),
Arguments.of("otel.experimental.resource.disabled.keys", Arrays.asList("a", "b")),
Arguments.of("otel.propagators", Arrays.asList("baggage", "b3")),
Arguments.of("otel.logs.exporter", Collections.singletonList("console")),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
otel:
experimental:
metrics:
view:
config: [ a,b ]
resource:
disabled:
keys: [ a,b ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public SpringBootServiceNameDetector() {

@Override
public Resource createResource(ConfigProperties config) {
return create();
}

Resource create() {
logger.log(FINER, "Performing Spring Boot service name auto-detection...");
// Note: The order should be consistent with the order of Spring matching, but noting
// that we have "first one wins" while Spring has "last one wins".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public SpringBootServiceVersionDetector() {

@Override
public Resource createResource(ConfigProperties config) {
return create();
}

Resource create() {
return getServiceVersionFromBuildInfo()
.map(
version -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.resources;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.resources.Resource;

/**
* Declarative config spring resource provider.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
@SuppressWarnings("rawtypes")
@AutoService(ComponentProvider.class)
public class SpringResourceComponentProvider implements ComponentProvider<Resource> {

@Override
public Class<Resource> getType() {
return Resource.class;
}

@Override
public String getName() {
return "spring";
}

@Override
public Resource create(DeclarativeConfigProperties config) {
return new SpringBootServiceVersionDetector()
.create()
.merge(new SpringBootServiceNameDetector().create());
}
}
1 change: 1 addition & 0 deletions javaagent-tooling/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
testImplementation(project(":testing-common"))
testImplementation("com.google.guava:guava")
testImplementation("org.junit-pioneer:junit-pioneer")
testImplementation("com.fasterxml.jackson.core:jackson-databind")
}

testing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.resources;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.resources.Resource;

@SuppressWarnings("rawtypes")
@AutoService(ComponentProvider.class)
public class DistroComponentProvider implements ComponentProvider<Resource> {

@Override
public Class<Resource> getType() {
return Resource.class;
}

@Override
public String getName() {
return "distribution";
}

@Override
public Resource create(DeclarativeConfigProperties config) {
return DistroResourceProvider.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling;
package io.opentelemetry.javaagent.tooling.resources;

import static io.opentelemetry.semconv.incubating.TelemetryIncubatingAttributes.TELEMETRY_DISTRO_NAME;
import static io.opentelemetry.semconv.incubating.TelemetryIncubatingAttributes.TELEMETRY_DISTRO_VERSION;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.javaagent.tooling.AgentVersion;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;

@AutoService(ResourceProvider.class)
public class DistroVersionResourceProvider implements ResourceProvider {
public class DistroResourceProvider implements ResourceProvider {

@Override
public Resource createResource(ConfigProperties config) {
return get();
}

static Resource get() {
return AgentVersion.VERSION == null
? Resource.empty()
: Resource.create(
Expand Down
Loading
Loading