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

Prevent beyla self-instrumentation #1048

Merged
merged 3 commits into from
Jul 23, 2024
Merged
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
13 changes: 13 additions & 0 deletions pkg/internal/discover/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package discover
import (
"context"
"log/slog"
"os"
"path"

"github.com/cilium/ebpf/link"
Expand All @@ -27,6 +28,7 @@ type TraceAttacher struct {
DeleteTracers chan *Instrumentable
Metrics imetrics.Reporter
pinPath string
beylaPID int

// processInstances keeps track of the instances of each process. This will help making sure
// that we don't remove the BPF resources of an executable until all their instances are removed
Expand All @@ -46,6 +48,7 @@ func (ta *TraceAttacher) attacherLoop() (pipe.FinalFunc[[]Event[Instrumentable]]
ta.log = slog.With("component", "discover.TraceAttacher")
ta.existingTracers = map[uint64]*ebpf.ProcessTracer{}
ta.processInstances = maps.MultiCounter[uint64]{}
ta.beylaPID = os.Getpid()
ta.pinPath = BuildPinPath(ta.Cfg)

if err := ta.init(); err != nil {
Expand Down Expand Up @@ -79,6 +82,10 @@ func (ta *TraceAttacher) attacherLoop() (pipe.FinalFunc[[]Event[Instrumentable]]
}, nil
}

func (ta *TraceAttacher) skipSelfInstrumentation(ie *Instrumentable) bool {
return ie.FileInfo.Pid == int32(ta.beylaPID) && !ta.Cfg.Discovery.AllowSelfInstrumentation
}

//nolint:cyclop
func (ta *TraceAttacher) getTracer(ie *Instrumentable) (*ebpf.ProcessTracer, bool) {
if tracer, ok := ta.existingTracers[ie.FileInfo.Ino]; ok {
Expand All @@ -96,6 +103,12 @@ func (ta *TraceAttacher) getTracer(ie *Instrumentable) (*ebpf.ProcessTracer, boo
ta.log.Debug(".done")
return nil, false
}

if ta.skipSelfInstrumentation(ie) {
ta.log.Info("skipping self-instrumentation of Beyla process", "cmd", ie.FileInfo.CmdExePath, "pid", ie.FileInfo.Pid)
return nil, false
}

ta.log.Info("instrumenting process", "cmd", ie.FileInfo.CmdExePath, "pid", ie.FileInfo.Pid)
ta.Metrics.InstrumentProcess(ie.FileInfo.ExecutableName())

Expand Down
57 changes: 57 additions & 0 deletions pkg/internal/discover/attacher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package discover

import (
"log/slog"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/grafana/beyla/pkg/beyla"
"github.com/grafana/beyla/pkg/internal/exec"
"github.com/grafana/beyla/pkg/services"
)

func TestSkipSelfInstrumentation(t *testing.T) {
pid := os.Getpid()

for _, tt := range []struct {
ta *TraceAttacher
isSelf bool
test string
}{
{
ta: &TraceAttacher{
log: slog.With("component", "discover.TraceAttacher"),
Cfg: &beyla.DefaultConfig,
beylaPID: pid,
},
isSelf: true,
test: "Default config",
},
{
ta: &TraceAttacher{
log: slog.With("component", "discover.TraceAttacher"),
Cfg: &beyla.DefaultConfig,
beylaPID: 0,
},
isSelf: false,
test: "Default config, non-beyla pid",
},
{
ta: &TraceAttacher{
log: slog.With("component", "discover.TraceAttacher"),
Cfg: &beyla.Config{Discovery: services.DiscoveryConfig{AllowSelfInstrumentation: true}},
beylaPID: pid,
},
isSelf: false,
test: "Beyla pid, allow self instrumentation",
},
} {
t.Run(tt.test, func(t *testing.T) {
i := Instrumentable{FileInfo: &exec.FileInfo{Pid: int32(pid)}}

assert.Equal(t, tt.isSelf, tt.ta.skipSelfInstrumentation(&i))
})
}
}
2 changes: 2 additions & 0 deletions pkg/services/criteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type DiscoveryConfig struct {
// gathered for certain languages, such as Golang.
SystemWide bool `yaml:"system_wide" env:"BEYLA_SYSTEM_WIDE"`

AllowSelfInstrumentation bool `yaml:"allow_self_instrumentation" env:"BEYLA_ALLOW_SELF_INSTRUMENTATION"`

// This can be enabled to use generic HTTP tracers only, no Go-specifics will be used:
SkipGoSpecificTracers bool `yaml:"skip_go_specific_tracers" env:"BEYLA_SKIP_GO_SPECIFIC_TRACERS"`

Expand Down
11 changes: 11 additions & 0 deletions test/integration/red_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,14 @@ func testPrometheusBeylaBuildInfo(t *testing.T) {
require.NotEmpty(t, results)
})
}

func testPrometheusNoBeylaEvents(t *testing.T) {
pq := prom.Client{HostPort: prometheusHostPort}
var results []prom.Result
test.Eventually(t, testTimeout, func(t require.TestingT) {
var err error
results, err = pq.Query(`http_server_request_duration_seconds_count{service_name="beyla"}`)
require.NoError(t, err)
require.Equal(t, 0, len(results))
})
}
3 changes: 3 additions & 0 deletions test/integration/suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ func TestSuite_PrometheusScrape(t *testing.T) {
compose.Env = append(compose.Env,
`INSTRUMENTER_CONFIG_SUFFIX=-promscrape`,
`PROM_CONFIG_SUFFIX=-promscrape`,
`BEYLA_EXECUTABLE_NAME=`,
`BEYLA_OPEN_PORT=8082,8999`, // force Beyla self-instrumentation to ensure we don't do it
)

require.NoError(t, err)
Expand All @@ -227,6 +229,7 @@ func TestSuite_PrometheusScrape(t *testing.T) {
t.Run("GRPC RED metrics", testREDMetricsGRPC)
t.Run("Internal Prometheus metrics", testInternalPrometheusExport)
t.Run("Testing Beyla Build Info metric", testPrometheusBeylaBuildInfo)
t.Run("Testing for no Beyla self metrics", testPrometheusNoBeylaEvents)
t.Run("Testing process-level metrics", testProcesses(map[string]string{
"process_executable_name": "testserver",
"process_executable_path": "/testserver",
Expand Down
Loading