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