77
88import static org .assertj .core .api .Assertions .assertThat ;
99import static org .mockito .Mockito .mock ;
10+ import static org .mockito .Mockito .verify ;
11+ import static org .mockito .Mockito .when ;
1012
13+ import io .opentelemetry .contrib .dynamic .policy .registry .PolicyInit ;
14+ import io .opentelemetry .contrib .dynamic .policy .tracesampling .TraceSamplingRatePolicy ;
1115import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizer ;
16+ import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
17+ import java .lang .reflect .Method ;
18+ import java .nio .charset .StandardCharsets ;
19+ import java .nio .file .Files ;
20+ import java .nio .file .Path ;
21+ import java .util .Map ;
22+ import java .util .function .Function ;
23+ import org .junit .jupiter .api .AfterEach ;
1224import org .junit .jupiter .api .Test ;
25+ import org .junit .jupiter .api .io .TempDir ;
26+ import org .mockito .ArgumentCaptor ;
1327
1428class DynamicControlAutoConfigurationTest {
29+ private static final String POLICY_INIT_CONFIG_PROPERTY_JSON =
30+ "otel.java.experimental.telemetry.policy.init.json" ;
31+ private static final String POLICY_INIT_CONFIG_PROPERTY_YAML =
32+ "otel.java.experimental.telemetry.policy.init.yaml" ;
33+
34+ @ TempDir Path tempDir ;
35+
36+ @ AfterEach
37+ void tearDown () throws Exception {
38+ invokeStaticNoArg (PolicyInit .class , "resetForTest" );
39+ invokeStaticNoArg (TraceSamplingRatePolicy .class , "resetForTest" );
40+ }
1541
1642 @ Test
17- void testCustomize () {
43+ void customizeRegistersPropertiesCustomizerThatInitializesConfiguredPolicy () throws Exception {
1844 DynamicControlAutoConfiguration config = new DynamicControlAutoConfiguration ();
1945 AutoConfigurationCustomizer customizer = mock (AutoConfigurationCustomizer .class );
2046
2147 config .customize (customizer );
48+ Function <ConfigProperties , Map <String , String >> propertiesCustomizer =
49+ capturePropertiesCustomizer (customizer );
2250
23- // The customize method should not throw and should be callable
24- // Logging is tested manually or via integration tests
51+ Path configPath = tempDir .resolve ("policy-init.yaml" );
52+ Files .write (configPath , minimalYamlInitConfig ().getBytes (StandardCharsets .UTF_8 ));
53+ ConfigProperties properties = mock (ConfigProperties .class );
54+ when (properties .getString (POLICY_INIT_CONFIG_PROPERTY_YAML )).thenReturn (configPath .toString ());
55+ when (properties .getString (POLICY_INIT_CONFIG_PROPERTY_JSON )).thenReturn (null );
56+
57+ Map <String , String > overrides = propertiesCustomizer .apply (properties );
58+
59+ assertThat (overrides ).isEmpty ();
60+ assertThat (TraceSamplingRatePolicy .getInitializedSampler ()).isNotNull ();
2561 }
2662
2763 @ Test
@@ -31,4 +67,31 @@ void testOrder() {
3167 // Default order should be 0
3268 assertThat (config .order ()).isEqualTo (0 );
3369 }
70+
71+ private static Function <ConfigProperties , Map <String , String >> capturePropertiesCustomizer (
72+ AutoConfigurationCustomizer customizer ) {
73+ @ SuppressWarnings ("unchecked" )
74+ ArgumentCaptor <Function <ConfigProperties , Map <String , String >>> captor =
75+ ArgumentCaptor .forClass (Function .class );
76+ verify (customizer ).addPropertiesCustomizer (captor .capture ());
77+ return captor .getValue ();
78+ }
79+
80+ private static String minimalYamlInitConfig () {
81+ return "sources:\n "
82+ + " - kind: opamp\n "
83+ + " format: jsonkeyvalue\n "
84+ + " location: vendor\n "
85+ + " mappings:\n "
86+ + " - sourceKey: sampling_rate\n "
87+ + " policyType: "
88+ + TraceSamplingRatePolicy .POLICY_TYPE
89+ + "\n " ;
90+ }
91+
92+ private static void invokeStaticNoArg (Class <?> targetClass , String methodName ) throws Exception {
93+ Method method = targetClass .getDeclaredMethod (methodName );
94+ method .setAccessible (true );
95+ method .invoke (null );
96+ }
3497}
0 commit comments