Skip to content

Commit 609b22e

Browse files
authored
Refactor probe loading to use ebpf.Collection (#899)
1 parent b18e7da commit 609b22e

File tree

10 files changed

+177
-539
lines changed

10 files changed

+177
-539
lines changed

internal/pkg/instrumentation/bpf/database/sql/probe.go

Lines changed: 9 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"os"
1919
"strconv"
2020

21-
"github.com/cilium/ebpf/link"
22-
"github.com/cilium/ebpf/perf"
2321
"github.com/go-logr/logr"
2422
"go.opentelemetry.io/otel/attribute"
2523
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
@@ -28,7 +26,6 @@ import (
2826

2927
"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
3028
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
31-
"go.opentelemetry.io/auto/internal/pkg/process"
3229
)
3330

3431
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c
@@ -58,89 +55,26 @@ func New(logger logr.Logger) probe.Probe {
5855
Val: shouldIncludeDBStatement(),
5956
},
6057
},
61-
Uprobes: []probe.Uprobe[bpfObjects]{
58+
Uprobes: []probe.Uprobe{
6259
{
63-
Sym: "database/sql.(*DB).queryDC",
64-
Fn: uprobeQueryDC,
65-
Optional: true,
60+
Sym: "database/sql.(*DB).queryDC",
61+
EntryProbe: "uprobe_queryDC",
62+
ReturnProbe: "uprobe_queryDC_Returns",
63+
Optional: true,
6664
},
6765
{
68-
Sym: "database/sql.(*DB).execDC",
69-
Fn: uprobeExecDC,
70-
Optional: true,
66+
Sym: "database/sql.(*DB).execDC",
67+
EntryProbe: "uprobe_execDC",
68+
ReturnProbe: "uprobe_execDC_Returns",
69+
Optional: true,
7170
},
7271
},
7372

74-
ReaderFn: func(obj bpfObjects) (*perf.Reader, error) {
75-
return perf.NewReader(obj.Events, os.Getpagesize())
76-
},
7773
SpecFn: loadBpf,
7874
ProcessFn: convertEvent,
7975
}
8076
}
8177

82-
func uprobeQueryDC(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
83-
offset, err := target.GetFunctionOffset(name)
84-
if err != nil {
85-
return nil, err
86-
}
87-
88-
opts := &link.UprobeOptions{Address: offset, PID: target.PID}
89-
l, err := exec.Uprobe("", obj.UprobeQueryDC, opts)
90-
if err != nil {
91-
return nil, err
92-
}
93-
94-
links := []link.Link{l}
95-
96-
retOffsets, err := target.GetFunctionReturns(name)
97-
if err != nil {
98-
return nil, err
99-
}
100-
101-
for _, ret := range retOffsets {
102-
opts := &link.UprobeOptions{Address: ret}
103-
l, err := exec.Uprobe("", obj.UprobeQueryDC_Returns, opts)
104-
if err != nil {
105-
return nil, err
106-
}
107-
links = append(links, l)
108-
}
109-
110-
return links, nil
111-
}
112-
113-
func uprobeExecDC(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
114-
offset, err := target.GetFunctionOffset(name)
115-
if err != nil {
116-
return nil, err
117-
}
118-
119-
opts := &link.UprobeOptions{Address: offset, PID: target.PID}
120-
l, err := exec.Uprobe("", obj.UprobeExecDC, opts)
121-
if err != nil {
122-
return nil, err
123-
}
124-
125-
links := []link.Link{l}
126-
127-
retOffsets, err := target.GetFunctionReturns(name)
128-
if err != nil {
129-
return nil, err
130-
}
131-
132-
for _, ret := range retOffsets {
133-
opts := &link.UprobeOptions{Address: ret}
134-
l, err := exec.Uprobe("", obj.UprobeExecDC_Returns, opts)
135-
if err != nil {
136-
return nil, err
137-
}
138-
links = append(links, l)
139-
}
140-
141-
return links, nil
142-
}
143-
14478
// event represents an event in an SQL database
14579
// request-response.
14680
type event struct {

internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/consumer/probe.go

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ package consumer
1616

1717
import (
1818
"fmt"
19-
"os"
2019

21-
"github.com/cilium/ebpf/link"
22-
"github.com/cilium/ebpf/perf"
2320
"github.com/go-logr/logr"
2421
"go.opentelemetry.io/otel/attribute"
2522
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
@@ -28,7 +25,6 @@ import (
2825

2926
"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
3027
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
31-
"go.opentelemetry.io/auto/internal/pkg/process"
3228
"go.opentelemetry.io/auto/internal/pkg/structfield"
3329
)
3430

@@ -80,51 +76,18 @@ func New(logger logr.Logger) probe.Probe {
8076
Val: structfield.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "ReaderConfig", "GroupID"),
8177
},
8278
},
83-
Uprobes: []probe.Uprobe[bpfObjects]{
79+
Uprobes: []probe.Uprobe{
8480
{
85-
Sym: "github.com/segmentio/kafka-go.(*Reader).FetchMessage",
86-
Fn: uprobeFetchMessage,
81+
Sym: "github.com/segmentio/kafka-go.(*Reader).FetchMessage",
82+
EntryProbe: "uprobe_FetchMessage",
83+
ReturnProbe: "uprobe_FetchMessage_Returns",
8784
},
8885
},
89-
ReaderFn: func(obj bpfObjects) (*perf.Reader, error) {
90-
return perf.NewReader(obj.Events, os.Getpagesize()*100)
91-
},
9286
SpecFn: loadBpf,
9387
ProcessFn: convertEvent,
9488
}
9589
}
9690

97-
func uprobeFetchMessage(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
98-
offset, err := target.GetFunctionOffset(name)
99-
if err != nil {
100-
return nil, err
101-
}
102-
103-
opts := &link.UprobeOptions{Address: offset}
104-
l, err := exec.Uprobe("", obj.UprobeFetchMessage, opts)
105-
if err != nil {
106-
return nil, err
107-
}
108-
109-
links := []link.Link{l}
110-
111-
retOffsets, err := target.GetFunctionReturns(name)
112-
if err != nil {
113-
return nil, err
114-
}
115-
116-
for _, ret := range retOffsets {
117-
opts := &link.UprobeOptions{Address: ret}
118-
l, err := exec.Uprobe("", obj.UprobeFetchMessageReturns, opts)
119-
if err != nil {
120-
return nil, err
121-
}
122-
links = append(links, l)
123-
}
124-
125-
return links, nil
126-
}
127-
12891
// event represents a kafka message received by the consumer.
12992
type event struct {
13093
context.BaseSpanProperties

internal/pkg/instrumentation/bpf/github.com/segmentio/kafka-go/producer/probe.go

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ package producer
1616

1717
import (
1818
"fmt"
19-
"os"
2019

21-
"github.com/cilium/ebpf/link"
22-
"github.com/cilium/ebpf/perf"
2320
"github.com/go-logr/logr"
2421
"go.opentelemetry.io/otel/attribute"
2522
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
@@ -28,7 +25,6 @@ import (
2825

2926
"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
3027
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
31-
"go.opentelemetry.io/auto/internal/pkg/process"
3228
"go.opentelemetry.io/auto/internal/pkg/structfield"
3329
)
3430

@@ -68,51 +64,18 @@ func New(logger logr.Logger) probe.Probe {
6864
Val: structfield.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Time"),
6965
},
7066
},
71-
Uprobes: []probe.Uprobe[bpfObjects]{
67+
Uprobes: []probe.Uprobe{
7268
{
73-
Sym: "github.com/segmentio/kafka-go.(*Writer).WriteMessages",
74-
Fn: uprobeWriteMessages,
69+
Sym: "github.com/segmentio/kafka-go.(*Writer).WriteMessages",
70+
EntryProbe: "uprobe_WriteMessages",
71+
ReturnProbe: "uprobe_WriteMessages_Returns",
7572
},
7673
},
77-
ReaderFn: func(obj bpfObjects) (*perf.Reader, error) {
78-
return perf.NewReader(obj.Events, os.Getpagesize()*100)
79-
},
8074
SpecFn: loadBpf,
8175
ProcessFn: convertEvent,
8276
}
8377
}
8478

85-
func uprobeWriteMessages(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
86-
offset, err := target.GetFunctionOffset(name)
87-
if err != nil {
88-
return nil, err
89-
}
90-
91-
opts := &link.UprobeOptions{Address: offset}
92-
l, err := exec.Uprobe("", obj.UprobeWriteMessages, opts)
93-
if err != nil {
94-
return nil, err
95-
}
96-
97-
links := []link.Link{l}
98-
99-
retOffsets, err := target.GetFunctionReturns(name)
100-
if err != nil {
101-
return nil, err
102-
}
103-
104-
for _, ret := range retOffsets {
105-
opts := &link.UprobeOptions{Address: ret}
106-
l, err := exec.Uprobe("", obj.UprobeWriteMessagesReturns, opts)
107-
if err != nil {
108-
return nil, err
109-
}
110-
links = append(links, l)
111-
}
112-
113-
return links, nil
114-
}
115-
11679
type messageAttributes struct {
11780
SpaID trace.SpanID
11881
Topic [256]byte

0 commit comments

Comments
 (0)