Skip to content

Commit

Permalink
feat: cache hit metrics (#3214)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathnogueira authored Oct 4, 2023
1 parent 784ef19 commit e28f2d5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (app *App) Start(opts ...appOption) error {
variableSetRepo := variableset.NewRepository(db)
linterRepo := analyzer.NewRepository(db)
testRepo := test.NewRepository(db)
runRepo := test.NewRunRepository(db, test.NewCache(instanceID))
runRepo := test.NewRunRepository(db, test.NewCache(instanceID, test.WithMetricMeter(meter)))
testRunnerRepo := testrunner.NewRepository(db)
tracesRepo := traces.NewTraceRepository(db)

Expand Down
2 changes: 1 addition & 1 deletion server/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newMeterProvider(ctx context.Context, cfg exporterConfig) (metric.MeterProv
return nil, fmt.Errorf("could not get collector exporter: %w", err)
}

interval := 60 * time.Second
interval := 10 * time.Second
if exporterConfig.MetricsReaderInterval != 0 {
interval = exporterConfig.MetricsReaderInterval
}
Expand Down
60 changes: 53 additions & 7 deletions server/test/run_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"github.com/kubeshop/tracetest/server/pkg/sqlutil"
"github.com/kubeshop/tracetest/server/variableset"
"github.com/patrickmn/go-cache"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
)

Expand All @@ -31,17 +34,46 @@ type RunRepository interface {
}

type Cache struct {
cache *cache.Cache
instanceID string
cache *cache.Cache
instanceID string
readRequestCounter metric.Int64Counter
cacheLatencyHistogram metric.Int64Histogram
}

func NewCache(instanceID string) *Cache {
return &Cache{
cache: cache.New(5*time.Minute, 10*time.Minute),
instanceID: instanceID,
type CacheConfig struct {
meter metric.Meter
}

type CacheOption func(*CacheConfig)

func WithMetricMeter(meter metric.Meter) CacheOption {
return func(c *CacheConfig) {
c.meter = meter
}
}

func NewCache(instanceID string, opts ...CacheOption) *Cache {
cacheConfig := &CacheConfig{
meter: noop.NewMeterProvider().Meter("noop"),
}

for _, opt := range opts {
opt(cacheConfig)
}

readRequestCounter, _ := cacheConfig.meter.Int64Counter("tracetest.cache.request.count")
cacheLatencyHistogram, _ := cacheConfig.meter.Int64Histogram("tracetest.cache.latency")

cache := &Cache{
cache: cache.New(5*time.Minute, 10*time.Minute),
instanceID: instanceID,
readRequestCounter: readRequestCounter,
cacheLatencyHistogram: cacheLatencyHistogram,
}

return cache
}

func (c *Cache) thisInstanceCacheKey(run Run) string {
return fmt.Sprintf("%s-%s-%d", c.instanceID, run.TestID, run.ID)
}
Expand All @@ -62,7 +94,21 @@ func (c *Cache) Set(_ context.Context, run Run) error {
}

func (c *Cache) Get(ctx context.Context, testID id.ID, runID int) (Run, bool) {
cached, found := c.cache.Get(c.lastInstanceCacheKey(ctx, Run{ID: runID, TestID: testID}))
begin := time.Now()
key := c.lastInstanceCacheKey(ctx, Run{ID: runID, TestID: testID})
cached, found := c.cache.Get(key)

duration := time.Since(begin)

attributeSet := attribute.NewSet(
attribute.String("object_type", "test_run"),
attribute.String("key", key),
attribute.Bool("hit", found),
)

c.readRequestCounter.Add(ctx, 1, metric.WithAttributeSet(attributeSet))
c.cacheLatencyHistogram.Record(ctx, duration.Milliseconds(), metric.WithAttributeSet(attributeSet))

if !found {
return Run{}, false
}
Expand Down

0 comments on commit e28f2d5

Please sign in to comment.