Skip to content

Commit

Permalink
fix(server): re-enable prometheus counters
Browse files Browse the repository at this point in the history
Commit: 0213c08 ("tracee: add ready callback") made prometheus stats to
stop being registered due to a timing issue. This patch re-enables it
by registering prometheus counters in the callback function itself
instead of in Tracee.New() (too early).

Fixes: #3303
  • Loading branch information
rafaeldtinoco committed Jul 18, 2023
1 parent d8fa964 commit 532b940
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
5 changes: 5 additions & 0 deletions pkg/cmd/flags/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"github.com/aquasecurity/tracee/pkg/errfmt"
"github.com/aquasecurity/tracee/pkg/logger"
"github.com/aquasecurity/tracee/pkg/server"
)

Expand All @@ -28,17 +29,21 @@ func PrepareServer(listenAddr string, metrics, healthz, pprof, pyro bool) (*serv
httpServer := server.New(listenAddr)

if metrics {
logger.Debugw("Enabling metrics endpoint")
httpServer.EnableMetricsEndpoint()
}

if healthz {
logger.Debugw("Enabling healthz endpoint")
httpServer.EnableHealthzEndpoint()
}

if pprof {
logger.Debugw("Enabling pprof endpoint")
httpServer.EnablePProfEndpoint()
}
if pyro {
logger.Debugw("Enabling pyroscope agent")
err := httpServer.EnablePyroAgent()
if err != nil {
return httpServer, err
Expand Down
35 changes: 22 additions & 13 deletions pkg/cmd/tracee.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ func (r Runner) Run(ctx context.Context) error {
return errfmt.Errorf("error creating Tracee: %v", err)
}

// start the server and write pid file only after tracee is ready
t.AddReadyCallback(func(ctx context.Context) {
logger.Debugw("Tracee is ready callback")
// Readiness Callback: Tracee is ready to receive events

// start server if one is configured
if r.Server != nil {
r.TraceeConfig.MetricsEnabled = r.Server.MetricsEndpointEnabled()
t.AddReadyCallback(
func(ctx context.Context) {
logger.Debugw("Tracee is ready callback")
if r.Server == nil {
return
}
if r.Server.MetricsEndpointEnabled() {
r.TraceeConfig.MetricsEnabled = true // TODO: is this needed ?
if err := t.Stats().RegisterPrometheus(); err != nil {
logger.Errorw("Registering prometheus metrics", "error", err)
}
}
go r.Server.Start(ctx)
}
})
},
)

// Initialize tracee

Expand All @@ -51,7 +58,7 @@ func (r Runner) Run(ctx context.Context) error {
}

// Manage PID file
// write pid file

if err := writePidFile(t.OutDir); err != nil {
return errfmt.WrapError(err)
}
Expand All @@ -61,11 +68,12 @@ func (r Runner) Run(ctx context.Context) error {
}
}()

// preeamble
// Preeamble

r.Printer.Preamble()

// Start event channel reception

go func() {
for {
select {
Expand All @@ -77,16 +85,17 @@ func (r Runner) Run(ctx context.Context) error {
}
}()

// This will block until the context is done
// Blocks (until ctx is Done)

err = t.Run(ctx)

// Drain and print the remaining channel events that were sent before full termination
// Drain remaininig channel events (sent during shutdown)

for {
select {
case event := <-r.TraceeConfig.ChanEvents:
r.Printer.Print(event)
default:
// Print statistics at the end
stats := t.Stats()
r.Printer.Epilogue(*stats)
r.Printer.Close()
Expand Down
9 changes: 0 additions & 9 deletions pkg/ebpf/tracee.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,6 @@ func New(cfg config.Config) (*Tracee, error) {

t.triggerContexts = trigger.NewContext()

// Export metrics to prometheus if enabled

if t.config.MetricsEnabled {
err := t.Stats().RegisterPrometheus()
if err != nil {
logger.Errorw("Registering prometheus metrics", "error", err)
}
}

return t, nil
}

Expand Down

0 comments on commit 532b940

Please sign in to comment.