Skip to content

Commit

Permalink
Add custom metrics (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhouliu9 authored Jul 7, 2022
1 parent 5209534 commit d6a7292
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/cert-manager/cert-manager v1.8.0
github.com/prometheus/client_golang v1.12.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.10.0
Expand Down Expand Up @@ -61,7 +62,6 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

certmanagerversionedclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
certmanagerinformers "github.com/cert-manager/cert-manager/pkg/client/informers/externalversions"
"github.com/kanopy-platform/gateway-certificate-controller/internal/prometheus"
v1beta1labels "github.com/kanopy-platform/gateway-certificate-controller/pkg/v1beta1/labels"
networkingv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
istioversionedclient "istio.io/client-go/pkg/clientset/versioned"
Expand All @@ -28,13 +29,15 @@ type GarbageCollectionController struct {
certmanagerClient certmanagerversionedclient.Interface
istioClient istioversionedclient.Interface
dryRun bool
managedCerts map[string]bool
}

func NewGarbageCollectionController(istioClient istioversionedclient.Interface, certClient certmanagerversionedclient.Interface, opts ...OptionsFunc) *GarbageCollectionController {
gc := &GarbageCollectionController{
name: "istio-garbage-collection-controller",
certmanagerClient: certClient,
istioClient: istioClient,
managedCerts: make(map[string]bool),
}

for _, opt := range opts {
Expand Down Expand Up @@ -75,6 +78,8 @@ func (c *GarbageCollectionController) Reconcile(ctx context.Context, request rec
log := log.FromContext(ctx)
log.V(1).Info("Running Garbage Collection Reconcile", "request", request.String())

c.managedCerts[request.String()] = true

certIface := c.certmanagerClient.CertmanagerV1().Certificates(request.Namespace)
cert, err := certIface.Get(ctx, request.Name, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -109,8 +114,11 @@ func (c *GarbageCollectionController) Reconcile(ctx context.Context, request rec
Requeue: true,
}, err
}

delete(c.managedCerts, request.String())
}

prometheus.UpdateManagedCertificatesCount(len(c.managedCerts))
return reconcile.Result{}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestNewGarbageCollectionController(t *testing.T) {
certmanagerClient: certmanagerClient,
istioClient: istioClient,
dryRun: dryRun,
managedCerts: map[string]bool{},
}

gc := NewGarbageCollectionController(istioClient, certmanagerClient, WithDryRun(dryRun))
Expand Down
30 changes: 30 additions & 0 deletions internal/prometheus/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package prometheus

import (
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
managedCertificatesCount = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "managed_certificates_count",
Help: "Count of controller managed certificates",
})
)

func init() {
metrics.Registry.MustRegister(managedCertificatesCount)
}

func Handler() http.Handler {
return promhttp.InstrumentMetricHandler(
metrics.Registry, promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{}),
)
}

func UpdateManagedCertificatesCount(count int) {
managedCertificatesCount.Set(float64(count))
}
22 changes: 22 additions & 0 deletions internal/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package prometheus

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMetrics(t *testing.T) {
t.Parallel()

req, err := http.NewRequest("GET", "/", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()

Handler().ServeHTTP(rr, req)
body := rr.Body.String()
assert.Contains(t, body, `managed_certificates_count`)
}

0 comments on commit d6a7292

Please sign in to comment.