Skip to content

Instantly share code, notes, and snippets.

@greywolve
Created August 17, 2020 10:43
Show Gist options
  • Save greywolve/bd3b3bb1355efe297488e8c819a5cdb6 to your computer and use it in GitHub Desktop.
Save greywolve/bd3b3bb1355efe297488e8c819a5cdb6 to your computer and use it in GitHub Desktop.
GoManila Cardinality Demo
package main
import (
//"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"math/rand"
"net/http"
"time"
)
var (
randSource = rand.NewSource(time.Now().UnixNano())
randGen = rand.New(randSource)
methods = []string{"GET", "POST", "PUT", "PATCH"}
paths = []string{"/foo", "/bar", "/baz"}
status = []string{"200", "400", "500"}
httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "myapp_http_requests_total",
Help: "The total number of http requests",
},
[]string{"method", "path", "status"},
)
)
func recordMetrics() {
go func() {
for {
for i := 0; i < 1000; i++ {
m := methods[randGen.Intn(len(methods))]
p := paths[randGen.Intn(len(paths))]
s := status[randGen.Intn(len(status))]
httpRequestsTotal.With(prometheus.Labels{
"method": m,
"path": p,
"status": s,
}).Inc()
}
time.Sleep(1 * time.Second)
}
}()
}
func main() {
prometheus.MustRegister(httpRequestsTotal)
recordMetrics()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}
package main
import (
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"math/rand"
"net/http"
"time"
)
var (
randSource = rand.NewSource(time.Now().UnixNano())
randGen = rand.New(randSource)
methods = []string{"GET", "POST", "PUT", "PATCH"}
paths = []string{"/foo", "/bar", "/baz"}
status = []string{"200", "400", "500"}
httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "myapp_http_requests_total",
Help: "The total number of http requests",
},
[]string{"method", "path", "status", "user_id"},
)
)
func recordMetrics() {
go func() {
for {
for i := 0; i < 1000; i++ {
m := methods[randGen.Intn(len(methods))]
p := paths[randGen.Intn(len(paths))]
s := status[randGen.Intn(len(status))]
httpRequestsTotal.With(prometheus.Labels{
"method": m,
"path": p,
"status": s,
"user_id": uuid.New().String(),
}).Inc()
}
time.Sleep(1 * time.Second)
}
}()
}
func main() {
prometheus.MustRegister(httpRequestsTotal)
recordMetrics()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}
# my global config
global:
scrape_interval: 5s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 'promthing'
scrape_interval: 5s
static_configs:
- targets: ['localhost:2112']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment