-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathtrace_otel.go
71 lines (58 loc) · 2.01 KB
/
trace_otel.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
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gtprof
import (
"strconv"
"code.gitea.io/gitea/modules/otelexporter"
)
func otelToSpan(traceID string, t *traceBuiltinSpan, scopeSpan *otelexporter.OtelScopeSpan) {
t.ts.mu.RLock()
defer t.ts.mu.RUnlock()
span := &otelexporter.OtelSpan{
TraceID: traceID,
SpanID: t.ts.id,
Name: t.ts.name,
StartTimeUnixNano: strconv.FormatInt(t.ts.startTime.UnixNano(), 10),
EndTimeUnixNano: strconv.FormatInt(t.ts.endTime.UnixNano(), 10),
Kind: 2,
}
if t.ts.parent != nil {
span.ParentSpanID = t.ts.parent.id
}
scopeSpan.Spans = append(scopeSpan.Spans, span)
for _, a := range t.ts.attributes {
var otelVal any
if a.Value.IsString() {
otelVal = otelexporter.OtelAttributeStringValue{StringValue: a.Value.AsString()}
} else {
otelVal = otelexporter.OtelAttributeIntValue{IntValue: strconv.FormatInt(a.Value.AsInt64(), 10)}
}
span.Attributes = append(span.Attributes, &otelexporter.OtelAttribute{Key: a.Key, Value: otelVal})
}
for _, c := range t.ts.children {
child := c.internalSpans[t.internalSpanIdx].(*traceBuiltinSpan)
otelToSpan(traceID, child, scopeSpan)
}
}
func otelRecordTrace(t *traceBuiltinSpan) {
exporter := otelexporter.GetDefaultOtelExporter()
if exporter == nil {
return
}
opts := tracerOptions.Load()
scopeSpan := &otelexporter.OtelScopeSpan{
Scope: &otelexporter.OtelScope{Name: "gitea-server", Version: opts.AppVer},
}
traceID := GetTracer().randomHexForBytes(16)
otelToSpan(traceID, t, scopeSpan)
resSpans := otelexporter.OtelResourceSpan{
Resource: &otelexporter.OtelResource{
Attributes: []*otelexporter.OtelAttribute{
{Key: "service.name", Value: otelexporter.OtelAttributeStringValue{StringValue: opts.ServiceName}},
},
},
ScopeSpans: []*otelexporter.OtelScopeSpan{scopeSpan},
}
otelTrace := &otelexporter.OtelTrace{ResourceSpans: []*otelexporter.OtelResourceSpan{&resSpans}}
exporter.ExportTrace(otelTrace)
}