Generating Custom Metrics
Pepr provides a metricCollector
utility that lets you define custom metrics such as counters and gauges for your module.
For example, to track how often a specific event occurs, you can create a custom counter and gauge like this:
import { Capability, a, metricsCollector } from "pepr";
export const HelloPepr = new Capability({ name: "hello-pepr", description: "An example using the metric collector.",});
// Register a metric collector to count the number of times the hello-pepr label has been appliedmetricsCollector.addCounter( "label_counter", "example counter for counting number of times hello-pepr label has been applied",);
// Register a gauge to count how many times a given label has been appliedmetricsCollector.addGauge( "label_gauge", "example gauge for counting how times times a given label has been applied", ["label"],);
const { When } = HelloPepr;When(a.Pod) .IsCreatedOrUpdated() .Mutate(po => { po.SetLabel("hello-pepr", "true"); po.SetLabel("blue", "true"); po.SetLabel("green", "true"); metricsCollector.incCounter("label_counter"); metricsCollector.incGauge("label_gauge", { label: "blue" }, 1); metricsCollector.incGauge("label_gauge", { label: "green" }, 1); metricsCollector.incGauge("label_gauge", { label: "hello-pepr" }, 1); });
You can access these metrics through the /metrics
endpoint of your module. For example, if you are running npx pepr dev --yes
, you can create some pods and then query the metrics like this:
terminal_a > npx pepr dev --yesterminal_b > curl -k http://localhost:3000/metrics...# HELP pepr_label_counter example counter for counting number of times hello-pepr label has been applied# TYPE pepr_label_counter counterpepr_label_counter 0
# HELP pepr_label_gauge example gauge for counting how times times a given label has been applied# TYPE pepr_label_gauge gaugeterminal_b > kubectl run a --image=nginxterminal_b > kubectl run b --image=nginxterminal_b > kubectl run c --image=nginxterminal_b > curl -k http://localhost:3000/metrics...# TYPE pepr_label_counter counterpepr_label_counter 3
# HELP pepr_label_gauge example gauge for counting how times times a given label has been applied# TYPE pepr_label_gauge gaugepepr_label_gauge{label="blue"} 3pepr_label_gauge{label="green"} 3pepr_label_gauge{label="hello-pepr"} 3