Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[maven-extension] Emit a warning rather than failing the build with an exception on illegal state #776

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.maven;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -42,7 +43,11 @@ public final class SpanRegistry {
*/
public void setRootSpan(Span rootSpan) {
if (this.rootSpan != null) {
throw new IllegalStateException("Root span already defined " + this.rootSpan);
logger.warn(
"Root span already defined "
+ this.rootSpan
+ ", can't overwrite root span with "
+ rootSpan);
}
this.rootSpan = rootSpan;
}
Expand All @@ -52,30 +57,38 @@ public Span getSpan(MavenProject mavenProject) {
MavenProjectKey key = MavenProjectKey.fromMavenProject(mavenProject);
Span span = this.mavenProjectKeySpanMap.get(key);
if (span == null) {
throw new IllegalStateException(
logger.warn(
"Span not started for project "
+ mavenProject.getGroupId()
+ ":"
+ mavenProject.getArtifactId());
return getInvalidSpan();
}
return span;
}

public Span getRootSpanNotNull() {
Span rootSpan = this.rootSpan;
if (rootSpan == null) {
throw new IllegalStateException("Root span not defined");
logger.warn("Root span not defined");
return getInvalidSpan();
}
return rootSpan;
}

private static Span getInvalidSpan() {
return OpenTelemetry.noop().getTracer("invalid").spanBuilder("invalid").startSpan();
cyrille-leclerc marked this conversation as resolved.
Show resolved Hide resolved
}

public Span removeRootSpan() {
Span rootSpan = this.rootSpan;
if (rootSpan == null) {
throw new IllegalStateException("Root span not defined");
logger.warn("Root span not defined");
return getInvalidSpan();
}
if (!this.mojoExecutionKeySpanMap.isEmpty()) {
throw new IllegalStateException(

logger.warn(
"Remaining children spans: "
+ this.mojoExecutionKeySpanMap.keySet().stream()
.map(MojoExecutionKey::toString)
Expand All @@ -90,7 +103,7 @@ public void putSpan(Span span, MavenProject mavenProject) {
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);
logger.warn("A span has already been started for " + mavenProject);
}
}

Expand All @@ -99,8 +112,7 @@ public void putSpan(Span span, MojoExecution mojoExecution, MavenProject project
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 + ", " + project);
logger.warn("A span has already been started for " + mojoExecution + ", " + project);
}
}

Expand All @@ -109,7 +121,8 @@ public Span removeSpan(MavenProject mavenProject) {
MavenProjectKey key = MavenProjectKey.fromMavenProject(mavenProject);
Span span = mavenProjectKeySpanMap.remove(key);
if (span == null) {
throw new IllegalStateException("No span found for " + mavenProject);
logger.warn("No span found for " + mavenProject);
return getInvalidSpan();
}
return span;
}
Expand All @@ -120,7 +133,8 @@ public Span removeSpan(MojoExecution mojoExecution, MavenProject project) {
MojoExecutionKey key = MojoExecutionKey.fromMojoExecution(mojoExecution, project);
Span span = mojoExecutionKeySpanMap.remove(key);
if (span == null) {
throw new IllegalStateException("No span found for " + mojoExecution + " " + project);
logger.warn("No span found for " + mojoExecution + " " + project);
return getInvalidSpan();
}
return span;
}
Expand Down