-
Notifications
You must be signed in to change notification settings - Fork 77
/
spanner.go
108 lines (94 loc) · 2.83 KB
/
spanner.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
package ebpfcommon
import (
"bytes"
"log/slog"
"net"
trace2 "go.opentelemetry.io/otel/trace"
"github.com/grafana/beyla/pkg/internal/request"
"github.com/grafana/beyla/pkg/internal/sqlprune"
)
var log = slog.With("component", "goexec.spanner")
func HTTPRequestTraceToSpan(trace *HTTPRequestTrace) request.Span {
// From C, assuming 0-ended strings
methodLen := bytes.IndexByte(trace.Method[:], 0)
if methodLen < 0 {
methodLen = len(trace.Method)
}
method := string(trace.Method[:methodLen])
pathLen := bytes.IndexByte(trace.Path[:], 0)
if pathLen < 0 {
pathLen = len(trace.Path)
}
path := string(trace.Path[:pathLen])
peer := ""
hostname := ""
hostPort := 0
if trace.Conn.S_port != 0 || trace.Conn.D_port != 0 {
peer, hostname = trace.hostInfo()
hostPort = int(trace.Conn.D_port)
}
return request.Span{
Type: request.EventType(trace.Type),
Method: method,
Path: path,
Peer: peer,
Host: hostname,
HostPort: hostPort,
ContentLength: trace.ContentLength,
RequestStart: int64(trace.GoStartMonotimeNs),
Start: int64(trace.StartMonotimeNs),
End: int64(trace.EndMonotimeNs),
Status: int(trace.Status),
TraceID: trace2.TraceID(trace.Tp.TraceId),
SpanID: trace2.SpanID(trace.Tp.SpanId),
ParentSpanID: trace2.SpanID(trace.Tp.ParentId),
Flags: trace.Tp.Flags,
Pid: request.PidInfo{
HostPID: trace.Pid.HostPid,
UserPID: trace.Pid.UserPid,
Namespace: trace.Pid.Ns,
},
}
}
func (trace *HTTPRequestTrace) hostInfo() (source, target string) {
src := make(net.IP, net.IPv6len)
dst := make(net.IP, net.IPv6len)
copy(src, trace.Conn.S_addr[:])
copy(dst, trace.Conn.D_addr[:])
return src.String(), dst.String()
}
func SQLRequestTraceToSpan(trace *SQLRequestTrace) request.Span {
if request.EventType(trace.Type) != request.EventTypeSQLClient {
log.Warn("unknown trace type", "type", trace.Type)
return request.Span{}
}
// From C, assuming 0-ended strings
sqlLen := bytes.IndexByte(trace.Sql[:], 0)
if sqlLen < 0 {
sqlLen = len(trace.Sql)
}
sql := string(trace.Sql[:sqlLen])
method, path := sqlprune.SQLParseOperationAndTable(sql)
return request.Span{
Type: request.EventType(trace.Type),
Method: method,
Path: path,
Peer: "",
Host: "",
HostPort: 0,
ContentLength: 0,
RequestStart: int64(trace.StartMonotimeNs),
Start: int64(trace.StartMonotimeNs),
End: int64(trace.EndMonotimeNs),
Status: int(trace.Status),
TraceID: trace2.TraceID(trace.Tp.TraceId),
SpanID: trace2.SpanID(trace.Tp.SpanId),
ParentSpanID: trace2.SpanID(trace.Tp.ParentId),
Flags: trace.Tp.Flags,
Pid: request.PidInfo{
HostPID: trace.Pid.HostPid,
UserPID: trace.Pid.UserPid,
Namespace: trace.Pid.Ns,
},
}
}