Skip to content

Commit 61074e7

Browse files
authored
Devenv: Adds docker for prometheus with high cardinality labels. (grafana#91035)
Signed-off-by: bergquist <carl.bergquist@gmail.com>
1 parent 3f88188 commit 61074e7

File tree

8 files changed

+200
-0
lines changed

8 files changed

+200
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
/devenv/docker/blocks/postgres_tests/ @grafana/oss-big-tent
210210
/devenv/docker/blocks/prometheus/ @grafana/observability-metrics
211211
/devenv/docker/blocks/prometheus_random_data/ @grafana/observability-metrics
212+
/devenv/docker/blocks/prometheus_high_card/ @grafana/observability-metrics
212213
/devenv/docker/blocks/pyroscope/ @grafana/observability-traces-and-profiling
213214
/devenv/docker/blocks/redis/ @bergquist
214215
/devenv/docker/blocks/sensugo/ @grafana/grafana-backend-group

devenv/docker/blocks/prometheus/docker-compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@
3737
build: docker/blocks/prometheus_random_data
3838
ports:
3939
- "8081:8080"
40+
41+
prometheus-high-card:
42+
build: docker/blocks/prometheus_high_card
43+
ports:
44+
- "9111:9111"

devenv/docker/blocks/prometheus/prometheus.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ scrape_configs:
3838
static_configs:
3939
- targets: ['prometheus-random-data:8080']
4040

41+
- job_name: 'prometheus-high-card'
42+
static_configs:
43+
- targets: ['prometheus-high-card:9111']
44+
4145
- job_name: 'mysql'
4246
static_configs:
4347
- targets: ['mysql-exporter:9104']
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:latest AS builder
2+
3+
ADD main.go /
4+
ADD go.mod /
5+
ADD go.sum /
6+
WORKDIR /
7+
8+
RUN go mod download
9+
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
10+
11+
FROM scratch
12+
WORKDIR /
13+
EXPOSE 9111
14+
COPY --from=builder /main /main
15+
ENTRYPOINT ["/main"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
prometheus_high_card:
2+
build: docker/blocks/prometheus_high_card
3+
ports:
4+
- "3012:3012"
5+
extra_hosts:
6+
- "host.docker.internal:host-gateway"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module high-card
2+
3+
go 1.22.4
4+
5+
require (
6+
github.com/prometheus/client_golang v1.20.2
7+
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
8+
)
9+
10+
require (
11+
github.com/beorn7/perks v1.0.1 // indirect
12+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
13+
github.com/klauspost/compress v1.17.9 // indirect
14+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
15+
github.com/prometheus/client_model v0.6.1 // indirect
16+
github.com/prometheus/common v0.55.0 // indirect
17+
github.com/prometheus/procfs v0.15.1 // indirect
18+
golang.org/x/sys v0.22.0 // indirect
19+
google.golang.org/protobuf v1.34.2 // indirect
20+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
2+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
3+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
4+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
5+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7+
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
8+
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
9+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
10+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
11+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
12+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
13+
github.com/prometheus/client_golang v1.20.2 h1:5ctymQzZlyOON1666svgwn3s6IKWgfbjsejTMiXIyjg=
14+
github.com/prometheus/client_golang v1.20.2/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
15+
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
16+
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
17+
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
18+
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
19+
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
20+
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
21+
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
22+
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
23+
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
24+
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
25+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
26+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"strconv"
8+
"time"
9+
10+
"github.com/prometheus/client_golang/prometheus"
11+
"github.com/prometheus/client_golang/prometheus/promauto"
12+
"github.com/prometheus/client_golang/prometheus/promhttp"
13+
"golang.org/x/exp/rand"
14+
)
15+
16+
func randomValues(max int) func() (string, bool) {
17+
i := 0
18+
return func() (string, bool) {
19+
i++
20+
return strconv.Itoa(i), i < max+1
21+
}
22+
}
23+
24+
func staticList(input []string) func() string {
25+
return func() string {
26+
i := rand.Intn(len(input))
27+
28+
return input[i]
29+
}
30+
}
31+
32+
type dimension struct {
33+
label string
34+
getNextValue func() string
35+
}
36+
37+
func main() {
38+
39+
fakeMetrics := []dimension{
40+
{
41+
label: "cluster",
42+
getNextValue: staticList([]string{"prod-uk1", "prod-eu1", "prod-uk2", "prod-eu2", "prod-uk3", "prod-eu3", "prod-uk4", "prod-eu4", "prod-uk5", "prod-eu5"}),
43+
},
44+
{
45+
label: "namespace",
46+
getNextValue: staticList([]string{"default", "kube-api", "kube-system", "kube-public", "kube-node-lease", "kube-ingress", "kube-logging", "kube-metrics", "kube-monitoring", "kube-network", "kube-storage"}),
47+
},
48+
{
49+
label: "pod",
50+
getNextValue: staticList([]string{"default"}),
51+
},
52+
{
53+
label: "container",
54+
getNextValue: staticList([]string{"container"}),
55+
},
56+
{
57+
label: "method",
58+
getNextValue: staticList([]string{"GET", "POST", "DELETE", "PUT", "PATCH"}),
59+
},
60+
{
61+
label: "address",
62+
getNextValue: staticList([]string{"/", "/api", "/api/dashboard", "/api/dashboard/:uid", "/api/dashboard/:uid/overview", "/api/dashboard/:uid/overview/:id", "/api/dashboard/:uid/overview/:id/summary", "/api/dashboard/:uid/overview/:id/summary/:type", "/api/dashboard/:uid/overview/:id/summary/:type/:subtype", "/api/dashboard/:uid/overview/:id/summary/:type/:subtype/:id"}),
63+
},
64+
{
65+
label: "extra_label_name1",
66+
getNextValue: staticList([]string{"default"}),
67+
},
68+
{
69+
label: "extra_label_name2",
70+
getNextValue: staticList([]string{"default"}),
71+
},
72+
{
73+
label: "extra_label_name3",
74+
getNextValue: staticList([]string{"default"}),
75+
},
76+
{
77+
label: "extra_label_name4",
78+
getNextValue: staticList([]string{"default"}),
79+
},
80+
{
81+
label: "extra_label_name5",
82+
getNextValue: staticList([]string{"default"}),
83+
},
84+
{
85+
label: "extra_label_name6",
86+
getNextValue: staticList([]string{"default"}),
87+
},
88+
}
89+
90+
dimensions := []string{}
91+
for _, dim := range fakeMetrics {
92+
dimensions = append(dimensions, dim.label)
93+
}
94+
95+
opsProcessed := promauto.NewCounterVec(prometheus.CounterOpts{
96+
Name: "fakedata_highcard_http_requests_total",
97+
Help: "a high cardinality counter",
98+
}, dimensions)
99+
100+
http.Handle("/metrics", promhttp.Handler())
101+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
102+
w.WriteHeader(http.StatusOK)
103+
w.Write([]byte("Hello, is it me you're looking for?"))
104+
})
105+
106+
go func() {
107+
for {
108+
labels := []string{}
109+
for _, dim := range fakeMetrics {
110+
value := dim.getNextValue()
111+
labels = append(labels, value)
112+
}
113+
114+
opsProcessed.WithLabelValues(labels...).Inc()
115+
116+
time.Sleep(time.Millisecond)
117+
}
118+
}()
119+
120+
fmt.Printf("Server started at :9111\n")
121+
122+
log.Fatal(http.ListenAndServe(":9111", nil))
123+
}

0 commit comments

Comments
 (0)