diff --git a/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/NullOTELHelperImpl.java b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/NullOTELHelperImpl.java index cd71082..27dc5d0 100644 --- a/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/NullOTELHelperImpl.java +++ b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/NullOTELHelperImpl.java @@ -9,7 +9,7 @@ public boolean isReady() { } @Override - public Span getParentSpan(String buildId) { + public Span getOrCreateParentSpan(String buildId) { return null; } diff --git a/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelper.java b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelper.java index d2abd39..934e8fd 100644 --- a/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelper.java +++ b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelper.java @@ -26,7 +26,7 @@ public interface OTELHelper { boolean isReady(); - Span getParentSpan(String buildId); + Span getOrCreateParentSpan(String buildId); Span createSpan(String spanName, Span parentSpan); diff --git a/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelperImpl.java b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelperImpl.java index bfa211a..ce31161 100644 --- a/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelperImpl.java +++ b/server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELHelperImpl.java @@ -46,19 +46,25 @@ public boolean isReady() { } @Override - public Span getParentSpan(String buildId) { + public Span getOrCreateParentSpan(String buildId) { return this.spanMap.computeIfAbsent(buildId, key -> this.tracer.spanBuilder(buildId).startSpan()); } @Override public Span createSpan(String spanName, Span parentSpan) { LOG.info("Creating child span " + spanName + " under parent " + parentSpan); - return this.spanMap.computeIfAbsent(spanName, key -> this.tracer.spanBuilder(spanName).setParent(Context.current().with(parentSpan)).startSpan()); + return this.spanMap.computeIfAbsent(spanName, key -> this.tracer + .spanBuilder(spanName) + .setParent(Context.current().with(parentSpan)) + .startSpan()); } @Override public Span createTransientSpan(String spanName, Span parentSpan, long startTime) { - return this.tracer.spanBuilder(spanName).setParent(Context.current().with(parentSpan)).setStartTimestamp(startTime, TimeUnit.MILLISECONDS).startSpan(); + return this.tracer.spanBuilder(spanName) + .setParent(Context.current().with(parentSpan)) + .setStartTimestamp(startTime, TimeUnit.MILLISECONDS) + .startSpan(); } @Override diff --git a/server/src/test/java/com/octopus/teamcity/opentelemetry/server/OTELHelperTest.java b/server/src/test/java/com/octopus/teamcity/opentelemetry/server/OTELHelperTest.java index 6a98e63..2d8095a 100644 --- a/server/src/test/java/com/octopus/teamcity/opentelemetry/server/OTELHelperTest.java +++ b/server/src/test/java/com/octopus/teamcity/opentelemetry/server/OTELHelperTest.java @@ -44,10 +44,10 @@ void isReadyTest() { } @Test - void getParentSpanShouldReturnABuildSpanAndBeAvailableInGetSpan(@Mock SRunningBuild build) { + void getParentSpanShouldReturnABuildSpanAndBeAvailableInGetOrCreateSpan(@Mock SRunningBuild build) { // Arrange String parentBuildId = String.valueOf(build.getBuildId()); - Span parentSpan = this.otelHelper.getParentSpan(parentBuildId); + Span parentSpan = this.otelHelper.getOrCreateParentSpan(parentBuildId); // Act Span buildSpan = this.otelHelper.getSpan(String.valueOf(build.getBuildId())); @@ -131,6 +131,6 @@ private Span createParentSpanForTest(SRunningBuild build) { BuildPromotion[] parentBuilds = build.getBuildPromotion().findTops(); BuildPromotion parentBuildPromotion = parentBuilds[0]; String parentBuildId = String.valueOf(parentBuildPromotion.getId()); - return this.otelHelper.getParentSpan(parentBuildId); + return this.otelHelper.getOrCreateParentSpan(parentBuildId); } } \ No newline at end of file diff --git a/server/src/test/java/com/octopus/teamcity/opentelemetry/server/TeamCityBuildListenerTest.java b/server/src/test/java/com/octopus/teamcity/opentelemetry/server/TeamCityBuildListenerTest.java index 7db1b24..e67e0e3 100644 --- a/server/src/test/java/com/octopus/teamcity/opentelemetry/server/TeamCityBuildListenerTest.java +++ b/server/src/test/java/com/octopus/teamcity/opentelemetry/server/TeamCityBuildListenerTest.java @@ -93,7 +93,7 @@ void buildStartedTriggeredWithAParentShouldCreateABuildSpanAndBeAvailableInGetSp // Act this.buildListener.buildStarted(build); - Span parentSpan = this.otelHelper.getParentSpan(String.valueOf(parentBuild.getBuildId())); + Span parentSpan = this.otelHelper.getOrCreateParentSpan(String.valueOf(parentBuild.getBuildId())); Span builtSpan = this.otelHelper.getSpan(String.valueOf(build.getBuildId())); Span expectedSpan = this.otelHelper.createSpan(String.valueOf(build.getBuildId()), parentSpan);