-
Notifications
You must be signed in to change notification settings - Fork 881
Add DeclarativeConfigContext #7293
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
||
import io.opentelemetry.api.incubator.config.DeclarativeConfigException; | ||
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; | ||
import java.io.Closeable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
/** Declarative configuration context and state carrier. */ | ||
class DeclarativeConfigContext { | ||
|
||
private final SpiHelper spiHelper; | ||
private final List<Closeable> closeables = new ArrayList<>(); | ||
|
||
DeclarativeConfigContext(SpiHelper spiHelper) { | ||
this.spiHelper = spiHelper; | ||
} | ||
|
||
/** | ||
* Add the {@code closeable} to the list of closeables to clean up if configuration fails | ||
* exceptionally, and return it. | ||
*/ | ||
<T extends Closeable> T addCloseable(T closeable) { | ||
closeables.add(closeable); | ||
return closeable; | ||
} | ||
|
||
List<Closeable> getCloseables() { | ||
return Collections.unmodifiableList(closeables); | ||
} | ||
|
||
/** | ||
* Find a registered {@link ComponentProvider} which {@link ComponentProvider#getType()} matching | ||
* {@code type}, {@link ComponentProvider#getName()} matching {@code name}, and call {@link | ||
* ComponentProvider#create(DeclarativeConfigProperties)} with the given {@code model}. | ||
* | ||
* @throws DeclarativeConfigException if no matching providers are found, or if multiple are found | ||
* (i.e. conflict), or if {@link ComponentProvider#create(DeclarativeConfigProperties)} throws | ||
*/ | ||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
<T> T loadComponent(Class<T> type, String name, Object model) { | ||
DeclarativeConfigProperties config = | ||
DeclarativeConfiguration.toConfigProperties(model, spiHelper.getComponentLoader()); | ||
|
||
// TODO(jack-berg): cache loaded component providers | ||
List<ComponentProvider> componentProviders = spiHelper.load(ComponentProvider.class); | ||
List<ComponentProvider<?>> matchedProviders = | ||
componentProviders.stream() | ||
.map( | ||
(Function<ComponentProvider, ComponentProvider<?>>) | ||
componentProvider -> componentProvider) | ||
.filter( | ||
componentProvider -> | ||
componentProvider.getType() == type && name.equals(componentProvider.getName())) | ||
.collect(Collectors.toList()); | ||
if (matchedProviders.isEmpty()) { | ||
throw new DeclarativeConfigException( | ||
"No component provider detected for " + type.getName() + " with name \"" + name + "\"."); | ||
} | ||
if (matchedProviders.size() > 1) { | ||
throw new DeclarativeConfigException( | ||
Check warning on line 71 in sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/DeclarativeConfigContext.java
|
||
"Component provider conflict. Multiple providers detected for " | ||
+ type.getName() | ||
Check warning on line 73 in sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/DeclarativeConfigContext.java
|
||
+ " with name \"" | ||
+ name | ||
+ "\": " | ||
+ componentProviders.stream() | ||
.map(provider -> provider.getClass().getName()) | ||
.collect(Collectors.joining(",", "[", "]"))); | ||
Check warning on line 79 in sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/DeclarativeConfigContext.java
|
||
} | ||
// Exactly one matching component provider | ||
ComponentProvider<T> provider = (ComponentProvider<T>) matchedProviders.get(0); | ||
|
||
try { | ||
return provider.create(config); | ||
} catch (Throwable throwable) { | ||
throw new DeclarativeConfigException( | ||
"Error configuring " + type.getName() + " with name \"" + name + "\"", throwable); | ||
Check warning on line 88 in sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/DeclarativeConfigContext.java
|
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.