Skip to content

Commit 2c3eabf

Browse files
add missing declarative config resource providers (#14222)
Co-authored-by: otelbot <[email protected]>
1 parent b444563 commit 2c3eabf

File tree

37 files changed

+683
-207
lines changed

37 files changed

+683
-207
lines changed

instrumentation/resources/library/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88
compileOnly("io.opentelemetry:opentelemetry-api-incubator")
99
implementation("io.opentelemetry:opentelemetry-sdk-common")
1010
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
11+
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-incubator")
1112
implementation("io.opentelemetry.semconv:opentelemetry-semconv")
1213

1314
annotationProcessor("com.google.auto.service:auto-service")

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/AttributeResourceProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public <T> AttributeBuilder add(AttributeKey<T> key, Function<D, Optional<T>> ge
4545
}
4646

4747
private Set<AttributeKey<?>> filteredKeys;
48-
4948
private final Map<AttributeKey<Object>, Function<D, Optional<?>>> attributeGetters =
5049
new HashMap<>();
5150

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/JarServiceNameDetector.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,25 @@
55

66
package io.opentelemetry.instrumentation.resources;
77

8-
import static java.util.logging.Level.FINE;
9-
108
import com.google.auto.service.AutoService;
11-
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.instrumentation.resources.internal.JarServiceNameResourceExtractor;
1210
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1311
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
1412
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
1513
import io.opentelemetry.sdk.resources.Resource;
1614
import io.opentelemetry.semconv.ServiceAttributes;
17-
import java.nio.file.Path;
1815
import java.util.Map;
19-
import java.util.Optional;
20-
import java.util.function.Supplier;
21-
import java.util.logging.Logger;
2216

2317
/**
24-
* A {@link ResourceProvider} that will attempt to detect the application name from the jar name.
18+
* A {@link ResourceProvider} that will attempt to detect the <code>service.name</code> from the
19+
* main jar file name.
2520
*/
2621
@AutoService(ResourceProvider.class)
2722
public final class JarServiceNameDetector implements ConditionalResourceProvider {
2823

29-
private static final Logger logger = Logger.getLogger(JarServiceNameDetector.class.getName());
30-
31-
private final Supplier<Optional<Path>> jarPathSupplier;
32-
33-
@SuppressWarnings("unused") // SPI
34-
public JarServiceNameDetector() {
35-
this(MainJarPathHolder::getJarPath);
36-
}
37-
38-
private JarServiceNameDetector(Supplier<Optional<Path>> jarPathSupplier) {
39-
this.jarPathSupplier = jarPathSupplier;
40-
}
41-
42-
// visible for tests
43-
JarServiceNameDetector(MainJarPathFinder jarPathFinder) {
44-
this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()));
45-
}
46-
4724
@Override
4825
public Resource createResource(ConfigProperties config) {
49-
return jarPathSupplier
50-
.get()
51-
.map(
52-
jarPath -> {
53-
String serviceName = getServiceName(jarPath);
54-
logger.log(
55-
FINE, "Auto-detected service name from the jar file name: {0}", serviceName);
56-
return Resource.create(Attributes.of(ServiceAttributes.SERVICE_NAME, serviceName));
57-
})
58-
.orElseGet(Resource::empty);
26+
return new JarServiceNameResourceExtractor().extract();
5927
}
6028

6129
@Override
@@ -67,12 +35,6 @@ public boolean shouldApply(ConfigProperties config, Resource existing) {
6735
&& "unknown_service:java".equals(existing.getAttribute(ServiceAttributes.SERVICE_NAME));
6836
}
6937

70-
private static String getServiceName(Path jarPath) {
71-
String jarName = jarPath.getFileName().toString();
72-
int dotIndex = jarName.lastIndexOf(".");
73-
return dotIndex == -1 ? jarName : jarName.substring(0, dotIndex);
74-
}
75-
7638
@Override
7739
public int order() {
7840
// make it run later than the SpringBootServiceNameDetector

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ManifestResourceProvider.java

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,48 @@
55

66
package io.opentelemetry.instrumentation.resources;
77

8-
import static java.util.logging.Level.FINE;
9-
108
import com.google.auto.service.AutoService;
9+
import io.opentelemetry.instrumentation.resources.internal.ManifestResourceExtractor;
1110
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
11+
import io.opentelemetry.sdk.resources.Resource;
1212
import io.opentelemetry.semconv.ServiceAttributes;
13-
import java.io.IOException;
14-
import java.nio.file.Path;
1513
import java.util.Optional;
16-
import java.util.function.Function;
1714
import java.util.function.Supplier;
18-
import java.util.jar.JarFile;
19-
import java.util.jar.Manifest;
20-
import java.util.logging.Logger;
2115

2216
/**
2317
* A {@link ResourceProvider} that will attempt to detect the <code>service.name</code> and <code>
2418
* service.version</code> from META-INF/MANIFEST.MF.
2519
*/
2620
@AutoService(ResourceProvider.class)
27-
public final class ManifestResourceProvider extends AttributeResourceProvider<Manifest> {
28-
29-
private static final Logger logger = Logger.getLogger(ManifestResourceProvider.class.getName());
21+
public final class ManifestResourceProvider extends AttributeResourceProvider<Resource> {
3022

3123
@SuppressWarnings("unused") // SPI
3224
public ManifestResourceProvider() {
33-
this(MainJarPathHolder::getJarPath, ManifestResourceProvider::readManifest);
25+
this(() -> new ManifestResourceExtractor().extract());
3426
}
3527

36-
private ManifestResourceProvider(
37-
Supplier<Optional<Path>> jarPathSupplier, Function<Path, Optional<Manifest>> manifestReader) {
28+
// Visible for testing
29+
ManifestResourceProvider(Supplier<Resource> resourceSupplier) {
3830
super(
39-
new AttributeProvider<Manifest>() {
31+
new AttributeProvider<Resource>() {
4032
@Override
41-
public Optional<Manifest> readData() {
42-
return jarPathSupplier.get().flatMap(manifestReader);
33+
public Optional<Resource> readData() {
34+
return Optional.of(resourceSupplier.get());
4335
}
4436

4537
@Override
46-
public void registerAttributes(Builder<Manifest> builder) {
38+
public void registerAttributes(Builder<Resource> builder) {
4739
builder
4840
.add(
4941
ServiceAttributes.SERVICE_NAME,
50-
manifest -> {
51-
String serviceName =
52-
manifest.getMainAttributes().getValue("Implementation-Title");
53-
return Optional.ofNullable(serviceName);
54-
})
42+
r -> Optional.ofNullable(r.getAttribute(ServiceAttributes.SERVICE_NAME)))
5543
.add(
5644
ServiceAttributes.SERVICE_VERSION,
57-
manifest -> {
58-
String serviceVersion =
59-
manifest.getMainAttributes().getValue("Implementation-Version");
60-
return Optional.ofNullable(serviceVersion);
61-
});
45+
r -> Optional.ofNullable(r.getAttribute(ServiceAttributes.SERVICE_VERSION)));
6246
}
6347
});
6448
}
6549

66-
// Visible for testing
67-
ManifestResourceProvider(
68-
MainJarPathFinder jarPathFinder, Function<Path, Optional<Manifest>> manifestReader) {
69-
this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()), manifestReader);
70-
}
71-
72-
private static Optional<Manifest> readManifest(Path jarPath) {
73-
try (JarFile jarFile = new JarFile(jarPath.toFile(), false)) {
74-
return Optional.of(jarFile.getManifest());
75-
} catch (IOException exception) {
76-
logger.log(FINE, "Error reading manifest", exception);
77-
return Optional.empty();
78-
}
79-
}
80-
8150
@Override
8251
public int order() {
8352
// make it run later than SpringBootServiceNameDetector

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ProcessArguments.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/ProcessResource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.opentelemetry.api.common.AttributeKey;
99
import io.opentelemetry.api.common.Attributes;
1010
import io.opentelemetry.api.common.AttributesBuilder;
11+
import io.opentelemetry.instrumentation.resources.internal.ProcessArguments;
1112
import io.opentelemetry.sdk.resources.Resource;
1213
import io.opentelemetry.semconv.SchemaUrls;
1314
import java.io.File;

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/internal/ContainerResourceComponentProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
@AutoService(ComponentProvider.class)
2020
public class ContainerResourceComponentProvider extends ResourceComponentProvider {
2121
public ContainerResourceComponentProvider() {
22-
super("container", ContainerResource::get);
22+
super("container", p -> ContainerResource.get());
2323
}
2424
}

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/internal/HostResourceComponentProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
@AutoService(ComponentProvider.class)
2222
public class HostResourceComponentProvider extends ResourceComponentProvider {
2323
public HostResourceComponentProvider() {
24-
super("host", () -> HostResource.get().merge(HostIdResource.get()).merge(OsResource.get()));
24+
super("host", p -> HostResource.get().merge(HostIdResource.get()).merge(OsResource.get()));
2525
}
2626
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.resources.internal;
7+
8+
import com.google.auto.service.AutoService;
9+
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
10+
11+
/**
12+
* Declarative config jar resource provider.
13+
*
14+
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
15+
* at any time.
16+
*/
17+
@SuppressWarnings("rawtypes")
18+
@AutoService(ComponentProvider.class)
19+
public class JarResourceComponentProvider extends ResourceComponentProvider {
20+
public JarResourceComponentProvider() {
21+
super("jar", p -> new JarServiceNameResourceExtractor().extract());
22+
}
23+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.resources.internal;
7+
8+
import static java.util.logging.Level.FINE;
9+
10+
import io.opentelemetry.api.common.Attributes;
11+
import io.opentelemetry.sdk.resources.Resource;
12+
import io.opentelemetry.semconv.ServiceAttributes;
13+
import java.nio.file.Path;
14+
import java.util.Optional;
15+
import java.util.function.Supplier;
16+
import java.util.logging.Logger;
17+
18+
/**
19+
* A resource extractor that will attempt to detect the <code>service.name</code> from the main jar
20+
* file name.
21+
*
22+
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
23+
* at any time.
24+
*/
25+
public final class JarServiceNameResourceExtractor {
26+
27+
private static final Logger logger =
28+
Logger.getLogger(JarServiceNameResourceExtractor.class.getName());
29+
30+
private final Supplier<Optional<Path>> jarPathSupplier;
31+
32+
public JarServiceNameResourceExtractor() {
33+
this(MainJarPathHolder::getJarPath);
34+
}
35+
36+
// visible for tests
37+
JarServiceNameResourceExtractor(MainJarPathFinder jarPathFinder) {
38+
this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()));
39+
}
40+
41+
private JarServiceNameResourceExtractor(Supplier<Optional<Path>> jarPathSupplier) {
42+
this.jarPathSupplier = jarPathSupplier;
43+
}
44+
45+
public Resource extract() {
46+
return jarPathSupplier
47+
.get()
48+
.map(
49+
jarPath -> {
50+
String serviceName = getServiceName(jarPath);
51+
logger.log(
52+
FINE, "Auto-detected service name from the jar file name: {0}", serviceName);
53+
return Resource.create(Attributes.of(ServiceAttributes.SERVICE_NAME, serviceName));
54+
})
55+
.orElseGet(Resource::empty);
56+
}
57+
58+
private static String getServiceName(Path jarPath) {
59+
String jarName = jarPath.getFileName().toString();
60+
int dotIndex = jarName.lastIndexOf(".");
61+
return dotIndex == -1 ? jarName : jarName.substring(0, dotIndex);
62+
}
63+
}

0 commit comments

Comments
 (0)