From e753c529e0ce719c7f0319ae2d189c2fd05c5809 Mon Sep 17 00:00:00 2001 From: k8spacket Date: Wed, 6 Mar 2024 13:08:34 +0100 Subject: [PATCH] added helm values to enable/disable exposing Prometheus metrics --- nodegraph/metrics/metrics.go | 20 +++++++++------- nodegraph/metrics/prometheus/prometheus.go | 11 ++++++--- tls-parser/metrics/metrics.go | 26 ++++++++++++--------- tls-parser/metrics/prometheus/prometheus.go | 11 ++++++--- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/nodegraph/metrics/metrics.go b/nodegraph/metrics/metrics.go index 2ab52a7..23baa41 100644 --- a/nodegraph/metrics/metrics.go +++ b/nodegraph/metrics/metrics.go @@ -11,11 +11,6 @@ import ( ) func StoreNodegraphMetric(event plugin_api.TCPEvent) { - hideSrcPort, _ := strconv.ParseBool(os.Getenv("K8S_PACKET_HIDE_SRC_PORT")) - var srcPortMetrics = strconv.Itoa(int(event.Client.Port)) - if hideSrcPort { - srcPortMetrics = "dynamic" - } var persistent = false var persistentDuration, _ = time.ParseDuration(os.Getenv("K8S_PACKET_TCP_PERSISTENT_DURATION")) @@ -23,9 +18,7 @@ func StoreNodegraphMetric(event plugin_api.TCPEvent) { persistent = true } - prometheus.K8sPacketBytesSentMetric.WithLabelValues(event.Client.Namespace, event.Client.Addr, event.Client.Name, srcPortMetrics, event.Server.Addr, event.Server.Name, strconv.Itoa(int(event.Server.Port)), strconv.FormatBool(persistent)).Observe(float64(event.TxB)) - prometheus.K8sPacketBytesReceivedMetric.WithLabelValues(event.Client.Namespace, event.Client.Addr, event.Client.Name, srcPortMetrics, event.Server.Addr, event.Server.Name, strconv.Itoa(int(event.Server.Port)), strconv.FormatBool(persistent)).Observe(float64(event.RxB)) - prometheus.K8sPacketDurationSecondsMetric.WithLabelValues(event.Client.Namespace, event.Client.Addr, event.Client.Name, srcPortMetrics, event.Server.Addr, event.Server.Name, strconv.Itoa(int(event.Server.Port)), strconv.FormatBool(persistent)).Observe(float64(event.DeltaUs)) + sendPrometheusMetrics(event, persistent) nodegraph.UpdateNodeGraph(event.Client.Addr, event.Client.Name, event.Client.Namespace, event.Server.Addr, event.Server.Name, event.Server.Namespace, persistent, float64(event.TxB), float64(event.RxB), float64(event.DeltaUs)) @@ -43,3 +36,14 @@ func StoreNodegraphMetric(event plugin_api.TCPEvent) { float64(event.RxB), float64(event.DeltaUs)) } + +func sendPrometheusMetrics(event plugin_api.TCPEvent, persistent bool) { + hideSrcPort, _ := strconv.ParseBool(os.Getenv("K8S_PACKET_TCP_METRICS_HIDE_SRC_PORT")) + var srcPortMetrics = strconv.Itoa(int(event.Client.Port)) + if hideSrcPort { + srcPortMetrics = "dynamic" + } + prometheus.K8sPacketBytesSentMetric.WithLabelValues(event.Client.Namespace, event.Client.Addr, event.Client.Name, srcPortMetrics, event.Server.Addr, event.Server.Name, strconv.Itoa(int(event.Server.Port)), strconv.FormatBool(persistent)).Observe(float64(event.TxB)) + prometheus.K8sPacketBytesReceivedMetric.WithLabelValues(event.Client.Namespace, event.Client.Addr, event.Client.Name, srcPortMetrics, event.Server.Addr, event.Server.Name, strconv.Itoa(int(event.Server.Port)), strconv.FormatBool(persistent)).Observe(float64(event.RxB)) + prometheus.K8sPacketDurationSecondsMetric.WithLabelValues(event.Client.Namespace, event.Client.Addr, event.Client.Name, srcPortMetrics, event.Server.Addr, event.Server.Name, strconv.Itoa(int(event.Server.Port)), strconv.FormatBool(persistent)).Observe(float64(event.DeltaUs)) +} diff --git a/nodegraph/metrics/prometheus/prometheus.go b/nodegraph/metrics/prometheus/prometheus.go index 3546898..ecbd8d0 100644 --- a/nodegraph/metrics/prometheus/prometheus.go +++ b/nodegraph/metrics/prometheus/prometheus.go @@ -2,6 +2,8 @@ package prometheus import ( "github.com/prometheus/client_golang/prometheus" + "os" + "strconv" ) var ( @@ -29,7 +31,10 @@ var ( ) func init() { - prometheus.MustRegister(K8sPacketBytesSentMetric) - prometheus.MustRegister(K8sPacketBytesReceivedMetric) - prometheus.MustRegister(K8sPacketDurationSecondsMetric) + sendTCPMetrics, _ := strconv.ParseBool(os.Getenv("K8S_PACKET_TCP_METRICS_ENABLED")) + if sendTCPMetrics { + prometheus.MustRegister(K8sPacketBytesSentMetric) + prometheus.MustRegister(K8sPacketBytesReceivedMetric) + prometheus.MustRegister(K8sPacketDurationSecondsMetric) + } } diff --git a/tls-parser/metrics/metrics.go b/tls-parser/metrics/metrics.go index cec6667..ce878ef 100644 --- a/tls-parser/metrics/metrics.go +++ b/tls-parser/metrics/metrics.go @@ -45,6 +45,21 @@ func StoreTLSMetrics(tlsEvent plugin_api.TLSEvent) { storeInDatabase(&tlsConnection, &tlsDetails) + sendPrometheusMetrics(tlsConnection, tlsDetails) + + var j, _ = json.Marshal(tlsConnection) + tls_parser_log.LOGGER.Println("TLS Record:", string(j)) +} + +func storeInDatabase(tlsConnection *model.TLSConnection, tlsDetails *model.TLSDetails) { + var id = strconv.Itoa(int(idb.HashId(fmt.Sprintf("%s-%s", tlsConnection.Src, tlsConnection.Dst)))) + tlsConnection.Id = id + tls_connection_db.Upsert(id, tlsConnection) + tlsDetails.Id = id + tls_detail_db.Upsert(id, tlsDetails, certificate.UpdateCertificateInfo) +} + +func sendPrometheusMetrics(tlsConnection model.TLSConnection, tlsDetails model.TLSDetails) { prometheus.K8sPacketTLSRecordMetric.WithLabelValues( tlsConnection.SrcNamespace, tlsConnection.Src, @@ -60,15 +75,4 @@ func StoreTLSMetrics(tlsEvent plugin_api.TLSEvent) { tlsDetails.Dst, strconv.Itoa(int(tlsDetails.Port)), tlsDetails.Domain).Add(1) - - var j, _ = json.Marshal(tlsConnection) - tls_parser_log.LOGGER.Println("TLS Record:", string(j)) -} - -func storeInDatabase(tlsConnection *model.TLSConnection, tlsDetails *model.TLSDetails) { - var id = strconv.Itoa(int(idb.HashId(fmt.Sprintf("%s-%s", tlsConnection.Src, tlsConnection.Dst)))) - tlsConnection.Id = id - tls_connection_db.Upsert(id, tlsConnection) - tlsDetails.Id = id - tls_detail_db.Upsert(id, tlsDetails, certificate.UpdateCertificateInfo) } diff --git a/tls-parser/metrics/prometheus/prometheus.go b/tls-parser/metrics/prometheus/prometheus.go index 3d798c5..a8e80dd 100644 --- a/tls-parser/metrics/prometheus/prometheus.go +++ b/tls-parser/metrics/prometheus/prometheus.go @@ -2,6 +2,8 @@ package prometheus import ( "github.com/prometheus/client_golang/prometheus" + "os" + "strconv" ) var ( @@ -30,7 +32,10 @@ var ( ) func init() { - prometheus.MustRegister(K8sPacketTLSRecordMetric) - prometheus.MustRegister(K8sPacketTLSCertificateExpirationMetric) - prometheus.MustRegister(K8sPacketTLSCertificateExpirationCounterMetric) + sendTLSMetrics, _ := strconv.ParseBool(os.Getenv("K8S_PACKET_TLS_METRICS_ENABLED")) + if sendTLSMetrics { + prometheus.MustRegister(K8sPacketTLSRecordMetric) + prometheus.MustRegister(K8sPacketTLSCertificateExpirationMetric) + prometheus.MustRegister(K8sPacketTLSCertificateExpirationCounterMetric) + } }