Skip to content

Commit a36d759

Browse files
committed
Add metrics
1 parent 6a56c40 commit a36d759

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

controllers/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func patchUnstructured(ctx context.Context, r ControllerDynClient, us *unstructu
317317

318318
force := true
319319

320+
metricPatchApply.Inc()
320321
if namespace != nil {
321322
dynamicClient := r.DynamicClient().Resource(*gvr).Namespace(*namespace)
322323
_, err = dynamicClient.Patch(ctx, us.GetName(), types.ApplyPatchType, jsonData, metav1.PatchOptions{
@@ -331,5 +332,8 @@ func patchUnstructured(ctx context.Context, r ControllerDynClient, us *unstructu
331332
})
332333
}
333334

335+
if err != nil {
336+
metricPatchApplyErrs.Inc()
337+
}
334338
return err
335339
}

controllers/metrics.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2023 TV 2 DANMARK A/S
3+
4+
Licensed under the Apache License, Version 2.0 (the "License") with the
5+
following modification to section 6. Trademarks:
6+
7+
Section 6. Trademarks is deleted and replaced by the following wording:
8+
9+
6. Trademarks. This License does not grant permission to use the trademarks and
10+
trade names of TV 2 DANMARK A/S, including but not limited to the TV 2® logo and
11+
word mark, except (a) as required for reasonable and customary use in describing
12+
the origin of the Work, e.g. as described in section 4(c) of the License, and
13+
(b) to reproduce the content of the NOTICE file. Any reference to the Licensor
14+
must be made by making a reference to "TV 2 DANMARK A/S", written in capitalized
15+
letters as in this example, unless the format in which the reference is made,
16+
requires lower case letters.
17+
18+
You may not use this software except in compliance with the License and the
19+
modifications set out above.
20+
21+
You may obtain a copy of the license at:
22+
23+
http://www.apache.org/licenses/LICENSE-2.0
24+
25+
Unless required by applicable law or agreed to in writing, software
26+
distributed under the License is distributed on an "AS IS" BASIS,
27+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
See the License for the specific language governing permissions and
29+
limitations under the License.
30+
*/
31+
32+
package controllers
33+
34+
import (
35+
"github.com/prometheus/client_golang/prometheus"
36+
"sigs.k8s.io/controller-runtime/pkg/metrics"
37+
)
38+
39+
var (
40+
metricPatchApply = prometheus.NewCounter(
41+
prometheus.CounterOpts{
42+
Name: "bifrost_patchapply_total",
43+
Help: "Number of server-side patch operations",
44+
},
45+
)
46+
metricPatchApplyErrs = prometheus.NewCounter(
47+
prometheus.CounterOpts{
48+
Name: "bifrost_patchapply_errors_total",
49+
Help: "Number of server-side patch errors",
50+
},
51+
)
52+
metricTemplateErrs = prometheus.NewCounter(
53+
prometheus.CounterOpts{
54+
Name: "bifrost_template_errors_total",
55+
Help: "Number of template render errors",
56+
},
57+
)
58+
metricTemplateParseErrs = prometheus.NewCounter(
59+
prometheus.CounterOpts{
60+
Name: "bifrost_template_parse_errors_total",
61+
Help: "Number of template parse errors",
62+
},
63+
)
64+
metricResourceGet = prometheus.NewCounter(
65+
prometheus.CounterOpts{
66+
Name: "bifrost_resource_get_total",
67+
Help: "Number of resources fetched to use as dependency in templates",
68+
},
69+
)
70+
)
71+
72+
func init() {
73+
// Register custom metrics with the global prometheus registry
74+
metrics.Registry.MustRegister(metricPatchApply, metricPatchApplyErrs, metricTemplateErrs, metricResourceGet)
75+
}

controllers/templating.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func parseTemplates(resourceTemplates map[string]string) ([]*ResourceTemplateSta
131131
r.StringTemplate = tmpl
132132
r.Template, err = parseSingleTemplate(tmplKey, tmpl)
133133
if err != nil {
134+
metricTemplateParseErrs.Inc()
134135
return nil, fmt.Errorf("cannot parse template %q: %w", tmplKey, err)
135136
}
136137
r.NewResources = make([]ResourceComposite, 0)
@@ -165,6 +166,7 @@ func renderTemplates(ctx context.Context, r ControllerDynClient, parent metav1.O
165166
// FIXME: These are convenient, but we should have a better logging design, i.e. it should be possible to enable rendering errors only
166167
fmt.Printf("Template:\n%s\n", tmpl.StringTemplate)
167168
fmt.Printf("Template values:\n%+v\n", values)
169+
metricTemplateErrs.Inc()
168170
}
169171
continue
170172
}
@@ -179,6 +181,7 @@ func renderTemplates(ctx context.Context, r ControllerDynClient, parent metav1.O
179181
} else {
180182
dynamicClient = r.DynamicClient().Resource(*res.GVR)
181183
}
184+
metricResourceGet.Inc()
182185
res.Current, err = dynamicClient.Get(ctx, res.Rendered.GetName(), metav1.GetOptions{})
183186
if err != nil {
184187
logger.Error(err, "cannot get current resource", "templateName", tmpl.TemplateName, "resIdx", resIdx)

doc/installing.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ GatewayClassBlueprints](../blueprints/README.md).
4343

4444
## Metrics and Observability
4545

46-
The controller provides [standard controller
46+
The controller provides the following Prometheus/OpenMetrics metrics:
47+
48+
| Metric | Type | Description |
49+
| ------------- | ------------- |
50+
| `bifrost_patchapply_total` | Counter | Number of server-side patch operations |
51+
| `bifrost_patchapply_errors_total` | Counter | Number of server-side patch errors |
52+
| `bifrost_template_errors_total` | Counter | Number of template render errors |
53+
| `bifrost_template_parse_errors_total` | Counter | Number of template parse errors |
54+
| `bifrost_resource_get_total` | Counter | Number of resources fetched to use as dependency in templates |
55+
56+
Additionally the controller provides [standard controller
4757
metrics](https://book.kubebuilder.io/reference/metrics-reference.html)
4858
and the Helm chart provides a
4959
[ServiceMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor)
50-
for integration with systems that understand these.
60+
for integration with systems that understand this CRD.
5161

5262
Observability of Gateway-API resources is possible through [Custom
5363
Resource State

0 commit comments

Comments
 (0)