Skip to content

Commit 37d4985

Browse files
renatoaquinonicksrandall
authored andcommitted
opencensus trace example (graph-gophers#58)
1 parent 1232fe3 commit 37d4985

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

TRACE.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Adding a new trace backend.
2+
3+
If you whant to add a new tracing backend all you need to do is implement the
4+
`Tracer` interface and pass it as an option to the dataloader on initialization.
5+
6+
As an example, this is how you could implement it to an OpenCensus backend.
7+
8+
```go
9+
package main
10+
11+
import (
12+
"context"
13+
"strings"
14+
15+
exp "go.opencensus.io/examples/exporter"
16+
"github.com/nicksrandall/dataloader"
17+
"go.opencensus.io/trace"
18+
)
19+
20+
// OpenCensusTracer Tracer implements a tracer that can be used with the Open Tracing standard.
21+
type OpenCensusTracer struct{}
22+
23+
// TraceLoad will trace a call to dataloader.LoadMany with Open Tracing
24+
func (OpenCensusTracer) TraceLoad(ctx context.Context, key dataloader.Key) (context.Context, dataloader.TraceLoadFinishFunc) {
25+
cCtx, cSpan := trace.StartSpan(ctx, "Dataloader: load")
26+
cSpan.AddAttributes(
27+
trace.StringAttribute("dataloader.key", key.String()),
28+
)
29+
return cCtx, func(thunk dataloader.Thunk) {
30+
// TODO: is there anything we should do with the results?
31+
cSpan.End()
32+
}
33+
}
34+
35+
// TraceLoadMany will trace a call to dataloader.LoadMany with Open Tracing
36+
func (OpenCensusTracer) TraceLoadMany(ctx context.Context, keys dataloader.Keys) (context.Context, dataloader.TraceLoadManyFinishFunc) {
37+
cCtx, cSpan := trace.StartSpan(ctx, "Dataloader: loadmany")
38+
cSpan.AddAttributes(
39+
trace.StringAttribute("dataloader.keys", strings.Join(keys.Keys(), ",")),
40+
)
41+
return cCtx, func(thunk dataloader.ThunkMany) {
42+
// TODO: is there anything we should do with the results?
43+
cSpan.End()
44+
}
45+
}
46+
47+
// TraceBatch will trace a call to dataloader.LoadMany with Open Tracing
48+
func (OpenCensusTracer) TraceBatch(ctx context.Context, keys dataloader.Keys) (context.Context, dataloader.TraceBatchFinishFunc) {
49+
cCtx, cSpan := trace.StartSpan(ctx, "Dataloader: batch")
50+
cSpan.AddAttributes(
51+
trace.StringAttribute("dataloader.keys", strings.Join(keys.Keys(), ",")),
52+
)
53+
return cCtx, func(results []*dataloader.Result) {
54+
// TODO: is there anything we should do with the results?
55+
cSpan.End()
56+
}
57+
}
58+
59+
func batchFunc(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
60+
// ...loader logic goes here
61+
}
62+
63+
func main(){
64+
//initialize an example exporter that just logs to the console
65+
trace.ApplyConfig(trace.Config{
66+
DefaultSampler: trace.AlwaysSample(),
67+
})
68+
trace.RegisterExporter(&exp.PrintExporter{})
69+
// initialize the dataloader with your new tracer backend
70+
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithTracer(OpenCensusTracer{}))
71+
// initialize a context since it's not receiving one from anywhere else.
72+
ctx, span := trace.StartSpan(context.TODO(), "Span Name")
73+
defer span.End()
74+
// request from the dataloader as usual
75+
value, err := loader.Load(ctx, dataloader.StringKey(SomeID))()
76+
// ...
77+
}
78+
```
79+
80+
Don't forget to initialize the exporters of your choice and register it with `trace.RegisterExporter(&exporterInstance)`.

0 commit comments

Comments
 (0)