Skip to content

Commit

Permalink
Add support for histograms in metrics scope (cadence-workflow#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyassrivatsan authored Jun 24, 2019
1 parent 55f697d commit 6ffb281
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
7 changes: 5 additions & 2 deletions common/metrics/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package metrics

import "github.com/uber-go/tally"

// types used/defined by the package
type (
// MetricName is the name of the metric
Expand All @@ -30,8 +32,9 @@ type (

// metricDefinition contains the definition for a metric
metricDefinition struct {
metricType MetricType // metric type
metricName MetricName // metric name
metricType MetricType // metric type
metricName MetricName // metric name
buckets tally.Buckets // buckets if we are emitting histograms
}

// scopeDefinition holds the tag definitions for a scope
Expand Down
12 changes: 8 additions & 4 deletions common/metrics/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ type (
IncCounter(counter int)
// AddCounter adds delta to the counter metric
AddCounter(counter int, delta int64)
// StartTimer starts a timer for the given
// metric name. Time will be recorded when stopwatch is stopped.
// StartTimer starts a timer for the given metric name.
// Time will be recorded when stopwatch is stopped.
StartTimer(timer int) Stopwatch
// RecordTimer starts a timer for the given
// metric name
// RecordTimer starts a timer for the given metric name
RecordTimer(timer int, d time.Duration)
// RecordHistogramDuration records a histogram duration value for the given
// metric name
RecordHistogramDuration(timer int, d time.Duration)
// RecordHistogramValue records a histogram value for the given metric name
RecordHistogramValue(timer int, value float64)
// UpdateGauge reports Gauge type absolute value metric
UpdateGauge(gauge int, value float64)
// Tagged return an internal scope that can be used to add additional
Expand Down
20 changes: 14 additions & 6 deletions common/metrics/mocks/Scope.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions common/metrics/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func (m *metricsScope) RecordTimer(id int, d time.Duration) {
}
}

func (m *metricsScope) RecordHistogramDuration(id int, value time.Duration) {
name := string(m.defs[id].metricName)
m.scope.Histogram(name, m.getBuckets(id)).RecordDuration(value)
}

func (m *metricsScope) RecordHistogramValue(id int, value float64) {
name := string(m.defs[id].metricName)
m.scope.Histogram(name, m.getBuckets(id)).RecordValue(value)
}

func (m *metricsScope) Tagged(tags ...Tag) Scope {
domainTagged := false
tagMap := make(map[string]string, len(tags))
Expand All @@ -86,6 +96,13 @@ func (m *metricsScope) Tagged(tags ...Tag) Scope {
return newMetricsScope(m.scope.Tagged(tagMap), m.defs, domainTagged)
}

func (m *metricsScope) getBuckets(id int) tally.Buckets {
if m.defs[id].buckets != nil {
return m.defs[id].buckets
}
return tally.DefaultBuckets
}

func isDomainTagged(tag Tag) bool {
return tag.Key() == domain && tag.Value() != domainAllValue
}
16 changes: 16 additions & 0 deletions service/worker/archiver/replay_metrics_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ func (r *replayMetricsScope) RecordTimer(timer int, d time.Duration) {
r.scope.RecordTimer(timer, d)
}

// RecordHistogramDuration records a duration value in a histogram
func (r *replayMetricsScope) RecordHistogramDuration(timer int, d time.Duration) {
if workflow.IsReplaying(r.ctx) {
return
}
r.scope.RecordHistogramDuration(timer, d)
}

// RecordHistogramValue records a value in a histogram
func (r *replayMetricsScope) RecordHistogramValue(timer int, value float64) {
if workflow.IsReplaying(r.ctx) {
return
}
r.scope.RecordHistogramValue(timer, value)
}

// UpdateGauge reports Gauge type absolute value metric
func (r *replayMetricsScope) UpdateGauge(gauge int, value float64) {
if workflow.IsReplaying(r.ctx) {
Expand Down

0 comments on commit 6ffb281

Please sign in to comment.