Skip to content

Commit

Permalink
Refactor method for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-richardson committed Sep 10, 2024
1 parent e8665bf commit f476067
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public boolean isReady() {
}

@Override
public Span getParentSpan(String buildId) {
public Span getOrCreateParentSpan(String buildId) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public interface OTELHelper {
boolean isReady();

Span getParentSpan(String buildId);
Span getOrCreateParentSpan(String buildId);

Span createSpan(String spanName, Span parentSpan);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f476067

Please sign in to comment.