Skip to content

Commit

Permalink
feat!: First working prometheus proxy (#1)
Browse files Browse the repository at this point in the history
* feat!: First working prometheus proxy

* update go mod
  • Loading branch information
Richard87 authored Oct 11, 2024
1 parent 519114c commit 25e4ab3
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 5 deletions.
34 changes: 34 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"net/url"

"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog/log"
)

type Config struct {
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
LogPretty bool `envconfig:"LOG_PRETTY" default:"false"`
Port int `envconfig:"PORT" default:"8000"`

Prometheus url.URL `envconfig:"PROMETHEUS" required:"true"`
}

func MustParseConfig() Config {
var c Config
err := envconfig.Process("", &c)
if err != nil {
_ = envconfig.Usage("", &c)
log.Fatal().Msg(err.Error())
}

initLogger(c)
log.Info().Msg("Starting")
log.Info().Int("Port", c.Port).Send()
log.Info().Str("Log level", c.LogLevel).Send()
log.Info().Bool("Log pretty", c.LogPretty).Send()
log.Info().Stringer("Prometheus", &c.Prometheus).Send()

return c
}
19 changes: 16 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
module github.com/equinor/radix-promeheus-proxy
module radix-prometheus-proxy

go 1.23.2

require github.com/rs/zerolog v1.33.0
require (
github.com/felixge/httpsnoop v1.0.4
github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.20.4
github.com/prometheus/common v0.55.0
github.com/rs/xid v1.6.0
github.com/rs/zerolog v1.33.0
github.com/urfave/negroni v1.0.0
golang.org/x/sys v0.22.0
)

require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
60 changes: 59 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,73 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
50 changes: 50 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"io"
"net/http"
"os"
"time"

"github.com/felixge/httpsnoop"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/negroni"
)

func initLogger(opts Config) {
logLevel, err := zerolog.ParseLevel(opts.LogLevel)
if err != nil {
logLevel = zerolog.InfoLevel
log.Warn().Msgf("Invalid log level '%s', fallback to '%s'", opts.LogLevel, logLevel.String())
}

if logLevel == zerolog.NoLevel {
logLevel = zerolog.InfoLevel
}
opts.LogLevel = logLevel.String()

var logWriter io.Writer = os.Stderr
if opts.LogPretty {
logWriter = &zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly}
}

zerolog.DurationFieldUnit = time.Millisecond
logger := zerolog.New(logWriter).Level(logLevel).With().Timestamp().Logger()

log.Logger = logger
zerolog.DefaultContextLogger = &logger
}

func NewLoggingMiddleware() negroni.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
metrics := httpsnoop.CaptureMetrics(next, writer, request)
log.Info().
Str("path", request.URL.Path).
Str("referer", request.Referer()).
Dur("duration", metrics.Duration).
Int("status_code", metrics.Code).
Int64("response_size", metrics.Written).
Msg("Handled request")
}
}
76 changes: 75 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,84 @@
package main

import (
"context"
"encoding/json"
"net/http"
"os/signal"
"time"

prometheusApi "github.com/prometheus/client_golang/api"
prometheusV1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/rs/zerolog/log"
"golang.org/x/sys/unix"
)

var query = `min_over_time(probe_success{instance="https://api.dev.radix.equinor.com/health/"}[5m])`

func main() {
log.Printf("Hello world!")
ctx, cancel := signal.NotifyContext(context.Background(), unix.SIGTERM, unix.SIGINT)
defer cancel()

config := MustParseConfig()
promController := NewPrometheusController(config)
router := NewRouter(promController)

log.Ctx(ctx).Info().Msgf("Starting server on http://localhost:%d/query", config.Port)
err := Serve(ctx, config.Port, router)
log.Err(err).Msg("Terminated")
}

func NewPrometheusController(config Config) RouteMapper {
apiClient, err := prometheusApi.NewClient(prometheusApi.Config{Address: config.Prometheus.String()})
if err != nil {
log.Fatal().Err(err).Msg("failed to create the Prometheus API client")
}
api := prometheusV1.NewAPI(apiClient)

return func(mux *http.ServeMux) {
mux.HandleFunc("/query", func(w http.ResponseWriter, r *http.Request) {
logger := log.Ctx(r.Context())
//
end := time.Now()
start := end.Add(24 * time.Hour * -30)
promRange := prometheusV1.Range{Start: start, End: end, Step: 5 * time.Minute}
content, warnings, err := api.QueryRange(r.Context(), query, promRange)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Internal Server Error"))
logger.Err(err).Msg("Failed to query prometheus")
return
}
for _, w := range warnings {
logger.Warn().Msg(w)
}

matrix, ok := content.(model.Matrix)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Internal Server Error"))
logger.Error().Str("promtype", content.Type().String()).Type("type", content).Msg("Failed to parse response type")
return
}
if len(matrix) != 1 {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Internal Server Error"))
logger.Error().Int("results", len(matrix)).Msg("the response did not have 1 item")
return
}

body, err := json.Marshal(matrix[0].Values)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Internal Server Error"))
logger.Err(err).Msg("Failed to marshall json")
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
})
}
}
18 changes: 18 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"net/http"

"github.com/rs/xid"
"github.com/rs/zerolog/log"
"github.com/urfave/negroni"
)

func NewZerologRequestIdMiddleware() negroni.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
logger := log.Ctx(r.Context()).With().Str("request_id", xid.New().String()).Logger()
r = r.WithContext(logger.WithContext(r.Context()))

next(w, r)
}
}
49 changes: 49 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"context"
"errors"
"fmt"
"net/http"
"time"

"github.com/rs/zerolog/log"
"github.com/urfave/negroni"
)

type RouteMapper func(mux *http.ServeMux)

func NewRouter(handlers ...RouteMapper) *negroni.Negroni {
mux := http.NewServeMux()
for _, handler := range handlers {
handler(mux)
}

return negroni.New(
negroni.NewRecovery(),
NewZerologRequestIdMiddleware(),
NewLoggingMiddleware(),
negroni.Wrap(mux),
)
}

func Serve(ctx context.Context, port int, router http.Handler) error {

s := &http.Server{
Handler: router,
Addr: fmt.Sprintf(":%d", port),
}
go func() {

if err := s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Ctx(ctx).Fatal().Msg(err.Error())
}
}()

<-ctx.Done()

shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
defer cancel()

return s.Shutdown(shutdownCtx)
}

0 comments on commit 25e4ab3

Please sign in to comment.