/
tracing.go
44 lines (37 loc) · 1.12 KB
/
tracing.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
package beekeeper
import (
"context"
"github.com/ethersphere/beekeeper/pkg/orchestration"
"github.com/opentracing/opentracing-go"
)
var _ Action = (*actionMiddleware)(nil)
type actionMiddleware struct {
tracer opentracing.Tracer
action Action
actionName string
}
// ActionMiddleware tracks request, and adds spans
// to context.
func NewActionMiddleware(tracer opentracing.Tracer, action Action, actionName string) Action {
return &actionMiddleware{
tracer: tracer,
action: action,
actionName: actionName,
}
}
// Run implements beekeeper.Action
func (am *actionMiddleware) Run(ctx context.Context, cluster orchestration.Cluster, o interface{}) (err error) {
span := createSpan(ctx, am.tracer, am.actionName)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return am.action.Run(ctx, cluster, o)
}
func createSpan(ctx context.Context, tracer opentracing.Tracer, opName string) opentracing.Span {
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
return tracer.StartSpan(
opName,
opentracing.ChildOf(parentSpan.Context()),
)
}
return tracer.StartSpan(opName)
}