-
Notifications
You must be signed in to change notification settings - Fork 940
Fix/declarative config no exceptions #8079
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,47 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.api.incubator.propagation; | ||
|
|
||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
| import java.util.Collections; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A {@link TextMapGetter} that extracts context from a map carrier, intended for use with | ||
| * environment variables. | ||
| * | ||
| * <p>Standard environment variable names are uppercase (e.g., {@code TRACEPARENT}, {@code | ||
| * TRACESTATE}, {@code BAGGAGE}). This getter translates keys to uppercase before looking them up in | ||
| * the carrier. | ||
| */ | ||
| public enum EnvironmentGetter implements TextMapGetter<Map<String, String>> { | ||
| INSTANCE; | ||
|
|
||
| @Override | ||
| public Iterable<String> keys(Map<String, String> carrier) { | ||
| if (carrier == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
| return carrier.keySet(); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String get(@Nullable Map<String, String> carrier, String key) { | ||
| if (carrier == null || key == null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should key be validated against |
||
| return null; | ||
| } | ||
| // Spec recommends using uppercase for environment variable names. | ||
| return carrier.get(key.toUpperCase(Locale.ROOT)); | ||
| } | ||
|
|
||
| @Override | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be safer in case of refactoring: return EnvironmentGetter.class.getName() |
||
| public String toString() { | ||
| return "EnvironmentGetter"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.api.incubator.propagation; | ||
|
|
||
| import io.opentelemetry.context.propagation.TextMapSetter; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A {@link TextMapSetter} that injects context into a map carrier, intended for use with | ||
| * environment variables. | ||
| * | ||
| * <p>Standard environment variable names are uppercase (e.g., {@code TRACEPARENT}, {@code | ||
| * TRACESTATE}, {@code BAGGAGE}). This setter translates keys to uppercase before inserting them | ||
| * into the carrier. | ||
| */ | ||
| public enum EnvironmentSetter implements TextMapSetter<Map<String, String>> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have similar comments as for |
||
| INSTANCE; | ||
|
|
||
| @Override | ||
| public void set(@Nullable Map<String, String> carrier, String key, String value) { | ||
| if (carrier == null || key == null || value == null) { | ||
| return; | ||
| } | ||
| // Spec recommends using uppercase for environment variable names. | ||
| carrier.put(key.toUpperCase(Locale.ROOT), value); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "EnvironmentSetter"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.api.incubator.propagation; | ||
|
|
||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class EnvironmentGetterTest { | ||
|
|
||
| @Test | ||
| void get() { | ||
| Map<String, String> carrier = new HashMap<>(); | ||
| carrier.put("TRACEPARENT", "val1"); | ||
| carrier.put("TRACESTATE", "val2"); | ||
| carrier.put("BAGGAGE", "val3"); | ||
| carrier.put("OTHER", "val4"); | ||
|
|
||
| assertThat(EnvironmentGetter.INSTANCE.get(carrier, "traceparent")).isEqualTo("val1"); | ||
| assertThat(EnvironmentGetter.INSTANCE.get(carrier, "TRACESTATE")).isEqualTo("val2"); | ||
| assertThat(EnvironmentGetter.INSTANCE.get(carrier, "Baggage")).isEqualTo("val3"); | ||
| assertThat(EnvironmentGetter.INSTANCE.get(carrier, "other")).isEqualTo("val4"); | ||
| } | ||
|
|
||
| @Test | ||
| void get_null() { | ||
| assertThat(EnvironmentGetter.INSTANCE.get(null, "key")).isNull(); | ||
| assertThat(EnvironmentGetter.INSTANCE.get(Collections.emptyMap(), null)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void keys() { | ||
| Map<String, String> carrier = new HashMap<>(); | ||
| carrier.put("K1", "V1"); | ||
| carrier.put("K2", "V2"); | ||
|
|
||
| assertThat(EnvironmentGetter.INSTANCE.keys(carrier)).containsExactlyInAnyOrder("K1", "K2"); | ||
| assertThat(EnvironmentGetter.INSTANCE.keys(null)).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void testToString() { | ||
| assertThat(EnvironmentGetter.INSTANCE.toString()).isEqualTo("EnvironmentGetter"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.api.incubator.propagation; | ||
|
|
||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class EnvironmentSetterTest { | ||
|
|
||
| @Test | ||
| void set() { | ||
| Map<String, String> carrier = new HashMap<>(); | ||
| EnvironmentSetter.INSTANCE.set(carrier, "traceparent", "val1"); | ||
| EnvironmentSetter.INSTANCE.set(carrier, "TRACESTATE", "val2"); | ||
| EnvironmentSetter.INSTANCE.set(carrier, "Baggage", "val3"); | ||
|
|
||
| assertThat(carrier).containsEntry("TRACEPARENT", "val1"); | ||
| assertThat(carrier).containsEntry("TRACESTATE", "val2"); | ||
| assertThat(carrier).containsEntry("BAGGAGE", "val3"); | ||
| } | ||
|
|
||
| @Test | ||
| void set_null() { | ||
| Map<String, String> carrier = new HashMap<>(); | ||
| EnvironmentSetter.INSTANCE.set(null, "key", "val"); | ||
| EnvironmentSetter.INSTANCE.set(carrier, null, "val"); | ||
| EnvironmentSetter.INSTANCE.set(carrier, "key", null); | ||
| assertThat(carrier).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void testToString() { | ||
| assertThat(EnvironmentSetter.INSTANCE.toString()).isEqualTo("EnvironmentSetter"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,8 +230,11 @@ void wrongType() { | |
| assertThat(otherProps.getDouble("str_key")).isNull(); | ||
| assertThat(otherProps.getBoolean("str_key")).isNull(); | ||
| assertThat(otherProps.getScalarList("str_key", String.class)).isNull(); | ||
| assertThat(otherProps.getScalarList("str_list_key", Long.class)).isNull(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be good to add also a cases for Boolean and Double list element types? |
||
| assertThat(otherProps.getStructured("str_key")).isNull(); | ||
| assertThat(otherProps.getStructuredList("str_key")).isNull(); | ||
| assertThat(otherProps.getStructured("str_list_key")).isNull(); | ||
| assertThat(otherProps.getStructuredList("map_key")).isNull(); | ||
|
|
||
| assertWarning("Ignoring value for key [int_key] because it is Integer instead of String: 1"); | ||
| assertWarning( | ||
|
|
@@ -240,6 +243,8 @@ void wrongType() { | |
| "Ignoring value for key [str_key] because it is String instead of Double: str_value"); | ||
| assertWarning( | ||
| "Ignoring value for key [str_key] because it is String instead of Boolean: str_value"); | ||
| assertWarning( | ||
| "Ignoring value for key [str_list_key] because it is String instead of Long: val1"); | ||
| } | ||
|
|
||
| private void assertWarning(String message) { | ||
|
|
||
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.
Is this class intentionally in this PR? I don't see it is used anywhere besides the test