|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.dynamic.policy.registry; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | + |
| 14 | +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; |
| 15 | +import io.opentelemetry.contrib.dynamic.policy.tracesampling.TraceSamplingRatePolicy; |
| 16 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; |
| 17 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.Function; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.io.TempDir; |
| 28 | +import org.mockito.ArgumentCaptor; |
| 29 | + |
| 30 | +class PolicyInitTest { |
| 31 | + @TempDir Path tempDir; |
| 32 | + |
| 33 | + @AfterEach |
| 34 | + void tearDown() throws Exception { |
| 35 | + PolicyInit.resetForTest(); |
| 36 | + invokeStaticNoArg(TraceSamplingRatePolicy.class, "resetForTest"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void doesNotInitializePolicyFromDeclarativeOnlyConfigInAutoConfigurationMode() { |
| 41 | + AutoConfigurationCustomizer customizer = mock(AutoConfigurationCustomizer.class); |
| 42 | + PolicyInit.init(customizer); |
| 43 | + Function<ConfigProperties, Map<String, String>> propertiesCustomizer = |
| 44 | + capturePropertiesCustomizer(customizer); |
| 45 | + |
| 46 | + ConfigProperties config = mock(ConfigProperties.class); |
| 47 | + when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_YAML)).thenReturn(null); |
| 48 | + when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_JSON)).thenReturn(null); |
| 49 | + Map<String, String> ignored = propertiesCustomizer.apply(config); |
| 50 | + |
| 51 | + assertThat(TraceSamplingRatePolicy.getInitializedSampler()).isNull(); |
| 52 | + assertThat(ignored).isNotNull(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void initializesPolicyFromInitConfigInAutoConfigurationMode() throws Exception { |
| 57 | + AutoConfigurationCustomizer customizer = mock(AutoConfigurationCustomizer.class); |
| 58 | + PolicyInit.init(customizer); |
| 59 | + Function<ConfigProperties, Map<String, String>> propertiesCustomizer = |
| 60 | + capturePropertiesCustomizer(customizer); |
| 61 | + |
| 62 | + Path configPath = tempDir.resolve("policy-init.json"); |
| 63 | + Files.write(configPath, minimalJsonInitConfig().getBytes(StandardCharsets.UTF_8)); |
| 64 | + |
| 65 | + ConfigProperties config = mock(ConfigProperties.class); |
| 66 | + when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_YAML)).thenReturn(null); |
| 67 | + when(config.getString(PolicyInitConfig.POLICY_INIT_CONFIG_PROPERTY_JSON)) |
| 68 | + .thenReturn(configPath.toString()); |
| 69 | + |
| 70 | + Map<String, String> ignored = propertiesCustomizer.apply(config); |
| 71 | + |
| 72 | + assertThat(ignored).isNotNull(); |
| 73 | + assertThat(TraceSamplingRatePolicy.getInitializedSampler()).isNotNull(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void initializesRegisteredPolicyTypeFromDeclarativeConfig() { |
| 78 | + ConfigProperties config = mock(ConfigProperties.class); |
| 79 | + |
| 80 | + PolicyInit.initFromDeclarativeConfig( |
| 81 | + telemetryPolicyNodeConfig(TraceSamplingRatePolicy.POLICY_TYPE), config); |
| 82 | + |
| 83 | + assertThat(TraceSamplingRatePolicy.getInitializedSampler()).isNotNull(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void throwsWhenDeclarativeConfigUsesUnknownPolicyType() { |
| 88 | + ConfigProperties config = mock(ConfigProperties.class); |
| 89 | + |
| 90 | + assertThatThrownBy( |
| 91 | + () -> |
| 92 | + PolicyInit.initFromDeclarativeConfig( |
| 93 | + telemetryPolicyNodeConfig("trace_sampling_rate_policy"), config)) |
| 94 | + .isInstanceOf(IllegalArgumentException.class) |
| 95 | + .hasMessageContaining("Unknown policyType"); |
| 96 | + } |
| 97 | + |
| 98 | + private static Function<ConfigProperties, Map<String, String>> capturePropertiesCustomizer( |
| 99 | + AutoConfigurationCustomizer customizer) { |
| 100 | + @SuppressWarnings("unchecked") |
| 101 | + ArgumentCaptor<Function<ConfigProperties, Map<String, String>>> captor = |
| 102 | + ArgumentCaptor.forClass(Function.class); |
| 103 | + verify(customizer).addPropertiesCustomizer(captor.capture()); |
| 104 | + return captor.getValue(); |
| 105 | + } |
| 106 | + |
| 107 | + private static void invokeStaticNoArg(Class<?> targetClass, String methodName) throws Exception { |
| 108 | + Method method = targetClass.getDeclaredMethod(methodName); |
| 109 | + method.setAccessible(true); |
| 110 | + method.invoke(null); |
| 111 | + } |
| 112 | + |
| 113 | + private static DeclarativeConfigProperties telemetryPolicyNodeConfig(String policyType) { |
| 114 | + DeclarativeConfigProperties telemetryPolicy = mock(DeclarativeConfigProperties.class); |
| 115 | + DeclarativeConfigProperties source = mock(DeclarativeConfigProperties.class); |
| 116 | + DeclarativeConfigProperties mapping = mock(DeclarativeConfigProperties.class); |
| 117 | + |
| 118 | + when(telemetryPolicy.getStructuredList(PolicyInitConfig.SOURCES_DECLARATIVE_KEY)) |
| 119 | + .thenReturn(Collections.singletonList(source)); |
| 120 | + when(source.getString(PolicyInitConfig.KIND_DECLARATIVE_KEY)).thenReturn("opamp"); |
| 121 | + when(source.getString(PolicyInitConfig.FORMAT_DECLARATIVE_KEY)).thenReturn("jsonkeyvalue"); |
| 122 | + when(source.getString(PolicyInitConfig.LOCATION_DECLARATIVE_KEY)).thenReturn("vendor"); |
| 123 | + when(source.getStructuredList(PolicyInitConfig.MAPPINGS_DECLARATIVE_KEY)) |
| 124 | + .thenReturn(Collections.singletonList(mapping)); |
| 125 | + when(mapping.getString(PolicyInitConfig.SOURCE_KEY_DECLARATIVE_KEY)) |
| 126 | + .thenReturn("sampling_rate"); |
| 127 | + when(mapping.getString(PolicyInitConfig.POLICY_TYPE_DECLARATIVE_KEY)).thenReturn(policyType); |
| 128 | + return telemetryPolicy; |
| 129 | + } |
| 130 | + |
| 131 | + private static String minimalJsonInitConfig() { |
| 132 | + return "{\"sources\":[{\"kind\":\"opamp\",\"format\":\"jsonkeyvalue\",\"location\":\"vendor\"," |
| 133 | + + "\"mappings\":[{\"sourceKey\":\"sampling_rate\",\"policyType\":\"" |
| 134 | + + TraceSamplingRatePolicy.POLICY_TYPE |
| 135 | + + "\"}]}]}"; |
| 136 | + } |
| 137 | +} |
0 commit comments