From dd2c1fbaf568cd87b0ad94d7faf5e3b6f7ece0d9 Mon Sep 17 00:00:00 2001 From: SylvainJuge <763082+SylvainJuge@users.noreply.github.com> Date: Wed, 8 May 2024 12:34:36 +0200 Subject: [PATCH] migrate maven extension to current semconv (#1299) --- maven-extension/build.gradle.kts | 10 +---- .../maven/handler/GoogleJibBuildHandler.java | 10 +++-- .../maven/handler/MavenDeployHandler.java | 10 +++-- .../maven/handler/SnykMonitorHandler.java | 13 +++--- .../maven/handler/SnykTestHandler.java | 13 +++--- .../handler/SpringBootBuildImageHandler.java | 10 +++-- .../resources/MavenResourceProvider.java | 6 +-- .../semconv/MavenOtelSemanticAttributes.java | 10 +++-- .../handler/MojoGoalExecutionHandlerTest.java | 40 ++++++++++--------- 9 files changed, 67 insertions(+), 55 deletions(-) diff --git a/maven-extension/build.gradle.kts b/maven-extension/build.gradle.kts index e27feee05..393ce187a 100644 --- a/maven-extension/build.gradle.kts +++ b/maven-extension/build.gradle.kts @@ -20,7 +20,8 @@ dependencies { implementation("io.opentelemetry:opentelemetry-sdk-trace") implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") - implementation("io.opentelemetry:opentelemetry-semconv") + implementation("io.opentelemetry.semconv:opentelemetry-semconv") + implementation("io.opentelemetry.semconv:opentelemetry-semconv-incubating:1.25.0-alpha") implementation("io.opentelemetry:opentelemetry-exporter-otlp") annotationProcessor("com.google.auto.value:auto-value") @@ -59,10 +60,3 @@ tasks { } tasks.getByName("test").dependsOn("shadowJar") - -configurations.all { - resolutionStrategy { - // TODO this module still needs to be updated to the latest semconv - force("io.opentelemetry:opentelemetry-semconv:1.28.0-alpha") - } -} diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/handler/GoogleJibBuildHandler.java b/maven-extension/src/main/java/io/opentelemetry/maven/handler/GoogleJibBuildHandler.java index 6e67790f7..5868b4085 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/handler/GoogleJibBuildHandler.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/handler/GoogleJibBuildHandler.java @@ -9,7 +9,9 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.maven.MavenGoal; import io.opentelemetry.maven.semconv.MavenOtelSemanticAttributes; -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import io.opentelemetry.semconv.HttpAttributes; +import io.opentelemetry.semconv.UrlAttributes; +import io.opentelemetry.semconv.incubating.PeerIncubatingAttributes; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -86,11 +88,11 @@ public void enrichSpan(SpanBuilder spanBuilder, ExecutionEvent executionEvent) { spanBuilder.setAttribute( MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL, "https://" + registryHostname); - spanBuilder.setAttribute(SemanticAttributes.HTTP_URL, "https://" + registryHostname); - spanBuilder.setAttribute(SemanticAttributes.HTTP_METHOD, "POST"); + spanBuilder.setAttribute(UrlAttributes.URL_FULL, "https://" + registryHostname); + spanBuilder.setAttribute(HttpAttributes.HTTP_REQUEST_METHOD, "POST"); // Note: setting the "peer.service" helps visualization on Jaeger but // may not fully comply with the OTel "peer.service" spec as we don't know if the remote // service will be instrumented and what it "service.name" would be - spanBuilder.setAttribute(SemanticAttributes.PEER_SERVICE, registryHostname); + spanBuilder.setAttribute(PeerIncubatingAttributes.PEER_SERVICE, registryHostname); } } diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/handler/MavenDeployHandler.java b/maven-extension/src/main/java/io/opentelemetry/maven/handler/MavenDeployHandler.java index 20edc3084..031fdc7b0 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/handler/MavenDeployHandler.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/handler/MavenDeployHandler.java @@ -9,7 +9,9 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.maven.MavenGoal; import io.opentelemetry.maven.semconv.MavenOtelSemanticAttributes; -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import io.opentelemetry.semconv.HttpAttributes; +import io.opentelemetry.semconv.UrlAttributes; +import io.opentelemetry.semconv.incubating.PeerIncubatingAttributes; import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; @@ -53,7 +55,7 @@ public void enrichSpan(SpanBuilder spanBuilder, ExecutionEvent execution) { // may not fully comply with the OTel "peer.service" spec as we don't know if the remote // service will be instrumented and what it "service.name" would be spanBuilder.setAttribute( - SemanticAttributes.PEER_SERVICE, new URL(artifactRepositoryUrl).getHost()); + PeerIncubatingAttributes.PEER_SERVICE, new URL(artifactRepositoryUrl).getHost()); } catch (MalformedURLException e) { logger.debug("Ignore exception parsing artifact repository URL", e); } @@ -68,8 +70,8 @@ public void enrichSpan(SpanBuilder spanBuilder, ExecutionEvent execution) { + artifact.getArtifactId() + '/' + artifact.getVersion(); - spanBuilder.setAttribute(SemanticAttributes.HTTP_URL, artifactRootUrl); - spanBuilder.setAttribute(SemanticAttributes.HTTP_METHOD, "POST"); + spanBuilder.setAttribute(UrlAttributes.URL_FULL, artifactRootUrl); + spanBuilder.setAttribute(HttpAttributes.HTTP_REQUEST_METHOD, "POST"); } } diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykMonitorHandler.java b/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykMonitorHandler.java index 36bb03cde..8cd350401 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykMonitorHandler.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykMonitorHandler.java @@ -8,7 +8,10 @@ import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.maven.MavenGoal; -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import io.opentelemetry.semconv.HttpAttributes; +import io.opentelemetry.semconv.UrlAttributes; +import io.opentelemetry.semconv.incubating.PeerIncubatingAttributes; +import io.opentelemetry.semconv.incubating.RpcIncubatingAttributes; import java.util.Collections; import java.util.List; import org.apache.maven.execution.ExecutionEvent; @@ -24,10 +27,10 @@ final class SnykMonitorHandler implements MojoGoalExecutionHandler { @Override public void enrichSpan(SpanBuilder spanBuilder, ExecutionEvent executionEvent) { spanBuilder.setSpanKind(SpanKind.CLIENT); - spanBuilder.setAttribute(SemanticAttributes.PEER_SERVICE, "snyk.io"); - spanBuilder.setAttribute(SemanticAttributes.HTTP_URL, "https://snyk.io/api/v1/monitor/maven"); - spanBuilder.setAttribute(SemanticAttributes.RPC_METHOD, "monitor"); - spanBuilder.setAttribute(SemanticAttributes.HTTP_METHOD, "POST"); + spanBuilder.setAttribute(PeerIncubatingAttributes.PEER_SERVICE, "snyk.io"); + spanBuilder.setAttribute(UrlAttributes.URL_FULL, "https://snyk.io/api/v1/monitor/maven"); + spanBuilder.setAttribute(RpcIncubatingAttributes.RPC_METHOD, "monitor"); + spanBuilder.setAttribute(HttpAttributes.HTTP_REQUEST_METHOD, "POST"); } @Override diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykTestHandler.java b/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykTestHandler.java index fa7544a01..37a38c61e 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykTestHandler.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/handler/SnykTestHandler.java @@ -8,7 +8,10 @@ import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.maven.MavenGoal; -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import io.opentelemetry.semconv.HttpAttributes; +import io.opentelemetry.semconv.UrlAttributes; +import io.opentelemetry.semconv.incubating.PeerIncubatingAttributes; +import io.opentelemetry.semconv.incubating.RpcIncubatingAttributes; import java.util.Collections; import java.util.List; import org.apache.maven.execution.ExecutionEvent; @@ -24,10 +27,10 @@ final class SnykTestHandler implements MojoGoalExecutionHandler { @Override public void enrichSpan(SpanBuilder spanBuilder, ExecutionEvent executionEvent) { spanBuilder.setSpanKind(SpanKind.CLIENT); - spanBuilder.setAttribute(SemanticAttributes.PEER_SERVICE, "snyk.io"); - spanBuilder.setAttribute(SemanticAttributes.HTTP_URL, "https://snyk.io/api/v1/test-dep-graph"); - spanBuilder.setAttribute(SemanticAttributes.RPC_METHOD, "test"); - spanBuilder.setAttribute(SemanticAttributes.HTTP_METHOD, "POST"); + spanBuilder.setAttribute(PeerIncubatingAttributes.PEER_SERVICE, "snyk.io"); + spanBuilder.setAttribute(UrlAttributes.URL_FULL, "https://snyk.io/api/v1/test-dep-graph"); + spanBuilder.setAttribute(RpcIncubatingAttributes.RPC_METHOD, "test"); + spanBuilder.setAttribute(HttpAttributes.HTTP_REQUEST_METHOD, "POST"); } @Override diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/handler/SpringBootBuildImageHandler.java b/maven-extension/src/main/java/io/opentelemetry/maven/handler/SpringBootBuildImageHandler.java index 3bfabfe01..f63d07d09 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/handler/SpringBootBuildImageHandler.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/handler/SpringBootBuildImageHandler.java @@ -9,7 +9,9 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.maven.MavenGoal; import io.opentelemetry.maven.semconv.MavenOtelSemanticAttributes; -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import io.opentelemetry.semconv.HttpAttributes; +import io.opentelemetry.semconv.UrlAttributes; +import io.opentelemetry.semconv.incubating.PeerIncubatingAttributes; import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; @@ -99,14 +101,14 @@ public void enrichSpan(SpanBuilder spanBuilder, ExecutionEvent executionEvent) { && (registryUrl.startsWith("http://") || registryUrl.startsWith("https://"))) { spanBuilder.setAttribute( MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL, registryUrl); - spanBuilder.setAttribute(SemanticAttributes.HTTP_URL, registryUrl); - spanBuilder.setAttribute(SemanticAttributes.HTTP_METHOD, "POST"); + spanBuilder.setAttribute(UrlAttributes.URL_FULL, registryUrl); + spanBuilder.setAttribute(HttpAttributes.HTTP_REQUEST_METHOD, "POST"); try { // Note: setting the "peer.service" helps visualization on Jaeger but // may not fully comply with the OTel "peer.service" spec as we don't know if the remote // service will be instrumented and what it "service.name" would be spanBuilder.setAttribute( - SemanticAttributes.PEER_SERVICE, new URL(registryUrl).getHost()); + PeerIncubatingAttributes.PEER_SERVICE, new URL(registryUrl).getHost()); } catch (MalformedURLException e) { logger.debug("Ignore exception parsing container registry URL", e); } diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/resources/MavenResourceProvider.java b/maven-extension/src/main/java/io/opentelemetry/maven/resources/MavenResourceProvider.java index 48fe6d102..2fe6989d5 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/resources/MavenResourceProvider.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/resources/MavenResourceProvider.java @@ -9,7 +9,7 @@ import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; import io.opentelemetry.sdk.resources.Resource; -import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; +import io.opentelemetry.semconv.ServiceAttributes; import org.apache.maven.rtinfo.RuntimeInformation; import org.apache.maven.rtinfo.internal.DefaultRuntimeInformation; @@ -20,8 +20,8 @@ public Resource createResource(ConfigProperties config) { // Maven Plexus Launcher RuntimeInformation runtimeInformation = new DefaultRuntimeInformation(); return Resource.builder() - .put(ResourceAttributes.SERVICE_NAME, MavenOtelSemanticAttributes.SERVICE_NAME_VALUE) - .put(ResourceAttributes.SERVICE_VERSION, runtimeInformation.getMavenVersion()) + .put(ServiceAttributes.SERVICE_NAME, MavenOtelSemanticAttributes.SERVICE_NAME_VALUE) + .put(ServiceAttributes.SERVICE_VERSION, runtimeInformation.getMavenVersion()) .build(); } } diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/semconv/MavenOtelSemanticAttributes.java b/maven-extension/src/main/java/io/opentelemetry/maven/semconv/MavenOtelSemanticAttributes.java index cee1d5fc4..a9599a0a8 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/semconv/MavenOtelSemanticAttributes.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/semconv/MavenOtelSemanticAttributes.java @@ -9,22 +9,24 @@ import static io.opentelemetry.api.common.AttributeKey.stringKey; import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; +import io.opentelemetry.semconv.incubating.ContainerIncubatingAttributes; import java.util.List; /** * Semantic attributes for Maven executions. * * @see io.opentelemetry.api.common.Attributes - * @see io.opentelemetry.semconv.trace.attributes.SemanticAttributes */ public class MavenOtelSemanticAttributes { - /** See {@link ResourceAttributes#CONTAINER_IMAGE_NAME} */ + /** + * See {@link + * io.opentelemetry.semconv.incubating.ContainerIncubatingAttributes#CONTAINER_IMAGE_NAME} + */ public static final AttributeKey MAVEN_BUILD_CONTAINER_IMAGE_NAME = stringKey("maven.build.container.image.name"); - /** See {@link ResourceAttributes#CONTAINER_IMAGE_TAG} */ + /** See {@link ContainerIncubatingAttributes#CONTAINER_IMAGE_TAGS} */ public static final AttributeKey> MAVEN_BUILD_CONTAINER_IMAGE_TAGS = stringArrayKey("maven.build.container.image.tags"); diff --git a/maven-extension/src/test/java/io/opentelemetry/maven/handler/MojoGoalExecutionHandlerTest.java b/maven-extension/src/test/java/io/opentelemetry/maven/handler/MojoGoalExecutionHandlerTest.java index 3680df325..b193ccdb5 100644 --- a/maven-extension/src/test/java/io/opentelemetry/maven/handler/MojoGoalExecutionHandlerTest.java +++ b/maven-extension/src/test/java/io/opentelemetry/maven/handler/MojoGoalExecutionHandlerTest.java @@ -13,7 +13,10 @@ import io.opentelemetry.maven.semconv.MavenOtelSemanticAttributes; import io.opentelemetry.sdk.trace.ReadableSpan; import io.opentelemetry.sdk.trace.SdkTracerProvider; -import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import io.opentelemetry.semconv.HttpAttributes; +import io.opentelemetry.semconv.UrlAttributes; +import io.opentelemetry.semconv.incubating.PeerIncubatingAttributes; +import io.opentelemetry.semconv.incubating.RpcIncubatingAttributes; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; @@ -75,13 +78,14 @@ public void testMavenDeploy() throws Exception { assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_REPOSITORY_ID)) .isEqualTo("snapshots"); - assertThat(span.getAttribute(SemanticAttributes.HTTP_METHOD)).isEqualTo("POST"); - assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)) + assertThat(span.getAttribute(HttpAttributes.HTTP_REQUEST_METHOD)).isEqualTo("POST"); + assertThat(span.getAttribute(UrlAttributes.URL_FULL)) .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.getAttribute(PeerIncubatingAttributes.PEER_SERVICE)) + .isEqualTo("maven.example.com"); assertThat(span.getKind()).isEqualTo(SpanKind.CLIENT); } @@ -124,8 +128,8 @@ public void testSpringBootBuildImage_springboot_1() throws Exception { 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(UrlAttributes.URL_FULL)).isEqualTo("https://docker.io"); + assertThat(span.getAttribute(PeerIncubatingAttributes.PEER_SERVICE)).isEqualTo("docker.io"); assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL)) .isEqualTo("https://docker.io"); } @@ -168,8 +172,8 @@ public void testSpringBootBuildImage_springboot_2() throws Exception { assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_IMAGE_TAGS)) .isEqualTo(Collections.singletonList("${project.version}")); - assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)).isEqualTo("https://docker.io"); - assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("docker.io"); + assertThat(span.getAttribute(UrlAttributes.URL_FULL)).isEqualTo("https://docker.io"); + assertThat(span.getAttribute(PeerIncubatingAttributes.PEER_SERVICE)).isEqualTo("docker.io"); assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL)) .isEqualTo("https://docker.io"); } @@ -209,8 +213,8 @@ public void testGoogleJibBuild_jib_1() throws Exception { assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_IMAGE_TAGS)) .isEqualTo(Arrays.asList("latest", "${project.version}")); - assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)).isEqualTo("https://docker.io"); - assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("docker.io"); + assertThat(span.getAttribute(UrlAttributes.URL_FULL)).isEqualTo("https://docker.io"); + assertThat(span.getAttribute(PeerIncubatingAttributes.PEER_SERVICE)).isEqualTo("docker.io"); assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL)) .isEqualTo("https://docker.io"); } @@ -250,8 +254,8 @@ public void testGoogleJibBuild_jib_2() throws Exception { assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_IMAGE_TAGS)) .isEqualTo(Collections.singletonList("1.0-SNAPSHOT")); - assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)).isEqualTo("https://gcr.io"); - assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("gcr.io"); + assertThat(span.getAttribute(UrlAttributes.URL_FULL)).isEqualTo("https://gcr.io"); + assertThat(span.getAttribute(PeerIncubatingAttributes.PEER_SERVICE)).isEqualTo("gcr.io"); assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL)) .isEqualTo("https://gcr.io"); } @@ -286,10 +290,10 @@ public void testSnykTest_snyk_1() throws Exception { assertThat(span.getKind()).isEqualTo(SpanKind.CLIENT); - assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)) + assertThat(span.getAttribute(UrlAttributes.URL_FULL)) .isEqualTo("https://snyk.io/api/v1/test-dep-graph"); - assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("snyk.io"); - assertThat(span.getAttribute(SemanticAttributes.RPC_METHOD)).isEqualTo("test"); + assertThat(span.getAttribute(PeerIncubatingAttributes.PEER_SERVICE)).isEqualTo("snyk.io"); + assertThat(span.getAttribute(RpcIncubatingAttributes.RPC_METHOD)).isEqualTo("test"); } } @@ -322,10 +326,10 @@ public void testSnykMonitor_snyk_1() throws Exception { assertThat(span.getKind()).isEqualTo(SpanKind.CLIENT); - assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)) + assertThat(span.getAttribute(UrlAttributes.URL_FULL)) .isEqualTo("https://snyk.io/api/v1/monitor/maven"); - assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("snyk.io"); - assertThat(span.getAttribute(SemanticAttributes.RPC_METHOD)).isEqualTo("monitor"); + assertThat(span.getAttribute(PeerIncubatingAttributes.PEER_SERVICE)).isEqualTo("snyk.io"); + assertThat(span.getAttribute(RpcIncubatingAttributes.RPC_METHOD)).isEqualTo("monitor"); } }