forked from lightstep/lightstep-tracer-go
/
trivial.go
62 lines (50 loc) · 1.87 KB
/
trivial.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// A trivial LightStep Go tracer example.
//
// $ go build -o lightstep_trivial github.com/lightstep/lightstep-tracer-go/examples/trivial
// $ ./lightstep_trivial --access_token=YOUR_ACCESS_TOKEN
package main
import (
"context"
"flag"
"fmt"
logger "log"
"time"
"github.com/lightstep/lightstep-tracer-go"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
var accessToken = flag.String("access_token", "", "your LightStep access token")
func subRoutine(ctx context.Context) {
trivialSpan, _ := opentracing.StartSpanFromContext(ctx, "test span")
defer trivialSpan.Finish()
trivialSpan.LogEvent("logged something")
trivialSpan.LogFields(log.String("string_key", "some string value"), log.Object("trivialSpan", trivialSpan))
subSpan := opentracing.StartSpan(
"child span", opentracing.ChildOf(trivialSpan.Context()))
trivialSpan.LogFields(log.Int("int_key", 42), log.Object("subSpan", subSpan),
log.String("time.eager", fmt.Sprint(time.Now())),
log.Lazy(func(fv log.Encoder) {
fv.EmitString("time.lazy", fmt.Sprint(time.Now()))
}))
defer subSpan.Finish()
}
type LoggingRecorder struct {
r lightstep.SpanRecorder
}
func (r *LoggingRecorder) RecordSpan(span lightstep.RawSpan) {
logger.Printf("span traceID: %v spanID: %v parentID: %v Operation: %v \n", span.Context.TraceID, span.Context.SpanID, span.ParentSpanID, span.Operation)
}
func main() {
loggableRecorder := &LoggingRecorder{}
// Use LightStep as the global OpenTracing Tracer.
opentracing.InitGlobalTracer(lightstep.NewTracer(lightstep.Options{
AccessToken: *accessToken,
Collector: lightstep.Endpoint{Host: "localhost", Port: 8360, Plaintext: true},
UseGRPC: true,
Recorder: loggableRecorder,
}))
// Do something that's traced.
subRoutine(context.Background())
// Force a flush before exit.
lightstep.Flush(context.Background(), opentracing.GlobalTracer())
}