Skip to content

Commit

Permalink
feat: support any query
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard87 committed Oct 14, 2024
1 parent 7900807 commit 5999d11
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RUN go build -ldflags "-s -w" -a -installsuffix cgo -o /radix-prometheus-proxy
FROM gcr.io/distroless/static

COPY --from=builder /radix-prometheus-proxy /radix-prometheus-proxy
COPY queries.yaml /queries.yaml

EXPOSE 8000
USER 1000
Expand Down
8 changes: 5 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

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

Prometheus url.URL `envconfig:"PROMETHEUS" required:"true"`
}
Expand All @@ -29,6 +30,7 @@ func MustParseConfig() Config {
log.Info().Str("Log level", c.LogLevel).Send()
log.Info().Bool("Log pretty", c.LogPretty).Send()
log.Info().Stringer("Prometheus", &c.Prometheus).Send()
log.Info().Str("Queries file", c.QueriesFile).Send()

return c
}
36 changes: 29 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"os"
"os/signal"
"time"

Expand All @@ -12,32 +13,53 @@ import (
"github.com/prometheus/common/model"
"github.com/rs/zerolog/log"
"golang.org/x/sys/unix"
"gopkg.in/yaml.v2"
)

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

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), unix.SIGTERM, unix.SIGINT)
defer cancel()

config := MustParseConfig()
promController := NewPrometheusController(config)
queries := mustParseQueryFile(config.QueriesFile)
promController := NewPrometheusController(config.Prometheus.String(), queries)

router := NewRouter(promController)

log.Ctx(ctx).Info().Msgf("Starting server on http://localhost:%d/query", config.Port)
log.Ctx(ctx).Info().Msgf("Starting server on http://localhost:%d/query/{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()})
func mustParseQueryFile(queriesFile string) map[string]string {
queries := map[string]string{}
content, err := os.ReadFile(queriesFile)
if err != nil {
log.Fatal().Err(err).Msg("unable to read queries file")
}

if err = yaml.Unmarshal(content, &queries); err != nil {
log.Fatal().Err(err).Msg("unable to parse queries file")
}
return queries
}

func NewPrometheusController(prometheusUrl string, queries map[string]string) RouteMapper {
apiClient, err := prometheusApi.NewClient(prometheusApi.Config{Address: prometheusUrl})
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) {
mux.HandleFunc("/query/{query}", func(w http.ResponseWriter, r *http.Request) {
query, ok := queries[r.PathValue("query")]
if !ok {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("Query not found"))
return
}

logger := log.Ctx(r.Context())
//
end := time.Now()
Expand Down
4 changes: 4 additions & 0 deletions queries.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dev: min_over_time(probe_success{instance="https://api.dev.radix.equinor.com/health/"}[5m])
playground: min_over_time(probe_success{instance="https://api.playground.radix.equinor.com/health/"}[5m])
platform: min_over_time(probe_success{instance="https://api.radix.equinor.com/health/"}[5m])
c2: min_over_time(probe_success{instance="https://api.c2.radix.equinor.com/health/"}[5m])

0 comments on commit 5999d11

Please sign in to comment.