Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metric: add prometheus-based histogram #85990

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/server/status/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ go_library(
"//pkg/util/timeutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_cockroachdb_redact//:redact",
"@com_github_codahale_hdrhistogram//:hdrhistogram",
"@com_github_dustin_go_humanize//:go-humanize",
"@com_github_elastic_gosigar//:gosigar",
"@com_github_shirou_gopsutil_v3//net",
Expand Down Expand Up @@ -148,6 +147,7 @@ go_test(
"//pkg/util/system",
"//pkg/util/timeutil",
"@com_github_kr_pretty//:pretty",
"@com_github_prometheus_client_golang//prometheus",
"@com_github_shirou_gopsutil_v3//net",
],
)
Expand Down
43 changes: 22 additions & 21 deletions pkg/server/status/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/system"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
"github.com/codahale/hdrhistogram"
humanize "github.com/dustin/go-humanize"
"github.com/dustin/go-humanize"
"github.com/elastic/gosigar"
)

Expand Down Expand Up @@ -528,26 +527,8 @@ type registryRecorder struct {
}

func extractValue(name string, mtr interface{}, fn func(string, float64)) error {
// TODO(tschottdorf,ajwerner): consider moving this switch to a single
// interface implemented by the individual metric types.
type (
float64Valuer interface{ Value() float64 }
int64Valuer interface{ Value() int64 }
int64Counter interface{ Count() int64 }
histogramValuer interface {
Windowed() (*hdrhistogram.Histogram, time.Duration)
}
)
switch mtr := mtr.(type) {
case float64:
fn(name, mtr)
case float64Valuer:
fn(name, mtr.Value())
case int64Valuer:
fn(name, float64(mtr.Value()))
case int64Counter:
fn(name, float64(mtr.Count()))
case histogramValuer:
case *metric.Histogram:
// TODO(mrtracy): Where should this comment go for better
// visibility?
//
Expand All @@ -567,6 +548,26 @@ func extractValue(name string, mtr interface{}, fn func(string, float64)) error
fn(name+pt.suffix, float64(curr.ValueAtQuantile(pt.quantile)))
}
fn(name+"-count", float64(curr.TotalCount()))
case *metric.HistogramV2:
n := float64(mtr.TotalCountWindowed())
fn(name+"-count", n)
avg := mtr.TotalSumWindowed() / n
if math.IsNaN(avg) || math.IsInf(avg, +1) || math.IsInf(avg, -1) {
avg = 0
}
fn(name+"-avg", avg)
for _, pt := range recordHistogramQuantiles {
fn(name+pt.suffix, mtr.ValueAtQuantileWindowed(pt.quantile))
}
case metric.PrometheusExportable:
// NB: this branch is intentionally at the bottom since all metrics implement it.
m := mtr.ToPrometheusMetric()
if m.Gauge != nil {
fn(name, *m.Gauge.Value)
} else if m.Counter != nil {
fn(name, *m.Counter.Value)
}

default:
return errors.Errorf("cannot extract value for type %T", mtr)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/server/status/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/system"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/kr/pretty"
"github.com/prometheus/client_golang/prometheus"
)

// byTimeAndName is a slice of tspb.TimeSeriesData.
Expand Down Expand Up @@ -202,6 +203,7 @@ func TestMetricsRecorder(t *testing.T) {
{"testGaugeFloat64", "floatgauge", 20},
{"testCounter", "counter", 5},
{"testHistogram", "histogram", 10},
{"testHistogramV2", "histogramV2", 9},
{"testLatency", "latency", 10},
{"testAggGauge", "agggauge", 4},
{"testAggCounter", "aggcounter", 7},
Expand Down Expand Up @@ -294,6 +296,16 @@ func TestMetricsRecorder(t *testing.T) {
addExpected(reg.prefix, data.name+q.suffix, reg.source, 100, data.val, reg.isNode)
}
addExpected(reg.prefix, data.name+"-count", reg.source, 100, 1, reg.isNode)
case "histogramV2":
h := metric.NewHistogramV2(metric.Metadata{Name: reg.prefix + data.name}, time.Second,
prometheus.HistogramOpts{Buckets: []float64{1.0, 10.0, 100.0, 1000.0}})
reg.reg.AddMetric(h)
h.RecordValue(data.val)
for _, q := range recordHistogramQuantiles {
addExpected(reg.prefix, data.name+q.suffix, reg.source, 100, 10, reg.isNode)
}
addExpected(reg.prefix, data.name+"-count", reg.source, 100, 1, reg.isNode)
addExpected(reg.prefix, data.name+"-avg", reg.source, 100, 9, reg.isNode)
case "latency":
l := metric.NewLatency(metric.Metadata{Name: reg.prefix + data.name}, time.Hour)
reg.reg.AddMetric(l)
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/metric/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ go_library(
"registry.go",
"rule.go",
"rule_registry.go",
"sliding_histogram.go",
"test_helpers.go",
"tick_helper.go",
],
embed = [":metric_go_proto"],
importpath = "github.com/cockroachdb/cockroach/pkg/util/metric",
Expand Down Expand Up @@ -51,6 +51,7 @@ go_test(
deps = [
"//pkg/util/log",
"@com_github_kr_pretty//:pretty",
"@com_github_prometheus_client_golang//prometheus",
"@com_github_prometheus_client_model//go",
"@com_github_stretchr_testify//require",
],
Expand Down
Loading