/
trace.go
200 lines (155 loc) · 4.31 KB
/
trace.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package flow
import (
"context"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// Carrier controls the trace
type Carrier struct {
Trace map[string]string
}
// Get returns a value of a trace
func (c *Carrier) Get(key string) string {
v, _ := c.Trace[key]
return v
}
// Keys returns all the keys of the trace
func (c *Carrier) Keys() []string {
var keys []string
for k := range c.Trace {
keys = append(keys, k)
}
return keys
}
// Set sets a trace key and value
func (c *Carrier) Set(key, val string) {
c.Trace[key] = val
}
func dbTrace(ctx context.Context) *Carrier {
carrier := &Carrier{
Trace: make(map[string]string),
}
prop := otel.GetTextMapPropagator()
prop.Inject(ctx, carrier)
return carrier
}
func traceAddWorkflowInstance(ctx context.Context, d *refData, im *instanceMemory) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(
attribute.KeyValue{
Key: "namespace",
Value: attribute.StringValue(d.namespace()),
},
attribute.KeyValue{
Key: "namespace-id",
Value: attribute.StringValue(d.ns().ID.String()),
},
attribute.KeyValue{
Key: "workflow",
Value: attribute.StringValue(d.path),
},
attribute.KeyValue{
Key: "workflow-id",
Value: attribute.StringValue(d.wf.ID.String()),
},
attribute.KeyValue{
Key: "revision",
Value: attribute.StringValue(fmt.Sprintf("%v", d.rev().ID.String())),
},
attribute.KeyValue{
Key: "instance",
Value: attribute.StringValue(im.in.ID.String()),
},
attribute.KeyValue{
Key: "as",
Value: attribute.StringValue(im.in.As),
},
)
}
func traceFullAddWorkflowInstance(ctx context.Context, d *refData, im *instanceMemory) (context.Context, error) {
traceAddWorkflowInstance(ctx, d, im)
tp := otel.GetTracerProvider()
tr := tp.Tracer("direktiv/flow")
ctx, span := tr.Start(ctx, "new-workflow-instance", trace.WithSpanKind(trace.SpanKindInternal))
defer span.End()
traceAddWorkflowInstance(ctx, d, im)
x := dbTrace(ctx)
s := marshal(x)
rt, err := im.in.Edges.Runtime.Update().SetInstanceContext(s).Save(ctx)
if err != nil {
return nil, err
}
rt.Edges = im.in.Edges.Runtime.Edges
im.in.Edges.Runtime = rt
return ctx, nil
}
func traceStateError(ctx context.Context, err error) {
span := trace.SpanFromContext(ctx)
span.SetStatus(codes.Error, err.Error())
span.AddEvent(fmt.Sprintf("state error: %v", err.Error()))
}
func traceSubflowInvoke(ctx context.Context, name, child string) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(
attribute.KeyValue{
Key: "child-instance",
Value: attribute.StringValue(child),
},
)
span.AddEvent(fmt.Sprintf("Calling subflow: %s (%s)", name, child))
}
func traceStateGenericBegin(ctx context.Context, im *instanceMemory) (context.Context, func(), error) {
tp := otel.GetTracerProvider()
tr := tp.Tracer("direktiv/flow")
prop := otel.GetTextMapPropagator()
var span trace.Span
carrier := new(Carrier)
err := unmarshal(im.in.Edges.Runtime.InstanceContext, carrier)
if err != nil {
return ctx, nil, err
}
ctx = prop.Extract(ctx, carrier)
ctx, span = tr.Start(ctx, im.logic.GetType().String(), trace.WithSpanKind(trace.SpanKindInternal))
x := dbTrace(ctx)
s := marshal(x)
rt, err := im.in.Edges.Runtime.Update().SetStateContext(s).Save(ctx)
if err != nil {
span.End()
return ctx, nil, err
}
rt.Edges = im.in.Edges.Runtime.Edges
im.in.Edges.Runtime = rt
finish := func() {
span.End()
}
return ctx, finish, nil
}
func traceStateGenericLogicThread(ctx context.Context, im *instanceMemory) (context.Context, func(), error) {
tp := otel.GetTracerProvider()
tr := tp.Tracer("direktiv/flow")
prop := otel.GetTextMapPropagator()
var span trace.Span
carrier := new(Carrier)
err := unmarshal(im.in.Edges.Runtime.StateContext, carrier)
if err != nil {
return nil, nil, err
}
ctx = prop.Extract(ctx, carrier)
ctx, span = tr.Start(ctx, fmt.Sprintf("%s-logic", im.logic.GetType().String()), trace.WithSpanKind(trace.SpanKindInternal))
finish := func() {
span.End()
}
return ctx, finish, nil
}
func traceActionResult(ctx context.Context, results *actionResultPayload) {
span := trace.SpanFromContext(ctx)
span.SetAttributes(
attribute.KeyValue{
Key: "returned-action",
Value: attribute.StringValue(results.ActionID),
},
)
}