Skip to content

Commit

Permalink
Fixed equality check on OtelSpan; fixes gh-262
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed May 15, 2023
1 parent 8ae7d6e commit 9df9a37
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ public boolean equals(Object o) {
return false;
}
OtelSpan otelSpan = (OtelSpan) o;
return Objects.equals(this.delegate, otelSpan.delegate);
io.opentelemetry.api.trace.Span span = otelSpan.delegate;
if (span instanceof SpanFromSpanContext) {
span = ((SpanFromSpanContext) span).span;
}
return Objects.equals(this.delegate, span);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package io.micrometer.tracing.otel.bridge;

import java.time.Instant;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import io.micrometer.common.lang.Nullable;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;

import java.time.Instant;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

class SpanFromSpanContext implements io.opentelemetry.api.trace.Span {

final io.opentelemetry.api.trace.Span span;
Expand Down Expand Up @@ -166,11 +166,18 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (!(o instanceof io.opentelemetry.api.trace.Span)) {
return false;
}
SpanFromSpanContext that = (SpanFromSpanContext) o;
return Objects.equals(span, that.span) && Objects.equals(this.newSpanContext, that.newSpanContext);
io.opentelemetry.api.trace.Span that = (io.opentelemetry.api.trace.Span) o;
boolean spansEqual = Objects.equals(span, that);
if (!spansEqual) {
return false;
}
if (getClass() == o.getClass()) {
return Objects.equals(this.newSpanContext, ((SpanFromSpanContext) that).newSpanContext);
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.tracing.otel.bridge;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
Expand All @@ -28,18 +29,22 @@

class OtelSpanTests {

ArrayListSpanProcessor arrayListSpanProcessor = new ArrayListSpanProcessor();

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn())
.addSpanProcessor(arrayListSpanProcessor)
.build();

OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setPropagators(ContextPropagators.create(B3Propagator.injectingSingleHeader()))
.build();

io.opentelemetry.api.trace.Tracer otelTracer = openTelemetrySdk.getTracer("io.micrometer.micrometer-tracing");

@Test
void should_set_status_to_error_when_recording_exception() {
ArrayListSpanProcessor arrayListSpanProcessor = new ArrayListSpanProcessor();
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn())
.addSpanProcessor(arrayListSpanProcessor)
.build();
OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setPropagators(ContextPropagators.create(B3Propagator.injectingSingleHeader()))
.build();
io.opentelemetry.api.trace.Tracer otelTracer = openTelemetrySdk.getTracer("io.micrometer.micrometer-tracing");
OtelSpan otelSpan = new OtelSpan(otelTracer.spanBuilder("foo").startSpan());

otelSpan.error(new RuntimeException("boom!")).end();
Expand All @@ -50,4 +55,15 @@ void should_set_status_to_error_when_recording_exception() {
then(poll.getEvents().get(0).getAttributes().asMap().values()).containsAnyOf("boom!");
}

@Test
void should_be_equal_when_two_span_delegates_are_equal() {
Span span = otelTracer.spanBuilder("foo").startSpan();
OtelSpan otelSpan = new OtelSpan(span);
OtelSpan otelSpanFromSpanContext = new OtelSpan(
new SpanFromSpanContext(span, null, new OtelTraceContext(span)));

then(otelSpan).isEqualTo(otelSpanFromSpanContext);
then(otelSpanFromSpanContext).isEqualTo(otelSpan);
}

}

0 comments on commit 9df9a37

Please sign in to comment.