Skip to content

Commit

Permalink
Merge pull request #10 from tparker00/metrics
Browse files Browse the repository at this point in the history
Add Metrics
  • Loading branch information
tparker00 authored Sep 2, 2024
2 parents 4de61a8 + 9a92980 commit 86882d5
Show file tree
Hide file tree
Showing 107 changed files with 9,745 additions and 312 deletions.
35 changes: 21 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,46 @@ module bws-cache

go 1.22.3

require (
github.com/bitwarden/sdk-go v0.1.1
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/httplog/v2 v2.1.1
github.com/go-chi/telemetry v0.3.4
github.com/google/uuid v1.6.0
github.com/jellydator/ttlcache/v3 v3.2.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bitwarden/sdk-go v0.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-chi/chi/v5 v5.1.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jellydator/ttlcache/v3 v3.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.52.3 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
github.com/uber-go/tally/v4 v4.1.16 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
197 changes: 183 additions & 14 deletions go.sum

Large diffs are not rendered by default.

61 changes: 57 additions & 4 deletions internal/pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,79 @@ import (
"fmt"
"log/slog"
"net/http"
"runtime/debug"
"strings"
"time"

"bws-cache/internal/pkg/client"
"bws-cache/internal/pkg/config"
"bws-cache/internal/pkg/metrics"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog/v2"
"github.com/go-chi/telemetry"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}

return ""
}()

type API struct {
SecretTTL time.Duration
WebTTL time.Duration
OrgID string
Client *client.Bitwarden
Metrics *metrics.BwsMetrics
}

func New(config *config.Config) http.Handler {
api := API{
SecretTTL: config.SecretTTL,
OrgID: config.OrgID,
Metrics: metrics.New(),
}

// Logger
logger := httplog.NewLogger("bws-cache", httplog.Options{
JSON: true,
LogLevel: slog.LevelInfo,
Concise: true,
RequestHeaders: true,
MessageFieldName: "message",
TimeFieldFormat: time.RFC3339,
Tags: map[string]string{
"version": commit,
},
QuietDownRoutes: []string{
"/",
"/metrics",
"/ping",
},
QuietDownPeriod: 10 * time.Minute,
})

router := chi.NewRouter()
router.Use(middleware.RequestID)
router.Use(middleware.RealIP)
router.Use(middleware.Logger)
router.Use(httplog.RequestLogger(logger))
router.Use(middleware.Recoverer)
router.Use(middleware.Timeout(config.WebTTL))

// telemetry.Collector middleware mounts /metrics endpoint
// with prometheus metrics collector.
router.Use(telemetry.Collector(telemetry.Config{
AllowAny: true,
}, []string{"/"})) // path prefix filters records generic http request metrics
router.Use(middleware.Heartbeat("/ping"))
// Enable profiler
router.Mount("/debug", middleware.Profiler())

Expand All @@ -52,12 +93,14 @@ func New(config *config.Config) http.Handler {
r.Get("/{secret_key}", api.getSecretByKey)
})
router.Get("/reset", api.resetConnection)
router.Handle("/metrics", promhttp.Handler())

return router
}

func (api *API) getSecretByID(w http.ResponseWriter, r *http.Request) {
tag := make(map[string]string)
tag["endpoint"] = "id"
api.Metrics.Counter("get", tag)
ctx := r.Context()
slog.DebugContext(ctx, "Getting secret by ID")
token, err := getAuthToken(r)
Expand All @@ -70,6 +113,8 @@ func (api *API) getSecretByID(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "secret_id")

slog.DebugContext(ctx, fmt.Sprintf("Getting secret by ID: %s", id))
span := api.Metrics.RecordSpan("get", tag)
defer span.Stop()
res, err := api.Client.GetByID(ctx, id, token)
if err != nil {
slog.ErrorContext(ctx, fmt.Sprintf("%+v", err))
Expand All @@ -81,6 +126,9 @@ func (api *API) getSecretByID(w http.ResponseWriter, r *http.Request) {
}

func (api *API) getSecretByKey(w http.ResponseWriter, r *http.Request) {
tag := make(map[string]string)
tag["endpoint"] = "key"
api.Metrics.Counter("get", tag)
ctx := r.Context()
slog.DebugContext(ctx, "Getting secret by key")
token, err := getAuthToken(r)
Expand All @@ -92,6 +140,8 @@ func (api *API) getSecretByKey(w http.ResponseWriter, r *http.Request) {
key := chi.URLParam(r, "secret_key")

slog.DebugContext(ctx, fmt.Sprintf("Searching for key: %s", key))
span := api.Metrics.RecordSpan("get", tag)
defer span.Stop()
res, err := api.Client.GetByKey(ctx, key, api.OrgID, token)
if err != nil {
slog.ErrorContext(ctx, fmt.Sprintf("%+v", err))
Expand All @@ -107,6 +157,9 @@ func (api *API) resetConnection(w http.ResponseWriter, r *http.Request) {
slog.InfoContext(ctx, "Resetting cache")

api.Client.Cache.Reset()
tag := make(map[string]string)
tag["endpoint"] = "cache"
api.Metrics.Counter("get", tag)
slog.InfoContext(ctx, "Cache reset")
}

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

import (
"github.com/go-chi/telemetry"
)

type BwsMetrics struct {
*telemetry.Scope
}

func (b *BwsMetrics) Counter(metric string, tags map[string]string) {
b.RecordHit(metric, tags)
}

func (b *BwsMetrics) Gauge(metric string, tags map[string]string, value float64) {
b.RecordGauge(metric, tags, value)
}

func New() *BwsMetrics {
return &BwsMetrics{telemetry.NewScope("bws-cache")}
}
2 changes: 2 additions & 0 deletions vendor/github.com/cespare/xxhash/v2/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions vendor/github.com/cespare/xxhash/v2/xxhash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/cespare/xxhash/v2/xxhash_asm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/cespare/xxhash/v2/xxhash_other.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/cespare/xxhash/v2/xxhash_safe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/cespare/xxhash/v2/xxhash_unsafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/go-chi/httplog/v2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 86882d5

Please sign in to comment.