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

Support counter metrics with the _total suffix. #146

Merged
merged 5 commits into from
Aug 14, 2019
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
7 changes: 5 additions & 2 deletions retrieval/series_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ func (c *seriesCache) refresh(ctx context.Context, ref uint64) error {
return errors.Wrap(err, "get metadata")
}
if metadata == nil {
// The full name didn't turn anything up. Check again in case it's a summary or histogram without
// the metric name suffix.
// The full name didn't turn anything up. Check again in case it's a summary,
// histogram, or counter without the metric name suffix.
var ok bool
if baseMetricName, suffix, ok = stripComplexMetricSuffix(metricName); ok {
metadata, err = c.metadata.Get(ctx, job, instance, baseMetricName)
Expand Down Expand Up @@ -429,6 +429,9 @@ func (c *seriesCache) refresh(ctx context.Context, ref uint64) error {
case textparse.MetricTypeCounter:
ts.MetricKind = metric_pb.MetricDescriptor_CUMULATIVE
ts.ValueType = metric_pb.MetricDescriptor_DOUBLE
if baseMetricName != "" && suffix == metricSuffixTotal {
ts.Metric.Type = c.getMetricType(c.metricsPrefix, baseMetricName)
}
case textparse.MetricTypeGauge, textparse.MetricTypeUnknown:
ts.MetricKind = metric_pb.MetricDescriptor_GAUGE
ts.ValueType = metric_pb.MetricDescriptor_DOUBLE
Expand Down
6 changes: 5 additions & 1 deletion retrieval/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ const (
metricSuffixBucket = "_bucket"
metricSuffixSum = "_sum"
metricSuffixCount = "_count"
metricSuffixTotal = "_total"
)

func stripComplexMetricSuffix(name string) (string, string, bool) {
func stripComplexMetricSuffix(name string) (prefix string, suffix string, ok bool) {
if strings.HasSuffix(name, metricSuffixBucket) {
return name[:len(name)-len(metricSuffixBucket)], metricSuffixBucket, true
}
Expand All @@ -144,6 +145,9 @@ func stripComplexMetricSuffix(name string) (string, string, bool) {
if strings.HasSuffix(name, metricSuffixSum) {
return name[:len(name)-len(metricSuffixSum)], metricSuffixSum, true
}
if strings.HasSuffix(name, metricSuffixTotal) {
return name[:len(name)-len(metricSuffixTotal)], metricSuffixTotal, true
}
return name, "", false
}

Expand Down
134 changes: 134 additions & 0 deletions retrieval/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,140 @@ func TestSampleBuilder(t *testing.T) {
},
},
},
// Any counter metric with the _total suffix should be treated as normal if metadata
// can be found for the original metric name.
{
series: seriesMap{
1: labels.FromStrings("job", "job1", "instance", "instance1", "a", "1", "__name__", "metric1_total"),
},
targets: targetMap{
"job1/instance1": &targets.Target{
Labels: promlabels.FromStrings("job", "job1", "instance", "instance1"),
DiscoveredLabels: promlabels.FromStrings("__resource_a", "resource2_a"),
},
},
metadata: metadataMap{
"job1/instance1/metric1_total": &scrape.MetricMetadata{Type: textparse.MetricTypeCounter, Metric: "metric1_total"},
},
metricPrefix: "test.googleapis.com",
input: []tsdb.RefSample{
{Ref: 1, T: 2000, V: 5.5},
{Ref: 1, T: 3000, V: 8},
},
result: []*monitoring_pb.TimeSeries{
nil, // Skipped by reset timestamp handling.
{
Resource: &monitoredres_pb.MonitoredResource{
Type: "resource2",
Labels: map[string]string{"resource_a": "resource2_a"},
},
Metric: &metric_pb.Metric{
Type: "test.googleapis.com/metric1_total",
Labels: map[string]string{"a": "1"},
},
MetricKind: metric_pb.MetricDescriptor_CUMULATIVE,
ValueType: metric_pb.MetricDescriptor_DOUBLE,
Points: []*monitoring_pb.Point{{
Interval: &monitoring_pb.TimeInterval{
StartTime: &timestamp_pb.Timestamp{Seconds: 2},
EndTime: &timestamp_pb.Timestamp{Seconds: 3},
},
Value: &monitoring_pb.TypedValue{
Value: &monitoring_pb.TypedValue_DoubleValue{2.5},
},
}},
},
},
},
// Any counter metric with the _total suffix should fail over to the metadata for
// the metric with the _total suffix removed while reporting the metric with the
// _total suffix removed in the metric name as well.
{
series: seriesMap{
1: labels.FromStrings("job", "job1", "instance", "instance1", "a", "1", "__name__", "metric1_total"),
},
targets: targetMap{
"job1/instance1": &targets.Target{
Labels: promlabels.FromStrings("job", "job1", "instance", "instance1"),
DiscoveredLabels: promlabels.FromStrings("__resource_a", "resource2_a"),
},
},
metadata: metadataMap{
"job1/instance1/metric1": &scrape.MetricMetadata{Type: textparse.MetricTypeCounter, Metric: "metric1"},
},
metricPrefix: "test.googleapis.com",
input: []tsdb.RefSample{
{Ref: 1, T: 2000, V: 5.5},
{Ref: 1, T: 3000, V: 8},
},
result: []*monitoring_pb.TimeSeries{
nil, // Skipped by reset timestamp handling.
{
Resource: &monitoredres_pb.MonitoredResource{
Type: "resource2",
Labels: map[string]string{"resource_a": "resource2_a"},
},
Metric: &metric_pb.Metric{
Type: "test.googleapis.com/metric1",
Labels: map[string]string{"a": "1"},
},
MetricKind: metric_pb.MetricDescriptor_CUMULATIVE,
ValueType: metric_pb.MetricDescriptor_DOUBLE,
Points: []*monitoring_pb.Point{{
Interval: &monitoring_pb.TimeInterval{
StartTime: &timestamp_pb.Timestamp{Seconds: 2},
EndTime: &timestamp_pb.Timestamp{Seconds: 3},
},
Value: &monitoring_pb.TypedValue{
Value: &monitoring_pb.TypedValue_DoubleValue{2.5},
},
}},
},
},
},
// Any non-counter metric with the _total suffix should fail over to the metadata
// for the metric with the _total suffix removed while reporting the metric with
// the original name.
{
series: seriesMap{
1: labels.FromStrings("job", "job1", "instance", "instance1", "a", "1", "__name__", "metric1_total"),
},
targets: targetMap{
"job1/instance1": &targets.Target{
Labels: promlabels.FromStrings("job", "job1", "instance", "instance1"),
DiscoveredLabels: promlabels.FromStrings("__resource_a", "resource2_a"),
},
},
metadata: metadataMap{
"job1/instance1/metric1": &scrape.MetricMetadata{Type: textparse.MetricTypeGauge, Metric: "metric1"},
},
metricPrefix: "test.googleapis.com",
input: []tsdb.RefSample{
{Ref: 1, T: 3000, V: 8},
},
result: []*monitoring_pb.TimeSeries{
{
Resource: &monitoredres_pb.MonitoredResource{
Type: "resource2",
Labels: map[string]string{"resource_a": "resource2_a"},
},
Metric: &metric_pb.Metric{
Type: "test.googleapis.com/metric1_total",
Labels: map[string]string{"a": "1"},
},
MetricKind: metric_pb.MetricDescriptor_GAUGE,
ValueType: metric_pb.MetricDescriptor_DOUBLE,
Points: []*monitoring_pb.Point{{
Interval: &monitoring_pb.TimeInterval{
EndTime: &timestamp_pb.Timestamp{Seconds: 3},
},
Value: &monitoring_pb.TypedValue{
Value: &monitoring_pb.TypedValue_DoubleValue{8},
},
}},
},
},
},
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down