-
Notifications
You must be signed in to change notification settings - Fork 83
/
probe.go
227 lines (203 loc) · 6.33 KB
/
probe.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package server
import (
"strings"
"github.com/go-logr/logr"
"github.com/hashicorp/go-version"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sys/unix"
"go.opentelemetry.io/auto/internal/pkg/inject"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/bpf/net/http"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/process"
"go.opentelemetry.io/auto/internal/pkg/structfield"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c
const (
// pkg is the package being instrumented.
pkg = "net/http"
)
// New returns a new [probe.Probe].
func New(logger logr.Logger) probe.Probe {
id := probe.ID{
SpanKind: trace.SpanKindServer,
InstrumentedPkg: pkg,
}
return &probe.Base[bpfObjects, event]{
ID: id,
Logger: logger.WithName(id.String()),
Consts: []probe.Const{
probe.RegistersABIConst{},
probe.StructFieldConst{
Key: "method_ptr_pos",
Val: structfield.NewID("std", "net/http", "Request", "Method"),
},
probe.StructFieldConst{
Key: "url_ptr_pos",
Val: structfield.NewID("std", "net/http", "Request", "URL"),
},
probe.StructFieldConst{
Key: "ctx_ptr_pos",
Val: structfield.NewID("std", "net/http", "Request", "ctx"),
},
probe.StructFieldConst{
Key: "path_ptr_pos",
Val: structfield.NewID("std", "net/url", "URL", "Path"),
},
probe.StructFieldConst{
Key: "headers_ptr_pos",
Val: structfield.NewID("std", "net/http", "Request", "Header"),
},
probe.StructFieldConst{
Key: "req_ptr_pos",
Val: structfield.NewID("std", "net/http", "response", "req"),
},
probe.StructFieldConst{
Key: "status_code_pos",
Val: structfield.NewID("std", "net/http", "response", "status"),
},
probe.StructFieldConst{
Key: "buckets_ptr_pos",
Val: structfield.NewID("std", "runtime", "hmap", "buckets"),
},
probe.StructFieldConst{
Key: "remote_addr_pos",
Val: structfield.NewID("std", "net/http", "Request", "RemoteAddr"),
},
probe.StructFieldConst{
Key: "host_pos",
Val: structfield.NewID("std", "net/http", "Request", "Host"),
},
probe.StructFieldConst{
Key: "proto_pos",
Val: structfield.NewID("std", "net/http", "Request", "Proto"),
},
probe.StructFieldConstMinVersion{
StructField: probe.StructFieldConst{
Key: "req_pat_pos",
Val: structfield.NewID("std", "net/http", "Request", "pat"),
},
MinVersion: patternPathMinVersion,
},
probe.StructFieldConstMinVersion{
StructField: probe.StructFieldConst{
Key: "pat_str_pos",
Val: structfield.NewID("std", "net/http", "pattern", "str"),
},
MinVersion: patternPathMinVersion,
},
patternPathSupportedConst{},
},
Uprobes: []probe.Uprobe{
{
Sym: "net/http.serverHandler.ServeHTTP",
EntryProbe: "uprobe_serverHandler_ServeHTTP",
ReturnProbe: "uprobe_serverHandler_ServeHTTP_Returns",
},
},
SpecFn: loadBpf,
ProcessFn: convertEvent,
}
}
type patternPathSupportedConst struct{}
var (
patternPathMinVersion = version.Must(version.NewVersion("1.22.0"))
isPatternPathSupported = false
)
func (c patternPathSupportedConst) InjectOption(td *process.TargetDetails) (inject.Option, error) {
isPatternPathSupported = td.GoVersion.GreaterThanOrEqual(patternPathMinVersion)
return inject.WithKeyValue("pattern_path_supported", isPatternPathSupported), nil
}
// event represents an event in an HTTP server during an HTTP
// request-response.
type event struct {
context.BaseSpanProperties
StatusCode uint64
Method [8]byte
Path [128]byte
PathPattern [128]byte
RemoteAddr [256]byte
Host [256]byte
Proto [8]byte
}
func convertEvent(e *event) []*probe.SpanEvent {
path := unix.ByteSliceToString(e.Path[:])
method := unix.ByteSliceToString(e.Method[:])
patternPath := unix.ByteSliceToString(e.PathPattern[:])
isValidPatternPath := true
patternPath, err := http.ParsePattern(patternPath)
if err != nil || patternPath == "" {
isValidPatternPath = false
}
proto := unix.ByteSliceToString(e.Proto[:])
sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: e.SpanContext.TraceID,
SpanID: e.SpanContext.SpanID,
TraceFlags: trace.FlagsSampled,
})
var pscPtr *trace.SpanContext
if e.ParentSpanContext.TraceID.IsValid() {
psc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: e.ParentSpanContext.TraceID,
SpanID: e.ParentSpanContext.SpanID,
TraceFlags: trace.FlagsSampled,
Remote: true,
})
pscPtr = &psc
} else {
pscPtr = nil
}
attributes := []attribute.KeyValue{
semconv.HTTPRequestMethodKey.String(method),
semconv.URLPath(path),
semconv.HTTPResponseStatusCodeKey.Int(int(e.StatusCode)),
}
// Client address and port
peerAddr, peerPort := http.NetPeerAddressPortAttributes(e.RemoteAddr[:])
if peerAddr.Valid() {
attributes = append(attributes, peerAddr)
}
if peerPort.Valid() {
attributes = append(attributes, peerPort)
}
// Server address and port
serverAddr, serverPort := http.ServerAddressPortAttributes(e.Host[:])
if serverAddr.Valid() {
attributes = append(attributes, serverAddr)
}
if serverPort.Valid() {
attributes = append(attributes, serverPort)
}
if proto != "" {
parts := strings.Split(proto, "/")
if len(parts) == 2 {
if parts[0] != "HTTP" {
attributes = append(attributes, semconv.NetworkProtocolName(parts[0]))
}
attributes = append(attributes, semconv.NetworkProtocolVersion(parts[1]))
}
}
spanName := method
if isPatternPathSupported && isValidPatternPath {
spanName = spanName + " " + patternPath
attributes = append(attributes, semconv.HTTPRouteKey.String(patternPath))
}
spanEvent := &probe.SpanEvent{
SpanName: spanName,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
SpanContext: &sc,
ParentSpanContext: pscPtr,
Attributes: attributes,
TracerSchema: semconv.SchemaURL,
}
if int(e.StatusCode) >= 500 && int(e.StatusCode) < 600 {
spanEvent.Status = probe.Status{Code: codes.Error}
}
return []*probe.SpanEvent{spanEvent}
}