/
api.go
58 lines (46 loc) · 1.16 KB
/
api.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
package api
import (
"fmt"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const httpPort = "8080"
var totalRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of HTTP requests processed, based on path",
},
[]string{"path"},
)
var responseStatus = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "response_status",
Help: "Status of HTTP responses",
},
[]string{"status"},
)
var httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_response_time_seconds",
Help: "Duration of HTTP requests",
}, []string{"path"})
// Config describes all application members
type Config struct{}
func init() {
prometheus.Register(totalRequests)
prometheus.Register(responseStatus)
prometheus.Register(httpDuration)
}
func Run() {
app := Config{}
log.Printf("listening on http port %s\n", httpPort)
srv := &http.Server{
Addr: fmt.Sprintf(":%s", httpPort),
Handler: app.Routes(),
ReadHeaderTimeout: 2 * time.Second,
}
err := srv.ListenAndServe()
log.Println(err)
}