Skip to content

Commit 6c1dae1

Browse files
authored
enable individual labels from aggregate (#269)
* enable individual labels from aggregate * added to unit test
1 parent bfbd89b commit 6c1dae1

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

pkg/pipeline/extract/aggregate/aggregate.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Aggregate struct {
5353

5454
type GroupState struct {
5555
normalizedValues NormalizedValues
56+
labels Labels
5657
recentRawValues []float64
5758
recentOpValue float64
5859
recentCount int
@@ -95,14 +96,14 @@ func (labels Labels) getNormalizedValues() NormalizedValues {
9596
return NormalizedValues(normalizedAsString)
9697
}
9798

98-
func (aggregate Aggregate) FilterEntry(entry config.GenericMap) (error, NormalizedValues) {
99+
func (aggregate Aggregate) FilterEntry(entry config.GenericMap) (error, NormalizedValues, Labels) {
99100
labels, allLabelsFound := aggregate.LabelsFromEntry(entry)
100101
if !allLabelsFound {
101-
return fmt.Errorf("missing keys in entry"), ""
102+
return fmt.Errorf("missing keys in entry"), "", nil
102103
}
103104

104105
normalizedValues := labels.getNormalizedValues()
105-
return nil, normalizedValues
106+
return nil, normalizedValues, labels
106107
}
107108

108109
func getInitValue(operation string) float64 {
@@ -120,15 +121,15 @@ func getInitValue(operation string) float64 {
120121
}
121122
}
122123

123-
func (aggregate Aggregate) UpdateByEntry(entry config.GenericMap, normalizedValues NormalizedValues) error {
124+
func (aggregate Aggregate) UpdateByEntry(entry config.GenericMap, normalizedValues NormalizedValues, labels Labels) error {
124125

125126
aggregate.mutex.Lock()
126127
defer aggregate.mutex.Unlock()
127128

128129
var groupState *GroupState
129130
oldEntry, ok := aggregate.cache.GetCacheEntry(string(normalizedValues))
130131
if !ok {
131-
groupState = &GroupState{normalizedValues: normalizedValues}
132+
groupState = &GroupState{normalizedValues: normalizedValues, labels: labels}
132133
initVal := getInitValue(string(aggregate.Definition.Operation))
133134
groupState.totalValue = initVal
134135
groupState.recentOpValue = initVal
@@ -183,13 +184,13 @@ func (aggregate Aggregate) UpdateByEntry(entry config.GenericMap, normalizedValu
183184
func (aggregate Aggregate) Evaluate(entries []config.GenericMap) error {
184185
for _, entry := range entries {
185186
// filter entries matching labels with aggregates
186-
err, normalizedValues := aggregate.FilterEntry(entry)
187+
err, normalizedValues, labels := aggregate.FilterEntry(entry)
187188
if err != nil {
188189
continue
189190
}
190191

191192
// update aggregate group by entry
192-
err = aggregate.UpdateByEntry(entry, normalizedValues)
193+
err = aggregate.UpdateByEntry(entry, normalizedValues, labels)
193194
if err != nil {
194195
log.Debugf("UpdateByEntry error %v", err)
195196
continue
@@ -208,7 +209,7 @@ func (aggregate Aggregate) GetMetrics() []config.GenericMap {
208209
// iterate over the items in the cache
209210
aggregate.cache.Iterate(func(key string, value interface{}) {
210211
group := value.(*GroupState)
211-
metrics = append(metrics, config.GenericMap{
212+
newEntry := config.GenericMap{
212213
"name": aggregate.Definition.Name,
213214
"operation": aggregate.Definition.Operation,
214215
"record_key": aggregate.Definition.RecordKey,
@@ -220,7 +221,12 @@ func (aggregate Aggregate) GetMetrics() []config.GenericMap {
220221
"recent_op_value": group.recentOpValue,
221222
"recent_count": group.recentCount,
222223
strings.Join(aggregate.Definition.By, "_"): string(group.normalizedValues),
223-
})
224+
}
225+
// add the items in aggregate.Definition.By individually to the entry
226+
for _, key := range aggregate.Definition.By {
227+
newEntry[key] = group.labels[key]
228+
}
229+
metrics = append(metrics, newEntry)
224230
// Once reported, we reset the recentXXX fields
225231
if aggregate.Definition.Operation == OperationRawValues {
226232
group.recentRawValues = make([]float64, 0)

pkg/pipeline/extract/aggregate/aggregate_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,17 @@ func Test_FilterEntry(t *testing.T) {
9696
aggregate := GetMockAggregate()
9797
entry := test.GetIngestMockEntry(false)
9898

99-
err, _ := aggregate.FilterEntry(entry)
99+
err, _, _ := aggregate.FilterEntry(entry)
100+
require.Equal(t, err, nil)
100101

102+
err, normalizedLabels, labels := aggregate.FilterEntry(entry)
101103
require.Equal(t, err, nil)
104+
require.Equal(t, Labels{"srcIP": "10.0.0.1", "dstIP": "20.0.0.2"}, labels)
105+
require.Equal(t, NormalizedValues("20.0.0.2,10.0.0.1"), normalizedLabels)
106+
102107
entry = test.GetIngestMockEntry(true)
103108

104-
err, _ = aggregate.FilterEntry(entry)
109+
err, _, _ = aggregate.FilterEntry(entry)
105110

106111
require.EqualError(t, err, "missing keys in entry")
107112
}

0 commit comments

Comments
 (0)