feat: use random counter
This commit is contained in:
parent
c6de46b8dc
commit
46a2c1c8f3
@ -1,4 +0,0 @@
|
|||||||
apiVersion: v1
|
|
||||||
kind: Namespace
|
|
||||||
metadata:
|
|
||||||
name: metrics-counter
|
|
45
main.go
45
main.go
@ -1,11 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -13,32 +14,26 @@ import (
|
|||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var counter = prometheus.NewCounterVec(
|
||||||
counter = prometheus.NewCounterVec(
|
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "custom_counter_total",
|
Name: "random_counter_total",
|
||||||
Help: "A counter that increments every 30 seconds",
|
Help: "A counter that increments randomly from 1 to 30 seconds",
|
||||||
},
|
},
|
||||||
[]string{},
|
[]string{},
|
||||||
)
|
)
|
||||||
counterValue uint64
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Register Prometheus metrics
|
// Register Prometheus metrics
|
||||||
prometheus.MustRegister(counter)
|
prometheus.MustRegister(counter)
|
||||||
|
|
||||||
// Start background counter incrementer
|
|
||||||
go incrementCounterEvery30s()
|
|
||||||
|
|
||||||
// Set up HTTP server
|
// Set up HTTP server
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
server := &http.Server{Addr: ":8080"}
|
server := &http.Server{Addr: ":8080"}
|
||||||
|
|
||||||
// Handle graceful shutdown
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
stop := make(chan os.Signal, 1)
|
defer stop()
|
||||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
|
||||||
|
|
||||||
|
go startCounting(ctx)
|
||||||
go func() {
|
go func() {
|
||||||
log.Println("Starting server on :8080")
|
log.Println("Starting server on :8080")
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
@ -46,17 +41,23 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
<-stop
|
<-ctx.Done()
|
||||||
log.Println("\nShutting down server...")
|
log.Println("Shutdown the server")
|
||||||
|
// Give one more second for gracefully shutdown
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
server.Shutdown(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func incrementCounterEvery30s() {
|
func startCounting(ctx context.Context) {
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
for {
|
||||||
defer ticker.Stop()
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
for range ticker.C {
|
return
|
||||||
newVal := atomic.AddUint64(&counterValue, 1)
|
default:
|
||||||
counter.WithLabelValues().Add(1)
|
counter.WithLabelValues().Add(1)
|
||||||
log.Printf("Counter incremented to %d", newVal)
|
sleepDuration := time.Duration(rand.Intn(29) + 1)
|
||||||
|
time.Sleep(sleepDuration * time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user