Skip to content

Commit

Permalink
Merge branch '1.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed May 4, 2023
2 parents 6c8384e + 3bece1e commit 6e1eb95
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,48 @@ void should_create_parent_child_relationship_via_observations() {
then(childFinishedSpan.getParentId()).isEqualTo(parentFinishedSpan.getSpanId());
}

@Test
void should_create_parent_child_relationship_via_observations_and_manual_spans() {
TestObservationRegistry registry = TestObservationRegistry.create();
registry.observationConfig().observationHandler(handler);

Span surprise = null;
Observation parent = Observation.start("parent", registry);
try (Observation.Scope scope = parent.openScope()) {
surprise = tracer.nextSpan().name("surprise").start();
try (Tracer.SpanInScope scope2 = tracer.withSpan(surprise)) {
Observation child = Observation.createNotStarted("child", registry).start();
child.scoped(() -> {
Observation grandchild = Observation.createNotStarted("grandchild", registry)
.parentObservation(child)
.start();
grandchild.stop();
});
child.stop();
}
surprise.end();
}
parent.stop();

List<FinishedSpan> spans = testSpanHandler.spans()
.stream()
.map(BraveFinishedSpan::fromBrave)
.collect(Collectors.toList());
SpansAssert.then(spans).haveSameTraceId();
FinishedSpan grandchildFinishedSpan = spans.get(0);
SpanAssert.then(grandchildFinishedSpan).hasNameEqualTo("grandchild");
FinishedSpan childFinishedSpan = spans.get(1);
SpanAssert.then(childFinishedSpan).hasNameEqualTo("child");
FinishedSpan surpriseSpan = spans.get(2);
SpanAssert.then(surpriseSpan).hasNameEqualTo("surprise");
FinishedSpan parentFinishedSpan = spans.get(3);
SpanAssert.then(parentFinishedSpan).hasNameEqualTo("parent");

then(grandchildFinishedSpan.getParentId()).isEqualTo(childFinishedSpan.getSpanId());
then(childFinishedSpan.getParentId()).isEqualTo(surprise.context().spanId());
then(surprise.context().parentId()).isEqualTo(parentFinishedSpan.getSpanId());
}

@Test
void should_use_contextual_name() {
Observation.Context context = new Observation.Context();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,48 @@ void should_create_parent_child_relationship_via_observations() {
then(childFinishedSpan.getParentId()).isEqualTo(parentFinishedSpan.getSpanId());
}

@Test
void should_create_parent_child_relationship_via_observations_and_manual_spans() {
TestObservationRegistry registry = TestObservationRegistry.create();
registry.observationConfig().observationHandler(handler);

Span surprise = null;
Observation parent = Observation.start("parent", registry);
try (Observation.Scope scope = parent.openScope()) {
surprise = tracer.nextSpan().name("surprise").start();
try (Tracer.SpanInScope scope2 = tracer.withSpan(surprise)) {
Observation child = Observation.createNotStarted("child", registry).start();
child.scoped(() -> {
Observation grandchild = Observation.createNotStarted("grandchild", registry)
.parentObservation(child)
.start();
grandchild.stop();
});
child.stop();
}
surprise.end();
}
parent.stop();

List<FinishedSpan> spans = testSpanProcessor.spans()
.stream()
.map(OtelFinishedSpan::fromOtel)
.collect(Collectors.toList());
SpansAssert.then(spans).haveSameTraceId();
FinishedSpan grandchildFinishedSpan = spans.get(0);
SpanAssert.then(grandchildFinishedSpan).hasNameEqualTo("grandchild");
FinishedSpan childFinishedSpan = spans.get(1);
SpanAssert.then(childFinishedSpan).hasNameEqualTo("child");
FinishedSpan surpriseSpan = spans.get(2);
SpanAssert.then(surpriseSpan).hasNameEqualTo("surprise");
FinishedSpan parentFinishedSpan = spans.get(3);
SpanAssert.then(parentFinishedSpan).hasNameEqualTo("parent");

then(grandchildFinishedSpan.getParentId()).isEqualTo(childFinishedSpan.getSpanId());
then(childFinishedSpan.getParentId()).isEqualTo(surprise.context().spanId());
then(surprise.context().parentId()).isEqualTo(parentFinishedSpan.getSpanId());
}

@Test
void should_use_contextual_name() {
Observation.Context context = new Observation.Context();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,22 @@ default void onScopeClosed(T context) {
default Span getParentSpan(Observation.ContextView context) {
// This would mean that the user has manually created a tracing context
TracingContext tracingContext = context.get(TracingContext.class);
Span currentSpan = getTracer().currentSpan();
if (tracingContext == null) {
ObservationView observation = context.getParentObservation();
if (observation != null) {
tracingContext = observation.getContextView().get(TracingContext.class);
if (tracingContext != null) {
return tracingContext.getSpan();
Span spanFromParentObservation = tracingContext.getSpan();
if (spanFromParentObservation == null && currentSpan != null) {
return currentSpan;
}
else if (currentSpan != null && !currentSpan.equals(spanFromParentObservation)) {
// User manually created a span
return currentSpan;
}
// No manually created span
return spanFromParentObservation;
}
}
}
Expand Down

0 comments on commit 6e1eb95

Please sign in to comment.