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

Allow for recording an error in spans without throwables #939

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -114,6 +114,12 @@ public Span error(Throwable throwable) {
return this;
}

@Override
public Span error(String message) {
this.delegate.tag("error", message);
return this;
}

@Override
public void end() {
this.delegate.finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import io.micrometer.tracing.Span;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.semconv.SemanticAttributes;

import java.time.Instant;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand All @@ -34,6 +36,8 @@
*/
public class OtelSpan implements Span {

private static final AttributeKey<String> ERROR_MESSAGE = AttributeKey.stringKey("error.message");

final io.opentelemetry.api.trace.Span delegate;

final OtelTraceContext otelTraceContext;
Expand Down Expand Up @@ -172,6 +176,14 @@ public Span error(Throwable throwable) {
return this;
}

@Override
public Span error(String message) {
Attributes attributes = Attributes.of(ERROR_MESSAGE, message);
this.delegate.setStatus(StatusCode.ERROR, message);
this.delegate.addEvent(message, attributes, Instant.now());
return this;
}

@Override
public void end() {
if (this.isStatusUnset()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ void should_set_status_to_error_when_recording_exception() {
then(poll.getEvents().get(0).getAttributes().asMap().values()).containsAnyOf("boom!");
}

@Test
void should_set_status_to_error_when_recording_error() {
OtelSpan otelSpan = new OtelSpan(otelTracer.spanBuilder("foo").startSpan());

otelSpan.error("boom!").end();

SpanData poll = arrayListSpanProcessor.spans().poll();
then(poll.getStatus()).isEqualTo(StatusData.create(StatusCode.ERROR, "boom!"));
then(poll.getEvents()).hasSize(1);
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public SimpleSpan error(Throwable throwable) {
return this;
}

@Override
public Span error(String message) {
return this;
}

@Override
public SimpleSpan remoteIpAndPort(String ip, int port) {
this.ip = ip;
Expand Down
12 changes: 12 additions & 0 deletions micrometer-tracing/src/main/java/io/micrometer/tracing/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public Span error(Throwable throwable) {
return this;
}

@Override
public Span error(String message) {
return this;
}

@Override
public void end() {

Expand Down Expand Up @@ -229,6 +234,13 @@ default Span tagOfBooleans(String key, List<Boolean> values) {
*/
Span error(Throwable throwable);

/**
* Records an error in this span.
* @param message to record
* @return this span
*/
Span error(String message);

/**
* Ends the span. The span gets stopped and recorded if not noop.
*/
Expand Down