package com.peterphi.std.guice.restclient.resteasy.impl;

import com.peterphi.std.util.tracing.Tracing;
import com.peterphi.std.util.tracing.TracingConstants;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

@Provider
public class TracingClientRequestFilter implements ClientRequestFilter
{
	@Override
	public void filter(final ClientRequestContext requestContext) throws IOException
	{
		final String traceId = Tracing.log("HTTP:req", () -> "" + requestContext.getUri());

		if (traceId != null)
		{
			requestContext.getHeaders().add(TracingConstants.HTTP_HEADER_CORRELATION_ID, traceId);

			if (Tracing.isVerbose())
				requestContext.getHeaders().add(TracingConstants.HTTP_HEADER_TRACE_VERBOSE, "true");
		}
	}
}

@@ -151,16 +151,48 @@
 nstants;
+%0Aimport org.apache.log4j.Logger;
 %0A%0Aimport
@@ -425,16 +425,105 @@
 ilter%0A%7B%0A
+%09private static final Logger log = Logger.getLogger(TracingClientRequestFilter.class);%0A%0A%0A
 %09@Overri
@@ -727,16 +727,20 @@
 %0A%09%09%7B%0A%09%09%09
+if (
 requestC
@@ -751,35 +751,317 @@
 xt.getHeaders().
-add
+containsKey(TracingConstants.HTTP_HEADER_CORRELATION_ID))%0A%09%09%09%09log.warn(%22Duplicate call to tracing filter %22 +%0A%09%09%09%09         this +%0A%09%09%09%09         %22 for %22 +%0A%09%09%09%09         requestContext +%0A%09%09%09%09         %22 for %22 +%0A%09%09%09%09         requestContext.getUri());%0A%0A%09%09%09requestContext.getHeaders().putSingle
 (TracingConstant
@@ -1165,11 +1165,17 @@
 s().
-add
+putSingle
 (Tra

Add logic to prevent the addition of duplicate X-Correlation-ID headers if the tracing filter is called multiple times (also logs a warning if this happens)