Skip to content

Commit

Permalink
Signalfx exporter: Add sum aggregation method to translations (#573)
Browse files Browse the repository at this point in the history
Add sum option to aggregate_metric translation rule
  • Loading branch information
dmitryax committed Jul 29, 2020
1 parent 8c120c1 commit 16e6555
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 3 deletions.
18 changes: 17 additions & 1 deletion exporter/signalfxexporter/translation/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type AggregationMethod string
const (
// AggregationMethodCount represents count aggregation method
AggregationMethodCount AggregationMethod = "count"
AggregationMethodSum AggregationMethod = "sum"
)

type Rule struct {
Expand Down Expand Up @@ -220,7 +221,7 @@ func validateTranslationRules(rules []Rule) error {
return fmt.Errorf("fields \"metric_name\", \"dimensions\", and \"aggregation_method\" "+
"are required for %q translation rule", tr.Action)
}
if tr.AggregationMethod != "count" {
if tr.AggregationMethod != "count" && tr.AggregationMethod != "sum" {
return fmt.Errorf("invalid \"aggregation_method\": %q provided for %q translation rule",
tr.AggregationMethod, tr.Action)
}
Expand Down Expand Up @@ -389,6 +390,21 @@ func aggregateDatapoints(
dp.Value = sfxpb.Datum{
IntValue: &value,
}
case AggregationMethodSum:
var intValue int64
var floatValue float64
value := sfxpb.Datum{}
for _, dp := range dps {
if dp.Value.IntValue != nil {
intValue += *dp.Value.IntValue
value.IntValue = &intValue
}
if dp.Value.DoubleValue != nil {
floatValue += *dp.Value.DoubleValue
value.DoubleValue = &floatValue
}
}
dp.Value = value
}
result = append(result, dp)
}
Expand Down
158 changes: 156 additions & 2 deletions exporter/signalfxexporter/translation/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ func TestTranslateDataPoints(t *testing.T) {
},
},
{
name: "aggregate_metric",
name: "aggregate_metric_count",
trs: []Rule{
{
Action: ActionAggregateMetric,
Expand Down Expand Up @@ -1039,6 +1039,160 @@ func TestTranslateDataPoints(t *testing.T) {
},
},
},
{
name: "aggregate_metric_sum_int",
trs: []Rule{
{
Action: ActionAggregateMetric,
MetricName: "metric1",
Dimensions: []string{"dim1"},
AggregationMethod: "sum",
},
},
dps: []*sfxpb.DataPoint{
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
IntValue: generateIntPtr(9),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val1",
},
{
Key: "dim2",
Value: "val1",
},
},
},
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
IntValue: generateIntPtr(8),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val1",
},
{
Key: "dim2",
Value: "val1",
},
},
},
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
IntValue: generateIntPtr(2),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val2",
},
{
Key: "dim2",
Value: "val2",
},
},
},
},
want: []*sfxpb.DataPoint{
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
IntValue: generateIntPtr(17),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val1",
},
},
},
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
IntValue: generateIntPtr(2),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val2",
},
},
},
},
},
{
name: "aggregate_metric_sum_float",
trs: []Rule{
{
Action: ActionAggregateMetric,
MetricName: "metric1",
AggregationMethod: "sum",
Dimensions: []string{"dim1"},
},
},
dps: []*sfxpb.DataPoint{
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
DoubleValue: generateFloatPtr(1.2),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val1",
},
},
},
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
DoubleValue: generateFloatPtr(2.2),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val1",
},
},
},
},
want: []*sfxpb.DataPoint{
{
Metric: "metric1",
Timestamp: msec,
MetricType: &gaugeType,
Value: sfxpb.Datum{
DoubleValue: generateFloatPtr(3.4),
},
Dimensions: []*sfxpb.Dimension{
{
Key: "dim1",
Value: "val1",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -1048,7 +1202,7 @@ func TestTranslateDataPoints(t *testing.T) {
got := mt.TranslateDataPoints(zap.NewNop(), tt.dps)

// Handle float values separately
for i, dp := range tt.dps {
for i, dp := range got {
if dp.GetValue().DoubleValue != nil {
assert.InDelta(t, *tt.want[i].GetValue().DoubleValue, *dp.GetValue().DoubleValue, 0.00000001)
*dp.GetValue().DoubleValue = *tt.want[i].GetValue().DoubleValue
Expand Down

0 comments on commit 16e6555

Please sign in to comment.