Skip to content

Instantly share code, notes, and snippets.

@martin-helmich
Created January 11, 2020 10:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-helmich/abc496e5455a0cc43927dadd5a4a462d to your computer and use it in GitHub Desktop.
Save martin-helmich/abc496e5455a0cc43927dadd5a4a462d to your computer and use it in GitHub Desktop.
Minimal example for using Jaeger Tracing with Go
version: "3"
services:
jaeger:
image: jaegertracing/all-in-one:1.8
ports:
- 6831:6831/udp
- 6832:6832/udp
- 5778:5778
- 16686:16686
- 14268:14268
module tracing-example
go 1.12
require (
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.8.1 // indirect
github.com/uber/jaeger-client-go v2.21.1+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
go.uber.org/atomic v1.5.1 // indirect
)
package main
import (
"context"
"fmt"
"time"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go/config"
)
func main() {
cfg, err := config.FromEnv()
if err != nil {
panic(err)
}
cfg.ServiceName = "tracing-example"
tracer, closer, err := cfg.NewTracer()
if err != nil {
panic(err)
}
defer closer.Close()
opentracing.SetGlobalTracer(tracer)
example()
}
func example() {
span := opentracing.StartSpan("some-operation")
span.SetTag("key", "some-value")
defer span.Finish()
ctx := opentracing.ContextWithSpan(context.Background(), span)
executeLongRunningOperation(ctx)
executeLongRunningOperation(ctx)
}
func executeLongRunningOperation(ctx context.Context) {
span, _ := opentracing.StartSpanFromContext(ctx, "long-running-operation")
defer span.Finish()
// "subCtx" kann nun WIEDER an weitere Funktionen weitergereicht werden,
// um Spans noch tiefer zu verschachteln
time.Sleep(5 * time.Second)
fmt.Println("done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment