Skip to content

Commit

Permalink
Add export metrics
Browse files Browse the repository at this point in the history
this is useful to figure out how much data is being exported and if the log
pipeline is behind in processing the incoming events.

Signed-off-by: Chance Zibolski <[email protected]>
  • Loading branch information
chancez authored and lambdanis committed Apr 25, 2024
1 parent b8edb7e commit 92a0d58
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,9 @@ func startExporter(ctx context.Context, server *server.Server) error {
}()
}

encoder := encoder.NewProtojsonEncoder(writer)
// Track how many bytes are written to the event export location
encoderWriter := exporter.NewExportedBytesTotalWriter(writer)
encoder := encoder.NewProtojsonEncoder(encoderWriter)
var rateLimiter *ratelimit.RateLimiter
if option.Config.ExportRateLimit >= 0 {
rateLimiter = ratelimit.NewRateLimiter(ctx, 1*time.Minute, option.Config.ExportRateLimit, encoder)
Expand Down
12 changes: 12 additions & 0 deletions docs/content/en/docs/reference/metrics.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ func (e *Exporter) Send(event *tetragon.GetEventsResponse) error {
e.rateLimiter.Drop()
return nil
}

if err := e.encoder.Encode(event); err != nil {
logger.GetLogger().WithError(err).Warning("Failed to JSON encode")
}
eventsExportedTotal.Inc()
eventsExportTimestamp.Set(float64(event.GetTime().GetSeconds()))
return nil
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/exporter/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package exporter

import (
"io"

"github.com/cilium/tetragon/pkg/metrics/consts"
"github.com/prometheus/client_golang/prometheus"
)

var (
eventsExportedTotal = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
Name: "events_exported_total",
Help: "Total number of events exported",
})

eventsExportedBytesTotal = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: consts.MetricsNamespace,
Name: "events_exported_bytes_total",
Help: "Number of bytes exported for events",
})

eventsExportTimestamp = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: consts.MetricsNamespace,
Name: "events_last_exported_timestamp",
Help: "Timestamp of the most recent event to be exported",
})
)

func InitMetrics(registry *prometheus.Registry) {
registry.MustRegister(eventsExportedTotal)
registry.MustRegister(eventsExportedBytesTotal)
registry.MustRegister(eventsExportTimestamp)
}

func newExportedBytesCounterWriter(w io.Writer, c prometheus.Counter) io.Writer {
return byteCounterWriter{Writer: w, bytesWritten: c}
}

type byteCounterWriter struct {
io.Writer
bytesWritten prometheus.Counter
}

func (w byteCounterWriter) Write(p []byte) (int, error) {
n, err := w.Writer.Write(p)
w.bytesWritten.Add(float64(n))
return n, err
}

func NewExportedBytesTotalWriter(w io.Writer) io.Writer {
return newExportedBytesCounterWriter(w, eventsExportedBytesTotal)
}
2 changes: 2 additions & 0 deletions pkg/metrics/metricsconfig/initmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package metricsconfig

import (
"github.com/cilium/tetragon/pkg/eventcache"
"github.com/cilium/tetragon/pkg/exporter"
"github.com/cilium/tetragon/pkg/grpc/tracing"
"github.com/cilium/tetragon/pkg/metrics/errormetrics"
"github.com/cilium/tetragon/pkg/metrics/eventcachemetrics"
Expand Down Expand Up @@ -43,6 +44,7 @@ func initHealthMetrics(registry *prometheus.Registry) {
observer.InitMetrics(registry)
tracing.InitMetrics(registry)
ratelimitmetrics.InitMetrics(registry)
exporter.InitMetrics(registry)

// register common third-party collectors
registry.MustRegister(grpcmetrics.NewServerMetrics())
Expand Down

0 comments on commit 92a0d58

Please sign in to comment.