|
13 | 13 | import com.linecorp.armeria.testing.junit5.server.SelfSignedCertificateExtension; |
14 | 14 | import io.github.netmikey.logunit.api.LogCapturer; |
15 | 15 | import io.opentelemetry.internal.testing.CleanupExtension; |
| 16 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 17 | +import io.opentelemetry.sdk.autoconfigure.internal.ComponentLoader; |
| 18 | +import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; |
16 | 19 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
17 | 20 | import java.io.ByteArrayInputStream; |
18 | 21 | import java.io.File; |
|
22 | 25 | import java.nio.file.Files; |
23 | 26 | import java.nio.file.Path; |
24 | 27 | import java.security.cert.CertificateEncodingException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
25 | 31 | import java.util.Objects; |
| 32 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeNameValueModel; |
| 33 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel; |
| 34 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.ResourceModel; |
| 35 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanProcessorModel; |
| 36 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TracerProviderModel; |
26 | 37 | import org.junit.jupiter.api.Test; |
27 | 38 | import org.junit.jupiter.api.extension.RegisterExtension; |
28 | 39 | import org.junit.jupiter.api.io.TempDir; |
@@ -147,6 +158,68 @@ void parseAndCreate_EmptyComponentProviderConfig() { |
147 | 158 | FileConfiguration.parseAndCreate( |
148 | 159 | new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8)))) |
149 | 160 | .doesNotThrowAnyException(); |
150 | | - ; |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void create_ModelCustomizer() { |
| 165 | + OpenTelemetryConfigurationModel model = new OpenTelemetryConfigurationModel(); |
| 166 | + model.withFileFormat("0.3"); |
| 167 | + model.withTracerProvider( |
| 168 | + new TracerProviderModel() |
| 169 | + .withProcessors( |
| 170 | + Collections.singletonList( |
| 171 | + new SpanProcessorModel().withAdditionalProperty("test", null)))); |
| 172 | + ComponentLoader componentLoader = |
| 173 | + SpiHelper.serviceComponentLoader(FileConfiguration.class.getClassLoader()); |
| 174 | + OpenTelemetrySdk sdk = |
| 175 | + FileConfiguration.create( |
| 176 | + model, |
| 177 | + new ComponentLoader() { |
| 178 | + @SuppressWarnings("unchecked") |
| 179 | + @Override |
| 180 | + public <T> Iterable<T> load(Class<T> spiClass) { |
| 181 | + if (OpenTelemetryConfigurationModelCustomizerProvider.class.equals(spiClass)) { |
| 182 | + return (Iterable<T>) Collections.singletonList(getCustomizerProvider()); |
| 183 | + } |
| 184 | + return componentLoader.load(spiClass); |
| 185 | + } |
| 186 | + }); |
| 187 | + assertThat(sdk.toString()) |
| 188 | + .contains( |
| 189 | + "resource=Resource{schemaUrl=null, attributes={" |
| 190 | + + "color=\"blue\", " |
| 191 | + + "foo=\"bar\", " |
| 192 | + + "order=\"second\", " |
| 193 | + + "service.name=\"unknown_service:java\", " |
| 194 | + + "shape=\"square\", " |
| 195 | + + "telemetry.sdk.language=\"java\", " |
| 196 | + + "telemetry.sdk.name=\"opentelemetry\", " |
| 197 | + + "telemetry.sdk.version=\"1.48.0-SNAPSHOT\"}}"); |
| 198 | + } |
| 199 | + |
| 200 | + private static OpenTelemetryConfigurationModelCustomizerProvider getCustomizerProvider() { |
| 201 | + return model -> { |
| 202 | + ResourceModel resource = model.getResource(); |
| 203 | + if (resource == null) { |
| 204 | + resource = new ResourceModel(); |
| 205 | + model.withResource(resource); |
| 206 | + } |
| 207 | + List<AttributeNameValueModel> attributes = resource.getAttributes(); |
| 208 | + if (attributes == null) { |
| 209 | + attributes = new ArrayList<>(); |
| 210 | + resource.withAttributes(attributes); |
| 211 | + } |
| 212 | + attributes.add( |
| 213 | + new AttributeNameValueModel() |
| 214 | + .withName("foo") |
| 215 | + .withType(AttributeNameValueModel.Type.STRING) |
| 216 | + .withValue("bar")); |
| 217 | + attributes.add( |
| 218 | + new AttributeNameValueModel() |
| 219 | + .withName("color") |
| 220 | + .withType(AttributeNameValueModel.Type.STRING) |
| 221 | + .withValue("blue")); |
| 222 | + return model; |
| 223 | + }; |
151 | 224 | } |
152 | 225 | } |
0 commit comments