Skip to content

Commit

Permalink
Add central logic to check obj layer initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
anjalshireesh committed Apr 29, 2024
1 parent 33deaef commit e9b4fd7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
6 changes: 2 additions & 4 deletions cmd/metrics-v3-cluster-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ func loadClusterConfigMetrics(ctx context.Context, m MetricValues, c *metricsCac
m.Set(configRRSParity, float64(clusterDriveMetrics.storageInfo.Backend.RRSCParity))

objLayer := newObjectLayerFn()
if objLayer != nil {
result := objLayer.Health(ctx, HealthOptions{})
m.Set(configWriteQuorum, float64(result.WriteQuorum))
}
result := objLayer.Health(ctx, HealthOptions{})
m.Set(configWriteQuorum, float64(result.WriteQuorum))

return nil
}
23 changes: 22 additions & 1 deletion cmd/metrics-v3-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ type MetricsGroup struct {
// Collect() call. This is protected by the `bucketsLock`.
bucketsLock sync.Mutex
buckets []string

// metricsGroupOpts - options for the metrics group.
metricsGroupOpts MetricsGroupOpts
}

// NewMetricsGroup creates a new MetricsGroup. To create a metrics group for
Expand Down Expand Up @@ -408,6 +411,14 @@ func (mg *MetricsGroup) AddExtraLabels(labels ...string) {
}
}

// requiresObjectLayer - use to add dependency on the global object layer API
// when creating the metrics group
// e.g. myMG := NewMetricsGroup(...).requiresObjectLayer()
func (mg *MetricsGroup) requiresObjectLayer(labels ...string) *MetricsGroup {
mg.metricsGroupOpts.dependGlobalObjectAPI = true
return mg
}

// IsBucketMetricsGroup - returns true if the given MetricsGroup is a bucket
// metrics group.
func (mg *MetricsGroup) IsBucketMetricsGroup() bool {
Expand All @@ -421,6 +432,10 @@ func (mg *MetricsGroup) Describe(ch chan<- *prometheus.Desc) {
}
}

func isObjectLayerInitialized() bool {
return newObjectLayerFn() != nil
}

// Collect - implements prometheus.Collector interface.
func (mg *MetricsGroup) Collect(ch chan<- prometheus.Metric) {
metricValues := newMetricValues(mg.descriptorMap)
Expand All @@ -429,7 +444,13 @@ func (mg *MetricsGroup) Collect(ch chan<- prometheus.Metric) {
if mg.IsBucketMetricsGroup() {
err = mg.bucketLoader(GlobalContext, metricValues, mg.cache, mg.buckets)
} else {
err = mg.loader(GlobalContext, metricValues, mg.cache)
canLoad := true
if mg.metricsGroupOpts.dependGlobalObjectAPI && !isObjectLayerInitialized() {
canLoad = false
}
if canLoad {
err = mg.loader(GlobalContext, metricValues, mg.cache)
}
}

// There is no way to handle errors here, so we panic the current goroutine
Expand Down
2 changes: 1 addition & 1 deletion cmd/metrics-v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func newMetricGroups(r *prometheus.Registry) *metricsV3Collection {
configStandardParityMD,
},
loadClusterConfigMetrics,
)
).requiresObjectLayer()

allMetricGroups := []*MetricsGroup{
apiRequestsMG,
Expand Down

0 comments on commit e9b4fd7

Please sign in to comment.