Skip to content

Commit

Permalink
chore: optimize metrics creation and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Oct 20, 2024
1 parent df79ef5 commit b4f22ad
Show file tree
Hide file tree
Showing 27 changed files with 523 additions and 429 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/go-generate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Terraform Provider testing workflow.
name: go-generate

# This GitHub action runs your tests for each pull request and push.
# Optionally, you can turn it on using a schedule for regular testing.
on:
pull_request:
paths:
- 'docs/**'
- 'tools/**'

jobs:
generate:
name: Generate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 # v3.5.0
- uses: actions/setup-go@v5 # v4.0.0
with:
go-version-file: 'go.mod'
- run: go generate ./...
- name: git diff
run: |
git diff --compact-summary --exit-code || \
(echo; echo "Unexpected difference in directories after code generation. Run 'go generate ./...' command and commit."; exit 1)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: 'Force pkg.go.dev release sync'

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
- '**/v[0-9]+.[0-9]+.[0-9]+'
release:
types: [published]

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jobs:
runs-on:
group: Default
steps:
- uses: creekorful/goreportcard-action@v1.0
- uses: creekorful/goreportcard-action@v1.0
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Unit tests

on:
pull_request_target:
pull_request:
workflow_dispatch:

permissions:
Expand Down
17 changes: 15 additions & 2 deletions .github/workflows/new-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,23 @@ jobs:
- name: Run Go unit tests
run: |
go test ./...
generate:
needs: [pre-check]
name: Generate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 # v3.5.0
- uses: actions/setup-go@v5 # v4.0.0
with:
go-version-file: 'go.mod'
- run: go generate ./...
- name: git diff
run: |
git diff --compact-summary --exit-code || \
(echo; echo "Unexpected difference in directories after code generation. Run 'go generate ./...' command and commit."; exit 1)
# * Step 2: Create a new tag
tag:
needs: [golangci-lint, pre-check, tag-already-exist, testsunit]
needs: [golangci-lint, pre-check, tag-already-exist, testsunit, generate]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions cmd/admission-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/orange-cloudavenue/kube-image-updater/internal/httpserver"
client "github.com/orange-cloudavenue/kube-image-updater/internal/kubeclient"
"github.com/orange-cloudavenue/kube-image-updater/internal/log"
"github.com/orange-cloudavenue/kube-image-updater/internal/metrics"
)

var (
Expand All @@ -38,6 +39,9 @@ var (
)

func init() {
// Init Metrics
metrics.AdmissionController()

// webhook server running namespace (default to "default")
if os.Getenv("POD_NAMESPACE") != "" {
webhookNamespace = os.Getenv("POD_NAMESPACE")
Expand Down
24 changes: 12 additions & 12 deletions cmd/admission-controller/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
// func serveHandler
func ServeHandler(w http.ResponseWriter, r *http.Request) {
// Prometheus metrics
metrics.AdmissionController().Total().Inc()
timeAC := metrics.AdmissionController().Duration()
metrics.AdmissionController().Total.Inc()
timeAC := metrics.AdmissionController().Duration.NewTimer()
defer timeAC.ObserveDuration()

var body []byte
Expand All @@ -34,7 +34,7 @@ func ServeHandler(w http.ResponseWriter, r *http.Request) {
}
if len(body) == 0 {
// increment the total number of errors
metrics.AdmissionController().TotalErr().Inc()
metrics.AdmissionController().ErrTotal.Inc()

log.Error("empty body")
http.Error(w, "empty body", http.StatusBadRequest)
Expand All @@ -45,7 +45,7 @@ func ServeHandler(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
// increment the total number of errors
metrics.AdmissionController().TotalErr().Inc()
metrics.AdmissionController().ErrTotal.Inc()

http.Error(w, "invalid Content-Type, expect `application/json`", http.StatusUnsupportedMediaType)
return
Expand All @@ -55,7 +55,7 @@ func ServeHandler(w http.ResponseWriter, r *http.Request) {
ar := admissionv1.AdmissionReview{}
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
// increment the total number of errors
metrics.AdmissionController().TotalErr().Inc()
metrics.AdmissionController().ErrTotal.Inc()

log.WithError(err).Warn("Can't decode body")
admissionResponse = &admissionv1.AdmissionResponse{
Expand Down Expand Up @@ -83,13 +83,13 @@ func ServeHandler(w http.ResponseWriter, r *http.Request) {
resp, err := json.Marshal(admissionReview)
if err != nil {
// increment the total number of errors
metrics.AdmissionController().TotalErr().Inc()
metrics.AdmissionController().ErrTotal.Inc()

http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
}
if _, err := w.Write(resp); err != nil {
// increment the total number of errors
metrics.AdmissionController().TotalErr().Inc()
metrics.AdmissionController().ErrTotal.Inc()

http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
}
Expand Down Expand Up @@ -138,16 +138,16 @@ func mutate(ctx context.Context, ar *admissionv1.AdmissionReview) *admissionv1.A
// create mutation patch for pod.
func createPatch(ctx context.Context, pod *corev1.Pod) ([]byte, error) {
// Metrics - increment the total number of patch
metrics.AdmissionControllerPatch().Total().Inc()
timePatch := metrics.AdmissionControllerPatch().Duration()
metrics.AdmissionController().PatchTotal.Inc()
timePatch := metrics.AdmissionController().PatchDuration.NewTimer()
defer timePatch.ObserveDuration()

var err error
// find annotation enabled
an := annotations.New(ctx, pod)
if !an.Enabled().Get() {
// increment the total number of errors
metrics.AdmissionControllerPatch().TotalErr().Inc()
metrics.AdmissionController().PatchErrTotal.Inc()

return nil, fmt.Errorf("annotation not enabled")
}
Expand All @@ -172,7 +172,7 @@ func createPatch(ctx context.Context, pod *corev1.Pod) ([]byte, error) {
image, err = kubeClient.Image().Find(ctx, pod.Namespace, container.Image)
if err != nil {
// increment the total number of errors
metrics.AdmissionControllerPatch().TotalErr().Inc()
metrics.AdmissionController().PatchErrTotal.Inc()

log.
WithFields(logrus.Fields{
Expand All @@ -187,7 +187,7 @@ func createPatch(ctx context.Context, pod *corev1.Pod) ([]byte, error) {
image, err = kubeClient.Image().Get(ctx, pod.Namespace, crdName)
if err != nil {
// increment the total number of errors
metrics.AdmissionControllerPatch().TotalErr().Inc()
metrics.AdmissionController().PatchErrTotal.Inc()

log.
WithFields(logrus.Fields{
Expand Down
8 changes: 8 additions & 0 deletions cmd/kimup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/orange-cloudavenue/kube-image-updater/internal/httpserver"
"github.com/orange-cloudavenue/kube-image-updater/internal/kubeclient"
"github.com/orange-cloudavenue/kube-image-updater/internal/log"
"github.com/orange-cloudavenue/kube-image-updater/internal/metrics"
"github.com/orange-cloudavenue/kube-image-updater/internal/models"
"github.com/orange-cloudavenue/kube-image-updater/internal/triggers"
)
Expand All @@ -24,6 +25,13 @@ var (
)

func init() {
// Initialize the metrics
metrics.Tags()
metrics.Events()
metrics.Actions()
metrics.Rules()
metrics.Registry()

// TODO add namespace scope
// Flag "loglevel" is set in log package
flag.Parse()
Expand Down
30 changes: 15 additions & 15 deletions cmd/kimup/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
// Add event lock
event.On(triggers.RefreshImage.String(), event.ListenerFunc(func(e event.Event) (err error) {
// Increment the counter for the events
metrics.Events().Total().Inc()
metrics.Events().Total.Inc()
// Start the timer for the event execution
timerEvents := metrics.Events().Duration()
timerEvents := metrics.Events().Duration.NewTimer()
defer timerEvents.ObserveDuration()

if l[e.Data()["namespace"].(string)+"/"+e.Data()["image"].(string)] == nil {
Expand Down Expand Up @@ -72,8 +72,8 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
i := utils.ImageParser(image.Spec.Image)

// Prometheus metrics - Increment the counter for the registry
metrics.Registry().Total().Inc()
timerRegistry := metrics.Registry().Duration()
metrics.Registry().Total.Inc()
timerRegistry := metrics.Registry().Duration.NewTimer()

re, err := registry.New(ctx, image.Spec.Image, registry.Settings{
InsecureTLS: image.Spec.InsecureSkipTLSVerify,
Expand All @@ -93,20 +93,20 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
timerRegistry.ObserveDuration()
if err != nil {
// Prometheus metrics - Increment the counter for the registry with error
metrics.Registry().TotalErr().Inc()
metrics.Registry().ErrTotal.Inc()

return err
}

// Prometheus metrics - Increment the counter for the tags
metrics.Tags().Total().Inc()
timerTags := metrics.Tags().Duration()
metrics.Tags().Total.Inc()
timerTags := metrics.Tags().Duration.NewTimer()

tagsAvailable, err := re.Tags()
timerTags.ObserveDuration()
if err != nil {
// Prometheus metrics - Increment the counter for the tags with error
metrics.Tags().TotalErr().Inc()
metrics.Tags().TotalErr.Inc()

return err
}
Expand All @@ -128,8 +128,8 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
r.Init(tag, tagsAvailable, rule.Value)

// Prometheus metrics - Increment the counter for the rules
metrics.Rules().Total().Inc()
timerRules := metrics.Rules().Duration()
metrics.Rules().Total.Inc()
timerRules := metrics.Rules().Duration.NewTimer()

match, newTag, err := r.Evaluate()

Expand All @@ -138,7 +138,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {

if err != nil {
// Prometheus metrics - Increment the counter for the evaluated rule with error
metrics.Rules().TotalErr().Inc()
metrics.Rules().ErrTotal.Inc()

log.Errorf("Error evaluating rule: %v", err)
continue
Expand All @@ -159,8 +159,8 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
}, &image, action.Data)

// Prometheus metrics - Increment the counter for the actions
metrics.Actions().Total().Inc()
timerActions := metrics.Actions().Duration()
metrics.Actions().Total.Inc()
timerActions := metrics.Actions().Duration.NewTimer()

err = a.Execute(ctx)

Expand All @@ -169,7 +169,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {

if err != nil {
// Prometheus metrics - Increment the counter for the executed action with error
metrics.Actions().TotalErr().Inc()
metrics.Actions().ErrTotal.Inc()

log.Errorf("Error executing action(%s): %v", action.Type, err)
continue
Expand All @@ -183,7 +183,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
})

// Prometheus metrics - Increment the counter for the events evaluated with error
metrics.Events().TotalErr().Inc()
metrics.Events().ErrTotal.Inc()
return retryErr
}), event.Normal)
}
47 changes: 47 additions & 0 deletions docs/advanced/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
hide:
- toc
---

# Metrics

kimup exposes metrics to monitor the performance. The metrics are exposed in the Prometheus format and can be scraped by Prometheus or any other monitoring tool that can scrape Prometheus.

## Settings

The following arguments can be used to configure the metrics *(Available in kimup-operator, kimup-controller and kimup-admission-controller)*:

| Flag | Default | Description |
| -------------- | -------- | ------------------------- |
| --metrics | false | Enable metrics collection |
| --metrics-port | :9080 | Port to expose metrics on |
| --metrics-path | /metrics | Path to expose metrics on |


## Metrics

The following metrics are exposed:

| Metrics | Description |
| ------------------------------------------ | ------------------------------------------------------- |
| kimup_actions_duration | The duration in seconds of action performed. |
| kimup_actions_err_total | The total number of action performed with error. |
| kimup_actions_total | The total number of action performed. |
| kimup_admission_controller_duration | The duration in seconds of action performed. |
| kimup_admission_controller_err_total | The total number of action performed with error. |
| kimup_admission_controller_patch_duration | The duration in seconds of patch action performed. |
| kimup_admission_controller_patch_err_total | The total number of patch action performed with error. |
| kimup_admission_controller_patch_total | The total number of patch action performed. |
| kimup_admission_controller_total | The total number of action performed. |
| kimup_events_duration | The duration in seconds of events performed. |
| kimup_events_err_total | The total number of events performed with error. |
| kimup_events_total | The total number of events performed. |
| kimup_registry_duration | The duration in seconds of registry evaluated. |
| kimup_registry_err_total | The total number of registry evaluated with error. |
| kimup_registry_total | The total number of registry evaluated. |
| kimup_rules_duration | The duration in seconds of rules evaluated. |
| kimup_rules_err_total | The total number of rules evaluated with error. |
| kimup_rules_total | The total number of rules evaluated. |
| kimup_tags_duration | The duration in seconds for func tags to list the tags. |
| kimup_tags_total | The total number of func tags is called to list tags. |
| kimup_tags_total_err | The total number return by the func tags with error. |
20 changes: 20 additions & 0 deletions docs/advanced/metrics.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
hide:
- toc
---

# Metrics

kimup exposes metrics to monitor the performance. The metrics are exposed in the Prometheus format and can be scraped by Prometheus or any other monitoring tool that can scrape Prometheus.

## Settings

The following arguments can be used to configure the metrics *(Available in kimup-operator, kimup-controller and kimup-admission-controller)*:

{{ tableSettings }}

## Metrics

The following metrics are exposed:

{{ tableMetrics }}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ require (
github.com/containers/image/v5 v5.32.2
github.com/containrrr/shoutrrr v0.8.0
github.com/crazy-max/diun/v4 v4.28.0
github.com/fbiville/markdown-table-formatter v0.3.0
github.com/go-chi/chi/v5 v5.1.0
github.com/gookit/event v1.1.2
github.com/iancoleman/strcase v0.3.0
github.com/onsi/ginkgo/v2 v2.20.2
github.com/ory/dockertest/v3 v3.11.0
github.com/prometheus/client_golang v1.20.4
Expand Down
Loading

0 comments on commit b4f22ad

Please sign in to comment.