Skip to content

Commit 18ab40c

Browse files
authored
[Metrics] Add expose function (#1043)
1 parent bca84f3 commit 18ab40c

14 files changed

+83
-18
lines changed

internal/metrics.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ type Alerting struct {
112112
}
113113

114114
type Label struct {
115-
Key string `json:"key" yaml:"key"`
116-
Description string `json:"description" yaml:"description"`
115+
Key string `json:"key" yaml:"key"`
116+
Description string `json:"description" yaml:"description"`
117+
Type *string `json:"type" yaml:"type"`
117118
}
118119

119120
func GenerateMetricsDocumentation(root string, in MetricsDoc) error {
@@ -323,12 +324,32 @@ func generateMetricsGO(root string, in MetricsDoc) error {
323324
}
324325
}
325326

327+
var keys []string
328+
var params []string
329+
330+
params = append(params, "value float64")
331+
keys = append(keys, "value")
332+
333+
for _, label := range details.Labels {
334+
k := strings.ToLower(label.Key)
335+
keys = append(keys, k)
336+
337+
if t := label.Type; t != nil {
338+
params = append(params, fmt.Sprintf("%s %s", k, *t))
339+
} else {
340+
params = append(params, fmt.Sprintf("%s string", k))
341+
}
342+
}
343+
326344
if err := i.Execute(out, map[string]interface{}{
327345
"name": mname,
328346
"fname": strings.Join(fnameParts, ""),
329347
"ename": strings.Join(tparts, ""),
330348
"shortDescription": details.ShortDescription,
331349
"labels": generateLabels(details.Labels),
350+
"type": details.Type,
351+
"fparams": strings.Join(params, ", "),
352+
"fkeys": strings.Join(keys, ", "),
332353
}); err != nil {
333354
return err
334355
}

internal/metrics.item.go.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ func init() {
3232

3333
func {{ .ename }}() metrics.Description {
3434
return {{ .fname }}
35+
}
36+
37+
func {{ .ename }}{{ .type }}({{ .fparams }}) metrics.Metric {
38+
return {{ .ename }}().Gauge({{ .fkeys }})
3539
}

pkg/deployment/agency/cache.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,29 @@ func (h health) Leader() (driver.Connection, bool) {
5757

5858
func (h health) CollectMetrics(m metrics.PushMetric) {
5959
if err := h.Serving(); err == nil {
60-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheServing().Gauge(1, h.namespace, h.name))
60+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheServingGauge(1, h.namespace, h.name))
6161
} else {
62-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheServing().Gauge(0, h.namespace, h.name))
62+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheServingGauge(0, h.namespace, h.name))
6363
}
6464

6565
if err := h.Healthy(); err == nil {
66-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthy().Gauge(1, h.namespace, h.name))
66+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthyGauge(1, h.namespace, h.name))
6767
} else {
68-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthy().Gauge(0, h.namespace, h.name))
68+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthyGauge(0, h.namespace, h.name))
6969
}
7070

7171
for _, name := range h.names {
7272
if i, ok := h.commitIndexes[name]; ok {
73-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheMemberServing().Gauge(1, h.namespace, h.name, name),
74-
metric_descriptions.ArangodbOperatorAgencyCacheMemberCommitOffset().Gauge(float64(i), h.namespace, h.name, name))
73+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheMemberServingGauge(1, h.namespace, h.name, name),
74+
metric_descriptions.ArangodbOperatorAgencyCacheMemberCommitOffsetGauge(float64(i), h.namespace, h.name, name))
7575
} else {
76-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheMemberServing().Gauge(0, h.namespace, h.name, name),
77-
metric_descriptions.ArangodbOperatorAgencyCacheMemberCommitOffset().Gauge(-1, h.namespace, h.name, name))
76+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheMemberServingGauge(0, h.namespace, h.name, name),
77+
metric_descriptions.ArangodbOperatorAgencyCacheMemberCommitOffsetGauge(-1, h.namespace, h.name, name))
7878
}
7979
}
8080

8181
for k, l := range h.election {
82-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheLeaders().Gauge(float64(l), h.namespace, h.name, k))
82+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheLeadersGauge(float64(l), h.namespace, h.name, k))
8383
}
8484
}
8585

pkg/deployment/metrics.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,20 @@ func (i *inventory) Add(d *Deployment) {
157157
}
158158

159159
func (d *Deployment) CollectMetrics(m metrics.PushMetric) {
160-
m.Push(metric_descriptions.ArangodbOperatorAgencyErrors().Gauge(float64(d.metrics.agency.errors), d.namespace, d.name))
161-
m.Push(metric_descriptions.ArangodbOperatorAgencyFetches().Gauge(float64(d.metrics.agency.fetches), d.namespace, d.name))
162-
m.Push(metric_descriptions.ArangodbOperatorAgencyIndex().Gauge(float64(d.metrics.agency.index), d.namespace, d.name))
160+
m.Push(metric_descriptions.ArangodbOperatorAgencyErrorsCount(float64(d.metrics.agency.errors), d.namespace, d.name))
161+
m.Push(metric_descriptions.ArangodbOperatorAgencyFetchesCount(float64(d.metrics.agency.fetches), d.namespace, d.name))
162+
m.Push(metric_descriptions.ArangodbOperatorAgencyIndexGauge(float64(d.metrics.agency.index), d.namespace, d.name))
163163

164164
if c := d.agencyCache; c != nil {
165-
m.Push(metric_descriptions.ArangodbOperatorAgencyCachePresent().Gauge(1, d.namespace, d.name))
165+
m.Push(metric_descriptions.ArangodbOperatorAgencyCachePresentGauge(1, d.namespace, d.name))
166166
if h, ok := c.Health(); ok {
167-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthPresent().Gauge(1, d.namespace, d.name))
167+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthPresentGauge(1, d.namespace, d.name))
168168

169169
h.CollectMetrics(m)
170170
} else {
171-
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthPresent().Gauge(0, d.namespace, d.name))
171+
m.Push(metric_descriptions.ArangodbOperatorAgencyCacheHealthPresentGauge(0, d.namespace, d.name))
172172
}
173173
} else {
174-
m.Push(metric_descriptions.ArangodbOperatorAgencyCachePresent().Gauge(0, d.namespace, d.name))
174+
m.Push(metric_descriptions.ArangodbOperatorAgencyCachePresentGauge(0, d.namespace, d.name))
175175
}
176176
}

pkg/generated/metric_descriptions/arangodb_operator_agency_cache_health_present.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generated/metric_descriptions/arangodb_operator_agency_cache_healthy.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generated/metric_descriptions/arangodb_operator_agency_cache_leaders.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generated/metric_descriptions/arangodb_operator_agency_cache_member_commit_offset.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generated/metric_descriptions/arangodb_operator_agency_cache_member_serving.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generated/metric_descriptions/arangodb_operator_agency_cache_present.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)