Skip to content

Commit 21d7756

Browse files
committed
Promote ComponentLoader to opentelemetry-context, standardize SPI loading
1 parent 0ca502b commit 21d7756

File tree

47 files changed

+287
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+287
-129
lines changed

api/incubator/src/main/java/io/opentelemetry/api/incubator/config/DeclarativeConfigProperties.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.opentelemetry.api.internal.ConfigUtil.defaultIfNull;
99

10+
import io.opentelemetry.context.ComponentLoader;
1011
import java.util.List;
1112
import java.util.Set;
1213
import javax.annotation.Nullable;
@@ -222,4 +223,9 @@ default List<DeclarativeConfigProperties> getStructuredList(
222223
* @return the configuration property keys
223224
*/
224225
Set<String> getPropertyKeys();
226+
227+
/** Return a {@link ComponentLoader} that should be used to load SPIs. */
228+
default ComponentLoader getComponentLoader() {
229+
return ComponentLoader.forClassLoader(DeclarativeConfigProperties.class.getClassLoader());
230+
}
225231
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/config/EmptyDeclarativeConfigProperties.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.api.incubator.config;
77

8+
import io.opentelemetry.context.ComponentLoader;
89
import java.util.Collections;
910
import java.util.List;
1011
import java.util.Set;
@@ -15,6 +16,8 @@ final class EmptyDeclarativeConfigProperties implements DeclarativeConfigPropert
1516

1617
private static final EmptyDeclarativeConfigProperties INSTANCE =
1718
new EmptyDeclarativeConfigProperties();
19+
private static final ComponentLoader COMPONENT_LOADER =
20+
ComponentLoader.forClassLoader(EmptyDeclarativeConfigProperties.class.getClassLoader());
1821

1922
private EmptyDeclarativeConfigProperties() {}
2023

@@ -74,4 +77,9 @@ public List<DeclarativeConfigProperties> getStructuredList(String name) {
7477
public Set<String> getPropertyKeys() {
7578
return Collections.emptySet();
7679
}
80+
81+
@Override
82+
public ComponentLoader getComponentLoader() {
83+
return COMPONENT_LOADER;
84+
}
7785
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.context;
7+
8+
import java.util.ServiceLoader;
9+
10+
/** A loader for components that are discovered via SPI. */
11+
public interface ComponentLoader {
12+
/**
13+
* Load implementations of an SPI.
14+
*
15+
* @param spiClass the SPI class
16+
* @param <T> the SPI type
17+
* @return iterable of SPI implementations
18+
*/
19+
<T> Iterable<T> load(Class<T> spiClass);
20+
21+
/**
22+
* Create an instance for the {@code classLoader} using {@link ServiceLoader#load(Class,
23+
* ClassLoader)}.
24+
*/
25+
static ComponentLoader forClassLoader(ClassLoader classLoader) {
26+
return new ServiceLoaderComponentLoader(classLoader);
27+
}
28+
}

context/src/main/java/io/opentelemetry/context/LazyStorage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
import java.util.ArrayList;
4141
import java.util.List;
42-
import java.util.ServiceLoader;
4342
import java.util.concurrent.atomic.AtomicReference;
4443
import java.util.function.Function;
4544
import java.util.logging.Level;
@@ -103,9 +102,10 @@ static ContextStorage createStorage(AtomicReference<Throwable> deferredStorageFa
103102
return ContextStorage.defaultStorage();
104103
}
105104

105+
ComponentLoader componentLoader =
106+
ComponentLoader.forClassLoader(LazyStorage.class.getClassLoader());
106107
List<ContextStorageProvider> providers = new ArrayList<>();
107-
for (ContextStorageProvider provider :
108-
ServiceLoader.load(ContextStorageProvider.class, LazyStorage.class.getClassLoader())) {
108+
for (ContextStorageProvider provider : componentLoader.load(ContextStorageProvider.class)) {
109109
if (provider
110110
.getClass()
111111
.getName()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.context;
7+
8+
import java.util.ServiceLoader;
9+
10+
class ServiceLoaderComponentLoader implements ComponentLoader {
11+
12+
private final ClassLoader classLoader;
13+
14+
ServiceLoaderComponentLoader(ClassLoader classLoader) {
15+
this.classLoader = classLoader;
16+
}
17+
18+
@Override
19+
public <T> Iterable<T> load(Class<T> spiClass) {
20+
return ServiceLoader.load(spiClass, classLoader);
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "ServiceLoaderComponentLoader{classLoader=" + classLoader + "}";
26+
}
27+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
Comparing source compatibility of opentelemetry-context-1.52.0-SNAPSHOT.jar against opentelemetry-context-1.51.0.jar
2-
No changes.
2+
+++ NEW INTERFACE: PUBLIC(+) ABSTRACT(+) io.opentelemetry.context.ComponentLoader (not serializable)
3+
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
4+
+++ NEW SUPERCLASS: java.lang.Object
5+
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.context.ComponentLoader forClassLoader(java.lang.ClassLoader)
6+
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.Iterable<T> load(java.lang.Class<T>)
7+
GENERIC TEMPLATES: +++ T:java.lang.Object
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
Comparing source compatibility of opentelemetry-exporter-otlp-1.52.0-SNAPSHOT.jar against opentelemetry-exporter-otlp-1.51.0.jar
2-
No changes.
2+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder setComponentLoader(io.opentelemetry.context.ComponentLoader)
5+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder (not serializable)
6+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
7+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder setComponentLoader(io.opentelemetry.context.ComponentLoader)
8+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder (not serializable)
9+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
10+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder setComponentLoader(io.opentelemetry.context.ComponentLoader)
11+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder (not serializable)
12+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
13+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder setComponentLoader(io.opentelemetry.context.ComponentLoader)
14+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder (not serializable)
15+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
16+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder setComponentLoader(io.opentelemetry.context.ComponentLoader)
17+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder (not serializable)
18+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
19+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder setComponentLoader(io.opentelemetry.context.ComponentLoader)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Comparing source compatibility of opentelemetry-sdk-extension-autoconfigure-spi-1.52.0-SNAPSHOT.jar against opentelemetry-sdk-extension-autoconfigure-spi-1.51.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.context.ComponentLoader getComponentLoader()
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Comparing source compatibility of opentelemetry-sdk-extension-autoconfigure-1.52.0-SNAPSHOT.jar against opentelemetry-sdk-extension-autoconfigure-1.51.0.jar
2-
No changes.
2+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder setComponentLoader(io.opentelemetry.context.ComponentLoader)

exporters/common/src/main/java/io/opentelemetry/exporter/internal/compression/CompressorUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import static io.opentelemetry.api.internal.Utils.checkArgument;
99
import static java.util.stream.Collectors.joining;
1010

11+
import io.opentelemetry.context.ComponentLoader;
1112
import java.util.HashMap;
1213
import java.util.Map;
13-
import java.util.ServiceLoader;
1414
import java.util.Set;
1515
import javax.annotation.Nullable;
1616

@@ -48,7 +48,8 @@ public static Compressor validateAndResolveCompressor(String compressionMethod)
4848
private static Map<String, Compressor> buildCompressorRegistry() {
4949
Map<String, Compressor> compressors = new HashMap<>();
5050
for (CompressorProvider spi :
51-
ServiceLoader.load(CompressorProvider.class, CompressorUtil.class.getClassLoader())) {
51+
ComponentLoader.forClassLoader(CompressorUtil.class.getClassLoader())
52+
.load(CompressorProvider.class)) {
5253
Compressor compressor = spi.getInstance();
5354
compressors.put(compressor.getEncoding(), compressor);
5455
}

0 commit comments

Comments
 (0)