Skip to content

Commit

Permalink
Refactor metrics code
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayangi committed Mar 18, 2024
1 parent 7905520 commit 64893bb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 27 deletions.
7 changes: 3 additions & 4 deletions adapter/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,9 @@ type tracing struct {

// Metrics defines the configuration for metrics collection.
type Metrics struct {
Enabled bool
Type string
Port int32
CollectionInterval int32
Enabled bool
Type string
Port int32
}

type analyticsAdapter struct {
Expand Down
11 changes: 5 additions & 6 deletions adapter/internal/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func InitOperator(metricsConfig config.Metrics) {

if metricsConfig.Enabled {
options.Metrics.BindAddress = fmt.Sprintf(":%d", metricsConfig.Port)
// Register the metrics collector
if strings.EqualFold(metricsConfig.Type, metrics.PrometheusMetricType) {
loggers.LoggerAPKOperator.Info("Registering Prometheus metrics collector.")
metrics.RegisterPrometheusCollector()
}
} else {
options.Metrics.BindAddress = "0"
}
Expand Down Expand Up @@ -145,12 +150,6 @@ func InitOperator(metricsConfig config.Metrics) {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2603, logging.BLOCKER, "Unable to set up ready check: %v", err))
}

// Register the metrics collector
if metricsConfig.Enabled && strings.EqualFold(metricsConfig.Type, metrics.PrometheusMetricType) {
loggers.LoggerAPKOperator.Info("Registering Prometheus metrics collector.")
go metrics.RegisterPrometheusCollector()
}

go synchronizer.HandleAPILifeCycleEvents(&ch, &successChannel)
go synchronizer.HandleGatewayLifeCycleEvents(&gatewaych)
if config.ReadConfigs().PartitionServer.Enabled {
Expand Down
7 changes: 3 additions & 4 deletions common-controller/internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ type webServer struct {

// Metrics defines the configuration for metrics collection.
type Metrics struct {
Enabled bool
Type string
Port int32
CollectionInterval int32
Enabled bool
Type string
Port int32
}

type database struct {
Expand Down
12 changes: 6 additions & 6 deletions common-controller/internal/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func InitOperator(metricsConfig config.Metrics) {

if metricsConfig.Enabled {
options.Metrics.BindAddress = fmt.Sprintf(":%d", metricsConfig.Port)

// Register the metrics collector
if strings.EqualFold(metricsConfig.Type, metrics.PrometheusMetricType) {
loggers.LoggerAPKOperator.Info("Registering Prometheus metrics collector.")
metrics.RegisterPrometheusCollector()
}
} else {
options.Metrics.BindAddress = "0"
}
Expand Down Expand Up @@ -208,12 +214,6 @@ func InitOperator(metricsConfig config.Metrics) {
}()
}

// Register the metrics collector
if metricsConfig.Enabled && strings.EqualFold(metricsConfig.Type, metrics.PrometheusMetricType) {
loggers.LoggerAPKOperator.Info("Registering Prometheus metrics collector.")
go metrics.RegisterPrometheusCollector()
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2604, logging.BLOCKER, "Problem running manager: %v", err))
Expand Down
7 changes: 0 additions & 7 deletions common-go-libs/pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ var (

// Collector contains the descriptions of the custom metrics exposed
type Collector struct {
internalRouteCount *prometheus.Desc
hostInfo *prometheus.Desc
availableCPUs *prometheus.Desc
freePhysicalMemory *prometheus.Desc
Expand All @@ -52,11 +51,6 @@ type Collector struct {
// CustomMetricsCollector contains the descriptions of the custom metrics exposed
func CustomMetricsCollector() *Collector {
return &Collector{
internalRouteCount: prometheus.NewDesc(
"internal_route_count",
"Number of internal routes created.",
nil, nil,
),
hostInfo: prometheus.NewDesc(
"host_info",
"Host Info",
Expand Down Expand Up @@ -98,7 +92,6 @@ func CustomMetricsCollector() *Collector {
// Describe sends all the descriptors of the metrics collected by this Collector
// to the provided channel.
func (collector *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.internalRouteCount
ch <- collector.hostInfo
ch <- collector.availableCPUs
ch <- collector.freePhysicalMemory
Expand Down

0 comments on commit 64893bb

Please sign in to comment.