Skip to content

Commit

Permalink
Add per-request Rest metrics to prometheus (#1651)
Browse files Browse the repository at this point in the history
* - Add per-request Rest metrics to prometheus

* - Made echo dep optional
  • Loading branch information
alexsporn authored and muXxer committed Dec 27, 2022
1 parent 730a9be commit b4ad5e5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/iotaledger/iota.go/v2 v2.0.2-0.20220225080023-871c779a63cb
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-badger v0.3.0
github.com/labstack/echo-contrib v0.13.0
github.com/labstack/echo/v4 v4.9.1
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.24.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down Expand Up @@ -607,6 +608,8 @@ github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo-contrib v0.13.0 h1:bzSG0SpuZZd7BmJLvsWtPfU23W0Enh3K0tok3aENVKA=
github.com/labstack/echo-contrib v0.13.0/go.mod h1:IF9+MJu22ADOZEHD+bAV67XMIO3vNXUy7Naz/ABPHEs=
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
github.com/labstack/echo/v4 v4.5.0/go.mod h1:czIriw4a0C1dFun+ObrXp7ok03xON0N1awStJ6ArI7Y=
github.com/labstack/echo/v4 v4.9.1 h1:GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y=
Expand Down
5 changes: 3 additions & 2 deletions plugins/prometheus/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package prometheus
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"time"

coreDatabase "github.com/iotaledger/hornet/core/database"
Expand Down Expand Up @@ -86,6 +86,7 @@ type dependencies struct {
SnapshotManager *snapshot.SnapshotManager
Coordinator *coordinator.Coordinator `optional:"true"`
MQTTBroker *mqtt.Broker `optional:"true"`
Echo *echo.Echo `optional:"true"`
}

func configure() {
Expand Down Expand Up @@ -154,7 +155,7 @@ func writeFileServiceDiscoveryFile() {
}

// this truncates an existing file
if err := ioutil.WriteFile(path, j, 0666); err != nil {
if err := os.WriteFile(path, j, 0666); err != nil {
Plugin.LogPanic("unable to write file service discovery file:", err)
}

Expand Down
9 changes: 9 additions & 0 deletions plugins/prometheus/rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prometheus
import (
"time"

echoprometheus "github.com/labstack/echo-contrib/prometheus"
"github.com/prometheus/client_golang/prometheus"

"github.com/iotaledger/hive.go/events"
Expand Down Expand Up @@ -87,6 +88,14 @@ func configureRestAPI() {
}))

addCollect(collectRestAPI)

if deps.Echo != nil {
p := echoprometheus.NewPrometheus("iota_restapi", nil)
for _, m := range p.MetricsList {
registry.MustRegister(m.MetricCollector)
}
deps.Echo.Use(p.HandlerFunc)
}
}

func collectRestAPI() {
Expand Down

0 comments on commit b4ad5e5

Please sign in to comment.