⚠️ Work in Progress
You are viewing development documentation built from the latest commit on main. APIs and features are subject to change.

Prometheus Builders

Prometheus Builders - Prometheus Operator CRD Constructors

Go Reference Go Reference

The prometheus package provides strongly-typed constructor functions for creating Prometheus Operator Kubernetes resources. These are the low-level building blocks used by Kure’s higher-level stack and workflow packages.

Overview

Each function takes a configuration struct and returns a fully initialized Prometheus Operator custom resource. The builders handle API version and kind metadata, letting you focus on the resource specification.

Supported Resources

ServiceMonitor

import "github.com/go-kure/kure/pkg/kubernetes/prometheus"

sm := prometheus.ServiceMonitor(&prometheus.ServiceMonitorConfig{
    Name:      "my-app",
    Namespace: "monitoring",
    Selector:  metav1.LabelSelector{
        MatchLabels: map[string]string{"app": "my-app"},
    },
    Endpoints: []monitoringv1.Endpoint{
        {Path: "/metrics", Port: "http", Interval: "30s"},
    },
    JobLabel:     "app",
    TargetLabels: []string{"app", "version"},
})
FieldTypeDescription
NamestringResource name
NamespacestringResource namespace
Selectormetav1.LabelSelectorLabel selector for target Services
Endpoints[]monitoringv1.EndpointScrape endpoint configurations
JobLabelstringLabel to use as the Prometheus job label
TargetLabels[]stringService labels to transfer to scraped metrics
NamespaceSelector*monitoringv1.NamespaceSelectorNamespaces to select Services from
SampleLimit*uint64Per-scrape sample limit
Labelsmap[string]stringAdditional labels for the resource

PodMonitor

pm := prometheus.PodMonitor(&prometheus.PodMonitorConfig{
    Name:      "my-app",
    Namespace: "monitoring",
    Selector:  metav1.LabelSelector{
        MatchLabels: map[string]string{"app": "my-app"},
    },
    PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
        {Path: "/metrics", Port: "http", Interval: "30s"},
    },
})
FieldTypeDescription
NamestringResource name
NamespacestringResource namespace
Selectormetav1.LabelSelectorLabel selector for target Pods
PodMetricsEndpoints[]monitoringv1.PodMetricsEndpointScrape endpoint configurations
JobLabelstringLabel to use as the Prometheus job label
PodTargetLabels[]stringPod labels to transfer to scraped metrics
NamespaceSelector*monitoringv1.NamespaceSelectorNamespaces to select Pods from
SampleLimit*uint64Per-scrape sample limit
Labelsmap[string]stringAdditional labels for the resource

PrometheusRule

rule := prometheus.PrometheusRule(&prometheus.PrometheusRuleConfig{
    Name:      "alerts",
    Namespace: "monitoring",
    Labels:    map[string]string{"role": "alert-rules"},
    Groups: []monitoringv1.RuleGroup{
        {
            Name: "my-app.rules",
            Rules: []monitoringv1.Rule{
                {
                    Alert: "HighErrorRate",
                    Expr:  intstr.FromString(`rate(http_requests_total{code=~"5.."}[5m]) > 0.1`),
                    For:   (*monitoringv1.Duration)(ptr("5m")),
                },
            },
        },
    },
})
FieldTypeDescription
NamestringResource name
NamespacestringResource namespace
Groups[]monitoringv1.RuleGroupRule groups containing alert and recording rules
Labelsmap[string]stringAdditional labels for the resource

Modifier Functions

Update existing resources after construction:

// ServiceMonitor modifiers
prometheus.AddServiceMonitorEndpoint(sm, monitoringv1.Endpoint{Path: "/metrics", Port: "http"})
prometheus.SetServiceMonitorJobLabel(sm, "app")
prometheus.AddServiceMonitorTargetLabel(sm, "version")
prometheus.SetServiceMonitorNamespaceSelector(sm, monitoringv1.NamespaceSelector{Any: true})
prometheus.SetServiceMonitorSampleLimit(sm, 10000)

// PodMonitor modifiers
prometheus.AddPodMonitorEndpoint(pm, monitoringv1.PodMetricsEndpoint{Path: "/metrics", Port: "http"})
prometheus.SetPodMonitorJobLabel(pm, "app")
prometheus.AddPodMonitorPodTargetLabel(pm, "version")
prometheus.SetPodMonitorNamespaceSelector(pm, monitoringv1.NamespaceSelector{Any: true})
prometheus.SetPodMonitorSampleLimit(pm, 10000)

// PrometheusRule modifiers
prometheus.AddPrometheusRuleGroup(rule, monitoringv1.RuleGroup{Name: "extra.rules"})