forked from abhirockzz/kubernetes-keda-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (52 loc) · 1.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/go-redis/redis"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var httpRequestsCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "http_requests",
Help: "number of http requests",
})
const redisCounterName = "access_count"
var once sync.Once
var client *redis.Client
func init() {
host := os.Getenv("REDIS_HOST")
if host == "" {
fmt.Printf("REDIS_HOST env var absent!")
os.Exit(1)
}
port := os.Getenv("REDIS_PORT")
if port == "" {
fmt.Printf("REDIS_PORT env var absent!")
os.Exit(1)
}
client = redis.NewClient(&redis.Options{Addr: host + ":" + port, PoolSize: 500})
err := client.Ping().Err()
if err != nil {
fmt.Printf("Unable to connect to Redis at %s:%s", host, port)
os.Exit(1)
}
}
func main() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
defer httpRequestsCounter.Inc()
count, err := client.Incr(redisCounterName).Result()
if err != nil {
fmt.Println("Unable to increment redis counter", err)
os.Exit(1)
}
resp := "Accessed on " + time.Now().String() + "\nAccess count " + strconv.Itoa(int(count))
w.Write([]byte(resp))
})
http.ListenAndServe(":8080", nil)
}