generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
315 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
) | ||
|
||
const ( | ||
// availabilityZonesUsedMetricKey name of the availability zones used metric. | ||
availabilityZonesUsedMetricKey = "nats_manager_availability_zones_used" | ||
// availabilityZonesUsedHelp help text for the availability zones used metric. | ||
availabilityZonesUsedHelp = "The number of availability zones used used by NATS Pods." | ||
|
||
// clusterSizeMetricKey name of the cluster size metric. | ||
clusterSizeMetricKey = "nats_manager_cr_cluster_size" | ||
// clusterSizeMetricHelp help text for the cluster size metric. | ||
clusterSizeMetricHelp = "The cluster size configured in the NATS CR." | ||
) | ||
|
||
// Perform a compile time check. | ||
var _ Collector = &PrometheusCollector{} | ||
|
||
//go:generate go run github.com/vektra/mockery/v2 --name=Collector --outpkg=mocks --case=underscore | ||
type Collector interface { | ||
RegisterMetrics() | ||
RecordAvailabilityZonesUsedMetric(int) | ||
RecordClusterSizeMetric(int) | ||
ResetAvailabilityZonesUsedMetric() | ||
ResetClusterSizeMetric() | ||
} | ||
|
||
// PrometheusCollector implements the prometheus.Collector interface. | ||
type PrometheusCollector struct { | ||
availabilityZonesUsed *prometheus.GaugeVec | ||
clusterSize *prometheus.GaugeVec | ||
} | ||
|
||
// NewPrometheusCollector a new instance of Collector. | ||
func NewPrometheusCollector() Collector { | ||
return &PrometheusCollector{ | ||
availabilityZonesUsed: prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: availabilityZonesUsedMetricKey, | ||
Help: availabilityZonesUsedHelp, | ||
}, | ||
nil, | ||
), | ||
clusterSize: prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: clusterSizeMetricKey, | ||
Help: clusterSizeMetricHelp, | ||
}, | ||
nil, | ||
), | ||
} | ||
} | ||
|
||
// Describe implements the prometheus.Collector interface Describe method. | ||
func (p *PrometheusCollector) Describe(ch chan<- *prometheus.Desc) { | ||
p.availabilityZonesUsed.Describe(ch) | ||
p.clusterSize.Describe(ch) | ||
} | ||
|
||
// Collect implements the prometheus.Collector interface Collect method. | ||
func (p *PrometheusCollector) Collect(ch chan<- prometheus.Metric) { | ||
p.availabilityZonesUsed.Collect(ch) | ||
p.clusterSize.Collect(ch) | ||
} | ||
|
||
// RegisterMetrics registers the metrics. | ||
func (p *PrometheusCollector) RegisterMetrics() { | ||
metrics.Registry.MustRegister(p.availabilityZonesUsed) | ||
metrics.Registry.MustRegister(p.clusterSize) | ||
} | ||
|
||
func (p *PrometheusCollector) RecordAvailabilityZonesUsedMetric(availabilityZonesUsed int) { | ||
p.availabilityZonesUsed.WithLabelValues().Set(float64(availabilityZonesUsed)) | ||
} | ||
|
||
func (p *PrometheusCollector) RecordClusterSizeMetric(clusterSize int) { | ||
p.clusterSize.WithLabelValues().Set(float64(clusterSize)) | ||
} | ||
|
||
func (p *PrometheusCollector) ResetAvailabilityZonesUsedMetric() { | ||
p.availabilityZonesUsed.Reset() | ||
} | ||
|
||
func (p *PrometheusCollector) ResetClusterSizeMetric() { | ||
p.clusterSize.Reset() | ||
} |
Oops, something went wrong.