prometheus: fetch all StatQueues when IDs list is empty

This commit is contained in:
ionutboangiu
2025-09-18 19:29:39 +03:00
committed by Dan Christian Bogos
parent 7ed66e7cd5
commit a0ced56275
3 changed files with 35 additions and 8 deletions

View File

@@ -21,6 +21,7 @@ package agents
import (
"fmt"
"net/http"
"strings"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/config"
@@ -216,11 +217,31 @@ func (pa *PrometheusAgent) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// updateStatsMetrics fetches and updates all StatQueue metrics by calling each
// configured StatS connection.
func (pa *PrometheusAgent) updateStatsMetrics() {
if len(pa.cfg.PrometheusAgentCfg().StatQueueIDs) == 0 {
return
}
for _, connID := range pa.cfg.PrometheusAgentCfg().StatSConns {
for _, sqID := range pa.cfg.PrometheusAgentCfg().StatQueueIDs {
sqIDs := pa.cfg.PrometheusAgentCfg().StatQueueIDs
// When no StatQueueIDs set, fetch all available ones.
if len(sqIDs) == 0 {
// Internal StatS connections cannot handle APIerS calls.
// Redirect *internal:*stats to *internal:*apier to get StatQueue IDs.
apiersConnID := connID
if strings.HasPrefix(connID, utils.MetaInternal) {
apiersConnID = utils.ConcatenatedKey(utils.MetaInternal,
utils.MetaApier)
}
if err := pa.cm.Call(context.Background(), []string{apiersConnID},
utils.APIerSv1GetStatQueueProfileIDs,
&utils.PaginatorWithTenant{}, &sqIDs); err != nil {
utils.Logger.Err(fmt.Sprintf(
"<%s> failed to retrieve all StatQueue IDs (connID=%q): %v",
utils.PrometheusAgent, apiersConnID, err))
continue
}
}
for _, sqID := range sqIDs {
tenantID := utils.NewTenantID(sqID)
if tenantID.Tenant == "" {

View File

@@ -1339,6 +1339,13 @@ func (cfg *CGRConfig) checkConfigSanity() error {
}
}
if cfg.prometheusAgentCfg.Enabled {
if len(cfg.prometheusAgentCfg.StatSConns) > 0 &&
len(cfg.prometheusAgentCfg.StatQueueIDs) == 0 &&
!cfg.apier.Enabled {
return fmt.Errorf(
"<%s> when StatQueueIDs is empty, %s must be enabled to retrieve all available StatQueue IDs",
utils.PrometheusAgent, utils.ApierS)
}
for _, connID := range cfg.prometheusAgentCfg.StatSConns {
if strings.HasPrefix(connID, utils.MetaInternal) && !cfg.statsCfg.Enabled {
return fmt.Errorf("<%s> not enabled but requested by <%s> component", utils.StatService, utils.PrometheusAgent)

View File

@@ -24,7 +24,6 @@ import (
"bytes"
"fmt"
"io"
"math/rand"
"net/http"
"testing"
"time"
@@ -68,7 +67,7 @@ func TestPrometheusAgentIT(t *testing.T) {
"*stat_filter_indexes",
"*rpc_connections"
],
"stats_conns": ["*localhost", "external"],
"stats_conns": ["*internal", "external"],
"stat_queue_ids": ["cgrates.org:SQ_1","SQ_2"]
}
}`
@@ -150,8 +149,8 @@ func processStats(t *testing.T, client *birpc.Client) {
Tenant: "cgrates.org",
ID: utils.GenUUID(),
Event: map[string]any{
utils.Usage: time.Duration(rand.Intn(3600)+60) * time.Second,
utils.Cost: rand.Float64()*20 + 0.1,
utils.Usage: time.Duration(i) * time.Second,
utils.Cost: i * 10,
},
APIOpts: map[string]any{
utils.OptsStatsProfileIDs: []string{fmt.Sprintf("SQ_%d", i+1)},