Skip to content

Commit

Permalink
metrics: listen on port 9090 for prometheus scrape
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Jun 1, 2023
1 parent d85a771 commit e9636ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/metal-toolbox/flasher/internal/app"
"github.com/metal-toolbox/flasher/internal/metrics"
"github.com/metal-toolbox/flasher/internal/model"
"github.com/metal-toolbox/flasher/internal/store"
"github.com/metal-toolbox/flasher/internal/worker"
Expand Down Expand Up @@ -49,6 +50,9 @@ func runWorker(ctx context.Context) {
log.Fatal(err)
}

// serve metrics endpoint
metrics.ListenAndServe()

// Setup cancel context with cancel func.
ctx, cancelFunc := context.WithCancel(ctx)

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

import (
"log"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
MetricsEndpoint = "0.0.0.0:9090"
)

// ListenAndServeMetrics exposes prometheus metrics as /metrics
func ListenAndServe() {
go func() {
http.Handle("/metrics", promhttp.Handler())

server := &http.Server{
Addr: MetricsEndpoint,
ReadHeaderTimeout: 2 * time.Second, // nolint:gomnd // time duration value is clear as is.
}

if err := server.ListenAndServe(); err != nil {
log.Println(err)
}
}()
}

0 comments on commit e9636ad

Please sign in to comment.