How to Disable Automatic Tracing Headers in Effect's HttpClient

Effect Tip: Disabling Automatic Tracing Headers


Effect’s HttpClient automatically propagates tracing context via headers (b3, traceparent). This is fantastic for distributed tracing, but some backends might reject requests with these unfamiliar headers.

The solution is the HttpClient.withTracerPropagation(false) combinator. You can easily add it to your HttpClient configuration when creating a custom layer.

Example:

Here’s how you can build a custom HttpClient layer that handles authentication and disables tracer propagation.

import { AtomHttpApi } from '@effect-atom/atom-react'
import { HttpClient, HttpClientRequest } from '@effect/platform'
import { Effect, Layer } from 'effect'
// ... other app-specific imports like GroupXXXApi, AuthService

const GroupXXXApiLive = Layer.effect(
    HttpClient.HttpClient,
    Effect.gen(function* () {
        const client = yield* HttpClient.HttpClient
        const auth = yield* AuthService // Assuming you have an auth service

        return client.pipe(
            // This creates a new client that won't add tracing headers
            HttpClient.withTracerPropagation(false),

            // You can still chain other customizations, like adding auth tokens
            HttpClient.mapRequestEffect(request =>
                Effect.gen(function* () {
                    // ... your auth logic to get a token and add it to the request ...
                    const token = yield* auth.getAccessToken().pipe(
                        Effect.catchAll(() => Effect.succeed(null))
                    )
                    return token === null
                        ? request
                        : HttpClientRequest.setHeader(request, "Authorization", `Bearer ${token}`)
                }),
            ),
        )
    }),
).pipe(/* ... provide necessary layers like FetchHttpClient, AuthServiceLive ... */)

...
Was this page helpful?