|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package deployment |
| 22 | + |
| 23 | +import ( |
| 24 | + "sync" |
| 25 | + |
| 26 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 27 | + "github.com/arangodb/kube-arangodb/pkg/generated/metric_descriptions" |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util/metrics" |
| 30 | +) |
| 31 | + |
| 32 | +type ConditionsMetricsMap map[api.ConditionType]bool |
| 33 | + |
| 34 | +type ConditionsMetrics struct { |
| 35 | + lock sync.Mutex |
| 36 | + |
| 37 | + conditions ConditionsMetricsMap |
| 38 | + |
| 39 | + memberConditions map[string]ConditionsMetricsMap |
| 40 | +} |
| 41 | + |
| 42 | +func (c *ConditionsMetrics) CollectMetrics(namespace, name string, m metrics.PushMetric) { |
| 43 | + c.lock.Lock() |
| 44 | + defer c.lock.Unlock() |
| 45 | + |
| 46 | + for k, v := range c.conditions { |
| 47 | + m.Push(metric_descriptions.ArangodbOperatorDeploymentConditionsGauge(util.BoolSwitch[float64](v, 1, 0), namespace, name, string(k))) |
| 48 | + } |
| 49 | + |
| 50 | + for member := range c.memberConditions { |
| 51 | + for k, v := range c.memberConditions[member] { |
| 52 | + m.Push(metric_descriptions.ArangodbOperatorMembersConditionsGauge(util.BoolSwitch[float64](v, 1, 0), namespace, name, member, string(k))) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (c *ConditionsMetrics) RefreshDeployment(conditions api.ConditionList, types ...api.ConditionType) { |
| 58 | + c.lock.Lock() |
| 59 | + defer c.lock.Unlock() |
| 60 | + |
| 61 | + c.conditions = c.extractConditionsMap(conditions, types...) |
| 62 | +} |
| 63 | + |
| 64 | +func (c *ConditionsMetrics) RefreshMembers(members api.DeploymentStatusMemberElements, types ...api.ConditionType) { |
| 65 | + c.lock.Lock() |
| 66 | + defer c.lock.Unlock() |
| 67 | + |
| 68 | + if len(members) == 0 { |
| 69 | + c.memberConditions = nil |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + ret := make(map[string]ConditionsMetricsMap, len(members)) |
| 74 | + |
| 75 | + for _, member := range members { |
| 76 | + ret[member.Member.ID] = c.extractConditionsMap(member.Member.Conditions, types...) |
| 77 | + } |
| 78 | + |
| 79 | + c.memberConditions = ret |
| 80 | +} |
| 81 | + |
| 82 | +func (c *ConditionsMetrics) extractConditionsMap(conditions api.ConditionList, types ...api.ConditionType) ConditionsMetricsMap { |
| 83 | + if len(types) == 0 { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + ret := make(ConditionsMetricsMap, len(types)) |
| 88 | + for _, t := range types { |
| 89 | + ret[t] = conditions.IsTrue(t) |
| 90 | + } |
| 91 | + |
| 92 | + return ret |
| 93 | +} |
0 commit comments