Johjen K Mathew
Johjen K Mathew

Reputation: 19

Show Traces in Red colour for Bad Requests in Zipkin UI

I am using spring Boot Version 1.5.14.RELEASE with spring cloud sleuth zipkin. If I return a ResponseEntity setting its HttpStatus as BAD_REQUEST then I see the trace highlighted in Blue color. Is there a way to highlight the trace in Red color for a bad request with ResponseEntity object?

I explicitly threw a custom Exception for bad requests and saw the zipkin trace highlighted in Red color in Zipkin UI. But I don't want to do this as I am returning a body in ResponseEntity.

public  ResponseEntity<ResponseDto> saveRecord(Employee employee) {
    if(isValidated(employee)) {
        return new ResponseEntity<ResponseDto>(repo.save(employee), HttpStatus.OK);
    } else {
        return new ResponseEntity<ResponseDto>(service.handleErrorResponse(employee), HttpStatus.BAD_REQUEST);
    }
}

I expect the Zipkin trace to be highlighted in Red color as it is a bad request but the actual color is Blue.

Actual Trace

Expected Trace

Upvotes: 0

Views: 629

Answers (1)

Johjen K Mathew
Johjen K Mathew

Reputation: 19

I used an Aspect and from the returned ResponseEntity object, decided whether or not to programatically add an error tag to span. With this tag, zipkin will identify and highlight the trace in red colour. Below is the code snippet to add error tag to span.

import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
----
@Autowired
private Tracer tracer;

public void addErrorTag(String message) {
    Span currentSpan = tracer.getCurrentSpan();
    currentSpan.logEvent("ERROR: " + message);
    tracer.addTag("error", message);
}

Upvotes: 1

Related Questions