Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace glog in metrics & telemetry packages #6587

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@
registry = prometheus.NewRegistry()
mc = collectors.NewLocalManagerMetricsCollector(constLabels)
cc = collectors.NewControllerMetricsCollector(*enableCustomResources, constLabels)
processCollector := collectors.NewNginxProcessesMetricsCollector(constLabels)
processCollector := collectors.NewNginxProcessesMetricsCollector(ctx, constLabels)

Check warning on line 717 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L717

Added line #L717 was not covered by tests
workQueueCollector := collectors.NewWorkQueueMetricsCollector(constLabels)

err = mc.Register(registry)
Expand Down Expand Up @@ -781,18 +781,18 @@
logger := kitlog.NewLogfmtLogger(os.Stdout)
logger = level.NewFilter(logger, level.AllowError())
plusCollector = nginxCollector.NewNginxPlusCollector(plusClient, "nginx_ingress_nginxplus", variableLabelNames, constLabels, logger)
go metrics.RunPrometheusListenerForNginxPlus(*prometheusMetricsListenPort, plusCollector, registry, prometheusSecret)
go metrics.RunPrometheusListenerForNginxPlus(ctx, *prometheusMetricsListenPort, plusCollector, registry, prometheusSecret)

Check warning on line 784 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L784

Added line #L784 was not covered by tests
} else {
httpClient := getSocketClient("/var/lib/nginx/nginx-status.sock")
client := metrics.NewNginxMetricsClient(httpClient)
go metrics.RunPrometheusListenerForNginx(*prometheusMetricsListenPort, client, registry, constLabels, prometheusSecret)
go metrics.RunPrometheusListenerForNginx(ctx, *prometheusMetricsListenPort, client, registry, constLabels, prometheusSecret)

Check warning on line 788 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L788

Added line #L788 was not covered by tests
}
if *enableLatencyMetrics {
lc = collectors.NewLatencyMetricsCollector(constLabels, upstreamServerVariableLabels, upstreamServerPeerVariableLabelNames)
lc = collectors.NewLatencyMetricsCollector(ctx, constLabels, upstreamServerVariableLabels, upstreamServerPeerVariableLabelNames)

Check warning on line 791 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L791

Added line #L791 was not covered by tests
if err := lc.Register(registry); err != nil {
nl.Errorf(l, "Error registering Latency Prometheus metrics: %v", err)
}
syslogListener = metrics.NewLatencyMetricsListener("/var/lib/nginx/nginx-syslog.sock", lc)
syslogListener = metrics.NewLatencyMetricsListener(ctx, "/var/lib/nginx/nginx-syslog.sock", lc)

Check warning on line 795 in cmd/nginx-ingress/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/nginx-ingress/main.go#L795

Added line #L795 was not covered by tests
go syslogListener.Run()
}
}
Expand Down
15 changes: 10 additions & 5 deletions internal/metrics/collectors/latency.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package collectors

import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strconv"
"strings"
"sync"

"github.com/golang/glog"
nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -73,10 +75,12 @@
metricsPublishedMap metricsPublishedMap
metricsPublishedMutex sync.Mutex
variableLabelsMutex sync.RWMutex
logger *slog.Logger
}

// NewLatencyMetricsCollector creates a new LatencyMetricsCollector
func NewLatencyMetricsCollector(
ctx context.Context,
constLabels map[string]string,
upstreamServerLabelNames []string,
upstreamServerPeerLabelNames []string,
Expand All @@ -96,6 +100,7 @@
metricsPublishedMap: make(metricsPublishedMap),
upstreamServerLabelNames: upstreamServerLabelNames,
upstreamServerPeerLabelNames: upstreamServerPeerLabelNames,
logger: nl.LoggerFromContext(ctx),

Check warning on line 103 in internal/metrics/collectors/latency.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/latency.go#L103

Added line #L103 was not covered by tests
}
}

Expand Down Expand Up @@ -141,7 +146,7 @@
for _, labelValues := range l.listAndDeleteMetricsPublished(name) {
success := l.httpLatency.DeleteLabelValues(labelValues...)
if !success {
glog.Warningf("could not delete metric for upstream server peer: %s with values: %v", name, labelValues)
nl.Warnf(l.logger, "could not delete metric for upstream server peer: %s with values: %v", name, labelValues)

Check warning on line 149 in internal/metrics/collectors/latency.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/latency.go#L149

Added line #L149 was not covered by tests
}
}
}
Expand Down Expand Up @@ -178,7 +183,7 @@
func (l *LatencyMetricsCollector) RecordLatency(syslogMsg string) {
lm, err := parseMessage(syslogMsg)
if err != nil {
glog.V(3).Infof("could not parse syslog message: %v", err)
nl.Debugf(l.logger, "could not parse syslog message: %v", err)

Check warning on line 186 in internal/metrics/collectors/latency.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/latency.go#L186

Added line #L186 was not covered by tests
return
}

Expand All @@ -188,13 +193,13 @@
// https://github.com/nginxinc/kubernetes-ingress/issues/5010
// https://github.com/nginxinc/kubernetes-ingress/issues/6124
if lm.Upstream == "-" {
glog.V(3).Infof("latency metrics for gRPC upstreams: %v", lm)
nl.Debugf(l.logger, "latency metrics for gRPC upstreams: %v", lm)

Check warning on line 196 in internal/metrics/collectors/latency.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/latency.go#L196

Added line #L196 was not covered by tests
return
}

labelValues, err := l.createLatencyLabelValues(lm)
if err != nil {
glog.Errorf("cannot record latency for upstream %s and server %s: %v", lm.Upstream, lm.Server, err)
nl.Errorf(l.logger, "cannot record latency for upstream %s and server %s: %v", lm.Upstream, lm.Server, err)

Check warning on line 202 in internal/metrics/collectors/latency.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/latency.go#L202

Added line #L202 was not covered by tests
return
}
l.httpLatency.WithLabelValues(labelValues...).Observe(lm.Latency * 1000)
Expand Down
10 changes: 7 additions & 3 deletions internal/metrics/collectors/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

import (
"bytes"
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/golang/glog"
nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
"github.com/prometheus/client_golang/prometheus"
)

// NginxProcessesMetricsCollector implements prometheus.Collector interface
type NginxProcessesMetricsCollector struct {
workerProcessTotal *prometheus.GaugeVec
logger *slog.Logger
}

// NewNginxProcessesMetricsCollector creates a new NginxProcessMetricsCollector
func NewNginxProcessesMetricsCollector(constLabels map[string]string) *NginxProcessesMetricsCollector {
func NewNginxProcessesMetricsCollector(ctx context.Context, constLabels map[string]string) *NginxProcessesMetricsCollector {

Check warning on line 24 in internal/metrics/collectors/processes.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/processes.go#L24

Added line #L24 was not covered by tests
return &NginxProcessesMetricsCollector{
workerProcessTotal: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -29,14 +32,15 @@
},
[]string{"generation"},
),
logger: nl.LoggerFromContext(ctx),

Check warning on line 35 in internal/metrics/collectors/processes.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/processes.go#L35

Added line #L35 was not covered by tests
}
}

// updateWorkerProcessCount sets the number of NGINX worker processes
func (pc *NginxProcessesMetricsCollector) updateWorkerProcessCount() {
currWorkerProcesses, prevWorkerProcesses, err := getWorkerProcesses()
if err != nil {
glog.Errorf("unable to collect process metrics : %v", err)
nl.Errorf(pc.logger, "unable to collect process metrics : %v", err)

Check warning on line 43 in internal/metrics/collectors/processes.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/collectors/processes.go#L43

Added line #L43 was not covered by tests
return
}
pc.workerProcessTotal.WithLabelValues("current").Set(float64(currWorkerProcesses))
Expand Down
29 changes: 17 additions & 12 deletions internal/metrics/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"strconv"
Expand All @@ -13,12 +14,13 @@
"github.com/go-chi/chi/v5"
kitlog "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/glog"
prometheusClient "github.com/nginxinc/nginx-prometheus-exporter/client"
nginxCollector "github.com/nginxinc/nginx-prometheus-exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
v1 "k8s.io/api/core/v1"

nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
)

// NewNginxMetricsClient creates an NginxClient to fetch stats from NGINX over an unix socket
Expand All @@ -27,41 +29,43 @@
}

// RunPrometheusListenerForNginx runs an http server to expose Prometheus metrics for NGINX
func RunPrometheusListenerForNginx(port int, client *prometheusClient.NginxClient, registry *prometheus.Registry, constLabels map[string]string, prometheusSecret *v1.Secret) {
func RunPrometheusListenerForNginx(ctx context.Context, port int, client *prometheusClient.NginxClient, registry *prometheus.Registry, constLabels map[string]string, prometheusSecret *v1.Secret) {

Check warning on line 32 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L32

Added line #L32 was not covered by tests
logger := kitlog.NewLogfmtLogger(os.Stdout)
logger = level.NewFilter(logger, level.AllowError())
registry.MustRegister(nginxCollector.NewNginxCollector(client, "nginx_ingress_nginx", constLabels, logger))
runServer(strconv.Itoa(port), registry, prometheusSecret)
runServer(ctx, strconv.Itoa(port), registry, prometheusSecret)

Check warning on line 36 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L36

Added line #L36 was not covered by tests
}

// RunPrometheusListenerForNginxPlus runs an http server to expose Prometheus metrics for NGINX Plus
func RunPrometheusListenerForNginxPlus(port int, nginxPlusCollector prometheus.Collector, registry *prometheus.Registry, prometheusSecret *v1.Secret) {
func RunPrometheusListenerForNginxPlus(ctx context.Context, port int, nginxPlusCollector prometheus.Collector, registry *prometheus.Registry, prometheusSecret *v1.Secret) {

Check warning on line 40 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L40

Added line #L40 was not covered by tests
registry.MustRegister(nginxPlusCollector)
runServer(strconv.Itoa(port), registry, prometheusSecret)
runServer(ctx, strconv.Itoa(port), registry, prometheusSecret)

Check warning on line 42 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L42

Added line #L42 was not covered by tests
}

// runServer starts the metrics server.
func runServer(port string, registry prometheus.Gatherer, prometheusSecret *v1.Secret) {
func runServer(ctx context.Context, port string, registry prometheus.Gatherer, prometheusSecret *v1.Secret) {

Check warning on line 46 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L46

Added line #L46 was not covered by tests
addr := fmt.Sprintf(":%s", port)
s, err := NewServer(addr, registry, prometheusSecret)
l := nl.LoggerFromContext(ctx)
s, err := newServer(ctx, addr, registry, prometheusSecret)

Check warning on line 49 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L48-L49

Added lines #L48 - L49 were not covered by tests
if err != nil {
glog.Fatal(err)
nl.Fatal(l, err)

Check warning on line 51 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L51

Added line #L51 was not covered by tests
}
glog.Infof("Starting prometheus listener on: %s/metrics", addr)
glog.Fatal(s.ListenAndServe())
nl.Infof(l, "Starting prometheus listener on: %s/metrics", addr)
nl.Fatal(l, s.ListenAndServe())

Check warning on line 54 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}

// Server holds information about NIC metrics server.
type Server struct {
Server *http.Server
URL string
Registry prometheus.Gatherer
logger *slog.Logger
}

// NewServer creates HTTP server for serving NIC metrics for Prometheus.
//
// Metrics are exposed on the `/metrics` endpoint.
func NewServer(addr string, registry prometheus.Gatherer, secret *v1.Secret) (*Server, error) {
func newServer(ctx context.Context, addr string, registry prometheus.Gatherer, secret *v1.Secret) (*Server, error) {

Check warning on line 68 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L68

Added line #L68 was not covered by tests
s := Server{
Server: &http.Server{
Addr: addr,
Expand All @@ -70,6 +74,7 @@
},
URL: fmt.Sprintf("http://%s/", addr),
Registry: registry,
logger: nl.LoggerFromContext(ctx),

Check warning on line 77 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L77

Added line #L77 was not covered by tests
}
// Secrets are read from K8s API. If the secret for Prometheus is present
// we configure Metrics Server with the key and cert.
Expand Down Expand Up @@ -99,7 +104,7 @@
</body>
</html>`))
if err != nil {
glog.Errorf("error while sending a response for the '/' path: %v", err)
nl.Errorf(s.logger, "error while sending a response for the '/' path: %v", err)

Check warning on line 107 in internal/metrics/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/listener.go#L107

Added line #L107 was not covered by tests
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
Expand Down
19 changes: 11 additions & 8 deletions internal/metrics/syslog_listener.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package metrics

import (
"context"
"errors"
"log/slog"
"net"

"github.com/golang/glog"

nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
"github.com/nginxinc/kubernetes-ingress/internal/metrics/collectors"
)

Expand All @@ -21,21 +22,23 @@
conn *net.UnixConn
addr string
collector collectors.LatencyCollector
logger *slog.Logger
}

// NewLatencyMetricsListener returns a LatencyMetricsListener that listens over a unix socket
// for syslog messages from nginx.
func NewLatencyMetricsListener(sockPath string, c collectors.LatencyCollector) SyslogListener {
glog.Infof("Starting latency metrics server listening on: %s", sockPath)
func NewLatencyMetricsListener(ctx context.Context, sockPath string, c collectors.LatencyCollector) SyslogListener {
l := nl.LoggerFromContext(ctx)
nl.Infof(l, "Starting latency metrics server listening on: %s", sockPath)

Check warning on line 32 in internal/metrics/syslog_listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/syslog_listener.go#L30-L32

Added lines #L30 - L32 were not covered by tests
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: sockPath,
Net: "unixgram",
})
if err != nil {
glog.Errorf("Failed to create latency metrics listener: %v. Latency metrics will not be collected.", err)
nl.Errorf(l, "Failed to create latency metrics listener: %v. Latency metrics will not be collected.", err)

Check warning on line 38 in internal/metrics/syslog_listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/syslog_listener.go#L38

Added line #L38 was not covered by tests
return NewSyslogFakeServer()
}
return &LatencyMetricsListener{conn: conn, addr: sockPath, collector: c}
return &LatencyMetricsListener{conn: conn, addr: sockPath, collector: c, logger: l}

Check warning on line 41 in internal/metrics/syslog_listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/syslog_listener.go#L41

Added line #L41 was not covered by tests
}

// Run reads from the unix connection until an unrecoverable error occurs or the connection is closed.
Expand All @@ -45,7 +48,7 @@
n, err := l.conn.Read(buffer)
if err != nil {
if !isErrorRecoverable(err) {
glog.Info("Stopping latency metrics listener")
nl.Info(l.logger, "Stopping latency metrics listener")

Check warning on line 51 in internal/metrics/syslog_listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/syslog_listener.go#L51

Added line #L51 was not covered by tests
return
}
}
Expand All @@ -57,7 +60,7 @@
func (l LatencyMetricsListener) Stop() {
err := l.conn.Close()
if err != nil {
glog.Errorf("error closing latency metrics unix connection: %v", err)
nl.Errorf(l.logger, "error closing latency metrics unix connection: %v", err)

Check warning on line 63 in internal/metrics/syslog_listener.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/syslog_listener.go#L63

Added line #L63 was not covered by tests
}
}

Expand Down
Loading
Loading