Skip to content

Commit

Permalink
Merge pull request #7 from k8spacket/prometheus-metrics-enabling-flag
Browse files Browse the repository at this point in the history
added helm values to enable/disable exposing Prometheus metrics
  • Loading branch information
k8spacket authored Mar 6, 2024
2 parents 90604e4 + e753c52 commit c31c08c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
20 changes: 12 additions & 8 deletions nodegraph/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@ 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"))
if int(event.DeltaUs) > int(persistentDuration.Milliseconds()) {
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))

Expand All @@ -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))
}
11 changes: 8 additions & 3 deletions nodegraph/metrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package prometheus

import (
"github.com/prometheus/client_golang/prometheus"
"os"
"strconv"
)

var (
Expand Down Expand Up @@ -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)
}
}
26 changes: 15 additions & 11 deletions tls-parser/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
11 changes: 8 additions & 3 deletions tls-parser/metrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package prometheus

import (
"github.com/prometheus/client_golang/prometheus"
"os"
"strconv"
)

var (
Expand Down Expand Up @@ -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)
}
}

0 comments on commit c31c08c

Please sign in to comment.