Skip to content

Commit

Permalink
Fix lifecycle to support the Maven Daemon (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille-leclerc committed Jan 7, 2022
1 parent b7e290b commit 4c2ee90
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Hold the state of the spans in progress */
/**
* Hold the state of the spans in progress.
*
* <p>As Maven processes, including the <a href="https://github.com/apache/maven-mvnd">Maven
* Daemon</a>, can't execute multiple builds concurrently, there is no need to differentiate spans
* per {@link org.apache.maven.execution.MavenSession}.
*/
@Component(role = SpanRegistry.class)
public final class SpanRegistry {

Expand Down Expand Up @@ -75,6 +81,7 @@ public Span removeRootSpan() {
.map(MojoExecutionKey::toString)
.collect(Collectors.joining(", ")));
}
this.rootSpan = null;
return rootSpan;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.maven;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import org.junit.jupiter.api.Test;

public class SpanRegistryTest {

/** MVND reuses the same Maven process and thus the Span Registry is reused. */
@Test
public void testSpanRegistryReuseWhenUsingMvnDaemon() {
SpanRegistry spanRegistry = new SpanRegistry();

Tracer tracer = OpenTelemetry.noop().getTracer("test");
Span firstRootSpan = tracer.spanBuilder("com.example:my-jar-1.1.0-SNAPSHOT").startSpan();
spanRegistry.setRootSpan(firstRootSpan);
Span firstRemovedRootSpan = spanRegistry.removeRootSpan();
assertThat(firstRemovedRootSpan).isEqualTo(firstRootSpan);

Span secondRootSpan = tracer.spanBuilder("com.example:my-jar-2.1.0-SNAPSHOT").startSpan();
spanRegistry.setRootSpan(secondRootSpan);
Span secondRemovedSpan = spanRegistry.removeRootSpan();
assertThat(secondRemovedSpan).isEqualTo(secondRootSpan);
}
}

0 comments on commit 4c2ee90

Please sign in to comment.