| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "log" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/VictoriaMetrics/metrics" |
| 11 | ) |
| 12 | |
| 13 | var Prometheus = NewPrometheusExporter() |
| 14 | |
| 15 | type PrometheusExporter struct { |
| 16 | // Number of currently connected clients. |
| 17 | fullClientCount metrics.Gauge |
| 18 | liteClientCount metrics.Gauge |
| 19 | domainClientCount metrics.Gauge |
| 20 | |
| 21 | tempCertMetricsLastRefreshed time.Time |
| 22 | tempCertMetrics CTMetrics |
| 23 | tempCertMetricsMutex sync.RWMutex |
| 24 | |
| 25 | skippedCertsCallback func() map[string]int64 |
| 26 | } |
| 27 | |
| 28 | // NewPrometheusExporter creates a new PrometheusExporter and registers the default metrics for the number of processed certificates. |
| 29 | func NewPrometheusExporter() *PrometheusExporter { |
| 30 | e := &PrometheusExporter{} |
| 31 | // Register metrics for the total number of certificates processed by the CT watcher. |
| 32 | metrics.GetOrCreateGauge("certstreamservergo_certificates_total{type=\"regular\"}", func() float64 { |
| 33 | return float64(GetProcessedCerts()) |
| 34 | }) |
| 35 | metrics.GetOrCreateGauge("certstreamservergo_certificates_total{type=\"precert\"}", func() float64 { |
| 36 | return float64(GetProcessedPrecerts()) |
| 37 | }) |
| 38 | return e |
| 39 | } |
| 40 | |
| 41 | // Write is a callback function that is called by a webserver in order to write metrics data to the http response. |
| 42 | func (pm *PrometheusExporter) Write(w io.Writer, exposeProcessMetrics bool) { |
| 43 | // getSkippedCertMetrics() |
| 44 | |
| 45 | metrics.WritePrometheus(w, exposeProcessMetrics) |
| 46 | } |
| 47 | |
| 48 | // RegisterGaugeMetric is a helper function that registers a new gauge metric with a float64 callback function. |
| 49 | func (pm *PrometheusExporter) RegisterGaugeMetric(label string, callback func() float64) { |
| 50 | metrics.GetOrCreateGauge(label, callback) |
| 51 | } |
| 52 | |
| 53 | // RegisterGaugeMetricInt is a helper function that registers a new gauge metric with an int64 callback function. |
| 54 | func (pm *PrometheusExporter) RegisterGaugeMetricInt(label string, callback func() int64) { |
| 55 | metrics.GetOrCreateGauge(label, func() float64 { return float64(callback()) }) |
| 56 | } |
| 57 | |
| 58 | // RegisterClient registers a new gauge metric for the client with the given name. |
| 59 | func (pm *PrometheusExporter) RegisterClient(name string, callback func() float64) { |
| 60 | label := fmt.Sprintf("certstreamservergo_skipped_certs{client=\"%s\"}", name) |
| 61 | metrics.GetOrCreateGauge(label, callback) |
| 62 | } |
| 63 | |
| 64 | // UnregisterClient unregisters the metric for the client with the given name. |
| 65 | func (pm *PrometheusExporter) UnregisterClient(name string) { |
| 66 | label := fmt.Sprintf("certstreamservergo_skipped_certs{client=\"%s\"}", name) |
| 67 | metrics.UnregisterMetric(label) |
| 68 | } |
| 69 | |
| 70 | // RegisterLog registers a new gauge metric for the given CT log. |
| 71 | // The metric will be named "certstreamservergo_certs_by_log_total{url=\"<url>\",operator=\"<operatorName>\"}" and |
| 72 | // will call the given callback function to get the current value of the metric. |
| 73 | func (pm *PrometheusExporter) RegisterLog(operatorName, url string) { |
| 74 | label := fmt.Sprintf("certstreamservergo_certs_by_log_total{url=\"%s\",operator=\"%s\"}", url, operatorName) |
| 75 | metrics.GetOrCreateGauge(label, func() float64 { |
| 76 | return float64(pm.getCertCountForLog(operatorName, url)) |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | // UnregisterMetric unregisters a metric with a given label. |
| 81 | func (pm *PrometheusExporter) UnregisterMetric(label string) { |
| 82 | metrics.UnregisterMetric(label) |
| 83 | } |
| 84 | |
| 85 | // getCertCountForLog returns the number of certificates processed from a specific CT log. |
| 86 | // It caches the result for 5 seconds. Subsequent calls to this method will return the cached result. |
| 87 | func (pm *PrometheusExporter) getCertCountForLog(operatorName, logname string) int64 { |
| 88 | pm.tempCertMetricsMutex.Lock() |
| 89 | defer pm.tempCertMetricsMutex.Unlock() |
| 90 | |
| 91 | // Add some caching to avoid having to lock the mutex every time |
| 92 | if time.Since(pm.tempCertMetricsLastRefreshed) > time.Second*5 { |
| 93 | pm.tempCertMetricsLastRefreshed = time.Now() |
| 94 | pm.tempCertMetrics = GetCertMetrics() |
| 95 | } |
| 96 | |
| 97 | operatorMetrics, ok := pm.tempCertMetrics[operatorName] |
| 98 | if !ok { |
| 99 | log.Printf("No metrics for operator \"%s\"", operatorName) |
| 100 | return 0 |
| 101 | } |
| 102 | |
| 103 | count, ok := operatorMetrics[logname] |
| 104 | if !ok { |
| 105 | log.Printf("No metrics for log \"%s\" of operator \"%s\"", logname, operatorName) |
| 106 | return 0 |
| 107 | } |
| 108 | |
| 109 | return count |
| 110 | } |