Skip to content

Commit 8e06ff8

Browse files
committed
format
1 parent 68c4e0b commit 8e06ff8

File tree

8 files changed

+215
-204
lines changed

8 files changed

+215
-204
lines changed

declarative-config-bridge/src/main/java/io/opentelemetry/contrib/sdk/autoconfigure/ConfigPropertiesUtil.java

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,43 @@
1414
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
1515

1616
public class ConfigPropertiesUtil {
17-
private ConfigPropertiesUtil() {
17+
private ConfigPropertiesUtil() {}
18+
19+
/** Resolve {@link ConfigProperties} from the {@code autoConfiguredOpenTelemetrySdk}. */
20+
public static ConfigProperties resolveConfigProperties(
21+
AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
22+
ConfigProperties sdkConfigProperties =
23+
AutoConfigureUtil.getConfig(autoConfiguredOpenTelemetrySdk);
24+
if (sdkConfigProperties != null) {
25+
return sdkConfigProperties;
1826
}
27+
ConfigProvider configProvider =
28+
AutoConfigureUtil.getConfigProvider(autoConfiguredOpenTelemetrySdk);
29+
if (configProvider != null) {
30+
DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig();
1931

20-
/**
21-
* Resolve {@link ConfigProperties} from the {@code autoConfiguredOpenTelemetrySdk}.
22-
*/
23-
public static ConfigProperties resolveConfigProperties(
24-
AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
25-
ConfigProperties sdkConfigProperties =
26-
AutoConfigureUtil.getConfig(autoConfiguredOpenTelemetrySdk);
27-
if (sdkConfigProperties != null) {
28-
return sdkConfigProperties;
29-
}
30-
ConfigProvider configProvider =
31-
AutoConfigureUtil.getConfigProvider(autoConfiguredOpenTelemetrySdk);
32-
if (configProvider != null) {
33-
DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig();
34-
35-
if (instrumentationConfig == null) {
36-
instrumentationConfig = DeclarativeConfigProperties.empty();
37-
}
38-
39-
return new DeclarativeConfigPropertiesBridge(instrumentationConfig);
40-
}
41-
// Should never happen
42-
throw new IllegalStateException(
43-
"AutoConfiguredOpenTelemetrySdk does not have ConfigProperties or DeclarativeConfigProperties. This is likely a programming error in opentelemetry-java");
44-
}
45-
46-
public static ConfigProperties resolveModel(OpenTelemetryConfigurationModel model) {
47-
SdkConfigProvider configProvider = SdkConfigProvider.create(model);
48-
DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig();
49-
if (instrumentationConfig == null) {
50-
instrumentationConfig = DeclarativeConfigProperties.empty();
51-
}
32+
if (instrumentationConfig == null) {
33+
instrumentationConfig = DeclarativeConfigProperties.empty();
34+
}
5235

53-
return new DeclarativeConfigPropertiesBridge(instrumentationConfig);
36+
return new DeclarativeConfigPropertiesBridge(instrumentationConfig);
5437
}
55-
56-
public static String propertyYamlPath(String propertyName) {
57-
return DeclarativeConfigPropertiesBridge.yamlPath(propertyName);
38+
// Should never happen
39+
throw new IllegalStateException(
40+
"AutoConfiguredOpenTelemetrySdk does not have ConfigProperties or DeclarativeConfigProperties. This is likely a programming error in opentelemetry-java");
41+
}
42+
43+
public static ConfigProperties resolveModel(OpenTelemetryConfigurationModel model) {
44+
SdkConfigProvider configProvider = SdkConfigProvider.create(model);
45+
DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig();
46+
if (instrumentationConfig == null) {
47+
instrumentationConfig = DeclarativeConfigProperties.empty();
5848
}
49+
50+
return new DeclarativeConfigPropertiesBridge(instrumentationConfig);
51+
}
52+
53+
public static String propertyYamlPath(String propertyName) {
54+
return DeclarativeConfigPropertiesBridge.yamlPath(propertyName);
55+
}
5956
}

declarative-config-bridge/src/main/java/io/opentelemetry/contrib/sdk/autoconfigure/DeclarativeConfigPropertiesBridge.java

Lines changed: 110 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55

66
package io.opentelemetry.contrib.sdk.autoconfigure;
77

8+
import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty;
9+
810
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
911
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
10-
11-
import javax.annotation.Nullable;
1212
import java.time.Duration;
1313
import java.util.Collections;
1414
import java.util.HashMap;
1515
import java.util.List;
1616
import java.util.Map;
1717
import java.util.function.BiFunction;
18-
19-
import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty;
18+
import javax.annotation.Nullable;
2019

2120
/**
2221
* A {@link ConfigProperties} which resolves properties based on {@link
@@ -49,123 +48,123 @@
4948
*/
5049
final class DeclarativeConfigPropertiesBridge implements ConfigProperties {
5150

52-
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation.";
53-
54-
// The node at .instrumentation.java
55-
private final DeclarativeConfigProperties instrumentationJavaNode;
56-
57-
DeclarativeConfigPropertiesBridge(DeclarativeConfigProperties instrumentationNode) {
58-
instrumentationJavaNode = instrumentationNode.getStructured("java", empty());
51+
private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation.";
52+
53+
// The node at .instrumentation.java
54+
private final DeclarativeConfigProperties instrumentationJavaNode;
55+
56+
DeclarativeConfigPropertiesBridge(DeclarativeConfigProperties instrumentationNode) {
57+
instrumentationJavaNode = instrumentationNode.getStructured("java", empty());
58+
}
59+
60+
@Nullable
61+
@Override
62+
public String getString(String propertyName) {
63+
return getPropertyValue(propertyName, DeclarativeConfigProperties::getString);
64+
}
65+
66+
@Nullable
67+
@Override
68+
public Boolean getBoolean(String propertyName) {
69+
return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean);
70+
}
71+
72+
@Nullable
73+
@Override
74+
public Integer getInt(String propertyName) {
75+
return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt);
76+
}
77+
78+
@Nullable
79+
@Override
80+
public Long getLong(String propertyName) {
81+
return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
82+
}
83+
84+
@Nullable
85+
@Override
86+
public Double getDouble(String propertyName) {
87+
return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble);
88+
}
89+
90+
@Nullable
91+
@Override
92+
public Duration getDuration(String propertyName) {
93+
Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
94+
if (millis == null) {
95+
return null;
5996
}
60-
61-
@Nullable
62-
@Override
63-
public String getString(String propertyName) {
64-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getString);
97+
return Duration.ofMillis(millis);
98+
}
99+
100+
@Override
101+
public List<String> getList(String propertyName) {
102+
List<String> propertyValue =
103+
getPropertyValue(
104+
propertyName,
105+
(properties, lastPart) -> properties.getScalarList(lastPart, String.class));
106+
return propertyValue == null ? Collections.emptyList() : propertyValue;
107+
}
108+
109+
@Override
110+
public Map<String, String> getMap(String propertyName) {
111+
DeclarativeConfigProperties propertyValue =
112+
getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured);
113+
if (propertyValue == null) {
114+
return Collections.emptyMap();
65115
}
66-
67-
@Nullable
68-
@Override
69-
public Boolean getBoolean(String propertyName) {
70-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean);
116+
Map<String, String> result = new HashMap<>();
117+
propertyValue
118+
.getPropertyKeys()
119+
.forEach(
120+
key -> {
121+
String value = propertyValue.getString(key);
122+
if (value == null) {
123+
return;
124+
}
125+
result.put(key, value);
126+
});
127+
return Collections.unmodifiableMap(result);
128+
}
129+
130+
@Nullable
131+
private <T> T getPropertyValue(
132+
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
133+
if (instrumentationJavaNode == null) {
134+
return null;
71135
}
72136

73-
@Nullable
74-
@Override
75-
public Integer getInt(String propertyName) {
76-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt);
137+
String[] segments = getSegments(property);
138+
if (segments.length == 0) {
139+
return null;
77140
}
78141

79-
@Nullable
80-
@Override
81-
public Long getLong(String propertyName) {
82-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
142+
// Extract the value by walking to the N-1 entry
143+
DeclarativeConfigProperties target = instrumentationJavaNode;
144+
if (segments.length > 1) {
145+
for (int i = 0; i < segments.length - 1; i++) {
146+
target = target.getStructured(segments[i], empty());
147+
}
83148
}
149+
String lastPart = segments[segments.length - 1];
84150

85-
@Nullable
86-
@Override
87-
public Double getDouble(String propertyName) {
88-
return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble);
89-
}
90-
91-
@Nullable
92-
@Override
93-
public Duration getDuration(String propertyName) {
94-
Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong);
95-
if (millis == null) {
96-
return null;
97-
}
98-
return Duration.ofMillis(millis);
99-
}
100-
101-
@Override
102-
public List<String> getList(String propertyName) {
103-
List<String> propertyValue =
104-
getPropertyValue(
105-
propertyName,
106-
(properties, lastPart) -> properties.getScalarList(lastPart, String.class));
107-
return propertyValue == null ? Collections.emptyList() : propertyValue;
108-
}
151+
return extractor.apply(target, lastPart);
152+
}
109153

110-
@Override
111-
public Map<String, String> getMap(String propertyName) {
112-
DeclarativeConfigProperties propertyValue =
113-
getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured);
114-
if (propertyValue == null) {
115-
return Collections.emptyMap();
116-
}
117-
Map<String, String> result = new HashMap<>();
118-
propertyValue
119-
.getPropertyKeys()
120-
.forEach(
121-
key -> {
122-
String value = propertyValue.getString(key);
123-
if (value == null) {
124-
return;
125-
}
126-
result.put(key, value);
127-
});
128-
return Collections.unmodifiableMap(result);
154+
private static String[] getSegments(String property) {
155+
if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
156+
property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length());
129157
}
130-
131-
@Nullable
132-
private <T> T getPropertyValue(
133-
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
134-
if (instrumentationJavaNode == null) {
135-
return null;
136-
}
137-
138-
String[] segments = getSegments(property);
139-
if (segments.length == 0) {
140-
return null;
141-
}
142-
143-
// Extract the value by walking to the N-1 entry
144-
DeclarativeConfigProperties target = instrumentationJavaNode;
145-
if (segments.length > 1) {
146-
for (int i = 0; i < segments.length - 1; i++) {
147-
target = target.getStructured(segments[i], empty());
148-
}
149-
}
150-
String lastPart = segments[segments.length - 1];
151-
152-
return extractor.apply(target, lastPart);
153-
}
154-
155-
private static String[] getSegments(String property) {
156-
if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
157-
property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length());
158-
}
159-
// Split the remainder of the property on "."
160-
return property.split("\\.");
158+
// Split the remainder of the property on "."
159+
return property.split("\\.");
160+
}
161+
162+
static String yamlPath(String property) {
163+
String[] segments = getSegments(property);
164+
if (segments.length == 0) {
165+
throw new IllegalArgumentException("Invalid property: " + property);
161166
}
162167

163-
static String yamlPath(String property) {
164-
String[] segments = getSegments(property);
165-
if (segments.length == 0) {
166-
throw new IllegalArgumentException("Invalid property: " + property);
167-
}
168-
169-
return "'instrumentation/development' / 'java' / '" + String.join("' / '", segments) + "'";
170-
}
168+
return "'instrumentation/development' / 'java' / '" + String.join("' / '", segments) + "'";
169+
}
171170
}

declarative-config-bridge/src/test/java/io/opentelemetry/contrib/sdk/autoconfigure/ConfigPropertiesUtilTest.java

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

66
package io.opentelemetry.contrib.sdk.autoconfigure;
77

8-
98
import static org.assertj.core.api.Assertions.assertThat;
109
import static org.mockito.ArgumentMatchers.any;
1110
import static org.mockito.ArgumentMatchers.eq;
@@ -89,6 +88,7 @@ void shouldUseConfigProviderForDeclarativeConfiguration_noInstrumentationConfig(
8988
@Test
9089
void propertyYamlPath() {
9190
assertThat(ConfigPropertiesUtil.propertyYamlPath("google.otel.auth.target.signals"))
92-
.isEqualTo("'instrumentation/development' / 'java' / 'google' / 'otel' / 'auth' / 'target' / 'signals'");
91+
.isEqualTo(
92+
"'instrumentation/development' / 'java' / 'google' / 'otel' / 'auth' / 'target' / 'signals'");
9393
}
9494
}

declarative-config-bridge/src/test/java/io/opentelemetry/contrib/sdk/autoconfigure/DeclarativeConfigPropertiesBridgeTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
package io.opentelemetry.contrib.sdk.autoconfigure;/*
1+
/*
22
* Copyright The OpenTelemetry Authors
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
package io.opentelemetry.contrib.sdk.autoconfigure; /*
7+
* Copyright The OpenTelemetry Authors
8+
* SPDX-License-Identifier: Apache-2.0
9+
*/
10+
611
import static org.assertj.core.api.Assertions.assertThat;
712

813
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;

gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ String getUserReadableName() {
100100
* @throws ConfigurationException if neither the environment variable nor the system property is
101101
* set.
102102
*/
103-
<T> T getConfiguredValue(ConfigProperties configProperties, BiFunction<ConfigProperties, String, T> extractor) {
103+
<T> T getConfiguredValue(
104+
ConfigProperties configProperties, BiFunction<ConfigProperties, String, T> extractor) {
104105
T configuredValue = extractor.apply(configProperties, this.getSystemProperty());
105106
if (configuredValue instanceof String) {
106107
String value = (String) configuredValue;
@@ -130,7 +131,9 @@ <T> T getConfiguredValue(ConfigProperties configProperties, BiFunction<ConfigPro
130131
* property, or the fallback function, in that order of precedence.
131132
*/
132133
<T> T getConfiguredValueWithFallback(
133-
ConfigProperties configProperties, Supplier<T> fallback, BiFunction<ConfigProperties, String, T> extractor) {
134+
ConfigProperties configProperties,
135+
Supplier<T> fallback,
136+
BiFunction<ConfigProperties, String, T> extractor) {
134137
try {
135138
return this.getConfiguredValue(configProperties, extractor);
136139
} catch (ConfigurationException e) {

0 commit comments

Comments
 (0)