Skip to content

Adding interceptor support so trace propagation can be plugged#315

Open
salaboy wants to merge 1 commit intoanthropics:mainfrom
salaboy:trace-propagation
Open

Adding interceptor support so trace propagation can be plugged#315
salaboy wants to merge 1 commit intoanthropics:mainfrom
salaboy:trace-propagation

Conversation

@salaboy
Copy link
Copy Markdown

@salaboy salaboy commented Apr 7, 2026

Supporting pluggable OK Http interceptor to for example support trace propagation

For example, to propagate X-Trace-Id from an incoming request

In a Spring Boot / Servlet filter context

  // Shared ThreadLocal — set this from your servlet filter or interceptor                                                                                    
  public static final ThreadLocal<String> TRACE_ID = new ThreadLocal<>();                                                                                       
                                                                                                                                                                
  // Build the client once at startup                                                                                                                           
  OkHttpClient httpClient = OkHttpClient.builder()                                                                                                              
      .backend(backend)                                                                                                                                         
      .addInterceptor(chain -> {
          String traceId = TRACE_ID.get();                                                                                                                      
          okhttp3.Request request = traceId != null                                                                                                           
              ? chain.request().newBuilder().addHeader("X-Trace-Id", traceId).build()
              : chain.request();                                                                                                                                
          return chain.proceed(request);
      })                                                                                                                                                        
      .build();                                                                                                                                               

  AnthropicClient client = AnthropicOkHttpClient.builder()                                                                                                      
      .apiKey("...")
      .httpClient(httpClient)                                                                                                                                   
      .build();                                                                                                                                               

  // In your Spring HandlerInterceptor or servlet Filter:                                                                                                       
  TRACE_ID.set(request.getHeader("X-Trace-Id"));
  try {                                                                                                                                                         
      // handle request — the Anthropic client will forward the header                                                                                          
  } finally {
      TRACE_ID.remove(); // prevent leaks across thread reuse                                                                                                   
  }      

With OpenTelemetry / Brave (if already using a tracing framework)

If you're already using OTel or Brave, you can use their context propagators directly instead of ThreadLocal:

  .addInterceptor(chain -> {                                                                                                                                    
      // OTel example                                                                                                                                         
      Span span = Span.current();
      String traceId = span.getSpanContext().getTraceId();                                                                                                      
      return chain.proceed(
          chain.request().newBuilder().addHeader("X-Trace-Id", traceId).build()                                                                                 
      );                                                                                                                                                      
  })     

@salaboy salaboy requested a review from a team as a code owner April 7, 2026 16:20
This was referenced Apr 7, 2026
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 this pull request may close these issues.

1 participant