Skip to content

Commit

Permalink
replace promlog with go-kit (#6052)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 authored and web-flow committed Jul 24, 2024
1 parent ebabfbd commit 30187f6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
15 changes: 5 additions & 10 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/nginxinc/nginx-plus-go-client/client"
nginxCollector "github.com/nginxinc/nginx-prometheus-exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/promlog"
api_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
util_version "k8s.io/apimachinery/pkg/util/version"
Expand All @@ -39,6 +38,9 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

kitlog "github.com/go-kit/log"
"github.com/go-kit/log/level"
)

// Injected during build
Expand Down Expand Up @@ -736,15 +738,8 @@ func createPlusAndLatencyCollectors(
streamServerZoneVariableLabels := []string{"resource_type", "resource_name", "resource_namespace"}
variableLabelNames := nginxCollector.NewVariableLabelNames(upstreamServerVariableLabels, serverZoneVariableLabels, upstreamServerPeerVariableLabelNames,
streamUpstreamServerVariableLabels, streamServerZoneVariableLabels, streamUpstreamServerPeerVariableLabelNames, nil, nil)
infoLevel := new(promlog.AllowedLevel)
err := infoLevel.Set("info")
if err != nil {
glog.Error("Error setting prometheus exporter log level")
}
promlogConfig := &promlog.Config{
Level: infoLevel,
}
logger := promlog.New(promlogConfig)
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)
} else {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/dlclark/regexp2 v1.11.2
github.com/gkampitakis/go-snaps v0.5.4
github.com/go-chi/chi/v5 v5.1.0
github.com/go-kit/log v0.2.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/glog v1.2.0
github.com/google/go-cmp v0.6.0
Expand All @@ -19,7 +20,6 @@ require (
github.com/nginxinc/nginx-service-mesh v1.7.0
github.com/nginxinc/telemetry-exporter v0.1.0
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/common v0.47.0
github.com/spiffe/go-spiffe/v2 v2.3.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.28.0
Expand Down Expand Up @@ -63,7 +63,6 @@ require (
github.com/gkampitakis/go-diff v1.3.2 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.6 // indirect
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-ldap/ldap/v3 v3.4.8 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand Down Expand Up @@ -96,6 +95,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.47.0 // indirect
github.com/prometheus/procfs v0.15.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
Expand Down
16 changes: 5 additions & 11 deletions internal/metrics/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
"errors"
"fmt"
"net/http"
"os"
"strconv"
"time"

"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"
"github.com/prometheus/common/promlog"
v1 "k8s.io/api/core/v1"
)

Expand All @@ -26,16 +28,8 @@ func NewNginxMetricsClient(httpClient *http.Client) *prometheusClient.NginxClien

// 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) {
infoLevel := new(promlog.AllowedLevel)
err := infoLevel.Set("info")
if err != nil {
glog.Error("Error setting prometheus exporter log level")
}
promlogConfig := &promlog.Config{
Level: infoLevel,
}

logger := promlog.New(promlogConfig)
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)
}
Expand Down

0 comments on commit 30187f6

Please sign in to comment.