Search in sources :

Example 16 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java-contrib by open-telemetry.

the class MojoGoalExecutionHandlerTest method testSpringBootBuildImage_springboot_1.

@Test
public void testSpringBootBuildImage_springboot_1() throws Exception {
    String pomXmlPath = "projects/springboot_1/pom.xml";
    String mojoGroupId = "org.springframework.boot";
    String mojoArtifactId = "spring-boot-maven-plugin";
    String mojoVersion = "2.5.6";
    String mojoGoal = "build-image";
    MavenProject project = newMavenProject(pomXmlPath);
    ExecutionEvent executionEvent = newMojoStartedExecutionEvent(project, mojoGroupId, mojoArtifactId, mojoVersion, mojoGoal);
    SpringBootBuildImageHandler buildImageHandler = new SpringBootBuildImageHandler();
    List<MavenGoal> supportedGoals = buildImageHandler.getSupportedGoals();
    assertThat(supportedGoals).isEqualTo(Collections.singletonList(MavenGoal.create(mojoGroupId, mojoArtifactId, mojoGoal)));
    try (SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().build()) {
        SpanBuilder spanBuilder = sdkTracerProvider.tracerBuilder("test-tracer").build().spanBuilder("spring-boot:build-image");
        buildImageHandler.enrichSpan(spanBuilder, executionEvent);
        ReadableSpan span = (ReadableSpan) spanBuilder.startSpan();
        // TODO improve the Maven test harness that interpolates the maven properties like
        // ${project.artifactId}
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_IMAGE_NAME)).isEqualTo("docker.io/john/${project.artifactId}");
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_IMAGE_TAGS)).isEqualTo(Collections.singletonList("latest"));
        assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)).isEqualTo("https://docker.io");
        assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("docker.io");
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL)).isEqualTo("https://docker.io");
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) MavenProject(org.apache.maven.project.MavenProject) ExecutionEvent(org.apache.maven.execution.ExecutionEvent) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) ReadableSpan(io.opentelemetry.sdk.trace.ReadableSpan) MavenGoal(io.opentelemetry.maven.MavenGoal) Test(org.junit.jupiter.api.Test)

Example 17 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java-contrib by open-telemetry.

the class MojoGoalExecutionHandlerTest method testMavenDeploy.

@Test
public void testMavenDeploy() throws Exception {
    String pomXmlPath = "projects/jar/pom.xml";
    String mojoGroupId = "org.apache.maven.plugins";
    String mojoArtifactId = "maven-deploy-plugin";
    String mojoVersion = "2.8.2";
    String mojoGoal = "deploy";
    MavenProject project = newMavenProject(pomXmlPath);
    ExecutionEvent executionEvent = newMojoStartedExecutionEvent(project, mojoGroupId, mojoArtifactId, mojoVersion, mojoGoal);
    MavenDeployHandler mavenDeployHandler = new MavenDeployHandler();
    List<MavenGoal> supportedGoals = mavenDeployHandler.getSupportedGoals();
    assertThat(supportedGoals).isEqualTo(Collections.singletonList(MavenGoal.create(mojoGroupId, mojoArtifactId, mojoGoal)));
    try (SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().build()) {
        SpanBuilder spanBuilder = sdkTracerProvider.tracerBuilder("test-tracer").build().spanBuilder("deploy");
        mavenDeployHandler.enrichSpan(spanBuilder, executionEvent);
        ReadableSpan span = (ReadableSpan) spanBuilder.startSpan();
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_REPOSITORY_ID)).isEqualTo("snapshots");
        assertThat(span.getAttribute(SemanticAttributes.HTTP_METHOD)).isEqualTo("POST");
        assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)).isEqualTo("https://maven.example.com/repository/maven-snapshots/io/opentelemetry/contrib/maven/test-jar/1.0-SNAPSHOT");
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_REPOSITORY_URL)).isEqualTo("https://maven.example.com/repository/maven-snapshots/");
        assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("maven.example.com");
        assertThat(span.getKind()).isEqualTo(SpanKind.CLIENT);
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) MavenProject(org.apache.maven.project.MavenProject) ExecutionEvent(org.apache.maven.execution.ExecutionEvent) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) ReadableSpan(io.opentelemetry.sdk.trace.ReadableSpan) MavenGoal(io.opentelemetry.maven.MavenGoal) Test(org.junit.jupiter.api.Test)

Example 18 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java-contrib by open-telemetry.

the class AwsXrayRemoteSamplerProviderTest method setEndpoint.

@Test
void setEndpoint() {
    Map<String, String> props = new HashMap<>();
    props.put("otel.traces.sampler", "xray");
    props.put("otel.traces.sampler.arg", "endpoint=http://localhost:3000");
    props.put("otel.service.name", "cat-service");
    props.put("otel.traces.exporter", "none");
    props.put("otel.metrics.exporter", "none");
    try (SdkTracerProvider tracerProvider = AutoConfiguredOpenTelemetrySdk.builder().addPropertiesSupplier(() -> props).setResultAsGlobal(false).build().getOpenTelemetrySdk().getSdkTracerProvider()) {
        // Inspect implementation details for simplicity, otherwise we'd probably need to make a
        // test HTTP server that records requests.
        assertThat(tracerProvider).extracting("sharedState").extracting("sampler").isInstanceOfSatisfying(AwsXrayRemoteSampler.class, sampler -> {
            assertThat(sampler.getClient().getSamplingRulesEndpoint()).isEqualTo("http://localhost:3000/GetSamplingRules");
            assertThat(sampler.getResource().getAttribute(ResourceAttributes.SERVICE_NAME)).isEqualTo("cat-service");
        });
    }
}
Also used : HashMap(java.util.HashMap) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) Test(org.junit.jupiter.api.Test)

Example 19 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.

the class TracerProviderConfigurationTest method configureTracerProvider.

@Test
void configureTracerProvider() {
    Map<String, String> properties = new HashMap<>();
    properties.put("otel.bsp.schedule.delay", "100000");
    properties.put("otel.traces.sampler", "always_off");
    properties.put("otel.traces.exporter", "none");
    Resource resource = Resource.create(Attributes.builder().put("cat", "meow").build());
    // We don't have any exporters on classpath for this test so check no-op case. Exporter cases
    // are verified in other test sets like testFullConfig.
    SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder().setResource(resource);
    TracerProviderConfiguration.configureTracerProvider(tracerProviderBuilder, DefaultConfigProperties.createForTest(properties), TracerProviderConfiguration.class.getClassLoader(), MeterProvider.noop(), (a, unused) -> a, (a, unused) -> a);
    SdkTracerProvider tracerProvider = tracerProviderBuilder.build();
    try {
        assertThat(tracerProvider.getSampler()).isEqualTo(Sampler.alwaysOff());
        assertThat(tracerProvider).extracting("sharedState").satisfies(sharedState -> {
            assertThat(sharedState).extracting("resource").isEqualTo(resource);
            assertThat(sharedState).extracting("activeSpanProcessor").isEqualTo(SpanProcessor.composite());
        });
    } finally {
        tracerProvider.shutdown();
    }
}
Also used : SdkTracerProviderBuilder(io.opentelemetry.sdk.trace.SdkTracerProviderBuilder) HashMap(java.util.HashMap) Resource(io.opentelemetry.sdk.resources.Resource) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) Test(org.junit.jupiter.api.Test)

Example 20 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.

the class AutoConfiguredOpenTelemetrySdkBuilder method build.

/**
 * Returns a new {@link AutoConfiguredOpenTelemetrySdk} holding components auto-configured using
 * the settings of this {@link AutoConfiguredOpenTelemetrySdkBuilder}.
 */
public AutoConfiguredOpenTelemetrySdk build() {
    if (!customized) {
        customized = true;
        mergeSdkTracerProviderConfigurer();
        for (AutoConfigurationCustomizerProvider customizer : ServiceLoader.load(AutoConfigurationCustomizerProvider.class, serviceClassLoader)) {
            customizer.customize(this);
        }
    }
    ConfigProperties config = getConfig();
    Resource resource = ResourceConfiguration.configureResource(config, serviceClassLoader, resourceCustomizer);
    SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder();
    meterProviderBuilder.setResource(resource);
    MeterProviderConfiguration.configureMeterProvider(meterProviderBuilder, config, serviceClassLoader, metricExporterCustomizer);
    meterProviderBuilder = meterProviderCustomizer.apply(meterProviderBuilder, config);
    SdkMeterProvider meterProvider = meterProviderBuilder.build();
    SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder();
    tracerProviderBuilder.setResource(resource);
    TracerProviderConfiguration.configureTracerProvider(tracerProviderBuilder, config, serviceClassLoader, meterProvider, spanExporterCustomizer, samplerCustomizer);
    tracerProviderBuilder = tracerProviderCustomizer.apply(tracerProviderBuilder, config);
    SdkTracerProvider tracerProvider = tracerProviderBuilder.build();
    SdkLogEmitterProviderBuilder logEmitterProviderBuilder = SdkLogEmitterProvider.builder();
    logEmitterProviderBuilder.setResource(resource);
    LogEmitterProviderConfiguration.configureLogEmitterProvider(logEmitterProviderBuilder, config, meterProvider, logExporterCustomizer);
    logEmitterProviderBuilder = logEmitterProviderCustomizer.apply(logEmitterProviderBuilder, config);
    SdkLogEmitterProvider logEmitterProvider = logEmitterProviderBuilder.build();
    if (registerShutdownHook) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            List<CompletableResultCode> shutdown = new ArrayList<>();
            shutdown.add(tracerProvider.shutdown());
            shutdown.add(meterProvider.shutdown());
            shutdown.add(logEmitterProvider.shutdown());
            CompletableResultCode.ofAll(shutdown).join(10, TimeUnit.SECONDS);
        }));
    }
    ContextPropagators propagators = PropagatorConfiguration.configurePropagators(config, serviceClassLoader, propagatorCustomizer);
    OpenTelemetrySdkBuilder sdkBuilder = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).setLogEmitterProvider(logEmitterProvider).setMeterProvider(meterProvider).setPropagators(propagators);
    OpenTelemetrySdk openTelemetrySdk = sdkBuilder.build();
    if (setResultAsGlobal) {
        GlobalOpenTelemetry.set(openTelemetrySdk);
        logger.log(Level.FINE, "Global OpenTelemetrySdk set to {0} by autoconfiguration", openTelemetrySdk);
    }
    return AutoConfiguredOpenTelemetrySdk.create(openTelemetrySdk, resource, config);
}
Also used : SdkTracerProviderBuilder(io.opentelemetry.sdk.trace.SdkTracerProviderBuilder) ConfigProperties(io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) Resource(io.opentelemetry.sdk.resources.Resource) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) OpenTelemetrySdkBuilder(io.opentelemetry.sdk.OpenTelemetrySdkBuilder) ArrayList(java.util.ArrayList) List(java.util.List) AutoConfigurationCustomizerProvider(io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider) SdkMeterProviderBuilder(io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) SdkLogEmitterProviderBuilder(io.opentelemetry.sdk.logs.SdkLogEmitterProviderBuilder)

Aggregations

SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)37 Test (org.junit.jupiter.api.Test)20 ReadableSpan (io.opentelemetry.sdk.trace.ReadableSpan)9 Resource (io.opentelemetry.sdk.resources.Resource)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)7 MavenGoal (io.opentelemetry.maven.MavenGoal)7 OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)7 ExecutionEvent (org.apache.maven.execution.ExecutionEvent)7 MavenProject (org.apache.maven.project.MavenProject)7 Span (io.opentelemetry.api.trace.Span)6 Tracer (io.opentelemetry.api.trace.Tracer)6 SpanData (io.opentelemetry.sdk.trace.data.SpanData)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)4 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)4 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)4 SdkMeterProvider (io.opentelemetry.sdk.metrics.SdkMeterProvider)4