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

Add metric for instrumented processes #928

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/sources/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ Beyla can be [configured to report internal metrics]({{< relref "./configure/opt
| `otel_trace_exports` | Counter | Length of the trace batches submitted to the remote OTEL collector |
| `otel_trace_export_errors` | CounterVec | Error count on each failed OTEL trace export, by error type |
| `prometheus_http_requests` | CounterVec | Number of requests towards the Prometheus Scrape endpoint, faceted by HTTP port and path |
| `beyla_discovered_services` | GaugeVec | Discovered services by Beyla, with process name |
2 changes: 2 additions & 0 deletions pkg/internal/discover/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (ta *TraceAttacher) getTracer(ie *Instrumentable) (*ebpf.ProcessTracer, boo
ta.log.Debug(".done")
return nil, false
}
ta.Metrics.DiscoverService(ie.FileInfo.ExecutableName())
ta.log.Info("instrumenting process", "cmd", ie.FileInfo.CmdExePath, "pid", ie.FileInfo.Pid)

// builds a tracer for that executable
Expand Down Expand Up @@ -205,6 +206,7 @@ func (ta *TraceAttacher) notifyProcessDeletion(ie *Instrumentable) {
// notifying the tracer to block any trace from that PID
// to avoid that a new process reusing this PID could send traces
// unless explicitly allowed
ta.Metrics.UndiscoverService(ie.FileInfo.ExecutableName())
tracer.BlockPID(uint32(ie.FileInfo.Pid), ie.FileInfo.Ns)

// if there are no more trace instances for a Go program, we need to notify that
Expand Down
6 changes: 6 additions & 0 deletions pkg/internal/imetrics/imetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type Reporter interface {
OTELTraceExportError(err error)
// PrometheusRequest is invoked every time the Prometheus exporter is invoked, for a given port and path
PrometheusRequest(port, path string)
// DiscoverService is invoked every time a new service is discovered by the eBPF tracer
DiscoverService(service string)
// UndiscoverService is invoked every time a new service is removed from the discovered services by the eBPF tracer
UndiscoverService(service string)
}

// NoopReporter is a metrics Reporter that just does nothing
Expand All @@ -40,3 +44,5 @@ func (n NoopReporter) OTELMetricExportError(_ error) {}
func (n NoopReporter) OTELTraceExport(_ int) {}
func (n NoopReporter) OTELTraceExportError(_ error) {}
func (n NoopReporter) PrometheusRequest(_, _ string) {}
func (n NoopReporter) DiscoverService(_ string) {}
func (n NoopReporter) UndiscoverService(_ string) {}
16 changes: 15 additions & 1 deletion pkg/internal/imetrics/iprom.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type PrometheusReporter struct {
otelTraceExports prometheus.Counter
otelTraceExportErrs *prometheus.CounterVec
prometheusRequests *prometheus.CounterVec
discoveredServices *prometheus.GaugeVec
}

func NewPrometheusReporter(cfg *PrometheusConfig, manager *connector.PrometheusManager) *PrometheusReporter {
Expand Down Expand Up @@ -61,14 +62,19 @@ func NewPrometheusReporter(cfg *PrometheusConfig, manager *connector.PrometheusM
Name: "prometheus_http_requests",
Help: "requests towards the Prometheus Scrape endpoint",
}, []string{"port", "path"}),
discoveredServices: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "beyla_discovered_services",
Help: "discovered services by Beyla",
}, []string{"service"}),
}
manager.Register(cfg.Port, cfg.Path,
pr.tracerFlushes,
pr.otelMetricExports,
pr.otelMetricExportErrs,
pr.otelTraceExports,
pr.otelTraceExportErrs,
pr.prometheusRequests)
pr.prometheusRequests,
pr.discoveredServices)

return pr
}
Expand Down Expand Up @@ -100,3 +106,11 @@ func (p *PrometheusReporter) OTELTraceExportError(err error) {
func (p *PrometheusReporter) PrometheusRequest(port, path string) {
p.prometheusRequests.WithLabelValues(port, path).Inc()
}

func (p *PrometheusReporter) DiscoverService(service string) {
p.discoveredServices.WithLabelValues(service).Set(1)
marctc marked this conversation as resolved.
Show resolved Hide resolved
}

func (p *PrometheusReporter) UndiscoverService(service string) {
p.discoveredServices.WithLabelValues(service).Set(0)
}
Loading