-
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 7538521
Showing
6 changed files
with
127 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,92 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"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, error) { | ||
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 { | ||
return nil, err | ||
} | ||
|
||
m.CachePruneDuration, err = m.meter.Int64Histogram(metricCachePruneDuration, | ||
metric.WithDescription("Measures the duration of cache prune operations."), | ||
metric.WithUnit("ms"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reg, err := m.meter.RegisterCallback(cm.collectMetrics, m.CacheRecords) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m.regs = append(m.regs, reg) | ||
|
||
return m, nil | ||
} | ||
|
||
func (m *metrics) MeasurePrune() (record func(ctx context.Context)) { | ||
if m == nil { | ||
return func(_ 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 { | ||
if m == nil { | ||
return nil | ||
} | ||
|
||
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