diff --git a/fragment.go b/fragment.go index d24c856d2..3f46c5444 100644 --- a/fragment.go +++ b/fragment.go @@ -30,6 +30,7 @@ import ( "os" "runtime/debug" "sort" + "strconv" "strings" "sync" "syscall" @@ -208,7 +209,9 @@ func (f *fragment) Open() error { // Read last bit to determine max row. f.maxRowID = f.storage.Max() / ShardWidth - f.stats.Gauge(MetricMaximumRow, float64(f.maxRowID), 1.0) + fieldTag := "field:" + f.field + shardTag := "shard:" + strconv.FormatInt(int64(f.shard), 10) + f.stats.GaugeWithCustomTags(MetricMaximumRow, float64(f.maxRowID), 1.0, []string{fieldTag, shardTag}) return nil }(); err != nil { f.close() @@ -581,7 +584,9 @@ func (f *fragment) unprotectedSetBit(rowID, columnID uint64) (changed bool, err // Update row count if they have increased. if rowID > f.maxRowID { f.maxRowID = rowID - f.stats.Gauge(MetricMaximumRow, float64(f.maxRowID), 1.0) + fieldTag := "field:" + f.field + shardTag := "shard:" + strconv.FormatInt(int64(f.shard), 10) + f.stats.GaugeWithCustomTags(MetricMaximumRow, float64(f.maxRowID), 1.0, []string{fieldTag, shardTag}) } return changed, nil diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index 551996fb9..8dfd966ec 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -202,6 +202,11 @@ func (c *prometheusClient) Gauge(name string, value float64, rate float64) { gauge.Set(float64(value)) } +// GaugeWithCustomTags sets the value of a metric with custom tags. +func (c *prometheusClient) GaugeWithCustomTags(name string, value float64, rate float64, t []string) { + c.WithTags(append(c.tags, t...)...).Gauge(name, value, rate) +} + // Histogram tracks statistical distribution of a metric. func (c *prometheusClient) Histogram(name string, value float64, rate float64) { mu.Lock() diff --git a/stats/stats.go b/stats/stats.go index c360baab0..226f6611f 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -44,6 +44,9 @@ type StatsClient interface { // Sets the value of a metric. Gauge(name string, value float64, rate float64) + // Sets the value of a metric with custom tags + GaugeWithCustomTags(name string, value float64, rate float64, tags []string) + // Tracks statistical distribution of a metric. Histogram(name string, value float64, rate float64) @@ -73,12 +76,14 @@ func (c *nopStatsClient) WithTags(tags ...string) StatsClient func (c *nopStatsClient) Count(name string, value int64, rate float64) {} func (c *nopStatsClient) CountWithCustomTags(name string, value int64, rate float64, tags []string) {} func (c *nopStatsClient) Gauge(name string, value float64, rate float64) {} -func (c *nopStatsClient) Histogram(name string, value float64, rate float64) {} -func (c *nopStatsClient) Set(name string, value string, rate float64) {} -func (c *nopStatsClient) Timing(name string, value time.Duration, rate float64) {} -func (c *nopStatsClient) SetLogger(logger logger.Logger) {} -func (c *nopStatsClient) Open() {} -func (c *nopStatsClient) Close() error { return nil } +func (c *nopStatsClient) GaugeWithCustomTags(name string, value float64, rate float64, tags []string) { +} +func (c *nopStatsClient) Histogram(name string, value float64, rate float64) {} +func (c *nopStatsClient) Set(name string, value string, rate float64) {} +func (c *nopStatsClient) Timing(name string, value time.Duration, rate float64) {} +func (c *nopStatsClient) SetLogger(logger logger.Logger) {} +func (c *nopStatsClient) Open() {} +func (c *nopStatsClient) Close() error { return nil } // expvarStatsClient writes stats out to expvars. type expvarStatsClient struct { @@ -132,6 +137,13 @@ func (c *expvarStatsClient) Gauge(name string, value float64, rate float64) { c.m.Set(name, &f) } +// GaugeWithCustomTags Sets the value of a metric with custom tags +func (c *expvarStatsClient) GaugeWithCustomTags(name string, value float64, rate float64, tags []string) { + var f expvar.Float + f.Set(value) + c.m.Set(name, &f) +} + // Histogram tracks statistical distribution of a metric. // This works the same as gauge for this client. func (c *expvarStatsClient) Histogram(name string, value float64, rate float64) { @@ -204,6 +216,13 @@ func (a MultiStatsClient) Gauge(name string, value float64, rate float64) { } } +// GaugeWithCustomTags Sets the value of a metric with custom tags +func (a MultiStatsClient) GaugeWithCustomTags(name string, value float64, rate float64, tags []string) { + for _, c := range a { + c.GaugeWithCustomTags(name, value, rate, tags) + } +} + // Histogram tracks statistical distribution of a metric on all clients. func (a MultiStatsClient) Histogram(name string, value float64, rate float64) { for _, c := range a {