Skip to content

Commit 8e03ae6

Browse files
committed
Code review changes.
UT added
1 parent 506425b commit 8e03ae6

File tree

3 files changed

+103
-38
lines changed

3 files changed

+103
-38
lines changed

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/extension/ConfigPropertiesUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
1212
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1313

14-
// TODO: Add unit test
1514
public class ConfigPropertiesUtil {
1615
private ConfigPropertiesUtil() {}
1716

@@ -28,9 +27,11 @@ public static ConfigProperties resolveConfigProperties(
2827
if (configProvider != null) {
2928
DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig();
3029

31-
if (instrumentationConfig != null) {
32-
return new DeclarativeConfigPropertiesBridge(instrumentationConfig);
30+
if (instrumentationConfig == null) {
31+
instrumentationConfig = DeclarativeConfigProperties.empty();
3332
}
33+
34+
return new DeclarativeConfigPropertiesBridge(instrumentationConfig);
3435
}
3536
// Should never happen
3637
throw new IllegalStateException(

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/extension/DeclarativeConfigPropertiesBridge.java

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -130,51 +130,28 @@ public Map<String, String> getMap(String propertyName) {
130130
@Nullable
131131
private <T> T getPropertyValue(
132132
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
133-
T value = getOtelPropertyValue(property, extractor);
134-
if (value == null) {
135-
value = getVendorPropertyValue(property, extractor);
133+
if (instrumentationJavaNode == null) {
134+
return null;
136135
}
137-
return value;
138-
}
139136

140-
@Nullable
141-
private <T> T getPropertyValue(
142-
String[] segments, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
137+
if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
138+
property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length());
139+
}
140+
// Split the remainder of the property on "."
141+
String[] segments = property.split("\\.");
142+
if (segments.length == 0) {
143+
return null;
144+
}
143145

146+
// Extract the value by walking to the N-1 entry
144147
DeclarativeConfigProperties target = instrumentationJavaNode;
145148
if (segments.length > 1) {
146149
for (int i = 0; i < segments.length - 1; i++) {
147150
target = target.getStructured(segments[i], empty());
148151
}
149152
}
150153
String lastPart = segments[segments.length - 1];
151-
return extractor.apply(target, lastPart);
152-
}
153154

154-
@Nullable
155-
private <T> T getOtelPropertyValue(
156-
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
157-
if (!property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
158-
return null;
159-
}
160-
String suffix = property.substring(OTEL_INSTRUMENTATION_PREFIX.length());
161-
// Split the remainder of the property on ".", and walk to the N-1 entry
162-
String[] segments = suffix.split("\\.");
163-
if (segments.length == 0) {
164-
return null;
165-
}
166-
167-
return getPropertyValue(segments, extractor);
168-
}
169-
170-
@Nullable
171-
private <T> T getVendorPropertyValue(
172-
String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) {
173-
String[] segments = property.split("\\.");
174-
if (segments.length == 0) {
175-
return null;
176-
}
177-
178-
return getPropertyValue(segments, extractor);
155+
return extractor.apply(target, lastPart);
179156
}
180157
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.extension;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.mockito.ArgumentMatchers.any;
10+
import static org.mockito.ArgumentMatchers.eq;
11+
import static org.mockito.Mockito.mock;
12+
import static org.mockito.Mockito.when;
13+
14+
import io.opentelemetry.api.incubator.config.ConfigProvider;
15+
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
16+
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
17+
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
18+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
19+
import org.junit.jupiter.api.Test;
20+
import org.mockito.MockedStatic;
21+
import org.mockito.Mockito;
22+
23+
@SuppressWarnings("DoNotMockAutoValue")
24+
class ConfigPropertiesUtilTest {
25+
@Test
26+
void shouldUseConfigPropertiesForAutoConfiguration() {
27+
ConfigProperties configPropertiesMock = mock(ConfigProperties.class);
28+
AutoConfiguredOpenTelemetrySdk sdkMock = mock(AutoConfiguredOpenTelemetrySdk.class);
29+
try (MockedStatic<AutoConfigureUtil> autoConfigureUtilMock =
30+
Mockito.mockStatic(AutoConfigureUtil.class)) {
31+
autoConfigureUtilMock
32+
.when(() -> AutoConfigureUtil.getConfig(sdkMock))
33+
.thenReturn(configPropertiesMock);
34+
35+
ConfigProperties configProperties = ConfigPropertiesUtil.resolveConfigProperties(sdkMock);
36+
37+
assertThat(configProperties).isSameAs(configPropertiesMock);
38+
}
39+
}
40+
41+
@Test
42+
void shouldUseConfigProviderForDeclarativeConfiguration() {
43+
String propertyName = "testProperty";
44+
String expectedValue = "the value";
45+
DeclarativeConfigProperties javaNodeMock = mock(DeclarativeConfigProperties.class);
46+
when(javaNodeMock.getString(propertyName)).thenReturn(expectedValue);
47+
48+
DeclarativeConfigProperties instrumentationConfigMock = mock(DeclarativeConfigProperties.class);
49+
when(instrumentationConfigMock.getStructured(eq("java"), any())).thenReturn(javaNodeMock);
50+
51+
ConfigProvider configProviderMock = mock(ConfigProvider.class);
52+
when(configProviderMock.getInstrumentationConfig()).thenReturn(instrumentationConfigMock);
53+
54+
AutoConfiguredOpenTelemetrySdk sdkMock = mock(AutoConfiguredOpenTelemetrySdk.class);
55+
56+
try (MockedStatic<AutoConfigureUtil> autoConfigureUtilMock =
57+
Mockito.mockStatic(AutoConfigureUtil.class)) {
58+
autoConfigureUtilMock.when(() -> AutoConfigureUtil.getConfig(sdkMock)).thenReturn(null);
59+
autoConfigureUtilMock
60+
.when(() -> AutoConfigureUtil.getConfigProvider(sdkMock))
61+
.thenReturn(configProviderMock);
62+
63+
ConfigProperties configProperties = ConfigPropertiesUtil.resolveConfigProperties(sdkMock);
64+
65+
assertThat(configProperties.getString(propertyName)).isEqualTo(expectedValue);
66+
}
67+
}
68+
69+
@Test
70+
void shouldUseConfigProviderForDeclarativeConfiguration_noInstrumentationConfig() {
71+
AutoConfiguredOpenTelemetrySdk sdkMock = mock(AutoConfiguredOpenTelemetrySdk.class);
72+
ConfigProvider configProviderMock = mock(ConfigProvider.class);
73+
when(configProviderMock.getInstrumentationConfig()).thenReturn(null);
74+
75+
try (MockedStatic<AutoConfigureUtil> autoConfigureUtilMock =
76+
Mockito.mockStatic(AutoConfigureUtil.class)) {
77+
autoConfigureUtilMock.when(() -> AutoConfigureUtil.getConfig(sdkMock)).thenReturn(null);
78+
autoConfigureUtilMock
79+
.when(() -> AutoConfigureUtil.getConfigProvider(sdkMock))
80+
.thenReturn(configProviderMock);
81+
82+
ConfigProperties configProperties = ConfigPropertiesUtil.resolveConfigProperties(sdkMock);
83+
84+
assertThat(configProperties.getString("testProperty")).isEqualTo(null);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)