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

Simplify interpretation of produced metrics #23

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
21 changes: 9 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
default_install_hook_types:
- pre-commit
- pre-push
default_stages:
- pre-commit
repos:
- repo: https:/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand All @@ -9,16 +14,6 @@ repos:
- id: check-yaml
exclude: ^charts/kube-transition-metrics/templates/
- id: check-added-large-files
- repo: https:/golangci/golangci-lint
rev: v1.52.2
hooks:
- id: golangci-lint
name: golangci-lint
description: Fast linters runner for Go.
entry: golangci-lint run --timeout=2m --fix --out-format checkstyle
types: [go]
language: golang
pass_filenames: false
- repo: https:/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
Expand All @@ -32,11 +27,13 @@ repos:
description: Detect secrets in your data.
entry: bash -c 'docker run --rm -v "$PWD:/src" ghcr.io/trufflesecurity/trufflehog:latest git --branch=HEAD file:///src --fail --only-verified'
language: system
stages: ["commit", "push"]
stages:
- pre-push
- repo: local
hooks:
- id: semgrep
name: semgrep
entry: bash -c 'docker run --rm -v "$PWD:/src" returntocorp/semgrep semgrep --config p/ci --error --skip-unknown-extensions'
language: system
stages: ["commit", "push"]
stages:
- pre-push
8 changes: 4 additions & 4 deletions cmd/kube-transition-metrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
_ "net/http/pprof"
"os"

"github.com/BackMarket-oss/kube-transition-metrics/internal/logging"
"github.com/BackMarket-oss/kube-transition-metrics/internal/options"
"github.com/BackMarket-oss/kube-transition-metrics/internal/prommetrics"
"github.com/BackMarket-oss/kube-transition-metrics/internal/statistics"
"github.com/BackMarket-oss/kube-transition-metrics/internal/zerologhttp"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -61,10 +60,11 @@ func getKubeconfig(options *options.Options) *rest.Config {
}

func main() {
logging.Configure()
prommetrics.Register()

options := options.Parse()
zerolog.SetGlobalLevel(options.LogLevel)
logging.SetOptions(options)

config := getKubeconfig(options)
clientset, err := kubernetes.NewForConfig(config)
Expand All @@ -80,7 +80,7 @@ func main() {
go podCollector.Run(clientset)

http.Handle("/metrics", promhttp.Handler())
handler := zerologhttp.NewHandler(http.DefaultServeMux)
handler := logging.NewHTTPHandler(http.DefaultServeMux)
// No timeouts can be set, but that's OK for us as this HTTP server will not be
// exposed publicly.
//nolint:gosec
Expand Down
552 changes: 552 additions & 0 deletions doc/SCHEMA.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/prometheus/client_golang v1.16.0
github.com/rs/zerolog v1.30.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
26 changes: 13 additions & 13 deletions internal/zerologhttp/http.go → internal/logging/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zerologhttp
package logging

import (
"fmt"
Expand All @@ -9,18 +9,18 @@ import (
"github.com/rs/zerolog/log"
)

type responseLogger struct {
type httpResponseLogger struct {
responseWriter http.ResponseWriter
statusCode int
bodyBytesSent int64
}

func (rl *responseLogger) WriteHeader(code int) {
func (rl *httpResponseLogger) WriteHeader(code int) {
rl.statusCode = code
rl.responseWriter.WriteHeader(code)
}

func (rl *responseLogger) Write(data []byte) (int, error) {
func (rl *httpResponseLogger) Write(data []byte) (int, error) {
length, err := rl.responseWriter.Write(data)
rl.bodyBytesSent += int64(length)

Expand All @@ -31,36 +31,36 @@ func (rl *responseLogger) Write(data []byte) (int, error) {
return length, err
}

func (rl *responseLogger) Header() http.Header {
func (rl *httpResponseLogger) Header() http.Header {
return rl.responseWriter.Header()
}

// Handler is a custom request logger middleware.
type Handler struct {
// HTTPHandler is a custom request logger middleware.
type HTTPHandler struct {
handler http.Handler
}

// NewHandler creates a new Handler middleware.
func NewHandler(handler http.Handler) *Handler {
return &Handler{handler: handler}
// NewHTTPHandler creates a new Handler middleware.
func NewHTTPHandler(handler http.Handler) *HTTPHandler {
return &HTTPHandler{handler: handler}
}

func (c *Handler) logger() *zerolog.Logger {
func (c *HTTPHandler) logger() *zerolog.Logger {
logger := log.With().
Str("subsystem", "http").
Logger()

return &logger
}

func (c *Handler) ServeHTTP(
func (c *HTTPHandler) ServeHTTP(
writer http.ResponseWriter,
req *http.Request,
) {
startTime := time.Now()
logger := c.logger()

responseLogger := &responseLogger{responseWriter: writer}
responseLogger := &httpResponseLogger{responseWriter: writer}
// Call the next handler in the chain
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the context of ServeHTTP method, an http prefix can be skipped IMHO

c.handler.ServeHTTP(responseLogger, req)

Expand Down
19 changes: 19 additions & 0 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package logging

import (
"time"

"github.com/BackMarket-oss/kube-transition-metrics/internal/options"
"github.com/rs/zerolog"
)

// Configure configures zerolog with the required global settings.
func Configure() {
zerolog.DurationFieldInteger = false
zerolog.DurationFieldUnit = time.Second
}

// SetOptions configures zerolog global settings based on user-configured options.
func SetOptions(options *options.Options) {
zerolog.SetGlobalLevel(options.LogLevel)
}
Loading
Loading