Skip to content
Snippets Groups Projects
Commit aad095b3 authored by Yordan Kinkov's avatar Yordan Kinkov
Browse files

add prometheus metrics endpoint

parent 4b44a6ff
No related branches found
No related tags found
1 merge request!16Expose prometheus metrics
Pipeline #52442 passed
Showing
with 106 additions and 0 deletions
...@@ -3,12 +3,14 @@ package main ...@@ -3,12 +3,14 @@ package main
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"log" "log"
"net" "net"
"net/http" "net/http"
"time" "time"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap" "go.uber.org/zap"
...@@ -162,6 +164,9 @@ func main() { ...@@ -162,6 +164,9 @@ func main() {
goahealthsrv.Mount(mux, healthServer) goahealthsrv.Mount(mux, healthServer)
goaopenapisrv.Mount(mux, openapiServer) goaopenapisrv.Mount(mux, openapiServer)
// expose metrics
go exposeMetrics(cfg.Metrics.Addr, logger)
var handler http.Handler = mux var handler http.Handler = mux
srv := &http.Server{ srv := &http.Server{
Addr: cfg.HTTP.Host + ":" + cfg.HTTP.Port, Addr: cfg.HTTP.Host + ":" + cfg.HTTP.Port,
...@@ -228,3 +233,12 @@ func httpClient() *http.Client { ...@@ -228,3 +233,12 @@ func httpClient() *http.Client {
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
} }
} }
func exposeMetrics(addr string, logger *zap.Logger) {
promMux := http.NewServeMux()
promMux.Handle("/metrics", promhttp.Handler())
logger.Info(fmt.Sprintf("exposing prometheus metrics at %s/metrics", addr))
if err := http.ListenAndServe(addr, promMux); err != nil {
logger.Error("error exposing prometheus metrics", zap.Error(err))
}
}
...@@ -7,6 +7,7 @@ require ( ...@@ -7,6 +7,7 @@ require (
github.com/cenkalti/backoff/v4 v4.1.3 github.com/cenkalti/backoff/v4 v4.1.3
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/kelseyhightower/envconfig v1.4.0 github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.12.2
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.8.4 go.mongodb.org/mongo-driver v1.8.4
go.uber.org/zap v1.21.0 go.uber.org/zap v1.21.0
...@@ -15,10 +16,13 @@ require ( ...@@ -15,10 +16,13 @@ require (
) )
require ( require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 // indirect github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 // indirect
github.com/dimfeld/httptreemux/v5 v5.4.0 // indirect github.com/dimfeld/httptreemux/v5 v5.4.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect github.com/golang/snappy v0.0.1 // indirect
github.com/google/go-cmp v0.5.6 // indirect github.com/google/go-cmp v0.5.6 // indirect
github.com/gopherjs/gopherjs v0.0.0-20220221023154-0b2280d3ff96 // indirect github.com/gopherjs/gopherjs v0.0.0-20220221023154-0b2280d3ff96 // indirect
...@@ -26,8 +30,12 @@ require ( ...@@ -26,8 +30,12 @@ require (
github.com/jtolds/gls v4.20.0+incompatible // indirect github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/compress v1.13.6 // indirect github.com/klauspost/compress v1.13.6 // indirect
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/sergi/go-diff v1.2.0 // indirect github.com/sergi/go-diff v1.2.0 // indirect
github.com/smartystreets/assertions v1.2.1 // indirect github.com/smartystreets/assertions v1.2.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect
...@@ -43,6 +51,7 @@ require ( ...@@ -43,6 +51,7 @@ require (
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.10 // indirect golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
) )
This diff is collapsed.
...@@ -9,6 +9,7 @@ type Config struct { ...@@ -9,6 +9,7 @@ type Config struct {
Executor executorConfig Executor executorConfig
ListExecutor listExecutorConfig ListExecutor listExecutorConfig
Cache cacheConfig Cache cacheConfig
Metrics metricsConfig
LogLevel string `envconfig:"LOG_LEVEL" default:"INFO"` LogLevel string `envconfig:"LOG_LEVEL" default:"INFO"`
} }
...@@ -45,3 +46,7 @@ type listExecutorConfig struct { ...@@ -45,3 +46,7 @@ type listExecutorConfig struct {
type cacheConfig struct { type cacheConfig struct {
Addr string `envconfig:"CACHE_ADDR" required:"true"` Addr string `envconfig:"CACHE_ADDR" required:"true"`
} }
type metricsConfig struct {
Addr string `envconfig:"METRICS_ADDR" default:":2112"`
}
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment