forked from signalfx/tracing-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (95 loc) · 2.82 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"math/rand"
"net/http"
"time"
"golang.org/x/net/context"
"go.opencensus.io/exporter/jaeger"
"go.opencensus.io/trace"
)
const (
// the HTTP thrift endpoint where the spans will be sent
ingestUrl = "https://ingest.signalfx.com/v1/trace"
// the service name to export these spans with
serviceName = "signalfx-opencensus-jaeger-go-example"
// please provide a valid access token
accessToken = ""
)
var (
span *trace.Span
random *rand.Rand
count int
)
func main() {
source := rand.NewSource(time.Now().UnixNano())
random = rand.New(source)
initExporter(serviceName, ingestUrl, accessToken)
if err := startServer().ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func initExporter(serviceName, ingestUrl, accessToken string) {
// create the jaeger exporter
exporter, err := jaeger.NewExporter(jaeger.Options{
CollectorEndpoint: ingestUrl,
Process: jaeger.Process{
ServiceName: serviceName,
},
Username: "auth",
Password: accessToken,
})
if err != nil {
fmt.Println("Failed to initialize exporter: ", err)
return
}
// this will flush out any pending spans
defer exporter.Flush()
// register the exporter and configure the tracer to always sample
trace.RegisterExporter(exporter)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}
func startServer() *http.Server {
// start a simple http server listening at localhost:8080/test
srv := &http.Server{Addr: ":8080"}
http.HandleFunc("/test", handle)
srv.ListenAndServe()
return srv
}
func handle(w http.ResponseWriter, r *http.Request) {
// this will be a parent span, so create a new blank context
ctx := context.Background()
// start the span and defer closing it until we leave the "handle" function
ctx, span := trace.StartSpan(ctx, "handle")
defer span.End()
count = 0
randInt := random.Intn(100)
fmt.Println("Random number: ", randInt)
for count < randInt {
// since we want to register the spans for the "increment" function
// as child spans of this one, we need to pass in the context
increment(ctx)
}
// add a tag with the final value of count
span.AddAttributes(trace.Int64Attribute("final_count", int64(count)))
fmt.Fprint(w, randInt)
}
func increment(ctx context.Context) {
// instead of creating a new empty context, use the context passed in
// from the parent span
ctx, span := trace.StartSpan(ctx, "increment")
defer span.End()
count++
// tag the span with the current value of count
span.AddAttributes(trace.Attribute{
key: "count",
value: count,
})
// an annotation is a group of attributes with a relative timestamp and is
// viewed as a log item on the span
attributes := []trace.Attribute{
trace.Int64Attribute("count", int64(count)),
trace.StringAttribute("full_timestamp", time.Now().String()),
}
span.Annotate(attributes, "attributes_list")
}