apm-agent-nodejs icon indicating copy to clipboard operation
apm-agent-nodejs copied to clipboard

disable tracestate and traceparent headers from http request

Open sibelius opened this issue 10 months ago • 7 comments

Is your feature request related to a problem? Please describe. Similar to this one https://discuss.elastic.co/t/how-to-disable-tracestate-and-traceparent-headers/324164

Describe the solution you'd like Be able to disable tracestate and traceparent headers on HTTP outgoing requests

Describe alternatives you've considered disabling apm

Additional context this is breaking our HTTP outgoing requests

sibelius avatar Feb 07 '25 14:02 sibelius

can I disable tracestate and traceparent for some domains and keep the others?

sibelius avatar Feb 07 '25 17:02 sibelius

Hi @sibelius

I would like to have more context on this issue. In which way having extra headers on your request breaks the request to the external service?

david-luna avatar Feb 10 '25 11:02 david-luna

it is similar to this one https://discuss.elastic.co/t/how-to-disable-tracestate-and-traceparent-headers/324164

bank APIs are very strict, if you add another header that is not expected the request is broken

sibelius avatar Feb 10 '25 11:02 sibelius

I'd like to disable these headers for some requests, hostname, but not for all

sibelius avatar Feb 10 '25 11:02 sibelius

what is the best way to add these options?

sibelius avatar Feb 10 '25 11:02 sibelius

Hi @sibelius

the team discussed this internally and we will proceed to implement the same config option as the Java agent has. This will completely remove tracestate and traceparent headers of all outgoing requests.

Having such an advanced feature like filtering per domain/hostname is not planned since we are focusing on the Elastic Distribution of OpenTelemetery for Node.js.

Cheers

david-luna avatar Feb 18 '25 12:02 david-luna

here is my patch

diff --git a/lib/instrumentation/http-shared.js b/lib/instrumentation/http-shared.js
index b01397c9238f9e899262a38a232a10769afe4b6c..286ac297e3c940e5702ade1ffbff4e4e912f83d9 100644
--- a/lib/instrumentation/http-shared.js
+++ b/lib/instrumentation/http-shared.js
@@ -275,12 +275,32 @@ exports.traceOutgoingRequest = function (agent, moduleName, method) {
         parentRunContext.currTransaction();
       if (parent) {
         const headers = Object.assign({}, options.headers);
-        parent.propagateTraceContextHeaders(
-          headers,
-          function (carrier, name, value) {
-            carrier[name] = value;
-          },
-        );
+
+
+        if (!options?.hostname?.includes('domainToExclude')) {
+          parent.propagateTraceContextHeaders(
+            headers,
+            function(carrier, name, value) {
+              carrier[name] = value;
+            },
+          );
+        }
         options.headers = headers;
       }
 

sibelius avatar Feb 18 '25 12:02 sibelius