Skip to content

Commit

Permalink
Declarative configuration missing pieces (#6677)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Sep 5, 2024
1 parent 61a4b46 commit 00b0e9f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@

package io.opentelemetry.sdk.autoconfigure.spi.internal;

import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;

/**
* Provides configured instances of SDK extension components. {@link ComponentProvider} allows SDK
* extension components which are not part of the core SDK to be referenced in file based
* configuration.
*
* <p>NOTE: when {@link #getType()} is {@link Resource}, the {@link #getName()} is not (currently)
* used, and {@link #create(StructuredConfigProperties)} is (currently) called with an empty {@link
* StructuredConfigProperties}.
*
* @param <T> the type of the SDK extension component. See {@link #getType()}. Supported values
* include: {@link SpanExporter}, {@link MetricExporter}, {@link LogRecordExporter}.
* include: {@link SpanExporter}, {@link MetricExporter}, {@link LogRecordExporter}, {@link
* SpanProcessor}, {@link LogRecordProcessor}, {@link TextMapPropagator}, {@link Sampler},
* {@link Resource}.
*/
// TODO: add support for Sampler, LogRecordProcessor, SpanProcessor, MetricReader
public interface ComponentProvider<T> {

/**
Expand All @@ -31,7 +41,8 @@ public interface ComponentProvider<T> {
* instances of a custom span exporter for the "acme" protocol, the name might be "acme".
*
* <p>This name MUST not be the same as any other component provider name which returns components
* of the same {@link #getType() type}.
* of the same {@link #getType() type}. In other words, {@link #getType()} and name form a
* composite key uniquely identifying the provider.
*/
String getName();

Expand Down
1 change: 1 addition & 0 deletions sdk-extensions/incubator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {

// io.opentelemetry.sdk.extension.incubator.fileconfig
implementation("com.fasterxml.jackson.core:jackson-databind")
api("com.fasterxml.jackson.core:jackson-annotations")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
implementation(project(":sdk-extensions:autoconfigure"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ static Object loadYaml(InputStream inputStream, Map<String, String> environmentV
}

/**
* Convert the {@code model} to a generic {@link StructuredConfigProperties}, which can be used to
* read configuration not part of the model.
* Convert the {@code model} to a generic {@link StructuredConfigProperties}.
*
* @param model the configuration model
* @return a generic {@link StructuredConfigProperties} representation of the model
Expand All @@ -133,6 +132,17 @@ public static StructuredConfigProperties toConfigProperties(OpenTelemetryConfigu
return toConfigProperties((Object) model);
}

/**
* Convert the {@code configuration} YAML to a generic {@link StructuredConfigProperties}.
*
* @param configuration configuration YAML
* @return a generic {@link StructuredConfigProperties} representation of the model
*/
public static StructuredConfigProperties toConfigProperties(InputStream configuration) {
Object yamlObj = loadYaml(configuration, System.getenv());
return toConfigProperties(yamlObj);
}

static StructuredConfigProperties toConfigProperties(Object model) {
Map<String, Object> configurationMap =
MAPPER.convertValue(model, new TypeReference<Map<String, Object>>() {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static YamlStructuredConfigProperties create(Map<String, Object> properties) {
for (Map.Entry<String, Object> entry : properties.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (isPrimitive(value)) {
if (isPrimitive(value) || value == null) {
simpleEntries.put(key, value);
continue;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ public String toString() {
}

/** Return a map representation of the data. */
Map<String, Object> toMap() {
public Map<String, Object> toMap() {
Map<String, Object> result = new HashMap<>(simpleEntries);
listEntries.forEach(
(key, value) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class YamlStructuredConfigPropertiesTest {
+ " int_key: 1\n"
+ " float_key: 1.1\n"
+ " bool_key: true\n"
+ " null_key:\n"
+ " str_list_key: [val1, val2]\n"
+ " int_list_key: [1, 2]\n"
+ " float_list_key: [1.1, 2.2]\n"
Expand Down Expand Up @@ -90,6 +91,7 @@ void additionalProperties() {
"int_key",
"float_key",
"bool_key",
"null_key",
"str_list_key",
"int_list_key",
"float_list_key",
Expand All @@ -101,7 +103,10 @@ void additionalProperties() {
assertThat(otherProps.getInt("int_key")).isEqualTo(1);
assertThat(otherProps.getLong("int_key")).isEqualTo(1);
assertThat(otherProps.getDouble("float_key")).isEqualTo(1.1);
assertThat(otherProps.getBoolean("bool_key")).isTrue();
assertThat(otherProps.getString("null_key")).isNull();
assertThat(otherProps.getInt("null_key")).isNull();
assertThat(otherProps.getLong("null_key")).isNull();
assertThat(otherProps.getBoolean("null_key")).isNull();
assertThat(otherProps.getScalarList("str_list_key", String.class))
.isEqualTo(Arrays.asList("val1", "val2"));
assertThat(otherProps.getScalarList("int_list_key", Long.class))
Expand Down

0 comments on commit 00b0e9f

Please sign in to comment.