@@ -3,6 +3,7 @@ package metrics
33import (
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+ }
0 commit comments