zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.24k stars 3.95k forks source link

hope to use envoy x-client-trace-id as trace id instead of generating a new one in go-zero when using with istio #2870

Open Danny5487401 opened 1 year ago

Danny5487401 commented 1 year ago

image

I want to have the same trace-id from istio ingress gateway or sicar envoy as that in go-zero application. Is it possible to extract from it by configuring?

example

POST /v1/releasemanager/gitlab_webhook?project_id=6 HTTP/1.1
Host: xxx.xxx.net
Content-Length: 3459
Content-Type: application/json
User-Agent: GitLab/13.12.15
X-B3-Parentspanid: 303ede01fc69eeec
X-B3-Sampled: 1
X-B3-Spanid: b037815e6d9c74b9
X-B3-Traceid: b48aabe5813661f8303ede01fc69eeec
X-Envoy-Attempt-Count: 1
X-Envoy-Internal: true
X-Forwarded-Client-Cert: By=spiffe://cluster.local/ns/artemis/sa/artemis;Hash=cde4d66344069283c3d78916581457f85a3e31b41ebbde7cdd2369d5d4ed0fb6;Subject="";URI=spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-internal-1-10-2-service-account
X-Forwarded-For: 172.16.10.5
X-Forwarded-Proto: http
X-Gitlab-Event: Pipeline Hook
X-Request-Id: f14d220b-d83d-9c26-91d7-71891f61ac32
kevwan commented 1 year ago

I think you can use a middleware to convert x-client-trace-id to Traceparent in HTTP headers.

Danny5487401 commented 1 year ago

is there any example code to learn from because i do not know too much about opentracing package with zipkin protocol?

yluchao commented 1 year ago

I think you can use a middleware to convert x-client-trace-id to Traceparent in HTTP headers.

I tried to use the global middleware, but it will be executed later than the trace middleware and will not take effect

Danny5487401 commented 1 year ago

I think you can use a middleware to convert x-client-trace-id to Traceparent in HTTP headers.

I tried to use the global middleware, but it will be executed later than the trace middleware and will not take effect

@yluchao it seems to be ok to use chain to change middleware order.

MustNewServer(RestConf{}, WithChain(chain.New(middleware1(), middleware2())))

But it will lose original middleware because local-scoped chain is hard to configure. @kevwan

image

ofey404 commented 1 year ago

Any updates?

I need this feature too, and I'd like to build a solution if time permits.

Danny5487401 commented 1 year ago

@ofey404 Not too much time to build by my own, hope somebody can。 image @kevwan it will be ok to set extra context middleware with istio trace id before go-zero TraceHandler

Danny5487401 commented 1 month ago

@kevwan would u consider adding some weight to middleware so that they can be added before default middleware ?

or another simple solution below : https://github.com/zeromicro/go-zero/pull/4364/files

func (ng *engine) bindRoute(fr featuredRoutes, router httpx.Router, metrics *stat.Metrics,
    route Route, verifier func(chain.Chain) chain.Chain) error {
    chn := ng.chain
    if chn == nil {
                chn = chain.New()

    }
        if ng.conf.AddNativeMiddleWare{
              buildChainWithNativeMiddlewares(chain chain.Chain,fr featuredRoutes, route Route,metrics *stat.Metrics)
        }
}