-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathprobe.go
143 lines (127 loc) · 4.24 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package grpc
import (
"fmt"
"strconv"
"strings"
"github.com/cilium/ebpf"
"github.com/go-logr/logr"
"go.opentelemetry.io/otel/attribute"
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/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"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 = "google.golang.org/grpc"
)
// New returns a new [probe.Probe].
func New(logger logr.Logger) probe.Probe {
id := probe.ID{
SpanKind: trace.SpanKindClient,
InstrumentedPkg: pkg,
}
return &probe.Base[bpfObjects, event]{
ID: id,
Logger: logger.WithName(id.String()),
Consts: []probe.Const{
probe.RegistersABIConst{},
probe.AllocationConst{},
probe.StructFieldConst{
Key: "clientconn_target_ptr_pos",
Val: structfield.NewID("google.golang.org/grpc", "google.golang.org/grpc", "ClientConn", "target"),
},
probe.StructFieldConst{
Key: "httpclient_nextid_pos",
Val: structfield.NewID("google.golang.org/grpc", "google.golang.org/grpc/internal/transport", "http2Client", "nextID"),
},
probe.StructFieldConst{
Key: "headerFrame_hf_pos",
Val: structfield.NewID("google.golang.org/grpc", "google.golang.org/grpc/internal/transport", "headerFrame", "hf"),
},
probe.StructFieldConst{
Key: "headerFrame_streamid_pos",
Val: structfield.NewID("google.golang.org/grpc", "google.golang.org/grpc/internal/transport", "headerFrame", "streamID"),
},
},
Uprobes: []probe.Uprobe{
{
Sym: "google.golang.org/grpc.(*ClientConn).Invoke",
EntryProbe: "uprobe_ClientConn_Invoke",
ReturnProbe: "uprobe_ClientConn_Invoke_Returns",
},
{
Sym: "google.golang.org/grpc/internal/transport.(*http2Client).NewStream",
EntryProbe: "uprobe_http2Client_NewStream",
},
{
Sym: "google.golang.org/grpc/internal/transport.(*loopyWriter).headerHandler",
EntryProbe: "uprobe_LoopyWriter_HeaderHandler",
},
},
SpecFn: verifyAndLoadBpf,
ProcessFn: convertEvent,
}
}
func verifyAndLoadBpf() (*ebpf.CollectionSpec, error) {
if !utils.SupportsContextPropagation() {
return nil, fmt.Errorf("the Linux Kernel doesn't support context propagation, please check if the kernel is in lockdown mode (/sys/kernel/security/lockdown)")
}
return loadBpf()
}
// event represents an event in the gRPC client during a gRPC request.
type event struct {
context.BaseSpanProperties
Method [50]byte
Target [50]byte
}
// According to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/rpc.md
func convertEvent(e *event) []*probe.SpanEvent {
method := unix.ByteSliceToString(e.Method[:])
target := unix.ByteSliceToString(e.Target[:])
var attrs []attribute.KeyValue
// remove port
if parts := strings.Split(target, ":"); len(parts) > 1 {
target = parts[0]
if remotePeerPortInt, err := strconv.Atoi(parts[1]); err == nil {
attrs = append(attrs, semconv.NetworkPeerPort(remotePeerPortInt))
}
}
attrs = append(attrs, semconv.RPCSystemKey.String("grpc"),
semconv.RPCServiceKey.String(method),
semconv.ServerAddress(target))
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
}
return []*probe.SpanEvent{
{
SpanName: method,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
Attributes: attrs,
SpanContext: &sc,
ParentSpanContext: pscPtr,
TracerSchema: semconv.SchemaURL,
},
}
}