@@ -40,12 +40,12 @@ func (m ms) Get(unescapedName types.MetricName, searchLabels types.Labels, timeO
4040 return value , found , err
4141}
4242
43- func (m ms ) get (name types.MetricName , searchLabels types.Labels , timeOp types.OperationOverTime , defaultAggregation types.AggregationOverVectors ) (float64 , types.Found , error ) {
43+ func (m ms ) get (name types.MetricName , searchLabels types.Labels , timeOp types.OperationOverTime , vecOp types.AggregationOverVectors ) (float64 , types.Found , error ) {
4444 now := time .Now ().Unix ()
4545 if err := util .CheckTimeOp (timeOp ); err != nil {
4646 return - 1. , false , err
4747 }
48- if err := checkDefaultAggregation ( defaultAggregation ); err != nil {
48+ if err := checkVectorAggregation ( vecOp ); err != nil {
4949 return - 1. , false , err
5050 }
5151 storedMetrics , found := m .store .Load (string (name ))
@@ -55,6 +55,9 @@ func (m ms) get(name types.MetricName, searchLabels types.Labels, timeOp types.O
5555 }
5656 if md , f := storedMetrics .Load (hashOfMap (searchLabels )); f {
5757 // found exact label match
58+ if vecOp == types .VecCount {
59+ return 1 , true , nil
60+ }
5861 if ! m .isStale (md .LastUpdate , now ) {
5962 ret , f := md .AggregatesOverTime .Load (timeOp )
6063 if ! f {
@@ -87,7 +90,7 @@ func (m ms) get(name types.MetricName, searchLabels types.Labels, timeOp types.O
8790 return true
8891 }
8992 counter += 1
90- accumulator = m .calculateAggregate (val , counter , accumulator , defaultAggregation )
93+ accumulator = m .calculateAggregate (val , counter , accumulator , vecOp )
9194 } else {
9295 defer func () {
9396 storedMetrics .Delete (hashOfMap (searchLabels ))
@@ -99,9 +102,9 @@ func (m ms) get(name types.MetricName, searchLabels types.Labels, timeOp types.O
99102 return accumulator , true , nil
100103}
101104
102- func checkDefaultAggregation (aggregation types.AggregationOverVectors ) error {
105+ func checkVectorAggregation (aggregation types.AggregationOverVectors ) error {
103106 switch aggregation {
104- case types .VecSum , types .VecAvg , types .VecMin , types .VecMax :
107+ case types .VecSum , types .VecAvg , types .VecMin , types .VecMax , types . VecCount :
105108 return nil
106109 default :
107110 return fmt .Errorf ("unknown AggregationOverVectors:%s" , aggregation )
@@ -173,7 +176,11 @@ func (m ms) isStale(datapoint uint32, now int64) bool {
173176
174177func (m ms ) calculateAggregate (value float64 , counter int , accumulator float64 , aggregation types.AggregationOverVectors ) float64 {
175178 if counter == 1 {
176- return value
179+ if aggregation == types .VecCount {
180+ return 1
181+ } else {
182+ return value
183+ }
177184 }
178185 switch aggregation {
179186 case types .VecSum :
@@ -188,6 +195,8 @@ func (m ms) calculateAggregate(value float64, counter int, accumulator float64,
188195 return math .Min (accumulator , value )
189196 case types .VecMax :
190197 return math .Max (accumulator , value )
198+ case types .VecCount :
199+ return accumulator + 1
191200 default :
192201 panic ("unknown aggregation function: " + aggregation )
193202 }
0 commit comments