Skip to content

Commit 1b36c6e

Browse files
committed
test coverage
1 parent 52a8adf commit 1b36c6e

File tree

3 files changed

+114
-35
lines changed

3 files changed

+114
-35
lines changed
Lines changed: 71 additions & 0 deletions
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 & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@
1414
import io.opentelemetry.common.ComponentLoader;
1515
import io.opentelemetry.sdk.OpenTelemetrySdk;
1616
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
17-
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
18-
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
1917
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
2018
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
2119
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SamplerModel;
22-
import io.opentelemetry.sdk.resources.Resource;
2320
import io.opentelemetry.sdk.trace.samplers.Sampler;
2421
import java.io.Closeable;
2522
import java.io.IOException;
2623
import java.io.InputStream;
27-
import java.lang.reflect.InvocationTargetException;
28-
import java.lang.reflect.Method;
2924
import java.util.Collections;
3025
import java.util.Map;
3126
import java.util.Objects;
@@ -137,39 +132,10 @@ private static OpenTelemetrySdk create(
137132
public static AutoConfiguredOpenTelemetrySdk createAutoConfiguredSdk(
138133
OpenTelemetryConfigurationModel configurationModel, ComponentLoader componentLoader) {
139134
DeclarativeConfigContext context = DeclarativeConfigContext.create(componentLoader);
140-
141135
OpenTelemetrySdk sdk = create(configurationModel, context);
142136
SdkConfigProvider provider = SdkConfigProvider.create(configurationModel, componentLoader);
143137

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

175141
/**
Lines changed: 42 additions & 0 deletions
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)