This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
/
trace_wrapper.go
65 lines (51 loc) · 1.66 KB
/
trace_wrapper.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
package wrappers
import (
"context"
"github.com/grafana/scribe/pipeline"
"github.com/grafana/scribe/pipeline/clients"
"github.com/grafana/scribe/plog"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
)
type TraceWrapper struct {
Opts clients.CommonOpts
Tracer opentracing.Tracer
}
func (l *TraceWrapper) Fields(ctx context.Context, step pipeline.Step) logrus.Fields {
fields := plog.DefaultFields(ctx, step, l.Opts)
return fields
}
func TagSpan(span opentracing.Span, opts clients.CommonOpts, step pipeline.Step) {
span.SetTag("job", "scribe")
span.SetTag("build_id", opts.Args.BuildID)
}
func (l *TraceWrapper) WrapStep(step pipeline.Step) pipeline.Step {
// Steps that provide a nil action should continue to provide a nil action.
// There is nothing for us to trace in the execution of this action anyways, though there is an implication that
// this step may execute something that is not defined in the pipeline.
if step.Action == nil {
return step
}
action := step.Action
step.Action = func(ctx context.Context, opts pipeline.ActionOpts) error {
parent := opentracing.SpanFromContext(ctx)
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, l.Tracer, step.Name, opentracing.ChildOf(parent.Context()))
TagSpan(span, l.Opts, step)
defer span.Finish()
if err := action(ctx, opts); err != nil {
span.SetTag("error", err)
return err
}
return nil
}
return step
}
func (l *TraceWrapper) Wrap(wf pipeline.StepWalkFunc) pipeline.StepWalkFunc {
return func(ctx context.Context, step pipeline.Step) error {
steps := l.WrapStep(step)
if err := wf(ctx, steps); err != nil {
return err
}
return nil
}
}