|
17 | 17 | // |
18 | 18 | // Copyright holder is ArangoDB GmbH, Cologne, Germany |
19 | 19 | // |
20 | | -// Author Ewout Prangsma |
21 | | -// |
22 | 20 |
|
23 | 21 | package deployment |
24 | 22 |
|
| 23 | +import ( |
| 24 | + "sync" |
| 25 | + |
| 26 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 27 | + "github.com/arangodb/kube-arangodb/pkg/util/metrics" |
| 28 | + "github.com/prometheus/client_golang/prometheus" |
| 29 | +) |
| 30 | + |
25 | 31 | const ( |
26 | 32 | // Component name for metrics of this package |
27 | 33 | metricsComponent = "deployment" |
28 | 34 | ) |
| 35 | + |
| 36 | +func init() { |
| 37 | + localInventory = inventory{ |
| 38 | + deployments: map[string]map[string]*Deployment{}, |
| 39 | + deploymentsMetric: metrics.NewDescription("arangodb_operator_deployments", "Number of active deployments", []string{"namespace", "deployment"}, nil), |
| 40 | + deploymentMetricsMembersMetric: metrics.NewDescription("arango_operator_deployment_members", "List of members", []string{"namespace", "deployment", "role", "id"}, nil), |
| 41 | + deploymentAgencyStateMetric: metrics.NewDescription("arango_operator_deployment_agency_state", "Reachability of agency", []string{"namespace", "deployment"}, nil), |
| 42 | + deploymentShardLeadersMetric: metrics.NewDescription("arango_operator_deployment_shard_leaders", "Deployment leader shards distribution", []string{"namespace", "deployment", "database", "collection", "shard", "server"}, nil), |
| 43 | + deploymentShardsMetric: metrics.NewDescription("arango_operator_deployment_shards", "Deployment shards distribution", []string{"namespace", "deployment", "database", "collection", "shard", "server"}, nil), |
| 44 | + } |
| 45 | + |
| 46 | + prometheus.MustRegister(&localInventory) |
| 47 | +} |
| 48 | + |
| 49 | +var localInventory inventory |
| 50 | + |
| 51 | +var _ prometheus.Collector = &inventory{} |
| 52 | + |
| 53 | +type inventory struct { |
| 54 | + lock sync.Mutex |
| 55 | + deployments map[string]map[string]*Deployment |
| 56 | + |
| 57 | + deploymentsMetric, deploymentMetricsMembersMetric, deploymentAgencyStateMetric, deploymentShardsMetric, deploymentShardLeadersMetric metrics.Description |
| 58 | +} |
| 59 | + |
| 60 | +func (i *inventory) Describe(descs chan<- *prometheus.Desc) { |
| 61 | + i.lock.Lock() |
| 62 | + defer i.lock.Unlock() |
| 63 | + |
| 64 | + metrics.NewPushDescription(descs).Push(i.deploymentsMetric, i.deploymentMetricsMembersMetric, i.deploymentAgencyStateMetric, i.deploymentShardLeadersMetric, i.deploymentShardsMetric) |
| 65 | +} |
| 66 | + |
| 67 | +func (i *inventory) Collect(m chan<- prometheus.Metric) { |
| 68 | + i.lock.Lock() |
| 69 | + defer i.lock.Unlock() |
| 70 | + |
| 71 | + p := metrics.NewPushMetric(m) |
| 72 | + for _, deployments := range i.deployments { |
| 73 | + for _, deployment := range deployments { |
| 74 | + p.Push(i.deploymentsMetric.Gauge(1, deployment.GetNamespace(), deployment.GetName())) |
| 75 | + |
| 76 | + spec := deployment.GetSpec() |
| 77 | + status, _ := deployment.GetStatus() |
| 78 | + |
| 79 | + for _, member := range status.Members.AsList() { |
| 80 | + p.Push(i.deploymentMetricsMembersMetric.Gauge(1, deployment.GetNamespace(), deployment.GetName(), member.Group.AsRole(), member.Member.ID)) |
| 81 | + } |
| 82 | + |
| 83 | + if spec.Mode.Get().HasAgents() { |
| 84 | + agency, agencyOk := deployment.GetAgencyCache() |
| 85 | + if !agencyOk { |
| 86 | + p.Push(i.deploymentAgencyStateMetric.Gauge(0, deployment.GetNamespace(), deployment.GetName())) |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + p.Push(i.deploymentAgencyStateMetric.Gauge(1, deployment.GetNamespace(), deployment.GetName())) |
| 91 | + |
| 92 | + if spec.Mode.Get() == api.DeploymentModeCluster { |
| 93 | + for db, collections := range agency.Current.Collections { |
| 94 | + for collection, shards := range collections { |
| 95 | + for shard, details := range shards { |
| 96 | + for id, server := range details.Servers { |
| 97 | + name := "UNKNOWN" |
| 98 | + if _, ok := agency.Plan.Collections[db]; ok { |
| 99 | + if _, ok := agency.Plan.Collections[db][collection]; ok { |
| 100 | + name = agency.Plan.Collections[db][collection].GetName(name) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + m := []string{ |
| 105 | + deployment.GetNamespace(), |
| 106 | + deployment.GetName(), |
| 107 | + db, |
| 108 | + name, |
| 109 | + shard, |
| 110 | + server, |
| 111 | + } |
| 112 | + |
| 113 | + if id == 0 { |
| 114 | + p.Push(i.deploymentShardLeadersMetric.Gauge(1, m...)) |
| 115 | + } |
| 116 | + p.Push(i.deploymentShardsMetric.Gauge(1, m...)) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func (i *inventory) Add(d *Deployment) { |
| 128 | + i.lock.Lock() |
| 129 | + defer i.lock.Unlock() |
| 130 | + |
| 131 | + name, namespace := d.GetName(), d.GetNamespace() |
| 132 | + |
| 133 | + if _, ok := i.deployments[namespace]; !ok { |
| 134 | + i.deployments[namespace] = map[string]*Deployment{} |
| 135 | + } |
| 136 | + |
| 137 | + i.deployments[namespace][name] = d |
| 138 | +} |
0 commit comments