Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor probe loading to use ebpf.Collection #899

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 9 additions & 75 deletions internal/pkg/instrumentation/bpf/database/sql/probe.go
Original file line number Diff line number Diff line change
@@ -18,8 +18,6 @@ import (
"os"
"strconv"

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

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/process"
)

//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 {
Val: shouldIncludeDBStatement(),
},
},
Uprobes: []probe.Uprobe[bpfObjects]{
Uprobes: []probe.Uprobe{
{
Sym: "database/sql.(*DB).queryDC",
Fn: uprobeQueryDC,
Optional: true,
Sym: "database/sql.(*DB).queryDC",
EntryProbe: "uprobe_queryDC",
ReturnProbe: "uprobe_queryDC_Returns",
Optional: true,
},
{
Sym: "database/sql.(*DB).execDC",
Fn: uprobeExecDC,
Optional: true,
Sym: "database/sql.(*DB).execDC",
EntryProbe: "uprobe_execDC",
ReturnProbe: "uprobe_execDC_Returns",
Optional: true,
},
},

ReaderFn: func(obj bpfObjects) (*perf.Reader, error) {
return perf.NewReader(obj.Events, os.Getpagesize())
},
SpecFn: loadBpf,
ProcessFn: convertEvent,
}
}

func uprobeQueryDC(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
offset, err := target.GetFunctionOffset(name)
if err != nil {
return nil, err
}

opts := &link.UprobeOptions{Address: offset, PID: target.PID}
l, err := exec.Uprobe("", obj.UprobeQueryDC, opts)
if err != nil {
return nil, err
}

links := []link.Link{l}

retOffsets, err := target.GetFunctionReturns(name)
if err != nil {
return nil, err
}

for _, ret := range retOffsets {
opts := &link.UprobeOptions{Address: ret}
l, err := exec.Uprobe("", obj.UprobeQueryDC_Returns, opts)
if err != nil {
return nil, err
}
links = append(links, l)
}

return links, nil
}

func uprobeExecDC(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
offset, err := target.GetFunctionOffset(name)
if err != nil {
return nil, err
}

opts := &link.UprobeOptions{Address: offset, PID: target.PID}
l, err := exec.Uprobe("", obj.UprobeExecDC, opts)
if err != nil {
return nil, err
}

links := []link.Link{l}

retOffsets, err := target.GetFunctionReturns(name)
if err != nil {
return nil, err
}

for _, ret := range retOffsets {
opts := &link.UprobeOptions{Address: ret}
l, err := exec.Uprobe("", obj.UprobeExecDC_Returns, opts)
if err != nil {
return nil, err
}
links = append(links, l)
}

return links, nil
}

// event represents an event in an SQL database
// request-response.
type event struct {
Original file line number Diff line number Diff line change
@@ -16,10 +16,7 @@ package consumer

import (
"fmt"
"os"

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

"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"
)

@@ -80,51 +76,18 @@ func New(logger logr.Logger) probe.Probe {
Val: structfield.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "ReaderConfig", "GroupID"),
},
},
Uprobes: []probe.Uprobe[bpfObjects]{
Uprobes: []probe.Uprobe{
{
Sym: "github.com/segmentio/kafka-go.(*Reader).FetchMessage",
Fn: uprobeFetchMessage,
Sym: "github.com/segmentio/kafka-go.(*Reader).FetchMessage",
EntryProbe: "uprobe_FetchMessage",
ReturnProbe: "uprobe_FetchMessage_Returns",
},
},
ReaderFn: func(obj bpfObjects) (*perf.Reader, error) {
return perf.NewReader(obj.Events, os.Getpagesize()*100)
},
SpecFn: loadBpf,
ProcessFn: convertEvent,
}
}

func uprobeFetchMessage(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
offset, err := target.GetFunctionOffset(name)
if err != nil {
return nil, err
}

opts := &link.UprobeOptions{Address: offset}
l, err := exec.Uprobe("", obj.UprobeFetchMessage, opts)
if err != nil {
return nil, err
}

links := []link.Link{l}

retOffsets, err := target.GetFunctionReturns(name)
if err != nil {
return nil, err
}

for _, ret := range retOffsets {
opts := &link.UprobeOptions{Address: ret}
l, err := exec.Uprobe("", obj.UprobeFetchMessageReturns, opts)
if err != nil {
return nil, err
}
links = append(links, l)
}

return links, nil
}

// event represents a kafka message received by the consumer.
type event struct {
context.BaseSpanProperties
Original file line number Diff line number Diff line change
@@ -16,10 +16,7 @@ package producer

import (
"fmt"
"os"

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

"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"
)

@@ -68,51 +64,18 @@ func New(logger logr.Logger) probe.Probe {
Val: structfield.NewID("github.com/segmentio/kafka-go", "github.com/segmentio/kafka-go", "Message", "Time"),
},
},
Uprobes: []probe.Uprobe[bpfObjects]{
Uprobes: []probe.Uprobe{
{
Sym: "github.com/segmentio/kafka-go.(*Writer).WriteMessages",
Fn: uprobeWriteMessages,
Sym: "github.com/segmentio/kafka-go.(*Writer).WriteMessages",
EntryProbe: "uprobe_WriteMessages",
ReturnProbe: "uprobe_WriteMessages_Returns",
},
},
ReaderFn: func(obj bpfObjects) (*perf.Reader, error) {
return perf.NewReader(obj.Events, os.Getpagesize()*100)
},
SpecFn: loadBpf,
ProcessFn: convertEvent,
}
}

func uprobeWriteMessages(name string, exec *link.Executable, target *process.TargetDetails, obj *bpfObjects) ([]link.Link, error) {
offset, err := target.GetFunctionOffset(name)
if err != nil {
return nil, err
}

opts := &link.UprobeOptions{Address: offset}
l, err := exec.Uprobe("", obj.UprobeWriteMessages, opts)
if err != nil {
return nil, err
}

links := []link.Link{l}

retOffsets, err := target.GetFunctionReturns(name)
if err != nil {
return nil, err
}

for _, ret := range retOffsets {
opts := &link.UprobeOptions{Address: ret}
l, err := exec.Uprobe("", obj.UprobeWriteMessagesReturns, opts)
if err != nil {
return nil, err
}
links = append(links, l)
}

return links, nil
}

type messageAttributes struct {
SpaID trace.SpanID
Topic [256]byte
Loading
Loading