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

Replace flag with kingpin and use promlog #420

Merged
merged 1 commit into from
Jun 9, 2023
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
24 changes: 8 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@ on:
push:
branches:
- main
paths-ignore:
- '**.md'
- 'LICENSE'
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
- "v[0-9]+.[0-9]+.[0-9]+"
pull_request:
branches:
- main
paths-ignore:
- '**.md'
- 'LICENSE'
types:
- opened
- reopened
- synchronize

env:
DOCKER_PLATFORMS: "linux/arm,linux/arm64,linux/amd64,linux/ppc64le,linux/s390x,linux/mips64le,linux/386"

jobs:

unit-tests:
name: Unit Tests
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -65,7 +54,6 @@ jobs:
run: |
echo "go_path=$(go env GOPATH)" >> $GITHUB_OUTPUT
- name: Setup QEMU

uses: docker/setup-qemu-action@e81a89b1732b9c48d79cd809d8d81d79c4647a18 # v2.1.0
with:
platforms: arm,arm64,ppc64le,s390x,mips64le,386
Expand Down Expand Up @@ -130,7 +118,7 @@ jobs:
- name: Publish Release Notes
uses: release-drafter/release-drafter@569eb7ee3a85817ab916c8f8ff03a5bd96c9c83e # v5.23.0
with:
publish: true
publish: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: ${{ startsWith(github.ref, 'refs/tags/') }}
Expand All @@ -142,18 +130,22 @@ jobs:
uses: goreleaser/goreleaser-action@f82d6c1c344bcacabba2c841718984797f664a6b # v4.2.0
with:
version: latest
args: ${{ !startsWith(github.ref, 'refs/tags/') && 'build --snapshot' || 'release' }} ${{ github.event_name == 'pull_request' && '--single-target' || '' }} --rm-dist
args: ${{ !startsWith(github.ref, 'refs/tags/') && 'build --snapshot' || 'release' }} ${{ github.event_name == 'pull_request' && '--single-target' || '' }} --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GOPATH: ${{ steps.go.outputs.go_path }}
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.NGINX_PAT }}
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_COMMUNITY }}

- name: Print NGINX Prometheus Exporter info
run: ./dist/nginx-prometheus-exporter_linux_amd64_v1/nginx-prometheus-exporter --version
continue-on-error: true

- name: Build and Push Docker Image
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 # v4.0.0
with:
file: build/Dockerfile
context: '.'
context: "."
target: goreleaser
platforms: ${{ github.event_name != 'pull_request' && env.DOCKER_PLATFORMS || '' }}
tags: ${{ steps.meta.outputs.tags }}
Expand Down
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ linters-settings:
- name: unused-parameter
- name: var-declaration
- name: var-naming
errcheck:
exclude-functions:
- (github.com/go-kit/log.Logger).Log

linters:
enable:
Expand Down
31 changes: 15 additions & 16 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
before:
hooks:
- go mod tidy
- go mod verify

builds:
- env:
- CGO_ENABLED=0
Expand Down Expand Up @@ -33,26 +28,27 @@ builds:
- all=-trimpath={{.Env.GOPATH}}
asmflags:
- all=-trimpath={{.Env.GOPATH}}
ldflags:
- '-s -w -X github.com/prometheus/common/version.Version={{.Version}} -X github.com/prometheus/common/version.BuildDate={{.Date}} -X github.com/prometheus/common/version.Branch={{.Branch}} -X github.com/prometheus/common/version.BuildUser=goreleaser'

sboms:
- artifacts: archive
changelog:
skip: true

archives:
- format_overrides:
- goos: windows
format: zip

checksum:
name_template: 'sha256sums.txt'
- format_overrides:
- goos: windows
format: zip

changelog:
skip: true
sboms:
- artifacts: archive
documents:
- "${artifact}.spdx.json"

brews:
- tap:
owner: nginxinc
name: homebrew-tap
token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
token: '{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}'
folder: Formula
homepage: https://www.nginx.com/
description: NGINX Prometheus Exporter for NGINX and NGINX Plus
Expand All @@ -71,3 +67,6 @@ announce:

milestones:
- close: true

snapshot:
name_template: 'edge'
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ PREFIX = nginx/nginx-prometheus-exporter

.PHONY: nginx-prometheus-exporter
nginx-prometheus-exporter:
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=$(VERSION)" -o nginx-prometheus-exporter
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X github.com/prometheus/common/version.Version=$(VERSION)" -o nginx-prometheus-exporter

.PHONY: build-goreleaser
build-goreleaser: ## Build all binaries using GoReleaser
@goreleaser -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with GoReleaser. Follow the docs to install it https://goreleaser.com/install\n"; exit $$code)
GOPATH=$(shell go env GOPATH) goreleaser build --rm-dist --snapshot
GOPATH=$(shell go env GOPATH) goreleaser build --clean --snapshot

.PHONY: lint
lint:
Expand Down
9 changes: 6 additions & 3 deletions collector/nginx.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package collector

import (
"log"
"sync"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/nginxinc/nginx-prometheus-exporter/client"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -14,12 +15,14 @@ type NginxCollector struct {
metrics map[string]*prometheus.Desc
upMetric prometheus.Gauge
mutex sync.Mutex
logger log.Logger
}

// NewNginxCollector creates an NginxCollector.
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string) *NginxCollector {
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string, logger log.Logger) *NginxCollector {
return &NginxCollector{
nginxClient: nginxClient,
logger: logger,
metrics: map[string]*prometheus.Desc{
"connections_active": newGlobalMetric(namespace, "connections_active", "Active client connections", constLabels),
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections", constLabels),
Expand Down Expand Up @@ -52,7 +55,7 @@ func (c *NginxCollector) Collect(ch chan<- prometheus.Metric) {
if err != nil {
c.upMetric.Set(nginxDown)
ch <- c.upMetric
log.Printf("Error getting stats: %v", err)
level.Error(c.logger).Log("msg", "Error getting stats", "error", err.Error())
return
}

Expand Down
27 changes: 12 additions & 15 deletions collector/nginx_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package collector

import (
"fmt"
"log"
"sync"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
plusclient "github.com/nginxinc/nginx-plus-go-client/client"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -51,6 +52,7 @@ type NginxPlusCollector struct {
upstreamServerPeerLabels map[string][]string
streamUpstreamServerPeerLabels map[string][]string
variableLabelsMutex sync.RWMutex
logger log.Logger
}

// UpdateUpstreamServerPeerLabels updates the Upstream Server Peer Labels
Expand Down Expand Up @@ -222,7 +224,7 @@ func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZone
}

// NewNginxPlusCollector creates an NginxPlusCollector.
func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string, variableLabelNames VariableLabelNames, constLabels map[string]string) *NginxPlusCollector {
func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string, variableLabelNames VariableLabelNames, constLabels map[string]string, logger log.Logger) *NginxPlusCollector {
upstreamServerVariableLabelNames := append(variableLabelNames.UpstreamServerVariableLabelNames, variableLabelNames.UpstreamServerPeerVariableLabelNames...)
streamUpstreamServerVariableLabelNames := append(variableLabelNames.StreamUpstreamServerVariableLabelNames, variableLabelNames.StreamUpstreamServerPeerVariableLabelNames...)
return &NginxPlusCollector{
Expand All @@ -234,6 +236,7 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string
streamUpstreamServerPeerLabels: make(map[string][]string),
streamUpstreamServerLabels: make(map[string][]string),
nginxClient: nginxClient,
logger: logger,
totalMetrics: map[string]*prometheus.Desc{
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections", constLabels),
"connections_dropped": newGlobalMetric(namespace, "connections_dropped", "Dropped client connections", constLabels),
Expand Down Expand Up @@ -551,7 +554,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
if err != nil {
c.upMetric.Set(nginxDown)
ch <- c.upMetric
log.Printf("Error getting stats: %v", err)
level.Warn(c.logger).Log("msg", "Error getting stats", "error", err.Error())
return
}

Expand Down Expand Up @@ -582,8 +585,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
varLabelValues := c.getServerZoneLabelValues(name)

if c.variableLabelNames.ServerZoneVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.ServerZoneVariableLabelNames) {
log.Printf("wrong number of labels for http zone %v. For labels %v, got values: %v. Empty labels will be used instead",
name, c.variableLabelNames.ServerZoneVariableLabelNames, varLabelValues)
level.Warn(c.logger).Log("msg", "wrong number of labels for http zone, empty labels will be used instead", "zone", name, "expected", len(c.variableLabelNames.ServerZoneVariableLabelNames), "got", len(varLabelValues))
for range c.variableLabelNames.ServerZoneVariableLabelNames {
labelValues = append(labelValues, "")
}
Expand Down Expand Up @@ -704,8 +706,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
varLabelValues := c.getStreamServerZoneLabelValues(name)

if c.variableLabelNames.StreamServerZoneVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.StreamServerZoneVariableLabelNames) {
log.Printf("wrong number of labels for stream server zone %v. For labels %v, got values: %v. Empty labels will be used instead",
name, c.variableLabelNames.StreamServerZoneVariableLabelNames, varLabelValues)
level.Warn(c.logger).Log("msg", "wrong number of labels for stream server zone, empty labels will be used instead", "zone", name, "expected", len(c.variableLabelNames.StreamServerZoneVariableLabelNames), "got", len(varLabelValues))
for range c.variableLabelNames.StreamServerZoneVariableLabelNames {
labelValues = append(labelValues, "")
}
Expand Down Expand Up @@ -742,8 +743,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
varLabelValues := c.getUpstreamServerLabelValues(name)

if c.variableLabelNames.UpstreamServerVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.UpstreamServerVariableLabelNames) {
log.Printf("wrong number of labels for upstream %v. For labels %v, got values: %v. Empty labels will be used instead",
name, c.variableLabelNames.UpstreamServerVariableLabelNames, varLabelValues)
level.Warn(c.logger).Log("msg", "wrong number of labels for upstream, empty labels will be used instead", "upstream", name, "expected", len(c.variableLabelNames.UpstreamServerVariableLabelNames), "got", len(varLabelValues))
for range c.variableLabelNames.UpstreamServerVariableLabelNames {
labelValues = append(labelValues, "")
}
Expand All @@ -754,8 +754,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
upstreamServer := fmt.Sprintf("%v/%v", name, peer.Server)
varPeerLabelValues := c.getUpstreamServerPeerLabelValues(upstreamServer)
if c.variableLabelNames.UpstreamServerPeerVariableLabelNames != nil && len(varPeerLabelValues) != len(c.variableLabelNames.UpstreamServerPeerVariableLabelNames) {
log.Printf("wrong number of labels for upstream peer %v. For labels %v, got values: %v. Empty labels will be used instead",
peer.Server, c.variableLabelNames.UpstreamServerPeerVariableLabelNames, varPeerLabelValues)
level.Warn(c.logger).Log("msg", "wrong number of labels for upstream peer, empty labels will be used instead", "upstream", name, "peer", peer.Server, "expected", len(c.variableLabelNames.UpstreamServerPeerVariableLabelNames), "got", len(varPeerLabelValues))
for range c.variableLabelNames.UpstreamServerPeerVariableLabelNames {
labelValues = append(labelValues, "")
}
Expand Down Expand Up @@ -901,8 +900,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
varLabelValues := c.getStreamUpstreamServerLabelValues(name)

if c.variableLabelNames.StreamUpstreamServerVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.StreamUpstreamServerVariableLabelNames) {
log.Printf("wrong number of labels for stream server %v. For labels %v, got values: %v. Empty labels will be used instead",
name, c.variableLabelNames.StreamUpstreamServerVariableLabelNames, varLabelValues)
level.Warn(c.logger).Log("msg", "wrong number of labels for stream server, empty labels will be used instead", "server", name, "labels", c.variableLabelNames.StreamUpstreamServerVariableLabelNames, "values", varLabelValues)
for range c.variableLabelNames.StreamUpstreamServerVariableLabelNames {
labelValues = append(labelValues, "")
}
Expand All @@ -913,8 +911,7 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
upstreamServer := fmt.Sprintf("%v/%v", name, peer.Server)
varPeerLabelValues := c.getStreamUpstreamServerPeerLabelValues(upstreamServer)
if c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames != nil && len(varPeerLabelValues) != len(c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames) {
log.Printf("wrong number of labels for stream upstream peer %v. For labels %v, got values: %v. Empty labels will be used instead",
peer.Server, c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames, varPeerLabelValues)
level.Warn(c.logger).Log("msg", "wrong number of labels for stream upstream peer, empty labels will be used instead", "server", upstreamServer, "labels", c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames, "values", varPeerLabelValues)
for range c.variableLabelNames.StreamUpstreamServerPeerVariableLabelNames {
labelValues = append(labelValues, "")
}
Expand Down
Loading