Skip to content

Commit 541f260

Browse files
committed
test coverage
1 parent c4c3fc7 commit 541f260

File tree

3 files changed

+114
-33
lines changed

3 files changed

+114
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.fileconfig;
7+
8+
import io.opentelemetry.api.incubator.config.DeclarativeConfigException;
9+
import io.opentelemetry.sdk.OpenTelemetrySdk;
10+
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
11+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
12+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
13+
import io.opentelemetry.sdk.resources.Resource;
14+
import java.lang.reflect.InvocationTargetException;
15+
import java.lang.reflect.Method;
16+
import java.util.Objects;
17+
18+
class AutoConfiguredOpenTelemetrySdkAccess {
19+
20+
private AutoConfiguredOpenTelemetrySdkAccess() {}
21+
22+
static AutoConfiguredOpenTelemetrySdk create(
23+
OpenTelemetrySdk sdk, Resource resource, SdkConfigProvider provider) {
24+
return createWithFactory(
25+
() -> {
26+
Method method =
27+
Class.forName(AutoConfiguredOpenTelemetrySdk.class.getName())
28+
.getDeclaredMethod(
29+
"create",
30+
OpenTelemetrySdk.class,
31+
Resource.class,
32+
ConfigProperties.class,
33+
Object.class);
34+
method.setAccessible(true);
35+
return (AutoConfiguredOpenTelemetrySdk)
36+
method.invoke(null, sdk, resource, null, provider);
37+
});
38+
}
39+
40+
// Visible for testing
41+
interface Factory {
42+
AutoConfiguredOpenTelemetrySdk create()
43+
throws ClassNotFoundException,
44+
NoSuchMethodException,
45+
IllegalAccessException,
46+
InvocationTargetException;
47+
}
48+
49+
// Visible for testing
50+
static AutoConfiguredOpenTelemetrySdk createWithFactory(Factory factory) {
51+
try {
52+
return factory.create();
53+
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
54+
throw new ConfigurationException(
55+
"Error configuring from file. Is opentelemetry-sdk-extension-incubator on the classpath?",
56+
e);
57+
} catch (InvocationTargetException e) {
58+
Throwable cause = e.getCause();
59+
if (cause instanceof DeclarativeConfigException) {
60+
throw toConfigurationException((DeclarativeConfigException) cause);
61+
}
62+
throw new ConfigurationException("Unexpected error configuring from file", e);
63+
}
64+
}
65+
66+
private static ConfigurationException toConfigurationException(
67+
DeclarativeConfigException exception) {
68+
String message = Objects.requireNonNull(exception.getMessage());
69+
return new ConfigurationException(message, exception);
70+
}
71+
}

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/DeclarativeConfiguration.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@
2121
import io.opentelemetry.sdk.extension.incubator.ExtendedOpenTelemetrySdk;
2222
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
2323
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SamplerModel;
24-
import io.opentelemetry.sdk.resources.Resource;
2524
import io.opentelemetry.sdk.trace.samplers.Sampler;
2625
import java.io.Closeable;
2726
import java.io.IOException;
2827
import java.io.InputStream;
29-
import java.lang.reflect.InvocationTargetException;
30-
import java.lang.reflect.Method;
3128
import java.util.Collections;
3229
import java.util.Map;
3330
import java.util.Objects;
@@ -143,39 +140,10 @@ private static OpenTelemetrySdk create(
143140
public static AutoConfiguredOpenTelemetrySdk createAutoConfiguredSdk(
144141
OpenTelemetryConfigurationModel configurationModel, ComponentLoader componentLoader) {
145142
DeclarativeConfigContext context = DeclarativeConfigContext.create(componentLoader);
146-
147143
OpenTelemetrySdk sdk = create(configurationModel, context);
148144
SdkConfigProvider provider = SdkConfigProvider.create(configurationModel, componentLoader);
149145

150-
try {
151-
Method method =
152-
Class.forName(AutoConfiguredOpenTelemetrySdk.class.getName())
153-
.getDeclaredMethod(
154-
"create",
155-
OpenTelemetrySdk.class,
156-
Resource.class,
157-
ConfigProperties.class,
158-
Object.class);
159-
method.setAccessible(true);
160-
return (AutoConfiguredOpenTelemetrySdk)
161-
method.invoke(null, sdk, context.getResource(), null, provider);
162-
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
163-
throw new ConfigurationException(
164-
"Error configuring from file. Is opentelemetry-sdk-extension-incubator on the classpath?",
165-
e);
166-
} catch (InvocationTargetException e) {
167-
Throwable cause = e.getCause();
168-
if (cause instanceof DeclarativeConfigException) {
169-
throw toConfigurationException((DeclarativeConfigException) cause);
170-
}
171-
throw new ConfigurationException("Unexpected error configuring from file", e);
172-
}
173-
}
174-
175-
private static ConfigurationException toConfigurationException(
176-
DeclarativeConfigException exception) {
177-
String message = Objects.requireNonNull(exception.getMessage());
178-
return new ConfigurationException(message, exception);
146+
return AutoConfiguredOpenTelemetrySdkAccess.create(sdk, context.getResource(), provider);
179147
}
180148

181149
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.fileconfig;
7+
8+
import static org.assertj.core.api.Assertions.assertThatCode;
9+
10+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
11+
import java.lang.reflect.InvocationTargetException;
12+
import org.junit.jupiter.api.Test;
13+
14+
class AutoConfiguredOpenTelemetrySdkAccessTest {
15+
16+
@Test
17+
void classNotFoundException() {
18+
assertThatCode(
19+
() ->
20+
AutoConfiguredOpenTelemetrySdkAccess.createWithFactory(
21+
() -> {
22+
Class.forName("foo");
23+
return null;
24+
}))
25+
.isInstanceOf(ConfigurationException.class)
26+
.hasMessage(
27+
"Error configuring from file. Is opentelemetry-sdk-extension-incubator on the classpath?");
28+
}
29+
30+
@Test
31+
void invocationTargetException() {
32+
assertThatCode(
33+
() ->
34+
AutoConfiguredOpenTelemetrySdkAccess.createWithFactory(
35+
() -> {
36+
throw new InvocationTargetException(new RuntimeException("test exception"));
37+
}))
38+
.isInstanceOf(ConfigurationException.class)
39+
.hasMessage("Unexpected error configuring from file")
40+
.hasRootCauseMessage("test exception");
41+
}
42+
}

0 commit comments

Comments
 (0)