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

Signalfx exporter: Add sum aggregation to metric translations #573

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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