Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache metrics #169

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions api/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package cache

import (

Check failure on line 3 in api/cache/cache.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
"github.com/eko/gocache/lib/v4/metrics"

Check failure on line 4 in api/cache/cache.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"github.com/prometheus/client_golang/prometheus"
"time"

"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"

Check failure on line 9 in api/cache/cache.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"github.com/eko/gocache/lib/v4/store"
gocacheStore "github.com/eko/gocache/store/go_cache/v4"
redis_store "github.com/eko/gocache/store/redis/v4"
gocache "github.com/patrickmn/go-cache"

Check failure on line 13 in api/cache/cache.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"github.com/redis/go-redis/v9"

"github.com/spacemeshos/go-spacemesh/log"
Expand All @@ -18,21 +20,37 @@
RedisAddress = ""
Expiration time.Duration = 0
ShortExpiration = 5 * time.Minute
promMetrics = metrics.NewPrometheus("explorer_cache")
LastUpdated = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "explorer_cache_last_updated",
Help: "The last time the cache was updated, labeled by endpoint and id",
},
[]string{"endpoint"},
)
)

func New() *marshaler.Marshaler {
var manager *cache.Cache[any]
prometheus.MustRegister(LastUpdated)
var manager *cache.MetricCache[any]
if RedisAddress != "" {
log.Info("using redis cache")
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{
Addr: RedisAddress,
}), store.WithExpiration(Expiration))
manager = cache.New[any](redisStore)
manager = cache.NewMetric[any](
promMetrics,
cache.New[any](redisStore),
)
} else {
log.Info("using memory cahe")
client := gocache.New(Expiration, 6*time.Hour)
s := gocacheStore.NewGoCache(client)
manager = cache.New[any](s)
manager = cache.NewMetric[any](
promMetrics,
cache.New[any](s),
)
}

return marshaler.New(manager)
}
1 change: 1 addition & 0 deletions api/handler/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Account(c echo.Context) error {
log.Warning("failed to cache account stats: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
cache.LastUpdated.WithLabelValues("/account/" + address).SetToCurrentTime()

return c.JSON(http.StatusOK, accountStats)
}
3 changes: 3 additions & 0 deletions api/handler/circulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import (
"context"
"github.com/spacemeshos/explorer-backend/api/cache"

Check failure on line 5 in api/handler/circulation.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"net/http"

"github.com/labstack/echo/v4"

Check failure on line 8 in api/handler/circulation.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"github.com/spacemeshos/explorer-backend/api/storage"

"github.com/spacemeshos/go-spacemesh/log"
Expand All @@ -27,6 +28,7 @@
log.Warning("failed to cache circulation: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
cache.LastUpdated.WithLabelValues("/circulation").SetToCurrentTime()

return c.JSON(http.StatusOK, circulation)
}
Expand All @@ -47,6 +49,7 @@
}

log.Info("circulation refreshed")
cache.LastUpdated.WithLabelValues("/refresh/circulation").SetToCurrentTime()
}()

return c.NoContent(http.StatusOK)
Expand Down
10 changes: 9 additions & 1 deletion api/handler/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import (
"context"
"fmt"
"github.com/spacemeshos/explorer-backend/api/cache"

Check failure on line 6 in api/handler/epoch.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"net/http"
"strconv"

"github.com/labstack/echo/v4"

Check failure on line 10 in api/handler/epoch.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"github.com/spacemeshos/explorer-backend/api/storage"

"github.com/spacemeshos/go-spacemesh/log"
Expand Down Expand Up @@ -34,6 +36,8 @@
return c.NoContent(http.StatusInternalServerError)
}

cache.LastUpdated.WithLabelValues("/epoch/" + c.Param("id")).SetToCurrentTime()

return c.JSON(http.StatusOK, epochStats)
}

Expand All @@ -57,6 +61,7 @@
}

log.Info("epoch %d refreshed", id)
cache.LastUpdated.WithLabelValues("/refresh/epoch/" + c.Param("id")).SetToCurrentTime()
}()

return c.NoContent(http.StatusOK)
Expand Down Expand Up @@ -85,6 +90,8 @@
return c.NoContent(http.StatusInternalServerError)
}

cache.LastUpdated.WithLabelValues(fmt.Sprintf("/epoch/%s/decentral", c.Param("id"))).SetToCurrentTime()

return c.JSON(http.StatusOK, epochStats)
}

Expand All @@ -107,7 +114,8 @@
return
}

log.Info("epoch decentral refreshed")
log.Info("epoch %d decentral refreshed", id)
cache.LastUpdated.WithLabelValues(fmt.Sprintf("/refresh/epoch/%s/decentral", c.Param("id"))).SetToCurrentTime()
}()

return c.NoContent(http.StatusOK)
Expand Down
2 changes: 2 additions & 0 deletions api/handler/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ func Layer(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}

cache.LastUpdated.WithLabelValues("/layer/" + c.Param("id")).SetToCurrentTime()

return c.JSON(http.StatusOK, layerStats)
}
4 changes: 4 additions & 0 deletions api/handler/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import (
"context"
"github.com/spacemeshos/explorer-backend/api/cache"

Check failure on line 5 in api/handler/overview.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"net/http"

"github.com/labstack/echo/v4"

Check failure on line 8 in api/handler/overview.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacemeshos/go-spacemesh) (gci)
"github.com/spacemeshos/explorer-backend/api/storage"

"github.com/spacemeshos/go-spacemesh/log"
Expand All @@ -28,6 +29,8 @@
return c.NoContent(http.StatusInternalServerError)
}

cache.LastUpdated.WithLabelValues("/overview").SetToCurrentTime()

return c.JSON(http.StatusOK, overview)
}

Expand All @@ -47,6 +50,7 @@
}

log.Info("overview refreshed")
cache.LastUpdated.WithLabelValues("/refresh/overview").SetToCurrentTime()
}()

return c.NoContent(http.StatusOK)
Expand Down
9 changes: 9 additions & 0 deletions api/handler/smesher.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func Smeshers(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}

cache.LastUpdated.WithLabelValues(fmt.Sprintf("/smeshers?limit=%d&offset=%d", limit, offset)).SetToCurrentTime()

return c.JSON(http.StatusOK, smeshers)
}

Expand All @@ -58,6 +60,7 @@ func SmeshersRefresh(c echo.Context) error {
}

log.Info("smeshers refreshed")
cache.LastUpdated.WithLabelValues("/refresh/smeshers").SetToCurrentTime()
}()

return c.NoContent(http.StatusOK)
Expand Down Expand Up @@ -90,6 +93,9 @@ func SmeshersByEpoch(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}

cache.LastUpdated.WithLabelValues(
fmt.Sprintf("/smeshers/%s?limit=%d&offset=%d", c.Param("epoch"), limit, offset)).SetToCurrentTime()

return c.JSON(http.StatusOK, smeshers)
}

Expand Down Expand Up @@ -119,6 +125,7 @@ func SmeshersByEpochRefresh(c echo.Context) error {
}

log.Info("smeshers by epoch %d refreshed", epochId)
cache.LastUpdated.WithLabelValues("/refresh/smeshers/" + c.Param("epoch")).SetToCurrentTime()
}()

return c.NoContent(http.StatusOK)
Expand Down Expand Up @@ -150,5 +157,7 @@ func Smesher(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}

cache.LastUpdated.WithLabelValues("/smesher/" + smesherId).SetToCurrentTime()

return c.JSON(http.StatusOK, smesher)
}
Loading