55
66package io .opentelemetry .sdk .autoconfigure ;
77
8+ import static java .util .Objects .requireNonNull ;
9+
810import io .opentelemetry .api .incubator .config .ConfigProvider ;
911import io .opentelemetry .api .incubator .config .DeclarativeConfigException ;
1012import io .opentelemetry .api .incubator .config .GlobalConfigProvider ;
1820import java .io .InputStream ;
1921import java .lang .reflect .InvocationTargetException ;
2022import java .lang .reflect .Method ;
21- import java .util .Objects ;
2223import java .util .logging .Logger ;
2324import javax .annotation .Nullable ;
2425
@@ -33,28 +34,32 @@ final class IncubatingUtil {
3334
3435 private IncubatingUtil () {}
3536
37+ // Visible for testing
38+ interface Factory {
39+ @ Nullable
40+ AutoConfiguredOpenTelemetrySdk create ()
41+ throws ClassNotFoundException ,
42+ NoSuchMethodException ,
43+ IllegalAccessException ,
44+ InvocationTargetException ;
45+ }
46+
3647 static AutoConfiguredOpenTelemetrySdk configureFromFile (
3748 Logger logger , String configurationFile , ComponentLoader componentLoader ) {
3849 logger .fine ("Autoconfiguring from configuration file: " + configurationFile );
3950 try (FileInputStream fis = new FileInputStream (configurationFile )) {
40- Object model =
41- Class .forName (
42- "io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration" )
43- .getMethod ("parse" , InputStream .class )
44- .invoke (null , fis );
45- return getOpenTelemetrySdk (model , componentLoader );
51+ return requireNonNull (
52+ createWithFactory (
53+ "file" ,
54+ () ->
55+ getOpenTelemetrySdk (
56+ Class .forName (
57+ "io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration" )
58+ .getMethod ("parse" , InputStream .class )
59+ .invoke (null , fis ),
60+ componentLoader )));
4661 } catch (FileNotFoundException e ) {
4762 throw new ConfigurationException ("Configuration file not found" , e );
48- } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e ) {
49- throw new ConfigurationException (
50- "Error configuring from file. Is opentelemetry-sdk-extension-incubator on the classpath?" ,
51- e );
52- } catch (InvocationTargetException e ) {
53- Throwable cause = e .getCause ();
54- if (cause instanceof DeclarativeConfigException ) {
55- throw toConfigurationException ((DeclarativeConfigException ) cause );
56- }
57- throw new ConfigurationException ("Unexpected error configuring from file" , e );
5863 } catch (IOException e ) {
5964 // IOException (other than FileNotFoundException which is caught above) is only thrown
6065 // above by FileInputStream.close()
@@ -64,30 +69,22 @@ static AutoConfiguredOpenTelemetrySdk configureFromFile(
6469
6570 @ Nullable
6671 public static AutoConfiguredOpenTelemetrySdk configureFromSpi (ComponentLoader componentLoader ) {
67- try {
68- Class <?> providerClass =
69- Class .forName (
70- "io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationProvider" );
71- Method getConfigurationModel = providerClass .getMethod ("getConfigurationModel" );
72+ return createWithFactory (
73+ "SPI" ,
74+ () -> {
75+ Class <?> providerClass =
76+ Class .forName (
77+ "io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationProvider" );
78+ Method getConfigurationModel = providerClass .getMethod ("getConfigurationModel" );
7279
73- for (Object configProvider : componentLoader .load (providerClass )) {
74- Object model = getConfigurationModel .invoke (configProvider );
75- if (model != null ) {
76- return getOpenTelemetrySdk (model , componentLoader );
77- }
78- }
79- return null ;
80- } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e ) {
81- throw new ConfigurationException (
82- "Error configuring from SPI. Is opentelemetry-sdk-extension-incubator on the classpath?" ,
83- e );
84- } catch (InvocationTargetException e ) {
85- Throwable cause = e .getCause ();
86- if (cause instanceof DeclarativeConfigException ) {
87- throw toConfigurationException ((DeclarativeConfigException ) cause );
88- }
89- throw new ConfigurationException ("Unexpected error configuring from SPI" , e );
90- }
80+ for (Object configProvider : componentLoader .load (providerClass )) {
81+ Object model = getConfigurationModel .invoke (configProvider );
82+ if (model != null ) {
83+ return getOpenTelemetrySdk (model , componentLoader );
84+ }
85+ }
86+ return null ;
87+ });
9188 }
9289
9390 private static AutoConfiguredOpenTelemetrySdk getOpenTelemetrySdk (
@@ -119,9 +116,29 @@ private static AutoConfiguredOpenTelemetrySdk getOpenTelemetrySdk(
119116 return AutoConfiguredOpenTelemetrySdk .create (sdk , Resource .getDefault (), null , configProvider );
120117 }
121118
119+ // Visible for testing
120+ @ Nullable
121+ static AutoConfiguredOpenTelemetrySdk createWithFactory (String name , Factory factory ) {
122+ try {
123+ return factory .create ();
124+ } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e ) {
125+ throw new ConfigurationException (
126+ String .format (
127+ "Error configuring from %s. Is opentelemetry-sdk-extension-incubator on the classpath?" ,
128+ name ),
129+ e );
130+ } catch (InvocationTargetException e ) {
131+ Throwable cause = e .getCause ();
132+ if (cause instanceof DeclarativeConfigException ) {
133+ throw toConfigurationException ((DeclarativeConfigException ) cause );
134+ }
135+ throw new ConfigurationException ("Unexpected error configuring from " + name , e );
136+ }
137+ }
138+
122139 private static ConfigurationException toConfigurationException (
123140 DeclarativeConfigException exception ) {
124- String message = Objects . requireNonNull (exception .getMessage ());
141+ String message = requireNonNull (exception .getMessage ());
125142 return new ConfigurationException (message , exception );
126143 }
127144
0 commit comments