|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling.config; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import io.opentelemetry.api.common.AttributeKey; |
| 11 | +import io.opentelemetry.api.common.Attributes; |
| 12 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 13 | +import io.opentelemetry.sdk.autoconfigure.SdkAutoconfigureAccess; |
| 14 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 15 | +import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; |
| 16 | +import io.opentelemetry.sdk.resources.Resource; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Stream; |
| 22 | +import javax.annotation.Nullable; |
| 23 | +import org.assertj.core.util.Strings; |
| 24 | +import org.junit.jupiter.api.DynamicTest; |
| 25 | +import org.junit.jupiter.api.TestFactory; |
| 26 | + |
| 27 | +public class ResourceProviderPropertiesCustomizerTest { |
| 28 | + |
| 29 | + public static final class Provider implements ResourceProvider { |
| 30 | + @Override |
| 31 | + public Resource createResource(ConfigProperties config) { |
| 32 | + return Resource.create(Attributes.of(AttributeKey.stringKey("key"), "value")); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static class EnabledTestCase { |
| 37 | + private final String name; |
| 38 | + private final boolean result; |
| 39 | + private final Set<String> enabledProviders; |
| 40 | + private final Set<String> disabledProviders; |
| 41 | + private final Boolean explicitEnabled; |
| 42 | + |
| 43 | + private EnabledTestCase( |
| 44 | + String name, |
| 45 | + boolean result, |
| 46 | + Set<String> enabledProviders, |
| 47 | + Set<String> disabledProviders, |
| 48 | + @Nullable Boolean explicitEnabled) { |
| 49 | + this.name = name; |
| 50 | + this.result = result; |
| 51 | + this.enabledProviders = enabledProviders; |
| 52 | + this.disabledProviders = disabledProviders; |
| 53 | + this.explicitEnabled = explicitEnabled; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @SuppressWarnings("BooleanParameter") |
| 58 | + @TestFactory |
| 59 | + Stream<DynamicTest> enabledTestCases() { |
| 60 | + String className = |
| 61 | + "io.opentelemetry.javaagent.tooling.config.ResourceProviderPropertiesCustomizerTest$Provider"; |
| 62 | + return Stream.of( |
| 63 | + new EnabledTestCase( |
| 64 | + "explicitEnabled", true, Collections.emptySet(), Collections.emptySet(), true), |
| 65 | + new EnabledTestCase( |
| 66 | + "explicitEnabledFalse", |
| 67 | + false, |
| 68 | + Collections.emptySet(), |
| 69 | + Collections.emptySet(), |
| 70 | + false), |
| 71 | + new EnabledTestCase( |
| 72 | + "enabledProvidersEmpty", |
| 73 | + false, |
| 74 | + Collections.emptySet(), |
| 75 | + Collections.emptySet(), |
| 76 | + null), |
| 77 | + new EnabledTestCase( |
| 78 | + "enabledProvidersContains", |
| 79 | + true, |
| 80 | + Collections.singleton(className), |
| 81 | + Collections.emptySet(), |
| 82 | + null), |
| 83 | + new EnabledTestCase( |
| 84 | + "enabledProvidersNotContains", |
| 85 | + false, |
| 86 | + Collections.singleton("otherClassName"), |
| 87 | + Collections.emptySet(), |
| 88 | + null), |
| 89 | + new EnabledTestCase( |
| 90 | + "disabledProvidersContains", |
| 91 | + false, |
| 92 | + Collections.emptySet(), |
| 93 | + Collections.singleton(className), |
| 94 | + null), |
| 95 | + new EnabledTestCase( |
| 96 | + "disabledProvidersNotContains", |
| 97 | + false, |
| 98 | + Collections.emptySet(), |
| 99 | + Collections.singleton("otherClassName"), |
| 100 | + null), |
| 101 | + new EnabledTestCase( |
| 102 | + "defaultEnabledFalse", false, Collections.emptySet(), Collections.emptySet(), null)) |
| 103 | + .map( |
| 104 | + tc -> |
| 105 | + DynamicTest.dynamicTest( |
| 106 | + tc.name, |
| 107 | + () -> { |
| 108 | + Map<String, String> props = new HashMap<>(); |
| 109 | + props.put( |
| 110 | + ResourceProviderPropertiesCustomizer.ENABLED_KEY, |
| 111 | + Strings.join(tc.enabledProviders).with(",")); |
| 112 | + props.put( |
| 113 | + ResourceProviderPropertiesCustomizer.DISABLED_KEY, |
| 114 | + Strings.join(tc.disabledProviders).with(",")); |
| 115 | + |
| 116 | + if (tc.explicitEnabled != null) { |
| 117 | + props.put( |
| 118 | + "otel.resource.providers.test.enabled", |
| 119 | + Boolean.toString(tc.explicitEnabled)); |
| 120 | + } |
| 121 | + |
| 122 | + Attributes attributes = |
| 123 | + SdkAutoconfigureAccess.getResourceAttributes( |
| 124 | + AutoConfiguredOpenTelemetrySdk.builder() |
| 125 | + .addPropertiesSupplier(() -> props) |
| 126 | + .build()); |
| 127 | + |
| 128 | + if (tc.result) { |
| 129 | + assertThat(attributes.get(AttributeKey.stringKey("key"))) |
| 130 | + .isEqualTo("value"); |
| 131 | + } else { |
| 132 | + assertThat(attributes.get(AttributeKey.stringKey("key"))).isNull(); |
| 133 | + } |
| 134 | + })); |
| 135 | + } |
| 136 | +} |
0 commit comments