-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache: capture metrics related to cache records and pruning
Signed-off-by: Jonathan A. Sternberg <[email protected]>
- Loading branch information
1 parent
686c0ad
commit 9b98442
Showing
7 changed files
with
130 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
const ( | ||
instrumentationName = "github.com/moby/buildkit/cache" | ||
metricCacheRecords = "cache.records.count" | ||
metricCachePruneDuration = "cache.prune.duration" | ||
) | ||
|
||
type metrics struct { | ||
CacheRecords metric.Int64ObservableGauge | ||
CachePruneDuration metric.Int64Histogram | ||
meter metric.Meter | ||
regs []metric.Registration | ||
} | ||
|
||
func newMetrics(cm *cacheManager, mp metric.MeterProvider) *metrics { | ||
m := &metrics{} | ||
|
||
var err error | ||
m.meter = mp.Meter(instrumentationName) | ||
|
||
m.CacheRecords, err = m.meter.Int64ObservableGauge(metricCacheRecords, | ||
metric.WithDescription("Number of cache records."), | ||
) | ||
if err != nil { | ||
otel.Handle(err) | ||
} | ||
|
||
m.CachePruneDuration, err = m.meter.Int64Histogram(metricCachePruneDuration, | ||
metric.WithDescription("Measures the duration of cache prune operations."), | ||
metric.WithUnit("ms"), | ||
) | ||
if err != nil { | ||
otel.Handle(err) | ||
} | ||
|
||
reg, err := m.meter.RegisterCallback(cm.collectMetrics, m.CacheRecords) | ||
if err != nil { | ||
otel.Handle(err) | ||
} | ||
m.regs = append(m.regs, reg) | ||
|
||
return m | ||
} | ||
|
||
func (m *metrics) MeasurePrune() (record func(ctx context.Context)) { | ||
start := time.Now() | ||
return func(ctx context.Context) { | ||
dur := int64(time.Since(start) / time.Millisecond) | ||
m.CachePruneDuration.Record(ctx, dur) | ||
} | ||
} | ||
|
||
func (m *metrics) Close() error { | ||
for _, reg := range m.regs { | ||
_ = reg.Unregister() | ||
} | ||
return nil | ||
} | ||
|
||
type cacheStats struct { | ||
NumRecords int64 | ||
} | ||
|
||
func (cm *cacheManager) readStats() (stats cacheStats) { | ||
cm.mu.Lock() | ||
defer cm.mu.Unlock() | ||
|
||
stats.NumRecords = int64(len(cm.records)) | ||
return stats | ||
} | ||
|
||
func (cm *cacheManager) collectMetrics(ctx context.Context, o metric.Observer) error { | ||
stats := cm.readStats() | ||
o.ObserveInt64(cm.metrics.CacheRecords, stats.NumRecords) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters