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

Example DB and HTTP custom Probe configs #1123

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
35 changes: 16 additions & 19 deletions internal/pkg/instrumentation/bpf/database/sql/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package sql

import (
"log/slog"
"os"
"strconv"

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
Expand All @@ -28,21 +26,33 @@ const (
IncludeDBStatementEnvVar = "OTEL_GO_AUTO_INCLUDE_DB_STATEMENT"
)

type Config struct {
// IncludeQueryText enables or disables inclusion of sql query text in the trace.
IncludeQueryText bool
}

func (c *Config) Package() string {
return pkg
}

// New returns a new [probe.Probe].
func New(logger *slog.Logger) probe.Probe {
func New(logger *slog.Logger, config probe.Config) probe.Probe {
cfg := config.(*Config)

id := probe.ID{
SpanKind: trace.SpanKindClient,
InstrumentedPkg: pkg,
}
return &probe.Base[bpfObjects, event]{
ID: id,
Logger: logger,
ID: id,
ProbeConfig: cfg,
Logger: logger,
Consts: []probe.Const{
probe.RegistersABIConst{},
probe.AllocationConst{},
probe.KeyValConst{
Key: "should_include_db_statement",
Val: shouldIncludeDBStatement(),
Val: cfg.IncludeQueryText,
},
},
Uprobes: []probe.Uprobe{
Expand Down Expand Up @@ -108,16 +118,3 @@ func convertEvent(e *event) []*probe.SpanEvent {
},
}
}

// shouldIncludeDBStatement returns if the user has configured SQL queries to be included.
func shouldIncludeDBStatement() bool {
val := os.Getenv(IncludeDBStatementEnvVar)
if val != "" {
boolVal, err := strconv.ParseBool(val)
if err == nil {
return boolVal
}
}

return false
}
19 changes: 15 additions & 4 deletions internal/pkg/instrumentation/bpf/net/http/client/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ const (
pkg = "net/http"
)

type Config struct {
// SupportsContextPropagation indicates whether the kernel supports context propagation.
SupportsContextPropagation bool
}

func (c *Config) Package() string {
return pkg
}

// New returns a new [probe.Probe].
func New(logger *slog.Logger) probe.Probe {
func New(logger *slog.Logger, config probe.Config) probe.Probe {
cfg := config.(*Config)
id := probe.ID{
SpanKind: trace.SpanKindClient,
InstrumentedPkg: pkg,
Expand All @@ -49,7 +59,7 @@ func New(logger *slog.Logger) probe.Probe {

// If the kernel supports context propagation, we enable the
// probe which writes the data in the outgoing buffer.
if utils.SupportsContextPropagation() {
if cfg.SupportsContextPropagation {
uprobes = append(uprobes,
probe.Uprobe{
Sym: "net/http.Header.writeSubset",
Expand All @@ -63,8 +73,9 @@ func New(logger *slog.Logger) probe.Probe {
}

return &probe.Base[bpfObjects, event]{
ID: id,
Logger: logger,
ID: id,
ProbeConfig: cfg,
Logger: logger,
Consts: []probe.Const{
probe.RegistersABIConst{},
probe.AllocationConst{},
Expand Down
13 changes: 11 additions & 2 deletions internal/pkg/instrumentation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
httpServer "go.opentelemetry.io/auto/internal/pkg/instrumentation/bpf/net/http/server"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/bpffs"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"go.opentelemetry.io/auto/internal/pkg/opentelemetry"
"go.opentelemetry.io/auto/internal/pkg/process"
)
Expand Down Expand Up @@ -366,8 +367,16 @@ func availableProbes(l *slog.Logger, withTraceGlobal bool) []probe.Probe {
grpcClient.New(l),
grpcServer.New(l),
httpServer.New(l),
httpClient.New(l),
dbSql.New(l),
httpClient.New(l,
&httpClient.Config{
SupportsContextPropagation: utils.SupportsContextPropagation(),
},
),
dbSql.New(l,
&dbSql.Config{
IncludeQueryText: true, //example
},
),
kafkaProducer.New(l),
kafkaConsumer.New(l),
autosdk.New(l),
Expand Down
10 changes: 10 additions & 0 deletions internal/pkg/instrumentation/probe/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package probe provides instrumentation probe types and definitions.
package probe

type Config interface {
// Package returns the name of the package instrumented by this [Probe].
Package() string
}
11 changes: 11 additions & 0 deletions internal/pkg/instrumentation/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type Probe interface {
// Run runs the events processing loop.
Run(eventsChan chan<- *Event)

// Config returns this Probe's Config
Config() Config

// Close stops the Probe.
Close() error
}
Expand Down Expand Up @@ -73,6 +76,8 @@ type Base[BPFObj any, BPFEvent any] struct {
// all records will be read directly into a new BPFEvent using the
// encoding/binary package.
ProcessRecord func(perf.Record) (BPFEvent, error)
// ProbeConfig is the Config for this Probe.
ProbeConfig Config

reader *perf.Reader
collection *ebpf.Collection
Expand All @@ -88,6 +93,10 @@ const (
DefaultBufferMapName = "events"
)

func (i *Base[BPFObj, BPFEvent]) Config() Config {
return i.ProbeConfig
}

// Manifest returns the Probe's instrumentation Manifest.
func (i *Base[BPFObj, BPFEvent]) Manifest() Manifest {
var structFieldIDs []structfield.ID
Expand Down Expand Up @@ -125,11 +134,13 @@ func (i *Base[BPFObj, BPFEvent]) Load(exec *link.Executable, td *process.TargetD
if err != nil {
return err
}
i.Logger.Info("loaded BPF collection", "pkg", i.ProbeConfig.Package())

err = i.loadUprobes(exec, td)
if err != nil {
return err
}
i.Logger.Info("loaded uprobes", "pkg", i.ProbeConfig.Package())

err = i.initReader()
if err != nil {
Expand Down
Loading