|
| 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