From 498b45eab33b0cc7229fc70fee1e0ad3781eab50 Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Thu, 1 Jan 2026 23:46:54 +0000 Subject: [PATCH] feat: expose prometheus metrics on different port --- Dockerfile | 1 + README.md | 2 ++ internal/server/prometheus.go | 14 ++++++++++++++ internal/server/router.go | 4 ---- main.go | 2 ++ 5 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 internal/server/prometheus.go diff --git a/Dockerfile b/Dockerfile index 56ecbcc..9230eab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,4 +18,5 @@ COPY --from=builder /app/api . COPY favicon.ico . EXPOSE 8888 +EXPOSE 8889 CMD [ "/app/api" ] diff --git a/README.md b/README.md index 227359a..b915ba3 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,5 @@ trafficlunar's api - computer statistics websocket - The application [here](https://github.com/trafficlunar/computer) is used to send data to this WebSocket - After the data is sent, it is turned into chart data and is displayed on my website. +- github + - Gets amount of stars for my repositories diff --git a/internal/server/prometheus.go b/internal/server/prometheus.go new file mode 100644 index 0000000..715d3e0 --- /dev/null +++ b/internal/server/prometheus.go @@ -0,0 +1,14 @@ +package server + +import ( + "net/http" + + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +func StartPrometheusServer() { + go func() { + http.HandleFunc("/", promhttp.Handler().ServeHTTP) + http.ListenAndServe(":8889", nil) + }() +} diff --git a/internal/server/router.go b/internal/server/router.go index fd77af1..082313d 100644 --- a/internal/server/router.go +++ b/internal/server/router.go @@ -11,7 +11,6 @@ import ( "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/cors" "github.com/go-chi/httprate" - "github.com/prometheus/client_golang/prometheus/promhttp" "api/internal/handler" app_middleware "api/internal/middleware" @@ -49,9 +48,6 @@ func NewRouter() { r.Group(func(r chi.Router) { r.Use(app_middleware.PrometheusMiddleware) - // Prometheus - r.Handle("/metrics", promhttp.Handler()) - var commit = "unknown" if os.Getenv("SOURCE_COMMIT") != "" { commit = os.Getenv("SOURCE_COMMIT")[:7] // shorten to 7 characters diff --git a/main.go b/main.go index ed81f93..8f285bf 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,7 @@ func main() { go worker.StartWorkers() // Shutdown-chan~~ + // this joke sucks shutdownChan := make(chan os.Signal, 1) signal.Notify(shutdownChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) @@ -53,5 +54,6 @@ func main() { os.Exit(0) }() + server.StartPrometheusServer() server.NewRouter() }