Skip to content

Commit fbf3bb6

Browse files
committed
OCPBUGS-14891: Initialize the most likely labels for the metrics
1 parent 8f17c80 commit fbf3bb6

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This example app serves as an example of how one can easily instrument HTTP hand
44

55
Usage is simple, on any request to `/` the request will result in a `200` response code. This increments the counter for this response code. Similarly the `/err` endpoint will result in a `404` response code, therefore increments that respective counter. Duration metrics are also exposed for any request to `/`.
66

7-
A Docker image is available at: `ghcr.io/rhobs/prometheus-example-app:0.3.0`
7+
A Docker image is available at: `ghcr.io/rhobs/prometheus-example-app`
88

99
## Deploying in a Kubernetes cluster
1010

@@ -33,7 +33,7 @@ The following metrics are exposed:
3333

3434
The sample output of the `/metric` endpoint after 5 incoming HTTP requests shown below.
3535

36-
Note: with no initial incoming request, only `version` metric is reported.
36+
Note: In addition to `version`, the other metrics are initialized with the most likely labels.
3737

3838
```
3939
# HELP http_request_duration_seconds Duration of all HTTP requests

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.4.1
1+
v0.4.2

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"golang.org/x/net/http2/h2c"
1313
)
1414

15+
const foundHandlerName = "found"
16+
1517
var (
1618
appVersion string
1719
version = prometheus.NewGauge(prometheus.GaugeOpts{
@@ -47,6 +49,11 @@ func main() {
4749
r.MustRegister(httpRequestDuration)
4850
r.MustRegister(version)
4951

52+
// Initialize the most likely labels.
53+
httpRequestDuration.WithLabelValues("200", foundHandlerName, "get")
54+
httpRequestsTotal.WithLabelValues("200", "get")
55+
httpRequestsTotal.WithLabelValues("404", "get")
56+
5057
foundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5158
w.WriteHeader(http.StatusOK)
5259
w.Write([]byte("Hello from example application."))
@@ -56,7 +63,7 @@ func main() {
5663
})
5764

5865
foundChain := promhttp.InstrumentHandlerDuration(
59-
httpRequestDuration.MustCurryWith(prometheus.Labels{"handler": "found"}),
66+
httpRequestDuration.MustCurryWith(prometheus.Labels{"handler": foundHandlerName}),
6067
promhttp.InstrumentHandlerCounter(httpRequestsTotal, foundHandler),
6168
)
6269

manifests/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ spec:
1616
spec:
1717
containers:
1818
- name: prometheus-example-app
19-
image: ghcr.io/rhobs/prometheus-example-app:0.3.0
19+
image: ghcr.io/rhobs/prometheus-example-app:latest
2020
ports:
2121
- name: web
2222
containerPort: 8080

0 commit comments

Comments
 (0)