Skip to content

Commit

Permalink
api: service metrics if enabled in config
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Jun 13, 2024
1 parent 44b3413 commit 9db2859
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions backend/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
dataaccess "github.com/gobitfly/beaconchain/pkg/api/data_access"

"github.com/gobitfly/beaconchain/pkg/commons/log"
"github.com/gobitfly/beaconchain/pkg/commons/metrics"
"github.com/gobitfly/beaconchain/pkg/commons/types"
"github.com/gobitfly/beaconchain/pkg/commons/utils"
"github.com/gobitfly/beaconchain/pkg/commons/version"
Expand Down Expand Up @@ -51,6 +52,16 @@ func main() {
router := api.NewApiRouter(dataAccessor, cfg)
router.Use(api.GetCorsMiddleware(cfg.CorsAllowedHosts))

if cfg.Metrics.Enabled {
router.Use(metrics.HttpMiddleware)
go func(addr string) {
log.Infof("serving metrics on %v", addr)
if err := metrics.Serve(addr); err != nil {
log.Fatal(err, "error serving metrics", 0)
}
}(cfg.Metrics.Address)
}

srv := &http.Server{
Handler: router,
Addr: net.JoinHostPort(cfg.Frontend.Server.Host, cfg.Frontend.Server.Port),
Expand Down
3 changes: 3 additions & 0 deletions backend/pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
dataaccess "github.com/gobitfly/beaconchain/pkg/api/data_access"
handlers "github.com/gobitfly/beaconchain/pkg/api/handlers"
"github.com/gobitfly/beaconchain/pkg/commons/log"
"github.com/gobitfly/beaconchain/pkg/commons/metrics"
"github.com/gobitfly/beaconchain/pkg/commons/types"
"github.com/gobitfly/beaconchain/pkg/commons/utils"
gorillaHandlers "github.com/gorilla/handlers"
Expand Down Expand Up @@ -41,6 +42,8 @@ func NewApiRouter(dataAccessor dataaccess.DataAccessor, cfg *types.Config) *mux.

addRoutes(handlerService, publicRouter, internalRouter, cfg)

router.Use(metrics.HttpMiddleware)

return router
}

Expand Down

0 comments on commit 9db2859

Please sign in to comment.