From ec1fabb19f6af216912bbad46f5df2314967df54 Mon Sep 17 00:00:00 2001 From: Geoffrey Israel Date: Tue, 3 Oct 2023 14:39:09 +0100 Subject: [PATCH] feat(lifecycle-operator): introduce metric showing readiness of operator (#2152) Signed-off-by: Geoffrey Israel Co-authored-by: Giovanni Liva --- lifecycle-operator/main.go | 16 ++++++++++++++++ .../04-teststep-check-active-metrics.yaml | 8 ++++++++ test/testmetrics/metrics/check-active-metrics.sh | 12 ++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 test/testmetrics/metrics/04-teststep-check-active-metrics.yaml create mode 100755 test/testmetrics/metrics/check-active-metrics.sh diff --git a/lifecycle-operator/main.go b/lifecycle-operator/main.go index 82a8298edd..42961ab3db 100644 --- a/lifecycle-operator/main.go +++ b/lifecycle-operator/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "flag" "fmt" "log" @@ -49,6 +50,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "go.opentelemetry.io/otel" otelprom "go.opentelemetry.io/otel/exporters/prometheus" + metricsapi "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/sdk/metric" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -97,6 +99,8 @@ type envConfig struct { KeptnOptionsCollectorURL string `envconfig:"OTEL_COLLECTOR_URL" default:""` } +const KeptnLifecycleActiveMetric = "keptn_lifecycle_active" + //nolint:funlen,gocognit,gocyclo func main() { var env envConfig @@ -121,6 +125,14 @@ func main() { } provider := metric.NewMeterProvider(metric.WithReader(exporter)) meter := provider.Meter("keptn/task") + + keptnLifecycleActive, err := meter.Int64Counter(KeptnLifecycleActiveMetric, metricsapi.WithDescription("signals that Keptn Lifecycle Operator is installed correctly and ready")) + + if err != nil { + setupLog.Error(err, "unable to create metric "+KeptnLifecycleActiveMetric) + os.Exit(1) + } + keptnMeters := telemetry.SetUpKeptnTaskMeters(meter) // Start the prometheus HTTP server and pass the exporter Collector to it @@ -350,6 +362,10 @@ func main() { setupLog.Error(err, "unable to set up ready check") os.Exit(1) } + + // Set the metric value as soon as the operator starts + setupLog.Info("Keptn lifecycle-operator is alive") + keptnLifecycleActive.Add(context.Background(), 1) if !disableWebhook { webhookBuilder = webhookBuilder.SetCertificateWatcher( certificates.NewCertificateWatcher( diff --git a/test/testmetrics/metrics/04-teststep-check-active-metrics.yaml b/test/testmetrics/metrics/04-teststep-check-active-metrics.yaml new file mode 100644 index 0000000000..3e8904738a --- /dev/null +++ b/test/testmetrics/metrics/04-teststep-check-active-metrics.yaml @@ -0,0 +1,8 @@ +apiVersion: kuttl.dev/v1 +kind: TestStep +commands: + - script: ./check-active-metrics.sh "keptn_lifecycle_active" + validate: + assert: + - contains: + - "keptn_lifecycle_active" diff --git a/test/testmetrics/metrics/check-active-metrics.sh b/test/testmetrics/metrics/check-active-metrics.sh new file mode 100755 index 0000000000..7ccf661faf --- /dev/null +++ b/test/testmetrics/metrics/check-active-metrics.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# Usage: ./retrieve-metrics.sh + +component_name=$1 + +metrics_url="http://lifecycle-operator-metrics-service.keptn-lifecycle-toolkit-system.svc.cluster.local:2222/metrics" + +# Fetch keptn_lifecycle_active metrics +metrics=$(curl -s $metrics_url | grep "${component_name}") + +echo "$metrics"