Skip to content

Commit c04a349

Browse files
committed
separate resource extraction from supported interfaces
1 parent f4fde98 commit c04a349

File tree

14 files changed

+318
-194
lines changed

14 files changed

+318
-194
lines changed

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

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +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;
12-
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
9+
import io.opentelemetry.instrumentation.resources.internal.JarServiceNameResourceExtractor;
1310
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1411
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
1512
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
1613
import io.opentelemetry.sdk.resources.Resource;
1714
import io.opentelemetry.semconv.ServiceAttributes;
18-
import java.nio.file.Path;
1915
import java.util.Map;
20-
import java.util.Optional;
21-
import java.util.function.Supplier;
22-
import java.util.logging.Logger;
2316

2417
/**
25-
* 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.
2620
*/
2721
@AutoService(ResourceProvider.class)
2822
public final class JarServiceNameDetector implements ConditionalResourceProvider {
2923

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

7129
@Override
@@ -77,12 +35,6 @@ public boolean shouldApply(ConfigProperties config, Resource existing) {
7735
&& "unknown_service:java".equals(existing.getAttribute(ServiceAttributes.SERVICE_NAME));
7836
}
7937

80-
private static String getServiceName(Path jarPath) {
81-
String jarName = jarPath.getFileName().toString();
82-
int dotIndex = jarName.lastIndexOf(".");
83-
return dotIndex == -1 ? jarName : jarName.substring(0, dotIndex);
84-
}
85-
8638
@Override
8739
public int order() {
8840
// make it run later than the SpringBootServiceNameDetector

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

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

66
package io.opentelemetry.instrumentation.resources;
77

8-
import static java.util.logging.Level.WARNING;
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(
30+
Supplier<Resource> resourceSupplier) {
3831
super(
39-
new AttributeProvider<Manifest>() {
32+
new AttributeProvider<Resource>() {
4033
@Override
41-
public Optional<Manifest> readData() {
42-
return jarPathSupplier.get().flatMap(manifestReader);
34+
public Optional<Resource> readData() {
35+
return Optional.of(resourceSupplier.get());
4336
}
4437

4538
@Override
46-
public void registerAttributes(Builder<Manifest> builder) {
39+
public void registerAttributes(Builder<Resource> builder) {
4740
builder
4841
.add(
4942
ServiceAttributes.SERVICE_NAME,
50-
manifest -> {
51-
String serviceName =
52-
manifest.getMainAttributes().getValue("Implementation-Title");
53-
return Optional.ofNullable(serviceName);
54-
})
43+
r -> Optional.ofNullable(r.getAttribute(ServiceAttributes.SERVICE_NAME)))
5544
.add(
5645
ServiceAttributes.SERVICE_VERSION,
57-
manifest -> {
58-
String serviceVersion =
59-
manifest.getMainAttributes().getValue("Implementation-Version");
60-
return Optional.ofNullable(serviceVersion);
61-
});
46+
r -> Optional.ofNullable(r.getAttribute(ServiceAttributes.SERVICE_VERSION)));
6247
}
6348
});
6449
}
6550

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(WARNING, "Error reading manifest", exception);
77-
return Optional.empty();
78-
}
79-
}
80-
8151
@Override
8252
public int order() {
8353
// 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/JarResourceComponentProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.instrumentation.resources.internal;
77

88
import com.google.auto.service.AutoService;
9-
import io.opentelemetry.instrumentation.resources.JarServiceNameDetector;
109
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
1110

1211
/**
@@ -19,6 +18,6 @@
1918
@AutoService(ComponentProvider.class)
2019
public class JarResourceComponentProvider extends ResourceComponentProvider {
2120
public JarResourceComponentProvider() {
22-
super("jar", p -> new JarServiceNameDetector().createResource(p));
21+
super("jar", p -> new JarServiceNameResourceExtractor().extract());
2322
}
2423
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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
20+
* main jar 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 = Logger.getLogger(JarServiceNameResourceExtractor.class.getName());
28+
29+
private final Supplier<Optional<Path>> jarPathSupplier;
30+
31+
public JarServiceNameResourceExtractor() {
32+
this(MainJarPathHolder::getJarPath);
33+
}
34+
35+
// visible for tests
36+
JarServiceNameResourceExtractor(MainJarPathFinder jarPathFinder) {
37+
this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()));
38+
}
39+
40+
private JarServiceNameResourceExtractor(Supplier<Optional<Path>> jarPathSupplier) {
41+
this.jarPathSupplier = jarPathSupplier;
42+
}
43+
44+
public Resource extract() {
45+
return jarPathSupplier
46+
.get()
47+
.map(
48+
jarPath -> {
49+
String serviceName = getServiceName(jarPath);
50+
logger.log(
51+
FINE, "Auto-detected service name from the jar file name: {0}", serviceName);
52+
return Resource.create(Attributes.of(ServiceAttributes.SERVICE_NAME, serviceName));
53+
})
54+
.orElseGet(Resource::empty);
55+
}
56+
57+
private static String getServiceName(Path jarPath) {
58+
String jarName = jarPath.getFileName().toString();
59+
int dotIndex = jarName.lastIndexOf(".");
60+
return dotIndex == -1 ? jarName : jarName.substring(0, dotIndex);
61+
}
62+
}

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathFinder.java renamed to instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/internal/MainJarPathFinder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.resources;
6+
package io.opentelemetry.instrumentation.resources.internal;
77

88
import java.nio.file.Files;
99
import java.nio.file.InvalidPathException;
@@ -14,6 +14,7 @@
1414
import java.util.function.Supplier;
1515
import javax.annotation.Nullable;
1616

17+
1718
class MainJarPathFinder {
1819
private final Supplier<String[]> getProcessHandleArguments;
1920
private final Function<String, String> getSystemProperty;

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/MainJarPathHolder.java renamed to instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/internal/MainJarPathHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.instrumentation.resources;
6+
package io.opentelemetry.instrumentation.resources.internal;
77

88
import java.nio.file.Path;
99
import java.util.Optional;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.instrumentation.resources.internal;
77

88
import com.google.auto.service.AutoService;
9-
import io.opentelemetry.instrumentation.resources.ManifestResourceProvider;
109
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
1110

1211
/**
@@ -19,6 +18,6 @@
1918
@AutoService(ComponentProvider.class)
2019
public class ManifestResourceComponentProvider extends ResourceComponentProvider {
2120
public ManifestResourceComponentProvider() {
22-
super("manifest", p -> new ManifestResourceProvider().createResource(p));
21+
super("manifest", p -> new ManifestResourceExtractor().extract());
2322
}
2423
}

0 commit comments

Comments
 (0)