-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
59 lines (51 loc) · 1.54 KB
/
trace.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
package main
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"log"
"time"
"contrib.go.opencensus.io/exporter/zipkin"
openzipkin "github.com/openzipkin/zipkin-go"
zipkinHTTP "github.com/openzipkin/zipkin-go/reporter/http"
"go.opencensus.io/trace"
)
func main() {
// Configure exporter to export traces to Zipkin.
localEndpoint, err := openzipkin.NewEndpoint("go-quickstart", "127.0.0.1:5454")
if err != nil {
log.Fatal(err)
}
reporter := zipkinHTTP.NewReporter("http://localhost:9411/api/v2/spans")
ze := zipkin.NewExporter(reporter, localEndpoint)
trace.RegisterExporter(ze)
// Configure 100% sample rate, otherwise, few traces will be sampled.
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
// Create a span with the background context, making this the parent sapn. A span must be closed.
ctx, span := trace.StartSpan(context.Background(), "main")
defer span.End()
for i := 0; i < 10; i++ {
doWork(ctx)
}
}
func doWork(ctx context.Context) {
_, span := trace.StartSpan(ctx, "doWork")
defer span.End()
fmt.Println("busy doing work")
time.Sleep(80 * time.Millisecond)
buf := bytes.NewBuffer([]byte{0xFF, 0x00, 0x00, 0x00})
num, err := binary.ReadVarint(buf)
if err != nil {
// Set status upon error.
span.SetStatus(trace.Status{
Code: trace.StatusCodeUnknown,
Message: err.Error(),
})
}
// Annotate our span to capture metadata abour our operation.
span.Annotate([]trace.Attribute{
trace.Int64Attribute("bytes to int", num),
}, "Invoking doWork")
time.Sleep(20 * time.Millisecond)
}