|
| 1 | +/* |
| 2 | +Copyright 2025 Tim Ebert. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + coordinationv1 "k8s.io/api/coordination/v1" |
| 22 | + "k8s.io/utils/ptr" |
| 23 | + |
| 24 | + shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1" |
| 25 | + "github.com/timebertt/kubernetes-controller-sharding/pkg/metrics/exporter" |
| 26 | + "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases" |
| 27 | +) |
| 28 | + |
| 29 | +var ShardExporter = exporter.Exporter[*coordinationv1.Lease, *coordinationv1.LeaseList]{ |
| 30 | + Namespace: Namespace, |
| 31 | + Subsystem: "shard", |
| 32 | + |
| 33 | + StaticLabelKeys: []string{"namespace", "shard", "controllerring"}, |
| 34 | + GenerateStaticLabelValues: func(lease *coordinationv1.Lease) []string { |
| 35 | + return []string{lease.Namespace, lease.Name, lease.Labels[shardingv1alpha1.LabelControllerRing]} |
| 36 | + }, |
| 37 | + |
| 38 | + Metrics: []exporter.Metric[*coordinationv1.Lease]{ |
| 39 | + { |
| 40 | + Name: "info", |
| 41 | + Help: "Information about a shard", |
| 42 | + LabelKeys: []string{"uid"}, |
| 43 | + |
| 44 | + Generate: func(desc *prometheus.Desc, lease *coordinationv1.Lease, staticLabelValues []string, ch chan<- prometheus.Metric) { |
| 45 | + ch <- prometheus.MustNewConstMetric( |
| 46 | + desc, |
| 47 | + prometheus.GaugeValue, |
| 48 | + 1, |
| 49 | + append(staticLabelValues, string(lease.UID))..., |
| 50 | + ) |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "state", |
| 55 | + Help: "The shard's current state observed by the shardlease controller", |
| 56 | + LabelKeys: []string{"state"}, |
| 57 | + |
| 58 | + Generate: exporter.GenerateStateSet[*coordinationv1.Lease]( |
| 59 | + exporter.KnownStatesStringer(leases.KnownShardStates), ptr.To(leases.Unknown.String()), |
| 60 | + func(lease *coordinationv1.Lease) string { |
| 61 | + return lease.Labels[shardingv1alpha1.LabelState] |
| 62 | + }, |
| 63 | + ), |
| 64 | + }, |
| 65 | + }, |
| 66 | +} |
0 commit comments