forked from elgris/microservice-app-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
47 lines (37 loc) · 1.17 KB
/
tracing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"net/http"
zipkin "github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/middleware/http"
zipkinhttpreporter "github.com/openzipkin/zipkin-go/reporter/http"
)
type TracedClient struct {
client *zipkinhttp.Client
}
func (c *TracedClient) Do(req *http.Request) (*http.Response, error) {
name := req.Method + " " + req.RequestURI
return c.client.DoWithAppSpan(req, name)
}
func initTracing(zipkinURL string) (func(http.Handler) http.Handler, *TracedClient, error) {
reporter := zipkinhttpreporter.NewReporter(zipkinURL)
endpoint, err := zipkin.NewEndpoint("auth-api", "")
if err != nil {
return nil, nil, err
}
tracer, err := zipkin.NewTracer(reporter,
zipkin.WithLocalEndpoint(endpoint),
zipkin.WithSharedSpans(false))
if err != nil {
return nil, nil, err
}
// create global zipkin http server middleware
serverMiddleware := zipkinhttp.NewServerMiddleware(
tracer, zipkinhttp.TagResponseSize(true),
)
// create global zipkin traced http client
client, err := zipkinhttp.NewClient(tracer, zipkinhttp.ClientTrace(true))
if err != nil {
return nil, nil, err
}
return serverMiddleware, &TracedClient{client}, nil
}