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

Add support for traceparent headers in DefaultRestClient #261

Closed
jandusil opened this issue Jan 23, 2024 · 3 comments · Fixed by #262
Closed

Add support for traceparent headers in DefaultRestClient #261

jandusil opened this issue Jan 23, 2024 · 3 comments · Fixed by #262
Assignees

Comments

@jandusil
Copy link
Contributor

No description provided.

@jandusil jandusil self-assigned this Jan 23, 2024
@jandusil jandusil changed the title Add support for traceable headers in DefaultRestClient Add support for traceparent headers in DefaultRestClient Jan 23, 2024
@jandusil
Copy link
Contributor Author

@jandusil
Copy link
Contributor Author

jandusil commented Jan 31, 2024

Some new observations - there are two tracing properties that are of interest for us:

management.tracing.sampling.probability - this property <0, 1> determines what percentage of traces is sent to the tracing backend (e. g. Zipkin). In other words, with this property we can edit the distributed tracing. When run locally the tracing works normally even when this property is set to 0.0. It is set to 0.1 by default.

management.tracing.enabled - with this property we can enable/disable all tracing. It is true by default.

@jandusil
Copy link
Contributor Author

jandusil commented Feb 1, 2024

Final testing report:
OK ✅

Based on a discussion with @zcgandcomp, I have tested the multi-threaded environment using JMeter to verify the correct propagation of traceId.

The test set up was as follows:

  1. Created a simple demo Client (akin to PAC - this client uses our DefaultRestClient in the same way PAC does).
  2. Created a simple demo Server (akin to PAS).
  3. Used JMeter to generate a random traceId for each request and added it to the request header as traceparent header.
import java.security.SecureRandom

// Generate a 16-byte (128-bit) array
byte[] bytes = new byte[16]
new SecureRandom().nextBytes(bytes)

// Convert bytes to hex string
def hexString = bytes.collect { String.format('%02x', it) }.join()

// Store the hex string in a JMeter variable
vars.put("TRACE_ID", hexString)
image
  1. The client's endpoint is invoked. The OpenTelemetry framework automatically picks up the traceparent header and sets it to the current tracing Span. This information is then propagated to the server endpoint call (handled in our DefaultRestClient with header injection).
  private ClientRequest addTraceparentHeader(final ClientRequest request) {
        final Span currentSpan = Span.current();
        if (currentSpan != null) {
            final SpanContext spanContext = currentSpan.getSpanContext();
            if (spanContext != null) {
                final String traceId = spanContext.getTraceId();
                /* The parentId of the next server Span will be the current spanId */
                final String parentId = spanContext.getSpanId();
                final TraceFlags traceFlags = spanContext.getTraceFlags();
                if (traceId != null && parentId != null && traceFlags != null) {
                    final String headerValue = String.format("00-%s-%s-%s",
                            traceId,
                            parentId,
                            traceFlags.asHex());
                    return ClientRequest.from(request)
                            .headers(headers -> headers.set(TRACEPARENT_HEADER_KEY, headerValue))
                            .build();
                }
            }
        }
        return request;
    }

The server retrieves the current traceId from the OpenTelemetry Span context:

final Span currentSpan = Span.current();
final SimpleMdcContextResponse simpleMdcContextResponse = new SimpleMdcContextResponse();
simpleMdcContextResponse.setTraceId(currentSpan.getSpanContext().getTraceId()); 
return new ObjectResponse<>(simpleMdcContextResponse);
  1. Asserted that the traceId in the response is the same as the one sent in the request header:
image

jandusil added a commit that referenced this issue Feb 1, 2024
* Fix #261:Add support for traceparent headers in DefaultRestClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant