2

We are looking at leveraging spring cloud sleuth for distributed tracing and we've worked on a POC. It seems like a great solution, works out of the box.

Had a follow up question though:

We use random UUID vs 64 bit ids as trace id. We understand that custom headers (A new trace Id for example) can be added along with sleuth headers but would it be possible to override the default trace id format for slueth? We have looked through the documentation and perhaps Propagation is the way to go. Can someone who has done this point us in the right direction and to some examples if possible. The help would be much appreciated.

We are using the latest release 2.0.1 which uses the brave library.

Any help/pointers would be greatly appreciated.

Thanks, GK

user10267174
  • 51
  • 2
  • 6

3 Answers3

1

Spring-sleuth doesn't provide a way to override the default ID's. According to OpenZipkin, 'Trace identifiers are 64 or 128-bit, but all span identifiers within a trace are 64-bit. All identifiers are opaque'

Refer this: https://github.com/openzipkin/b3-propagation#identifiers

So, You can either put the generated request ID as Tag ('tag':'requestID') or You can place generate UID in a different field and use propagation technique. Refer ExtraFieldPropagationTest for reference. https://github.com/openzipkin/brave/blob/master/brave/src/test/java/brave/propagation/ExtraFieldPropagationTest.java

Nitin1706
  • 621
  • 1
  • 11
  • 21
1

Even though this is not possible (afaik), if your use case is to use custom headers for log correlation, all that's needed is setting these properties (Related SO Answer):

# To add request-id (to MDC?) via sleuth
spring.sleuth.baggage.correlation-fields=x-request-id
spring.sleuth.baggage.remote-fields=x-request-id

And then this can be used in your logging pattern:

%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{traceId:-},%X{spanId:-},%X{x-request-id:-}] [%thread] %logger{40} : %msg%n

Now along with the built-in traceId & spanId, the value of the header x-request-id will also be logged:

2022-06-28 19:55:40.071 WARN  [8add19deba73c0f3,cda65c8122e5e025,some-request-id] [reactor-http-epoll-8] c.i.p.g.s.MyService : My warn log

To make this more concise, you can skip traceId & spanId if not required. A better way could have been to use them as a fallback when your own custom correlation header is not available, but logback currently does not (and probably will not) support nesting default values for MDC.

aksh1618
  • 2,245
  • 18
  • 37
0

What you can do is to generate the id in a different field and propagate it further on. Check this part of the documentation https://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html#_propagating_extra_fields

52.1 Propagating extra fields Sometimes you need to propagate extra fields, such as a request ID or an alternate trace context. For example, if you are in a Cloud Foundry environment, you might want to pass the request ID, as shown in the following example:

// when you initialize the builder, define the extra field you want to propagate Tracing.newBuilder().propagationFactory(
ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-vcap-request-id") );

// later, you can tag that request ID or use it in log correlation requestId = ExtraFieldPropagation.get("x-vcap-request-id"); You may also need to propagate a trace context that you are not using. For example, you may be in an Amazon Web Services environment but not be reporting data to X-Ray. To ensure X-Ray can co-exist correctly, pass-through its tracing header, as shown in the following example:

tracingBuilder.propagationFactory(
ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-amzn-trace-id") ); [Tip] In Spring Cloud Sleuth all elements of the tracing builder Tracing.newBuilder() are defined as beans. So if you want to pass a custom PropagationFactory, it’s enough for you to create a bean of that type and we will set it in the Tracing bean.

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Hi Marcin. Thank you so much for responding. So as I understand, it wont be possible to override/configure the default format for trace id in Sleuth and propagating an alternate trace id is the only way to go? – user10267174 Aug 27 '18 at 01:40
  • I guess this is the best option to go with. You can use custom headers that would contain trace and span ids but at the end of the day you still would have to map it to longs. – Marcin Grzejszczak Aug 27 '18 at 05:06
  • Thank you Marcin. Appreciate your prompt response. – user10267174 Aug 27 '18 at 06:09
  • We have multiple microservices and plan to incorporate slueth for a few. Non-sleuth-enabled apps will be calling into slueth-enabled-apps and vice versa. We want to retain trace/span/parentspanIds across requests. How do we ensure that we manually generate a valid ids for apps that are not on slueth. What are the requirements of a slueth id? Is is just a 64 bit long or is it also encoded? What kind of id generation logic should we use to comply to sleuth standards? – user10267174 Oct 09 '18 at 08:33