Skip to content
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

Declarative configuration missing pieces #6677

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, open-telemetry/opentelemetry-java-contrib#1440 is full of warnings like:

/Users/jberg/.m2/repository/io/opentelemetry/opentelemetry-sdk-extension-incubator/1.42.0-alpha-SNAPSHOT/opentelemetry-sdk-extension-incubator-1.42.0-alpha-SNAPSHOT.jar(/io/opentelemetry/sdk/extension/incubator/fileconfig/internal/model/OpenTelemetryConfiguration.class): warning: Cannot find annotation method 'value()' in type 'JsonProperty'

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 @@
}

/**
* 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 @@
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);

Check warning on line 143 in sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/FileConfiguration.java

View check run for this annotation

Codecov / codecov/patch

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/FileConfiguration.java#L142-L143

Added lines #L142 - L143 were not covered by tests
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an essential test utility, allowing you to write chunks of YAML, convert them to StructuredConfigProperties, and call ComponentProvider, i.e. https://github.com/open-telemetry/opentelemetry-java-contrib/pull/1440/files#diff-3d0118f231281243bf34a7e7beb3809e7a6523e8ece8a17b83f1784953389ceb


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
Loading