File: store.go

package info (click to toggle)
golang-github-canonical-candid 1.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,016 kB
  • sloc: python: 1,903; sh: 235; makefile: 81
file content (43 lines) | stat: -rw-r--r-- 1,032 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package monitoring

import (
	"context"

	"github.com/juju/loggo"
	"github.com/prometheus/client_golang/prometheus"

	"github.com/canonical/candid/store"
)

var logger = loggo.GetLogger("candid.internal.monitoring")

type StoreCollector struct {
	Store store.Store
}

var storeIdentiesDesc = prometheus.NewDesc(
	"candid_store_identities",
	"Number of stored identities",
	[]string{"provider"},
	nil,
)

// Describe implements prometheus.Collector
func (c StoreCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- storeIdentiesDesc
}

// Describe implements prometheus.Collector
func (c StoreCollector) Collect(ch chan<- prometheus.Metric) {
	counts, err := c.Store.IdentityCounts(context.Background())
	if err != nil {
		logger.Infof("error collecting metrics: %s", err)
		return
	}
	for provider, count := range counts {
		ch <- prometheus.MustNewConstMetric(storeIdentiesDesc, prometheus.GaugeValue, float64(count), provider)
	}
}