|
| 1 | +# Tracing Library for Go |
| 2 | + |
| 3 | +This library provides tracing for go using [Zipkin](https://zipkin.io/) |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +### Server Tracing Middleware & http client tracing |
| 8 | + |
| 9 | +```go |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "github.com/labstack/echo-contrib/zipkintracing" |
| 14 | + "github.com/labstack/echo/v4" |
| 15 | + "github.com/openzipkin/zipkin-go" |
| 16 | + zipkinhttp "github.com/openzipkin/zipkin-go/middleware/http" |
| 17 | + zipkinHttpReporter "github.com/openzipkin/zipkin-go/reporter/http" |
| 18 | + "io/ioutil" |
| 19 | + "net/http" |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + e := echo.New() |
| 24 | + endpoint, err := zipkin.NewEndpoint("echo-service", "") |
| 25 | + if err != nil { |
| 26 | + e.Logger.Fatalf("error creating zipkin endpoint: %s", err.Error()) |
| 27 | + } |
| 28 | + reporter := zipkinHttpReporter.NewReporter("http://localhost:9411/api/v2/spans") |
| 29 | + traceTags := make(map[string]string) |
| 30 | + traceTags["availability_zone"] = "us-east-1" |
| 31 | + tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint), zipkin.WithTags(traceTags)) |
| 32 | + client, _ := zipkinhttp.NewClient(tracer, zipkinhttp.ClientTrace(true)) |
| 33 | + if err != nil { |
| 34 | + e.Logger.Fatalf("tracing init failed: %s", err.Error()) |
| 35 | + } |
| 36 | + //Wrap & Use trace server middleware, this traces all server calls |
| 37 | + e.Use(zipkintracing.TraceServer(tracer)) |
| 38 | + //.... |
| 39 | + e.GET("/echo", func(c echo.Context) error { |
| 40 | + //trace http request calls. |
| 41 | + req, _ := http.NewRequest("GET", "https://echo.labstack.com/", nil) |
| 42 | + resp, _ := zipkintracing.DoHTTP(c, req, client) |
| 43 | + body, _ := ioutil.ReadAll(resp.Body) |
| 44 | + return c.String(http.StatusOK, string(body)) |
| 45 | + }) |
| 46 | + |
| 47 | + defer reporter.Close() //defer close reporter |
| 48 | + e.Logger.Fatal(e.Start(":8080")) |
| 49 | +} |
| 50 | +``` |
| 51 | +### Reverse Proxy Tracing |
| 52 | + |
| 53 | +```go |
| 54 | +package main |
| 55 | + |
| 56 | +import ( |
| 57 | + "github.com/labstack/echo-contrib/zipkintracing" |
| 58 | + "github.com/labstack/echo/v4" |
| 59 | + "github.com/labstack/echo/v4/middleware" |
| 60 | + "github.com/openzipkin/zipkin-go" |
| 61 | + zipkinHttpReporter "github.com/openzipkin/zipkin-go/reporter/http" |
| 62 | + "net/http/httputil" |
| 63 | + "net/url" |
| 64 | +) |
| 65 | + |
| 66 | +func main() { |
| 67 | + e := echo.New() |
| 68 | + //new tracing instance |
| 69 | + endpoint, err := zipkin.NewEndpoint("echo-service", "") |
| 70 | + if err != nil { |
| 71 | + e.Logger.Fatalf("error creating zipkin endpoint: %s", err.Error()) |
| 72 | + } |
| 73 | + reporter := zipkinHttpReporter.NewReporter("http://localhost:9411/api/v2/spans") |
| 74 | + traceTags := make(map[string]string) |
| 75 | + traceTags["availability_zone"] = "us-east-1" |
| 76 | + tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint), zipkin.WithTags(traceTags)) |
| 77 | + if err != nil { |
| 78 | + e.Logger.Fatalf("tracing init failed: %s", err.Error()) |
| 79 | + } |
| 80 | + //.... |
| 81 | + e.GET("/echo", func(c echo.Context) error { |
| 82 | + proxyURL, _ := url.Parse("https://echo.labstack.com/") |
| 83 | + httputil.NewSingleHostReverseProxy(proxyURL) |
| 84 | + return nil |
| 85 | + }, zipkintracing.TraceProxy(tracer)) |
| 86 | + |
| 87 | + defer reporter.Close() //close reporter |
| 88 | + e.Logger.Fatal(e.Start(":8080")) |
| 89 | + |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Trace function calls |
| 94 | + |
| 95 | +To trace function calls e.g. to trace `s3Func` |
| 96 | + |
| 97 | +```go |
| 98 | +package main |
| 99 | + |
| 100 | +import ( |
| 101 | + "github.com/labstack/echo-contrib/zipkintracing" |
| 102 | + "github.com/labstack/echo/v4" |
| 103 | + "github.com/openzipkin/zipkin-go" |
| 104 | +) |
| 105 | + |
| 106 | +func s3Func(c echo.Context, tracer *zipkin.Tracer) { |
| 107 | + defer zipkintracing.TraceFunc(c, "s3_read", zipkintracing.DefaultSpanTags, tracer)() |
| 108 | + //s3Func logic here... |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### Create Child Span |
| 113 | + |
| 114 | +```go |
| 115 | +package main |
| 116 | + |
| 117 | +import ( |
| 118 | + "github.com/labstack/echo-contrib/zipkintracing" |
| 119 | + "github.com/labstack/echo/v4" |
| 120 | + "github.com/openzipkin/zipkin-go" |
| 121 | +) |
| 122 | + |
| 123 | +func traceWithChildSpan(c echo.Context, tracer *zipkin.Tracer) { |
| 124 | + span := zipkintracing.StartChildSpan(c, "someMethod", tracer) |
| 125 | + //func logic..... |
| 126 | + span.Finish() |
| 127 | +} |
| 128 | +``` |
0 commit comments