Search code examples
traceistioenvoyproxy

Override x-request-id header in istio


I was trying to understand the tracing in istio.

According to istio documentation, x-request-id can be used for tracing purpose.(https://istio.io/latest/docs/tasks/observability/distributed-tracing/overview/)

I am seeing different behavior in Istio vs pure envoy proxy.

For tracing istio and also pure envoy proxy set the x-request-id. (generated guid)

However in istio the client can send a header x-request-id and the same is forwarded to the microservices.

Whereas if I have pure envoy - the x-request-id sent by the client is not considered and envoy overrides it with a generated guid.

Can istio be configured to over-ride this x-request-id if required?


Solution

  • It seems it is possible to implement using envoy filter only. This is not my solution - I found it, however it looks very promising and maybe resolve your issue.

    Please take a look at Istio EnvoyFilter to add x-request-id to all responses

    apiVersion: networking.istio.io/v1alpha3
    kind: EnvoyFilter
    metadata:
      name: gateway-response
      namespace: istio-system
    spec:
      workloadSelector:
        labels:
          istio: ingressgateway
      configPatches:
      - applyTo: HTTP_FILTER
        match:
          context: GATEWAY
          listener:
            filterChain:
              filter:
                name: "envoy.http_connection_manager"
                subFilter:
                  name: "envoy.router"
        patch:
          operation: INSERT_BEFORE
          value:
           name: envoy.lua
           typed_config:
             "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
             inlineCode: |
              function envoy_on_request(handle)
                 local metadata = handle:streamInfo():dynamicMetadata()
                 local headers = handle:headers()
                 local rid = headers:get("x-request-id")
    
                 -- for key, value in pairs(handle:headers()) do
                 --   handle:logTrace("key:" .. key .. " <--> value:" .. value)
                 -- end
    
                 if rid ~= nil then
                   metadata:set("envoy.filters.http.lua", "req.x-request-id", rid)
                 end
               end
    
               function envoy_on_response(handle)
                 local metadata = handle:streamInfo():dynamicMetadata():get("envoy.filters.http.lua")
                 local rid = metadata["req.x-request-id"]
                 if rid ~= nil then
                   handle:headers():add("x-request-id", rid)
                 end
               end
    

    And second one - just my assumption..

    maybe you can try to add the x-request-id in the header of VirtualService? virtualservice-headers