implement config object for prometheus_agent

This commit is contained in:
ionutboangiu
2025-02-24 11:01:48 +02:00
committed by Dan Christian Bogos
parent 00c192251d
commit 875fdb3993
9 changed files with 273 additions and 105 deletions

View File

@@ -474,6 +474,12 @@ func storeDiffSection(ctx *context.Context, section string, db ConfigDB, v1, v2
return
}
return db.SetSection(ctx, section, diffDNSAgentJsonCfg(jsn, v1.DNSAgentCfg(), v2.DNSAgentCfg()))
case PrometheusAgentJSON:
jsn := new(PrometheusAgentJsonCfg)
if err = db.GetSection(ctx, section, jsn); err != nil {
return
}
return db.SetSection(ctx, section, diffPrometheusAgentJsonCfg(jsn, v1.PrometheusAgentCfg(), v2.PrometheusAgentCfg()))
case AttributeSJSON:
jsn := new(AttributeSJsonCfg)
if err = db.GetSection(ctx, section, jsn); err != nil {

View File

@@ -186,8 +186,9 @@ func newCGRConfig(config []byte) (cfg *CGRConfig, err error) {
ClientDictionaries: make(map[string]string),
ClientSecrets: make(map[string]string),
},
dnsAgentCfg: new(DNSAgentCfg),
janusAgentCfg: new(JanusAgentCfg),
dnsAgentCfg: new(DNSAgentCfg),
janusAgentCfg: new(JanusAgentCfg),
prometheusAgentCfg: new(PrometheusAgentCfg),
attributeSCfg: &AttributeSCfg{Opts: &AttributesOpts{
ProfileIDs: []*DynamicStringSliceOpt{},
ProcessRuns: []*DynamicIntOpt{{value: AttributesProcessRunsDftOpt}},
@@ -334,51 +335,52 @@ type CGRConfig struct {
rpcConns RPCConns
templates FCTemplates
generalCfg *GeneralCfg // General config
loggerCfg *LoggerCfg // Logger config
dataDbCfg *DataDbCfg // Database config
storDbCfg *StorDbCfg // StorDb config
tlsCfg *TLSCfg // TLS config
cacheCfg *CacheCfg // Cache config
listenCfg *ListenCfg // Listen config
httpCfg *HTTPCfg // HTTP config
filterSCfg *FilterSCfg // FilterS config
cdrsCfg *CdrsCfg // Cdrs config
sessionSCfg *SessionSCfg // SessionS config
fsAgentCfg *FsAgentCfg // FreeSWITCHAgent config
kamAgentCfg *KamAgentCfg // KamailioAgent config
asteriskAgentCfg *AsteriskAgentCfg // AsteriskAgent config
diameterAgentCfg *DiameterAgentCfg // DiameterAgent config
radiusAgentCfg *RadiusAgentCfg // RadiusAgent config
dnsAgentCfg *DNSAgentCfg // DNSAgent config
janusAgentCfg *JanusAgentCfg // JanusAgent config
attributeSCfg *AttributeSCfg // AttributeS config
chargerSCfg *ChargerSCfg // ChargerS config
resourceSCfg *ResourceSConfig // ResourceS config
statsCfg *StatSCfg // StatS config
thresholdSCfg *ThresholdSCfg // ThresholdS config
routeSCfg *RouteSCfg // RouteS config
trendSCfg *TrendSCfg // TrendS config
rankingSCfg *RankingSCfg // RankingS config
sureTaxCfg *SureTaxCfg // SureTax config
registrarCCfg *RegistrarCCfgs // RegistrarC config
loaderCgrCfg *LoaderCgrCfg // LoaderCgr config
migratorCgrCfg *MigratorCgrCfg // MigratorCgr config
analyzerSCfg *AnalyzerSCfg // AnalyzerS config
admS *AdminSCfg // APIer config
ersCfg *ERsCfg // EventReader config
eesCfg *EEsCfg // EventExporter config
efsCfg *EFsCfg // EventFailover config
rateSCfg *RateSCfg // RateS config
actionSCfg *ActionSCfg // ActionS config
sipAgentCfg *SIPAgentCfg // SIPAgent config
configSCfg *ConfigSCfg // ConfigS config
apiBanCfg *APIBanCfg // APIBan config
sentryPeerCfg *SentryPeerCfg //SentryPeer config
coreSCfg *CoreSCfg // CoreS config
accountSCfg *AccountSCfg // AccountS config
tpeSCfg *TpeSCfg // TpeS config
configDBCfg *ConfigDBCfg // ConfigDB conifg
generalCfg *GeneralCfg // General config
loggerCfg *LoggerCfg // Logger config
dataDbCfg *DataDbCfg // Database config
storDbCfg *StorDbCfg // StorDb config
tlsCfg *TLSCfg // TLS config
cacheCfg *CacheCfg // Cache config
listenCfg *ListenCfg // Listen config
httpCfg *HTTPCfg // HTTP config
filterSCfg *FilterSCfg // FilterS config
cdrsCfg *CdrsCfg // Cdrs config
sessionSCfg *SessionSCfg // SessionS config
fsAgentCfg *FsAgentCfg // FreeSWITCHAgent config
kamAgentCfg *KamAgentCfg // KamailioAgent config
asteriskAgentCfg *AsteriskAgentCfg // AsteriskAgent config
diameterAgentCfg *DiameterAgentCfg // DiameterAgent config
radiusAgentCfg *RadiusAgentCfg // RadiusAgent config
dnsAgentCfg *DNSAgentCfg // DNSAgent config
prometheusAgentCfg *PrometheusAgentCfg // PrometheusAgent config
janusAgentCfg *JanusAgentCfg // JanusAgent config
attributeSCfg *AttributeSCfg // AttributeS config
chargerSCfg *ChargerSCfg // ChargerS config
resourceSCfg *ResourceSConfig // ResourceS config
statsCfg *StatSCfg // StatS config
thresholdSCfg *ThresholdSCfg // ThresholdS config
routeSCfg *RouteSCfg // RouteS config
trendSCfg *TrendSCfg // TrendS config
rankingSCfg *RankingSCfg // RankingS config
sureTaxCfg *SureTaxCfg // SureTax config
registrarCCfg *RegistrarCCfgs // RegistrarC config
loaderCgrCfg *LoaderCgrCfg // LoaderCgr config
migratorCgrCfg *MigratorCgrCfg // MigratorCgr config
analyzerSCfg *AnalyzerSCfg // AnalyzerS config
admS *AdminSCfg // APIer config
ersCfg *ERsCfg // EventReader config
eesCfg *EEsCfg // EventExporter config
efsCfg *EFsCfg // EventFailover config
rateSCfg *RateSCfg // RateS config
actionSCfg *ActionSCfg // ActionS config
sipAgentCfg *SIPAgentCfg // SIPAgent config
configSCfg *ConfigSCfg // ConfigS config
apiBanCfg *APIBanCfg // APIBan config
sentryPeerCfg *SentryPeerCfg //SentryPeer config
coreSCfg *CoreSCfg // CoreS config
accountSCfg *AccountSCfg // AccountS config
tpeSCfg *TpeSCfg // TpeS config
configDBCfg *ConfigDBCfg // ConfigDB conifg
cacheDP utils.MapStorage
cacheDPMux sync.RWMutex
@@ -450,6 +452,13 @@ func (cfg *CGRConfig) DNSAgentCfg() *DNSAgentCfg {
return cfg.dnsAgentCfg
}
// PrometheusAgentCfg returns the config for Prometheus Agent
func (cfg *CGRConfig) PrometheusAgentCfg() *PrometheusAgentCfg {
cfg.lks[PrometheusAgentJSON].Lock()
defer cfg.lks[PrometheusAgentJSON].Unlock()
return cfg.prometheusAgentCfg
}
// AttributeSCfg returns the config for AttributeS
func (cfg *CGRConfig) AttributeSCfg() *AttributeSCfg {
cfg.lks[AttributeSJSON].Lock()
@@ -1026,57 +1035,58 @@ func (cfg *CGRConfig) Clone() (cln *CGRConfig) {
DataFolderPath: cfg.DataFolderPath,
ConfigPath: cfg.ConfigPath,
loaderCfg: *cfg.loaderCfg.Clone(),
httpAgentCfg: *cfg.httpAgentCfg.Clone(),
rpcConns: cfg.rpcConns.Clone(),
templates: cfg.templates.Clone(),
generalCfg: cfg.generalCfg.Clone(),
loggerCfg: cfg.loggerCfg.Clone(),
dataDbCfg: cfg.dataDbCfg.Clone(),
storDbCfg: cfg.storDbCfg.Clone(),
tlsCfg: cfg.tlsCfg.Clone(),
cacheCfg: cfg.cacheCfg.Clone(),
listenCfg: cfg.listenCfg.Clone(),
httpCfg: cfg.httpCfg.Clone(),
filterSCfg: cfg.filterSCfg.Clone(),
cdrsCfg: cfg.cdrsCfg.Clone(),
sessionSCfg: cfg.sessionSCfg.Clone(),
fsAgentCfg: cfg.fsAgentCfg.Clone(),
kamAgentCfg: cfg.kamAgentCfg.Clone(),
janusAgentCfg: cfg.janusAgentCfg.Clone(),
asteriskAgentCfg: cfg.asteriskAgentCfg.Clone(),
diameterAgentCfg: cfg.diameterAgentCfg.Clone(),
radiusAgentCfg: cfg.radiusAgentCfg.Clone(),
dnsAgentCfg: cfg.dnsAgentCfg.Clone(),
attributeSCfg: cfg.attributeSCfg.Clone(),
chargerSCfg: cfg.chargerSCfg.Clone(),
resourceSCfg: cfg.resourceSCfg.Clone(),
statsCfg: cfg.statsCfg.Clone(),
thresholdSCfg: cfg.thresholdSCfg.Clone(),
trendSCfg: cfg.trendSCfg.Clone(),
rankingSCfg: cfg.rankingSCfg.Clone(),
routeSCfg: cfg.routeSCfg.Clone(),
sureTaxCfg: cfg.sureTaxCfg.Clone(),
registrarCCfg: cfg.registrarCCfg.Clone(),
loaderCgrCfg: cfg.loaderCgrCfg.Clone(),
migratorCgrCfg: cfg.migratorCgrCfg.Clone(),
analyzerSCfg: cfg.analyzerSCfg.Clone(),
admS: cfg.admS.Clone(),
ersCfg: cfg.ersCfg.Clone(),
eesCfg: cfg.eesCfg.Clone(),
efsCfg: cfg.efsCfg.Clone(),
rateSCfg: cfg.rateSCfg.Clone(),
sipAgentCfg: cfg.sipAgentCfg.Clone(),
configSCfg: cfg.configSCfg.Clone(),
apiBanCfg: cfg.apiBanCfg.Clone(),
sentryPeerCfg: cfg.sentryPeerCfg.Clone(),
coreSCfg: cfg.coreSCfg.Clone(),
actionSCfg: cfg.actionSCfg.Clone(),
accountSCfg: cfg.accountSCfg.Clone(),
tpeSCfg: cfg.tpeSCfg.Clone(),
configDBCfg: cfg.configDBCfg.Clone(),
rldCh: make(chan string),
cacheDP: make(utils.MapStorage),
loaderCfg: *cfg.loaderCfg.Clone(),
httpAgentCfg: *cfg.httpAgentCfg.Clone(),
rpcConns: cfg.rpcConns.Clone(),
templates: cfg.templates.Clone(),
generalCfg: cfg.generalCfg.Clone(),
loggerCfg: cfg.loggerCfg.Clone(),
dataDbCfg: cfg.dataDbCfg.Clone(),
storDbCfg: cfg.storDbCfg.Clone(),
tlsCfg: cfg.tlsCfg.Clone(),
cacheCfg: cfg.cacheCfg.Clone(),
listenCfg: cfg.listenCfg.Clone(),
httpCfg: cfg.httpCfg.Clone(),
filterSCfg: cfg.filterSCfg.Clone(),
cdrsCfg: cfg.cdrsCfg.Clone(),
sessionSCfg: cfg.sessionSCfg.Clone(),
fsAgentCfg: cfg.fsAgentCfg.Clone(),
kamAgentCfg: cfg.kamAgentCfg.Clone(),
janusAgentCfg: cfg.janusAgentCfg.Clone(),
asteriskAgentCfg: cfg.asteriskAgentCfg.Clone(),
diameterAgentCfg: cfg.diameterAgentCfg.Clone(),
radiusAgentCfg: cfg.radiusAgentCfg.Clone(),
dnsAgentCfg: cfg.dnsAgentCfg.Clone(),
prometheusAgentCfg: cfg.prometheusAgentCfg.Clone(),
attributeSCfg: cfg.attributeSCfg.Clone(),
chargerSCfg: cfg.chargerSCfg.Clone(),
resourceSCfg: cfg.resourceSCfg.Clone(),
statsCfg: cfg.statsCfg.Clone(),
thresholdSCfg: cfg.thresholdSCfg.Clone(),
trendSCfg: cfg.trendSCfg.Clone(),
rankingSCfg: cfg.rankingSCfg.Clone(),
routeSCfg: cfg.routeSCfg.Clone(),
sureTaxCfg: cfg.sureTaxCfg.Clone(),
registrarCCfg: cfg.registrarCCfg.Clone(),
loaderCgrCfg: cfg.loaderCgrCfg.Clone(),
migratorCgrCfg: cfg.migratorCgrCfg.Clone(),
analyzerSCfg: cfg.analyzerSCfg.Clone(),
admS: cfg.admS.Clone(),
ersCfg: cfg.ersCfg.Clone(),
eesCfg: cfg.eesCfg.Clone(),
efsCfg: cfg.efsCfg.Clone(),
rateSCfg: cfg.rateSCfg.Clone(),
sipAgentCfg: cfg.sipAgentCfg.Clone(),
configSCfg: cfg.configSCfg.Clone(),
apiBanCfg: cfg.apiBanCfg.Clone(),
sentryPeerCfg: cfg.sentryPeerCfg.Clone(),
coreSCfg: cfg.coreSCfg.Clone(),
actionSCfg: cfg.actionSCfg.Clone(),
accountSCfg: cfg.accountSCfg.Clone(),
tpeSCfg: cfg.tpeSCfg.Clone(),
configDBCfg: cfg.configDBCfg.Clone(),
rldCh: make(chan string),
cacheDP: make(utils.MapStorage),
}
cln.sections = newSections(cln)
for _, sec := range cfg.sections[len(cln.sections):] {

View File

@@ -1020,6 +1020,16 @@ const CGRATES_CFG_JSON = `
},
"prometheus_agent": {
"enabled": false, // enables the prometheus agent: <true|false>
"path": "/prometheus", // endpoint for prometheus metrics
"collect_go_metrics": false, // include Go runtime metrics (memory, GC, goroutines)
"collect_process_metrics": false, // include process metrics (CPU, file descriptors)
// "stats_conns": [], // connections to StatS, empty to disable: <""|*internal|$rpc_conns_id>
// "stat_queue_ids": [] // StatQueue IDs to collect metrics from <[tenant]:ID>
},
"attributes": {
"enabled": false, // starts attribute service: <true|false>
"stats_conns": [], // connections to StatS, empty to disable: <""|*internal|$rpc_conns_id>

View File

@@ -42,6 +42,7 @@ const (
DiameterAgentJSON = "diameter_agent"
RadiusAgentJSON = "radius_agent"
HTTPAgentJSON = "http_agent"
PrometheusAgentJSON = "prometheus_agent"
AttributeSJSON = "attributes"
ResourceSJSON = "resources"
JanusAgentJSON = "janus_agent"
@@ -99,6 +100,7 @@ var (
RadiusAgentJSON: utils.RadiusAgent,
DiameterAgentJSON: utils.DiameterAgent,
HTTPAgentJSON: utils.HTTPAgent,
PrometheusAgentJSON: utils.PrometheusAgent,
LoaderSJSON: utils.LoaderS,
AnalyzerSJSON: utils.AnalyzerS,
DataDBJSON: utils.DataDB,
@@ -191,6 +193,7 @@ func newSections(cfg *CGRConfig) Sections {
cfg.fsAgentCfg,
cfg.kamAgentCfg,
cfg.diameterAgentCfg,
cfg.prometheusAgentCfg,
cfg.radiusAgentCfg,
cfg.janusAgentCfg,
&cfg.httpAgentCfg,

File diff suppressed because one or more lines are too long

View File

@@ -396,9 +396,7 @@ func TestGeneralCfgCloneSection(t *testing.T) {
DigestEqual: "",
MaxParallelConns: 50,
DefaultCaching: utils.MetaClear,
Opts: &GeneralOpts{
ExporterIDs: []*DynamicStringSliceOpt{},
},
Opts: &GeneralOpts{},
}
rcv := gnrCfg.CloneSection()

View File

@@ -95,8 +95,11 @@ type DynamicDurationPointerOpt struct {
rsVal utils.RSRParsers
}
func CloneDynamicStringSliceOpt(in []*DynamicStringSliceOpt) (cl []*DynamicStringSliceOpt) {
cl = make([]*DynamicStringSliceOpt, len(in))
func CloneDynamicStringSliceOpt(in []*DynamicStringSliceOpt) []*DynamicStringSliceOpt {
if in == nil {
return nil
}
cl := make([]*DynamicStringSliceOpt, len(in))
for i, val := range in {
cl[i] = &DynamicStringSliceOpt{
Tenant: val.Tenant,
@@ -104,7 +107,7 @@ func CloneDynamicStringSliceOpt(in []*DynamicStringSliceOpt) (cl []*DynamicStrin
Values: slices.Clone(val.Values),
}
}
return
return cl
}
func CloneDynamicStringOpt(in []*DynamicStringOpt) (cl []*DynamicStringOpt) {

132
config/prometheus.go Normal file
View File

@@ -0,0 +1,132 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package config
import (
"slices"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/utils"
)
// PrometheusAgentJsonCfg holds the unparsed prometheus_agent as found in the config file.
type PrometheusAgentJsonCfg struct {
Enabled *bool `json:"enabled"`
Path *string `json:"path"`
CollectGoMetrics *bool `json:"collect_go_metrics"`
CollectProcessMetrics *bool `json:"collect_process_metrics"`
StatSConns *[]string `json:"stats_conns"`
StatQueueIDs *[]string `json:"stat_queue_ids"`
}
// PrometheusAgentCfg represents the configuration of the Prometheus Agent.
type PrometheusAgentCfg struct {
Enabled bool
Path string
CollectGoMetrics bool
CollectProcessMetrics bool
StatSConns []string
StatQueueIDs []string
}
// Load loads the PrometheusAgent section of the configuration.
func (c *PrometheusAgentCfg) Load(ctx *context.Context, db ConfigDB, _ *CGRConfig) error {
jc := new(PrometheusAgentJsonCfg)
if err := db.GetSection(ctx, PrometheusAgentJSON, jc); err != nil {
return err
}
return c.loadFromJSONCfg(jc)
}
func (c *PrometheusAgentCfg) loadFromJSONCfg(jc *PrometheusAgentJsonCfg) error {
if jc == nil {
return nil
}
if jc.Enabled != nil {
c.Enabled = *jc.Enabled
}
if jc.Path != nil {
c.Path = *jc.Path
}
if jc.CollectGoMetrics != nil {
c.CollectGoMetrics = *jc.CollectGoMetrics
}
if jc.CollectProcessMetrics != nil {
c.CollectProcessMetrics = *jc.CollectProcessMetrics
}
if jc.StatSConns != nil {
c.StatSConns = updateBiRPCInternalConns(*jc.StatSConns, utils.MetaStats)
}
if jc.StatQueueIDs != nil {
c.StatQueueIDs = *jc.StatQueueIDs
}
return nil
}
// AsMapInterface returns the prometheus_agent config as a map[string]any.
func (c PrometheusAgentCfg) AsMapInterface() any {
return map[string]any{
utils.EnabledCfg: c.Enabled,
utils.PathCfg: c.Path,
utils.CollectGoMetricsCfg: c.CollectGoMetrics,
utils.CollectProcessMetricsCfg: c.CollectProcessMetrics,
utils.StatSConnsCfg: getBiRPCInternalJSONConns(c.StatSConns),
utils.StatQueueIDsCfg: c.StatQueueIDs,
}
}
func (PrometheusAgentCfg) SName() string { return PrometheusAgentJSON }
func (c PrometheusAgentCfg) CloneSection() Section { return c.Clone() }
// Clone returns a deep copy of PrometheusAgentCfg.
func (c PrometheusAgentCfg) Clone() *PrometheusAgentCfg {
return &PrometheusAgentCfg{
Enabled: c.Enabled,
Path: c.Path,
CollectGoMetrics: c.CollectGoMetrics,
CollectProcessMetrics: c.CollectProcessMetrics,
StatSConns: slices.Clone(c.StatSConns),
StatQueueIDs: slices.Clone(c.StatQueueIDs),
}
}
func diffPrometheusAgentJsonCfg(d *PrometheusAgentJsonCfg, v1, v2 *PrometheusAgentCfg) *PrometheusAgentJsonCfg {
if d == nil {
d = new(PrometheusAgentJsonCfg)
}
if v1.Enabled != v2.Enabled {
d.Enabled = utils.BoolPointer(v2.Enabled)
}
if v1.Path != v2.Path {
d.Path = utils.StringPointer(v2.Path)
}
if v1.CollectGoMetrics != v2.CollectGoMetrics {
d.CollectGoMetrics = utils.BoolPointer(v2.CollectGoMetrics)
}
if v1.CollectProcessMetrics != v2.CollectProcessMetrics {
d.CollectProcessMetrics = utils.BoolPointer(v2.CollectProcessMetrics)
}
if !slices.Equal(v1.StatSConns, v2.StatSConns) {
d.StatSConns = utils.SliceStringPointer(getBiRPCInternalJSONConns(v2.StatSConns))
}
if !slices.Equal(v1.StatQueueIDs, v2.StatQueueIDs) {
d.StatQueueIDs = utils.SliceStringPointer(v2.StatQueueIDs)
}
return d
}

View File

@@ -1825,6 +1825,7 @@ const (
HTTPAgent = "HTTPAgent"
SIPAgent = "SIPAgent"
JanusAgent = "JanusAgent"
PrometheusAgent = "PrometheusAgent"
)
// Google_API
@@ -2110,6 +2111,11 @@ const (
TemplatesCfg = "templates"
RequestProcessorsCfg = "request_processors"
// PrometheusAgentCfg
CollectGoMetricsCfg = "collect_go_metrics"
CollectProcessMetricsCfg = "collect_process_metrics"
StatQueueIDsCfg = "stat_queue_ids"
// RequestProcessor
RequestFieldsCfg = "request_fields"
ReplyFieldsCfg = "reply_fields"