From 723a1f769f53fbb1e5c6e6e353e89f9b2fd43072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Mon, 9 Feb 2026 01:13:20 +0100 Subject: [PATCH] test: add histogram verification to deployment metrics tests The existing tests for RecordDeploymentEnd and RecordDeploymentFailure only verified the counter was incremented, not that the histogram was updated with duration observations. Add histogram verification to: - TestCollector_RecordDeploymentEnd_Success - TestCollector_RecordDeploymentEnd_Failure - TestCollector_RecordDeploymentFailure Also add listener tests to verify metrics are properly initialized when MetricsEnabled is true and that the recording functions work correctly in the context of deployment handling. Co-Authored-By: Claude Opus 4.5 --- cmd/homelab-deploy/main.go | 2 +- internal/listener/listener_test.go | 151 +++++++++++++++ internal/metrics/metrics_test.go | 291 +++++++++++++++++++++++++++++ 3 files changed, 443 insertions(+), 1 deletion(-) diff --git a/cmd/homelab-deploy/main.go b/cmd/homelab-deploy/main.go index 5b2c338..4c5bdde 100644 --- a/cmd/homelab-deploy/main.go +++ b/cmd/homelab-deploy/main.go @@ -16,7 +16,7 @@ import ( "github.com/urfave/cli/v3" ) -const version = "0.1.12" +const version = "0.1.13" func main() { app := &cli.Command{ diff --git a/internal/listener/listener_test.go b/internal/listener/listener_test.go index 9299af9..4d31d3b 100644 --- a/internal/listener/listener_test.go +++ b/internal/listener/listener_test.go @@ -2,8 +2,14 @@ package listener import ( "log/slog" + "strings" "testing" "time" + + "git.t-juice.club/torjus/homelab-deploy/internal/messages" + "git.t-juice.club/torjus/homelab-deploy/internal/metrics" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" ) func TestNew(t *testing.T) { @@ -51,3 +57,148 @@ func TestNew_WithLogger(t *testing.T) { t.Error("should use provided logger") } } + +func TestNew_WithMetricsEnabled(t *testing.T) { + cfg := Config{ + Hostname: "test-host", + Tier: "test", + MetricsEnabled: true, + MetricsAddr: ":0", + } + + l := New(cfg, nil) + + if l.metricsServer == nil { + t.Error("metricsServer should not be nil when MetricsEnabled is true") + } + if l.metrics == nil { + t.Error("metrics should not be nil when MetricsEnabled is true") + } +} + +func TestListener_MetricsRecordedOnDeployment(t *testing.T) { + // This test verifies that the listener correctly calls metrics functions + // when processing deployments. We test this by directly calling the internal + // metrics recording logic that handleDeployRequest uses. + + reg := prometheus.NewRegistry() + collector := metrics.NewCollector(reg) + + // Simulate what handleDeployRequest does for a successful deployment + collector.RecordDeploymentStart() + collector.RecordDeploymentEnd(messages.ActionSwitch, true, 120.5) + + // Verify counter was incremented + counterExpected := ` +# HELP homelab_deploy_deployments_total Total deployment requests processed +# TYPE homelab_deploy_deployments_total counter +homelab_deploy_deployments_total{action="boot",error_code="",status="completed"} 0 +homelab_deploy_deployments_total{action="boot",error_code="",status="failed"} 0 +homelab_deploy_deployments_total{action="dry-activate",error_code="",status="completed"} 0 +homelab_deploy_deployments_total{action="dry-activate",error_code="",status="failed"} 0 +homelab_deploy_deployments_total{action="switch",error_code="",status="completed"} 1 +homelab_deploy_deployments_total{action="switch",error_code="",status="failed"} 0 +homelab_deploy_deployments_total{action="test",error_code="",status="completed"} 0 +homelab_deploy_deployments_total{action="test",error_code="",status="failed"} 0 +` + if err := testutil.GatherAndCompare(reg, strings.NewReader(counterExpected), "homelab_deploy_deployments_total"); err != nil { + t.Errorf("unexpected counter metrics: %v", err) + } + + // Verify histogram was updated (120.5 seconds falls into le="300" and higher buckets) + histogramExpected := ` +# HELP homelab_deploy_deployment_duration_seconds Deployment execution time +# TYPE homelab_deploy_deployment_duration_seconds histogram +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="300"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="600"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="900"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1200"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1800"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="+Inf"} 1 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="true"} 120.5 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="true"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="true"} 0 +` + if err := testutil.GatherAndCompare(reg, strings.NewReader(histogramExpected), "homelab_deploy_deployment_duration_seconds"); err != nil { + t.Errorf("unexpected histogram metrics: %v", err) + } +} diff --git a/internal/metrics/metrics_test.go b/internal/metrics/metrics_test.go index 2fe7a74..91126f7 100644 --- a/internal/metrics/metrics_test.go +++ b/internal/metrics/metrics_test.go @@ -78,6 +78,103 @@ homelab_deploy_deployments_total{action="test",error_code="",status="failed"} 0 if err := testutil.GatherAndCompare(reg, strings.NewReader(counterExpected), "homelab_deploy_deployments_total"); err != nil { t.Errorf("unexpected counter metrics: %v", err) } + + // Check histogram recorded the duration (120.5 seconds falls into le="300" and higher buckets) + histogramExpected := ` +# HELP homelab_deploy_deployment_duration_seconds Deployment execution time +# TYPE homelab_deploy_deployment_duration_seconds histogram +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="300"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="600"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="900"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1200"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1800"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="+Inf"} 1 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="true"} 120.5 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="true"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="true"} 0 +` + if err := testutil.GatherAndCompare(reg, strings.NewReader(histogramExpected), "homelab_deploy_deployment_duration_seconds"); err != nil { + t.Errorf("unexpected histogram metrics: %v", err) + } } func TestCollector_RecordDeploymentEnd_Failure(t *testing.T) { @@ -102,6 +199,103 @@ homelab_deploy_deployments_total{action="test",error_code="",status="failed"} 0 if err := testutil.GatherAndCompare(reg, strings.NewReader(counterExpected), "homelab_deploy_deployments_total"); err != nil { t.Errorf("unexpected counter metrics: %v", err) } + + // Check histogram recorded the duration (60.0 seconds falls into le="60" and higher buckets) + histogramExpected := ` +# HELP homelab_deploy_deployment_duration_seconds Deployment execution time +# TYPE homelab_deploy_deployment_duration_seconds histogram +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="60"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="120"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="300"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="600"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="900"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1200"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1800"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="+Inf"} 1 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="false"} 60 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="false"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="true"} 0 +` + if err := testutil.GatherAndCompare(reg, strings.NewReader(histogramExpected), "homelab_deploy_deployment_duration_seconds"); err != nil { + t.Errorf("unexpected histogram metrics: %v", err) + } } func TestCollector_RecordDeploymentFailure(t *testing.T) { @@ -127,6 +321,103 @@ homelab_deploy_deployments_total{action="test",error_code="",status="failed"} 0 if err := testutil.GatherAndCompare(reg, strings.NewReader(counterExpected), "homelab_deploy_deployments_total"); err != nil { t.Errorf("unexpected counter metrics: %v", err) } + + // Check histogram recorded the duration (300.0 seconds falls into le="300" and higher buckets) + histogramExpected := ` +# HELP homelab_deploy_deployment_duration_seconds Deployment execution time +# TYPE homelab_deploy_deployment_duration_seconds histogram +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="boot",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="boot",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="dry-activate",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="dry-activate",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="300"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="600"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="900"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1200"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="1800"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="false",le="+Inf"} 1 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="false"} 300 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="false"} 1 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="switch",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="switch",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="switch",success="true"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="false",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="false"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="30"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="60"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="120"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="300"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="600"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="900"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1200"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="1800"} 0 +homelab_deploy_deployment_duration_seconds_bucket{action="test",success="true",le="+Inf"} 0 +homelab_deploy_deployment_duration_seconds_sum{action="test",success="true"} 0 +homelab_deploy_deployment_duration_seconds_count{action="test",success="true"} 0 +` + if err := testutil.GatherAndCompare(reg, strings.NewReader(histogramExpected), "homelab_deploy_deployment_duration_seconds"); err != nil { + t.Errorf("unexpected histogram metrics: %v", err) + } } func TestCollector_RecordRejection(t *testing.T) {