Skip to content

Commit

Permalink
add: improve metrics test
Browse files Browse the repository at this point in the history
  • Loading branch information
coufalja committed Jan 2, 2024
1 parent b70056e commit 5f2f109
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 33 deletions.
20 changes: 0 additions & 20 deletions storage/table/fsm/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ package fsm

import (
"fmt"
"os"
"path"
"testing"

"github.com/cockroachdb/pebble/vfs"
"github.com/jamf/regatta/regattapb"
"github.com/jamf/regatta/util"
sm "github.com/lni/dragonboat/v4/statemachine"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
pb "google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -101,23 +98,6 @@ func TestSM_Open(t *testing.T) {
}
}

func TestFSM_Metrics(t *testing.T) {
p := &FSM{
fs: vfs.NewMem(),
clusterID: 1,
nodeID: 1,
dirname: "/tmp",
log: zap.NewNop().Sugar(),
metrics: newMetrics(testTable, 1),
}
_, _ = p.Open(nil)
inFile, err := os.Open(path.Join("testdata", "metrics"))
require.NoError(t, err)
defer inFile.Close()
require.NoError(t, err)
require.NoError(t, testutil.CollectAndCompare(p, inFile))
}

func TestFSM_ReOpen(t *testing.T) {
r := require.New(t)
fs := vfs.NewMem()
Expand Down
42 changes: 29 additions & 13 deletions storage/table/fsm/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const (
appliedIndexMetricName = "regatta_applied_index"
tableCacheHitsMetricName = "regatta_table_storage_cache_hits"
tableCacheMissesMetricName = "regatta_table_storage_cache_misses"
tableCacheSizeMetricName = "regatta_table_storage_cache_size_bytes"
tableCacheCountMetricName = "regatta_table_storage_cache_count"
filterHitsMetricName = "regatta_table_storage_filter_hits"
filterMissesMetricName = "regatta_table_storage_filter_misses"
diskUsageMetricName = "regatta_table_storage_disk_usage_bytes"
readAmpMetricName = "regatta_table_storage_read_amp"
writeAmpMetricName = "regatta_table_storage_total_write_amp"
bytesInMetricName = "regatta_table_storage_total_bytes_in"
compactionCountMetricName = "regatta_table_storage_compaction_count"
compactionDebtMetricName = "regatta_table_storage_compaction_debt_bytes"
)

type metrics struct {
appliedIndex prometheus.Gauge
cacheHits *prometheus.GaugeVec
Expand All @@ -32,7 +48,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
return &metrics{
appliedIndex: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_applied_index",
Name: appliedIndexMetricName,
Help: "Regatta table applied index",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -42,7 +58,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
cacheHits: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "regatta_table_storage_cache_hits",
Name: tableCacheHitsMetricName,
Help: "Regatta table storage block/table cache hits",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -52,7 +68,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
cacheMisses: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "regatta_table_storage_cache_misses",
Name: tableCacheMissesMetricName,
Help: "Regatta table storage block/table cache misses",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -62,7 +78,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
cacheSize: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "regatta_table_storage_cache_size_bytes",
Name: tableCacheSizeMetricName,
Help: "Regatta table storage block/table cache size in bytes",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -72,7 +88,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
cacheCount: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "regatta_table_storage_cache_count",
Name: tableCacheCountMetricName,
Help: "Regatta table storage block/table cache items count",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -82,7 +98,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
filterHits: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_table_storage_filter_hits",
Name: filterHitsMetricName,
Help: "Regatta table storage bloom filter hits",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -92,7 +108,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
filterMisses: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_table_storage_filter_misses",
Name: filterMissesMetricName,
Help: "Regatta table storage bloom filter misses",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -102,7 +118,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
diskSpaceUsageBytes: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_table_storage_disk_usage_bytes",
Name: diskUsageMetricName,
Help: "Regatta table storage estimated disk usage, including temp files and WAL",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -112,7 +128,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
readAmplification: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_table_storage_read_amp",
Name: readAmpMetricName,
Help: "Regatta table storage read amplification",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -122,7 +138,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
totalWriteAmplification: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_table_storage_total_write_amp",
Name: writeAmpMetricName,
Help: "Regatta table storage total write amplification",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -132,7 +148,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
totalBytesIn: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_table_storage_total_bytes_in",
Name: bytesInMetricName,
Help: "Regatta table storage total bytes in",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -142,7 +158,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
compactCount: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "regatta_table_storage_compaction_count",
Name: compactionCountMetricName,
Help: "Regatta table storage compaction count by kind",
ConstLabels: map[string]string{
"table": tableName,
Expand All @@ -152,7 +168,7 @@ func newMetrics(tableName string, clusterID uint64) *metrics {
),
compactDebt: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "regatta_table_storage_compaction_debt_bytes",
Name: compactionDebtMetricName,
Help: "Regatta table storage compaction debt in bytes",
ConstLabels: map[string]string{
"table": tableName,
Expand Down
44 changes: 44 additions & 0 deletions storage/table/fsm/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright JAMF Software, LLC

package fsm

import (
"os"
"path"
"testing"

"github.com/cockroachdb/pebble/vfs"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

func TestFSM_Metrics(t *testing.T) {
p := &FSM{
fs: vfs.NewMem(),
clusterID: 1,
nodeID: 1,
dirname: "/tmp",
log: zap.NewNop().Sugar(),
metrics: newMetrics(testTable, 1),
}
_, _ = p.Open(nil)
inFile, err := os.Open(path.Join("testdata", "metrics"))
require.NoError(t, err)
defer inFile.Close()
require.NoError(t, err)
require.NoError(t, testutil.CollectAndCompare(p, inFile,
appliedIndexMetricName,
tableCacheHitsMetricName,
tableCacheMissesMetricName,
tableCacheSizeMetricName,
tableCacheCountMetricName,
filterHitsMetricName,
filterMissesMetricName,
readAmpMetricName,
writeAmpMetricName,
bytesInMetricName,
compactionCountMetricName,
compactionDebtMetricName,
))
}

0 comments on commit 5f2f109

Please sign in to comment.