-
Notifications
You must be signed in to change notification settings - Fork 42
/
telemetry.go
114 lines (89 loc) · 3.05 KB
/
telemetry.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
package engine
import (
"context"
"fmt"
"strings"
"github.com/direktiv/direktiv/pkg/flow/nohome/recipient"
"github.com/direktiv/direktiv/pkg/refactor/core"
"go.opentelemetry.io/otel/trace"
)
func (instance *Instance) GetAttributes(recipientType recipient.RecipientType) map[string]string {
tags := make(map[string]string)
callpath := ""
for _, v := range instance.DescentInfo.Descent {
callpath += "/" + v.ID.String()
}
tags["recipientType"] = string(recipientType)
tags["instance-id"] = instance.Instance.ID.String()
tags["invoker"] = instance.Instance.Invoker
tags["callpath"] = callpath
tags["workflow"] = getWorkflow(instance.Instance.WorkflowPath)
tags["namespace-id"] = instance.Instance.NamespaceID.String()
tags["namespace"] = instance.TelemetryInfo.NamespaceName
return tags
}
func (instance *Instance) WithTags(ctx context.Context) context.Context {
tags, ok := ctx.Value(core.LogTagsKey).([]interface{})
if !ok {
tags = make([]interface{}, 0)
}
callpath := ""
for _, v := range instance.DescentInfo.Descent {
callpath += "/" + v.ID.String()
}
tags = append(tags, "instance", instance.Instance.ID)
tags = append(tags, "invoker", instance.Instance.Invoker) // TODO: value is empty.
tags = append(tags, "callpath", callpath)
tags = append(tags, "workflow", instance.Instance.WorkflowPath) // TODO: value is empty.
return context.WithValue(ctx, core.LogTagsKey, tags)
}
func AddTag(ctx context.Context, key, value interface{}) context.Context {
tags, ok := ctx.Value(core.LogTagsKey).([]interface{})
if !ok {
tags = make([]interface{}, 0)
}
tags = append(tags, key, value)
return context.WithValue(ctx, core.LogTagsKey, tags)
}
func getSlogAttributes(ctx context.Context) []interface{} {
tags, ok := ctx.Value(core.LogTagsKey).([]interface{})
if !ok {
tags = make([]interface{}, 0)
}
if trackValue, ok := ctx.Value(core.LogTrackKey).(string); ok {
tags = append(tags, "track", trackValue)
}
span := trace.SpanFromContext(ctx)
traceID := span.SpanContext().TraceID().String()
spanID := span.SpanContext().SpanID().String()
tags = append(tags, "trace", traceID)
tags = append(tags, "span", spanID)
return tags
}
func GetSlogAttributesWithStatus(ctx context.Context, status core.LogStatus) []interface{} {
tags := getSlogAttributes(ctx)
tags = append(tags, "status", status)
return tags
}
func GetSlogAttributesWithError(ctx context.Context, err error) []interface{} {
tags := getSlogAttributes(ctx)
tags = append(tags, "error", err)
tags = append(tags, "status", "error")
return tags
}
func WithTrack(ctx context.Context, track string) context.Context {
return context.WithValue(ctx, core.LogTrackKey, track)
}
func BuildNamespaceTrack(namespace string) string {
return fmt.Sprintf("%v.%v", "namespace", namespace)
}
func BuildInstanceTrack(instance *Instance) string {
callpath := instance.Instance.ID.String()
for _, v := range instance.DescentInfo.Descent {
callpath += "/" + v.ID.String()
}
return fmt.Sprintf("%v.%v", "instance", callpath)
}
func getWorkflow(path string) string {
return strings.Split(path, ":")[0]
}