Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/unkeyed/unkey
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Sep 15, 2023
2 parents 24f7d62 + 9074f5e commit 8904c0b
Show file tree
Hide file tree
Showing 13 changed files with 943 additions and 347 deletions.
27 changes: 22 additions & 5 deletions apps/agent/cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/unkeyed/unkey/apps/agent/pkg/env"
"github.com/unkeyed/unkey/apps/agent/pkg/events"
"github.com/unkeyed/unkey/apps/agent/pkg/logging"
metricsPkg "github.com/unkeyed/unkey/apps/agent/pkg/metrics"
"github.com/unkeyed/unkey/apps/agent/pkg/ratelimit"
"github.com/unkeyed/unkey/apps/agent/pkg/services/workspaces"

Expand Down Expand Up @@ -74,6 +75,20 @@ var AgentCmd = &cobra.Command{
logger = logger.With(zap.String("allocId", allocId))
}

metrics := metricsPkg.NewNoop()
if runtimeConfig.enableAxiom {
realMetrics, err := metricsPkg.New(metricsPkg.Config{
AxiomOrgId: e.String("AXIOM_ORG_ID"),
AxiomToken: e.String("AXIOM_TOKEN"),
Logger: logger.With(zap.String("pkg", "metrics")),
Region: region,
})
if err != nil {
logger.Fatal("unable to start metrics", zap.Error(err))
}
metrics = realMetrics
}

// Setup Axiom

tracer := tracing.NewNoop()
Expand Down Expand Up @@ -108,7 +123,7 @@ var AgentCmd = &cobra.Command{
ReplicaAsia: e.String("DATABASE_DSN_ASIA", ""),
FlyRegion: region,
},
database.WithLogging(logger),
database.WithMetrics(metrics),
database.WithTracing(tracer),
)
if err != nil {
Expand Down Expand Up @@ -177,10 +192,11 @@ var AgentCmd = &cobra.Command{
}
return key, found
},
Logger: logger.With(zap.String("cacheType", "key")),
Logger: logger.With(zap.String("cacheType", "key")),
Metrics: metrics,
})
keyCache = cacheMiddleware.WithTracing[entities.Key](keyCache, tracer)
keyCache = cacheMiddleware.WithLogging[entities.Key](keyCache, logger.With(zap.String("cacheType", "key")))
keyCache = cacheMiddleware.WithMetrics[entities.Key](keyCache, metrics, "key")

apiCache := cache.New[entities.Api](cache.Config[entities.Api]{
Fresh: time.Minute * 5,
Expand All @@ -194,10 +210,11 @@ var AgentCmd = &cobra.Command{
}
return key, found
},
Logger: logger.With(zap.String("cacheType", "api")),
Logger: logger.With(zap.String("cacheType", "api")),
Metrics: metrics,
})
apiCache = cacheMiddleware.WithTracing[entities.Api](apiCache, tracer)
apiCache = cacheMiddleware.WithLogging[entities.Api](apiCache, logger.With(zap.String("cacheType", "api")))
apiCache = cacheMiddleware.WithMetrics[entities.Api](apiCache, metrics, "api")

eventBus.OnKeyEvent(func(ctx context.Context, e events.KeyEvent) error {

Expand Down
13 changes: 6 additions & 7 deletions apps/agent/pkg/analytics/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type KeyVerificationV1Event struct {
}

type KeyVerificationEvent struct {
ApiId string `json:"apiId"`
WorkspaceId string `json:"workspaceId"`
KeyId string `json:"keyId"`
Ratelimited bool `json:"ratelimited"`
UsageExceeded bool `json:"usageExceeded"`
Time int64 `json:"time"`
ApiId string `json:"apiId"`
WorkspaceId string `json:"workspaceId"`
KeyId string `json:"keyId"`
Ratelimited bool `json:"ratelimited"`
UsageExceeded bool `json:"usageExceeded"`
Time int64 `json:"time"`

EdgeRegion string `json:"edgeRegion"`
Region string `json:"region"`
Expand All @@ -30,7 +30,6 @@ type KeyVerificationEvent struct {

// custom field the user may provide, like a URL or some id
RequestedResource string `json:"requestedResource"`

}

type ValueAtTime struct {
Expand Down
22 changes: 14 additions & 8 deletions apps/agent/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"github.com/unkeyed/unkey/apps/agent/pkg/metrics"
"go.uber.org/zap"
)

Expand All @@ -33,6 +34,7 @@ type cache[T any] struct {
logger *zap.Logger
maxSize int
lru *list.List
metrics metrics.Metrics
}

type Config[T any] struct {
Expand All @@ -51,6 +53,8 @@ type Config[T any] struct {

// Start evicting the least recently used entry when the cache grows to MaxSize
MaxSize int

Metrics metrics.Metrics
}

func New[T any](config Config[T]) Cache[T] {
Expand All @@ -64,6 +68,7 @@ func New[T any](config Config[T]) Cache[T] {
logger: config.Logger.With(zap.String("pkg", "cache")),
maxSize: config.MaxSize,
lru: list.New(),
metrics: config.Metrics,
}

go c.runEviction()
Expand All @@ -77,14 +82,15 @@ func (c *cache[T]) runReporting() {
c.RLock()
size := len(c.data)
utilization := float64(size) / math.Max(1, float64(c.maxSize))
c.logger.Info(
"report.cache.health",
zap.Int("cacheSize", size),
zap.Int("cacheMaxSize", c.maxSize),
zap.Int("lruSize", c.lru.Len()),
zap.Int("refreshQueueSize", len(c.refreshC)),
zap.Float64("utilization", utilization),
)

c.metrics.ReportCacheHealth(metrics.CacheHealthReport{
CacheSize: size,
CacheMaxSize: c.maxSize,
LruSize: c.lru.Len(),
RefreshQueueSize: len(c.refreshC),
Utilization: utilization,
})

if size != c.lru.Len() {
c.logger.Error(
"cache skew detected",
Expand Down
37 changes: 0 additions & 37 deletions apps/agent/pkg/cache/middleware/logging.go

This file was deleted.

40 changes: 40 additions & 0 deletions apps/agent/pkg/cache/middleware/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package middleware

import (
"context"
"time"

"github.com/unkeyed/unkey/apps/agent/pkg/cache"
"github.com/unkeyed/unkey/apps/agent/pkg/metrics"
)

type metricsMiddleware[T any] struct {
next cache.Cache[T]
metrics metrics.Metrics
resource string
}

func WithMetrics[T any](c cache.Cache[T], m metrics.Metrics, resource string) cache.Cache[T] {
return &metricsMiddleware[T]{next: c, metrics: m, resource: resource}
}

func (mw *metricsMiddleware[T]) Get(ctx context.Context, key string) (T, bool) {
start := time.Now()
value, hit := mw.next.Get(ctx, key)
mw.metrics.ReportCacheHit(metrics.CacheHitReport{
Key: key,
Hit: hit,
Resource: mw.resource,
Latency: time.Since(start).Milliseconds(),
})
return value, hit
}
func (mw *metricsMiddleware[T]) Set(ctx context.Context, key string, value T) {
mw.next.Set(ctx, key, value)

}
func (mw *metricsMiddleware[T]) Remove(ctx context.Context, key string) {

mw.next.Remove(ctx, key)

}
154 changes: 0 additions & 154 deletions apps/agent/pkg/database/middleware_logging.go

This file was deleted.

Loading

1 comment on commit 8904c0b

@vercel
Copy link

@vercel vercel bot commented on 8904c0b Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

unkey – ./

unkey-unkey.vercel.app
unkey-git-main-unkey.vercel.app
www.unkey.dev
unkey.dev
unkey.vercel.app

Please sign in to comment.