Skip to content

Commit

Permalink
support Maven Parallel Builds (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille-leclerc authored Jan 3, 2022
1 parent 4281833 commit 8cf7160
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void mojoStarted(ExecutionEvent executionEvent) {
MavenOtelSemanticAttributes.MAVEN_EXECUTION_LIFECYCLE_PHASE,
mojoExecution.getLifecyclePhase())
.startSpan();
spanRegistry.putSpan(span, mojoExecution);
spanRegistry.putSpan(span, mojoExecution, executionEvent.getProject());
}

@Override
Expand All @@ -216,8 +216,11 @@ public void mojoSucceeded(ExecutionEvent executionEvent) {
return;
}
MojoExecution mojoExecution = executionEvent.getMojoExecution();
logger.debug("OpenTelemetry: End succeeded mojo execution span: {}", mojoExecution);
Span mojoExecutionSpan = spanRegistry.removeSpan(mojoExecution);
logger.debug(
"OpenTelemetry: End succeeded mojo execution span: {}, {}",
mojoExecution,
executionEvent.getProject());
Span mojoExecutionSpan = spanRegistry.removeSpan(mojoExecution, executionEvent.getProject());
mojoExecutionSpan.setStatus(StatusCode.OK);

mojoExecutionSpan.end();
Expand All @@ -229,8 +232,11 @@ public void mojoFailed(ExecutionEvent executionEvent) {
return;
}
MojoExecution mojoExecution = executionEvent.getMojoExecution();
logger.debug("OpenTelemetry: End failed mojo execution span: {}", mojoExecution);
Span mojoExecutionSpan = spanRegistry.removeSpan(mojoExecution);
logger.debug(
"OpenTelemetry: End failed mojo execution span: {}, {}",
mojoExecution,
executionEvent.getProject());
Span mojoExecutionSpan = spanRegistry.removeSpan(mojoExecution, executionEvent.getProject());
mojoExecutionSpan.setStatus(StatusCode.ERROR, "Mojo Failed"); // TODO verify description
mojoExecutionSpan.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Hold the state of the spans in progress */
@Component(role = SpanRegistry.class)
public final class SpanRegistry {

private static final Logger logger = LoggerFactory.getLogger(SpanRegistry.class);

private final Map<MojoExecutionKey, Span> mojoExecutionKeySpanMap = new ConcurrentHashMap<>();
private final Map<MavenProjectKey, Span> mavenProjectKeySpanMap = new ConcurrentHashMap<>();
@Nullable private Span rootSpan;
Expand All @@ -38,6 +42,7 @@ public void setRootSpan(Span rootSpan) {
}

public Span getSpan(MavenProject mavenProject) {
logger.debug("OpenTelemetry: getSpan({}, {})", mavenProject, Thread.currentThread());
final MavenProjectKey key = MavenProjectKey.fromMavenProject(mavenProject);
final Span span = this.mavenProjectKeySpanMap.get(key);
if (span == null) {
Expand Down Expand Up @@ -74,22 +79,27 @@ public Span removeRootSpan() {
}

public void putSpan(Span span, MavenProject mavenProject) {
logger.debug("OpenTelemetry: putSpan({}, {})", mavenProject, Thread.currentThread());
MavenProjectKey key = MavenProjectKey.fromMavenProject(mavenProject);
Span previousSpanForKey = mavenProjectKeySpanMap.put(key, span);
if (previousSpanForKey != null) {
throw new IllegalStateException("A span has already been started for " + mavenProject);
}
}

public void putSpan(Span span, MojoExecution mojoExecution) {
MojoExecutionKey key = MojoExecutionKey.fromMojoExecution(mojoExecution);
public void putSpan(Span span, MojoExecution mojoExecution, MavenProject project) {
logger.debug(
"OpenTelemetry: putSpan({}, {}, {})", mojoExecution, project, Thread.currentThread());
MojoExecutionKey key = MojoExecutionKey.fromMojoExecution(mojoExecution, project);
Span previousSpanForKey = mojoExecutionKeySpanMap.put(key, span);
if (previousSpanForKey != null) {
throw new IllegalStateException("A span has already been started for " + mojoExecution);
throw new IllegalStateException(
"A span has already been started for " + mojoExecution + ", " + project);
}
}

public Span removeSpan(MavenProject mavenProject) {
logger.debug("OpenTelemetry: removeSpan({}, {})", mavenProject, Thread.currentThread());
MavenProjectKey key = MavenProjectKey.fromMavenProject(mavenProject);
Span span = mavenProjectKeySpanMap.remove(key);
if (span == null) {
Expand All @@ -99,11 +109,13 @@ public Span removeSpan(MavenProject mavenProject) {
}

@Nonnull
public Span removeSpan(MojoExecution mojoExecution) {
MojoExecutionKey key = MojoExecutionKey.fromMojoExecution(mojoExecution);
public Span removeSpan(MojoExecution mojoExecution, MavenProject project) {
logger.debug(
"OpenTelemetry: removeSpan({}, {}, {})", mojoExecution, project, Thread.currentThread());
MojoExecutionKey key = MojoExecutionKey.fromMojoExecution(mojoExecution, project);
Span span = mojoExecutionKeySpanMap.remove(key);
if (span == null) {
throw new IllegalStateException("No span found for " + mojoExecution);
throw new IllegalStateException("No span found for " + mojoExecution + " " + project);
}
return span;
}
Expand All @@ -114,9 +126,11 @@ abstract static class MavenProjectKey {

abstract String artifactId();

public static MavenProjectKey fromMavenProject(@Nonnull MavenProject mavenProject) {
abstract String version();

public static MavenProjectKey fromMavenProject(@Nonnull MavenProject project) {
return new AutoValue_SpanRegistry_MavenProjectKey(
mavenProject.getGroupId(), mavenProject.getArtifactId());
project.getGroupId(), project.getArtifactId(), project.getVersion());
}
}

Expand All @@ -134,7 +148,9 @@ abstract static class MojoExecutionKey {

abstract String pluginArtifactId();

static MojoExecutionKey fromMojoExecution(MojoExecution mojoExecution) {
abstract MavenProjectKey projectKey();

static MojoExecutionKey fromMojoExecution(MojoExecution mojoExecution, MavenProject project) {
if (mojoExecution == null) {
throw new NullPointerException("Given MojoExecution is null");
}
Expand All @@ -149,7 +165,8 @@ static MojoExecutionKey fromMojoExecution(MojoExecution mojoExecution) {
mojoExecution.getGroupId(),
mojoExecution.getArtifactId(),
plugin.getGroupId(),
plugin.getArtifactId());
plugin.getArtifactId(),
MavenProjectKey.fromMavenProject(project));
}
}
}

0 comments on commit 8cf7160

Please sign in to comment.