metrics

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2020 License: Apache-2.0 Imports: 4 Imported by: 6

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Dummy = &dummy{}

Dummy is a dummy recorder useful for tests.

Functions

This section is empty.

Types

type Operation

type Operation = admissionv1beta1.Operation

Operation is the operation type of the admission review.

type Prometheus

type Prometheus struct {
	// contains filtered or unexported fields
}

Prometheus is the implementation of a metrics Recorder for Prometheus system.

Example (ServePrometheusMetrics)

Prometheus shows how to serve a webhook and its prometheus metrics in a separate server.

package main

import (
	"context"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	corev1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	whhttp "github.com/slok/kubewebhook/pkg/http"
	"github.com/slok/kubewebhook/pkg/observability/metrics"
	"github.com/slok/kubewebhook/pkg/webhook/mutating"
)

func main() {

	// Create the prometheus registry. This registry can be used for custom metrics
	// when serving the prometheus metrics our custom metrics and the webhook metrics
	// will be served.
	reg := prometheus.NewRegistry()

	// Create our metrics service.
	metricsRec := metrics.NewPrometheus(reg)

	// Create a stub mutator.
	m := mutating.MutatorFunc(func(_ context.Context, obj metav1.Object) (bool, error) {
		return false, nil
	})

	// Create webhooks (don't check error).
	mcfg := mutating.WebhookConfig{
		Name: "instrucmentedWebhook",
		Obj:  &corev1.Pod{},
	}
	mwh, _ := mutating.NewWebhook(mcfg, m, nil, metricsRec, nil)

	// Run our webhook server (not checking error in this example).
	whHandler, _ := whhttp.HandlerFor(mwh)
	go func() {
		_ = http.ListenAndServeTLS(":8080", "file.cert", "file.key", whHandler)
	}()

	// Run our metrics in a separate port (not checking error in this example).
	promHandler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
	go func() {
		_ = http.ListenAndServe(":8081", promHandler)
	}()
}
Output:

func NewPrometheus

func NewPrometheus(registry prometheus.Registerer) *Prometheus

NewPrometheus returns a new Prometheus metrics backend.

func (*Prometheus) IncAdmissionReview

func (p *Prometheus) IncAdmissionReview(webhook, namespace, resource string, operation Operation, kind ReviewKind)

IncAdmissionReview satisfies Recorder interface.

func (*Prometheus) IncAdmissionReviewError

func (p *Prometheus) IncAdmissionReviewError(webhook, namespace, resource string, operation Operation, kind ReviewKind)

IncAdmissionReviewError satisfies Recorder interface.

func (*Prometheus) IncValidationReviewResult added in v0.11.0

func (p *Prometheus) IncValidationReviewResult(webhook, namespace, resource string, operation Operation, allowed bool)

IncValidationReviewResult satisfies Recorder interface.

func (*Prometheus) ObserveAdmissionReviewDuration

func (p *Prometheus) ObserveAdmissionReviewDuration(webhook, namespace, resource string, operation Operation, kind ReviewKind, start time.Time)

ObserveAdmissionReviewDuration satisfies Recorder interface.

type Recorder

type Recorder interface {
	// IncAdmissionReview will increment in one the admission review counter.
	IncAdmissionReview(webhook, namespace, resource string, operation Operation, kind ReviewKind)
	// IncAdmissionReviewError will increment in one the admission review counter errors.
	IncAdmissionReviewError(webhook, namespace, resource string, operation Operation, kind ReviewKind)
	// ObserveAdmissionReviewDuration will observe the duration of a admission review.
	ObserveAdmissionReviewDuration(webhook, namespace, resource string, operation Operation, kind ReviewKind, start time.Time)
	// IncValidationReviewResult will increment in one the admission review allowed counter.
	IncValidationReviewResult(webhook, namespace, resource string, operation Operation, allowed bool)
}

Recorder knows how to record metrics.

type ReviewKind

type ReviewKind string

ReviewKind is the kind of admission review.

const (
	// MutatingReviewKind is a mutating review kind.
	MutatingReviewKind ReviewKind = "mutating"

	// ValidatingReviewKind is a validating review kind.
	ValidatingReviewKind ReviewKind = "validating"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL