Skip to content

Commit

Permalink
feat(lifecycle-operator): introduce metric showing readiness of opera…
Browse files Browse the repository at this point in the history
…tor (keptn#2152)

Signed-off-by: Geoffrey Israel <[email protected]>
Co-authored-by: Giovanni Liva <[email protected]>
  • Loading branch information
2 people authored and StackScribe committed Oct 13, 2023
1 parent e775657 commit ec1fabb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lifecycle-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions test/testmetrics/metrics/check-active-metrics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Usage: ./retrieve-metrics.sh <component_name>

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"

0 comments on commit ec1fabb

Please sign in to comment.