Skip to content

Commit be2488d

Browse files
committed
feat: add metric for skipped certs per client
fixes #34
1 parent 2354802 commit be2488d

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

internal/metrics/prometheus.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package metrics
33
import (
44
"fmt"
55
"io"
6+
"strings"
67
"sync"
78
"time"
89

@@ -46,6 +47,9 @@ func WritePrometheus(w io.Writer, exposeProcessMetrics bool) {
4647
initCtLogMetrics()
4748
}
4849
ctLogMetricsInitMutex.Unlock()
50+
51+
getSkippedCertMetrics()
52+
4953
metrics.WritePrometheus(w, exposeProcessMetrics)
5054
}
5155

@@ -82,3 +86,33 @@ func getCertCountForLog(operatorName, logname string) int64 {
8286

8387
return tempCertMetrics[operatorName][logname]
8488
}
89+
90+
// getSkippedCertMetrics gets the number of skipped certificates for each client and creates metrics for it.
91+
// It also removes metrics for clients that are not connected anymore.
92+
func getSkippedCertMetrics() {
93+
skippedCerts := web.ClientHandler.GetSkippedCerts()
94+
for clientName := range skippedCerts {
95+
// Get or register a new counter for each client
96+
metricName := fmt.Sprintf("certstreamservergo_skipped_certs{client=\"%s\"}", clientName)
97+
c := metrics.GetOrCreateCounter(metricName)
98+
c.Set(skippedCerts[clientName])
99+
}
100+
101+
// Remove all metrics that are not in the list of current client skipped cert metrics
102+
// Get a list of current client skipped cert metrics
103+
for _, metricName := range metrics.ListMetricNames() {
104+
if !strings.HasPrefix(metricName, "certstreamservergo_skipped_certs") {
105+
continue
106+
}
107+
108+
clientName := strings.TrimPrefix(metricName, "certstreamservergo_skipped_certs{client=\"")
109+
clientName = strings.TrimSuffix(clientName, "\"}")
110+
111+
// Check if the registered metric is in the list of current client skipped cert metrics
112+
// If not, unregister the metric
113+
_, exists := skippedCerts[clientName]
114+
if !exists {
115+
metrics.UnregisterMetric(metricName)
116+
}
117+
}
118+
}

internal/web/broadcastmanager.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ func (bm *BroadcastManager) clientCountByType(subType SubscriptionType) (count i
7373
return count
7474
}
7575

76+
func (bm *BroadcastManager) GetSkippedCerts() map[string]uint64 {
77+
bm.clientLock.RLock()
78+
defer bm.clientLock.RUnlock()
79+
80+
skippedCerts := make(map[string]uint64, len(bm.clients))
81+
for _, c := range bm.clients {
82+
skippedCerts[c.name] = c.skippedCerts
83+
}
84+
85+
return skippedCerts
86+
}
87+
7688
// broadcaster is run in a goroutine and handles the dispatching of entries to clients.
7789
func (bm *BroadcastManager) broadcaster() {
7890
for {

internal/web/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type client struct {
2222
broadcastChan chan []byte
2323
name string
2424
subType SubscriptionType
25-
skippedCerts int64
25+
skippedCerts uint64
2626
}
2727

2828
func newClient(conn *websocket.Conn, subType SubscriptionType, name string, certBufferSize int) *client {

0 commit comments

Comments
 (0)