Skip to content

Commit

Permalink
updated mongo scaler
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-100ms committed Sep 25, 2023
1 parent 1a114dc commit a432d73
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/scalers/mongo_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ func (s *mongoDBScaler) Close(ctx context.Context) error {
return nil
}

type resultVal struct {
Value int64 `json:"value" bson:"value"`
}

// getQueryResult query mongoDB by meta.query
func (s *mongoDBScaler) getQueryResult(ctx context.Context) (int64, error) {
ctx, cancel := context.WithTimeout(ctx, mongoDBDefaultTimeOut)
Expand All @@ -221,13 +225,16 @@ func (s *mongoDBScaler) getQueryResult(ctx context.Context) (int64, error) {
return 0, err
}

docsNum, err := s.client.Database(s.metadata.dbName).Collection(s.metadata.collection).CountDocuments(ctx, filter)
var result resultVal

err = s.client.Database(s.metadata.dbName).Collection(s.metadata.collection).FindOne(ctx, filter).Decode(&result)

if err != nil {
s.logger.Error(err, fmt.Sprintf("failed to query %v in %v, because of %v", s.metadata.dbName, s.metadata.collection, err))
return 0, err
}

return docsNum, nil
return result.Value, nil
}

// GetMetricsAndActivity query from mongoDB,and return to external metrics
Expand Down

0 comments on commit a432d73

Please sign in to comment.