@@ -12,68 +12,72 @@ import (
12
12
" context"
13
13
" strings"
14
14
15
- exp " go.opencensus.io/examples/exporter "
16
- " github.com/nicksrandall/dataloader "
15
+ " github.com/graph-gophers/dataloader/v7 "
16
+ exp " go.opencensus.io/examples/exporter "
17
17
" go.opencensus.io/trace"
18
18
)
19
19
20
+ type User struct {
21
+ ID string
22
+ }
23
+
20
24
// OpenCensusTracer Tracer implements a tracer that can be used with the Open Tracing standard.
21
25
type OpenCensusTracer struct {}
22
26
23
27
// 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 ) {
28
+ func (OpenCensusTracer ) TraceLoad (ctx context .Context , key string ) (context .Context , dataloader .TraceLoadFinishFunc [* User ] ) {
25
29
cCtx , cSpan := trace.StartSpan (ctx, " Dataloader: load" )
26
30
cSpan.AddAttributes (
27
- trace.StringAttribute (" dataloader.key" , key. String () ),
31
+ trace.StringAttribute (" dataloader.key" , key),
28
32
)
29
- return cCtx, func (thunk dataloader.Thunk ) {
33
+ return cCtx, func (thunk dataloader.Thunk [*User] ) {
30
34
// TODO: is there anything we should do with the results?
31
35
cSpan.End ()
32
36
}
33
37
}
34
38
35
39
// 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 ) {
40
+ func (OpenCensusTracer ) TraceLoadMany (ctx context .Context , keys [] string ) (context .Context , dataloader .TraceLoadManyFinishFunc [* User ] ) {
37
41
cCtx , cSpan := trace.StartSpan (ctx, " Dataloader: loadmany" )
38
42
cSpan.AddAttributes (
39
- trace.StringAttribute (" dataloader.keys" , strings.Join (keys. Keys () , " ," )),
43
+ trace.StringAttribute (" dataloader.keys" , strings.Join (keys, " ," )),
40
44
)
41
- return cCtx, func (thunk dataloader.ThunkMany ) {
45
+ return cCtx, func (thunk dataloader.ThunkMany [*User] ) {
42
46
// TODO: is there anything we should do with the results?
43
47
cSpan.End ()
44
48
}
45
49
}
46
50
47
51
// 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 ) {
52
+ func (OpenCensusTracer ) TraceBatch (ctx context .Context , keys [] string ) (context .Context , dataloader .TraceBatchFinishFunc [* User ] ) {
49
53
cCtx , cSpan := trace.StartSpan (ctx, " Dataloader: batch" )
50
54
cSpan.AddAttributes (
51
- trace.StringAttribute (" dataloader.keys" , strings.Join (keys. Keys () , " ," )),
55
+ trace.StringAttribute (" dataloader.keys" , strings.Join (keys, " ," )),
52
56
)
53
- return cCtx, func (results []*dataloader.Result ) {
57
+ return cCtx, func (results []*dataloader.Result [*User] ) {
54
58
// TODO: is there anything we should do with the results?
55
59
cSpan.End ()
56
60
}
57
61
}
58
62
59
- func batchFunc (ctx context .Context , keys dataloader . Keys ) []*dataloader .Result {
60
- // ...loader logic goes here
63
+ func batchFunc (ctx context .Context , keys [] string ) []*dataloader .Result [* User ] {
64
+ // ...loader logic goes here
61
65
}
62
66
63
- func main (){
64
- // initialize an example exporter that just logs to the console
65
- trace.ApplyConfig (trace.Config {
67
+ func main () {
68
+ // initialize an example exporter that just logs to the console
69
+ trace.ApplyConfig (trace.Config {
66
70
DefaultSampler: trace.AlwaysSample (),
67
71
})
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
- // ...
72
+ trace.RegisterExporter (&exp.PrintExporter {})
73
+ // initialize the dataloader with your new tracer backend
74
+ loader := dataloader.NewBatchedLoader (batchFunc, dataloader.WithTracer [ string , *User] (OpenCensusTracer{}))
75
+ // initialize a context since it's not receiving one from anywhere else.
76
+ ctx , span := trace.StartSpan (context.TODO (), " Span Name" )
77
+ defer span.End ()
78
+ // request from the dataloader as usual
79
+ value , err := loader.Load (ctx, SomeID)()
80
+ // ...
77
81
}
78
82
```
79
83
0 commit comments