Skip to content

Commit

Permalink
policystatemetrics: Initialize custom collectors for docs
Browse files Browse the repository at this point in the history
policyStateCollector doesn't report any metrics if Tetragon isn't fully
initialized - so also when we generate metrics docs with tetra. To include
policy state metrics in metrics docs, let's define a "fake" collector -
policyStateZeroCollector - reporting same metrics as policyStateZeroCollector
but with hardcoded zero values.

Signed-off-by: Anna Kapuscinska <[email protected]>
  • Loading branch information
lambdanis committed Feb 28, 2024
1 parent 17d1de7 commit fedd884
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion pkg/metrics/policystatemetrics/policystatemetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func InitMetrics(registry *prometheus.Registry) {
registry.MustRegister(newPolicyStateCollector(observer.GetSensorManager()))
}

func InitMetricsForDocs(registry *prometheus.Registry) {}
func InitMetricsForDocs(registry *prometheus.Registry) {
registry.MustRegister(newPolicyStateZeroCollector())
}

// This metric collector converts the output of ListTracingPolicies into a few
// gauges metrics on collection. Thus, it needs a sensor manager to query.
Expand Down Expand Up @@ -85,3 +87,47 @@ func (c *policyStateCollector) Collect(ch chan<- prometheus.Metric) {
strings.TrimPrefix(strings.ToLower(tetragon.TracingPolicyState_TP_STATE_ENABLED.String()), "tp_state_"),
)
}

// policyStateZeroCollector implements prometheus.Collector. It collects "zero"
// metrics. It's intended to be used when the sensor manager doesn't exist, but
// we still want Prometheus metrics to be exposed.
type policyStateZeroCollector struct {
policyStateCollector
}

func newPolicyStateZeroCollector() prometheus.Collector {
return &policyStateZeroCollector{
policyStateCollector: *newPolicyStateCollector(nil),
}
}

func (c *policyStateZeroCollector) Describe(ch chan<- *prometheus.Desc) {
c.policyStateCollector.Describe(ch)
}

func (c *policyStateZeroCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
c.descriptor,
prometheus.GaugeValue,
0,
strings.TrimPrefix(strings.ToLower(tetragon.TracingPolicyState_TP_STATE_LOAD_ERROR.String()), "tp_state_"),
)
ch <- prometheus.MustNewConstMetric(
c.descriptor,
prometheus.GaugeValue,
0,
strings.TrimPrefix(strings.ToLower(tetragon.TracingPolicyState_TP_STATE_ERROR.String()), "tp_state_"),
)
ch <- prometheus.MustNewConstMetric(
c.descriptor,
prometheus.GaugeValue,
0,
strings.TrimPrefix(strings.ToLower(tetragon.TracingPolicyState_TP_STATE_DISABLED.String()), "tp_state_"),
)
ch <- prometheus.MustNewConstMetric(
c.descriptor,
prometheus.GaugeValue,
0,
strings.TrimPrefix(strings.ToLower(tetragon.TracingPolicyState_TP_STATE_ENABLED.String()), "tp_state_"),
)
}

0 comments on commit fedd884

Please sign in to comment.