-
Notifications
You must be signed in to change notification settings - Fork 78
/
traces.go
156 lines (138 loc) · 4.52 KB
/
traces.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
package alloy
import (
"context"
"log/slog"
"github.com/mariomac/pipes/pipe"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"github.com/grafana/beyla/pkg/beyla"
"github.com/grafana/beyla/pkg/internal/export/otel"
"github.com/grafana/beyla/pkg/internal/request"
)
// TracesReceiver creates a terminal node that consumes request.Spans and sends OpenTelemetry traces to the configured consumers.
func TracesReceiver(ctx context.Context, cfg *beyla.TracesReceiverConfig) pipe.FinalProvider[[]request.Span] {
return (&tracesReceiver{ctx: ctx, cfg: cfg}).provideLoop
}
type tracesReceiver struct {
ctx context.Context
cfg *beyla.TracesReceiverConfig
}
func (tr *tracesReceiver) provideLoop() (pipe.FinalFunc[[]request.Span], error) {
if !tr.cfg.Enabled() {
return pipe.IgnoreFinal[[]request.Span](), nil
}
return func(in <-chan []request.Span) {
for spans := range in {
for i := range spans {
span := &spans[i]
if span.IgnoreSpan == request.IgnoreTraces {
continue
}
for _, tc := range tr.cfg.Traces {
traces := generateTraces(span)
err := tc.ConsumeTraces(tr.ctx, traces)
if err != nil {
slog.Error("error sending trace to consumer", "error", err)
}
}
}
}
}, nil
}
// generateTraces creates a ptrace.Traces from a request.Span
func generateTraces(span *request.Span) ptrace.Traces {
t := span.Timings()
start := otel.SpanStartTime(t)
hasSubSpans := t.Start.After(start)
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
ss := rs.ScopeSpans().AppendEmpty()
resourceAttrs := attrsToMap(otel.Resource(span.ServiceID).Attributes())
resourceAttrs.CopyTo(rs.Resource().Attributes())
traceID := pcommon.TraceID(span.TraceID)
spanID := pcommon.SpanID(otel.RandomSpanID())
if traceID.IsEmpty() {
traceID = pcommon.TraceID(otel.RandomTraceID())
}
if hasSubSpans {
createSubSpans(span, spanID, traceID, &ss, t)
} else if span.SpanID.IsValid() {
spanID = pcommon.SpanID(span.SpanID)
}
// Create a parent span for the whole request session
s := ss.Spans().AppendEmpty()
s.SetName(otel.TraceName(span))
s.SetKind(ptrace.SpanKind(otel.SpanKind(span)))
s.SetStartTimestamp(pcommon.NewTimestampFromTime(start))
// Set trace and span IDs
s.SetSpanID(spanID)
s.SetTraceID(traceID)
if span.ParentSpanID.IsValid() {
s.SetParentSpanID(pcommon.SpanID(span.ParentSpanID))
}
// Set span attributes
attrs := otel.TraceAttributes(span)
m := attrsToMap(attrs)
m.CopyTo(s.Attributes())
// Set status code
statusCode := codeToStatusCode(otel.SpanStatusCode(span))
s.Status().SetCode(statusCode)
s.SetEndTimestamp(pcommon.NewTimestampFromTime(t.End))
return traces
}
// createSubSpans creates the internal spans for a request.Span
func createSubSpans(span *request.Span, parentSpanID pcommon.SpanID, traceID pcommon.TraceID, ss *ptrace.ScopeSpans, t request.Timings) {
// Create a child span showing the queue time
spQ := ss.Spans().AppendEmpty()
spQ.SetName("in queue")
spQ.SetStartTimestamp(pcommon.NewTimestampFromTime(t.RequestStart))
spQ.SetKind(ptrace.SpanKindInternal)
spQ.SetEndTimestamp(pcommon.NewTimestampFromTime(t.Start))
spQ.SetTraceID(traceID)
spQ.SetSpanID(pcommon.SpanID(otel.RandomSpanID()))
spQ.SetParentSpanID(parentSpanID)
// Create a child span showing the processing time
spP := ss.Spans().AppendEmpty()
spP.SetName("processing")
spP.SetStartTimestamp(pcommon.NewTimestampFromTime(t.Start))
spP.SetKind(ptrace.SpanKindInternal)
spP.SetEndTimestamp(pcommon.NewTimestampFromTime(t.End))
spP.SetTraceID(traceID)
if span.SpanID.IsValid() {
spP.SetSpanID(pcommon.SpanID(span.SpanID))
} else {
spP.SetSpanID(pcommon.SpanID(otel.RandomSpanID()))
}
spP.SetParentSpanID(parentSpanID)
}
// attrsToMap converts a slice of attribute.KeyValue to a pcommon.Map
func attrsToMap(attrs []attribute.KeyValue) pcommon.Map {
m := pcommon.NewMap()
for _, attr := range attrs {
switch v := attr.Value.AsInterface().(type) {
case string:
m.PutStr(string(attr.Key), v)
case int64:
m.PutInt(string(attr.Key), v)
case float64:
m.PutDouble(string(attr.Key), v)
case bool:
m.PutBool(string(attr.Key), v)
}
}
return m
}
// codeToStatusCode converts a codes.Code to a ptrace.StatusCode
func codeToStatusCode(code codes.Code) ptrace.StatusCode {
switch code {
case codes.Unset:
return ptrace.StatusCodeUnset
case codes.Error:
return ptrace.StatusCodeError
case codes.Ok:
return ptrace.StatusCodeOk
}
return ptrace.StatusCodeUnset
}