-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (63 loc) · 1.91 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"log"
"fmt"
"net/http"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/uptrace/opentelemetry-go-extra/otelsql"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/otel"
)
var tracer = otel.Tracer("")
func main() {
otelShutdown, err := setupOTelSDK(context.Background())
if err != nil {
log.Panic("Badness", err)
}
defer func() {
otelShutdown(context.Background())
}()
db, err := otelsql.Open("sqlite3", "services.db")
if err != nil {
log.Fatalf("Failed to connect to db %s\n", err)
}
repo, err := NewDbRepo(db)
if err != nil {
log.Panic(err)
}
handler := NewServiceHandler(repo)
// authMiddleware, err := NewAuthMiddleware(db)
if err != nil {
log.Panicf("Badness %s", err)
}
router := mux.NewRouter()
router.Use(otelmux.Middleware("services"))
// router.Use(authMiddleware.Middleware)
router.Use(prometheusMiddleware)
router.HandleFunc("/services", handler.GetServices).Methods("GET")
router.HandleFunc("/service/{id}", handler.GetService).Methods("GET")
router.Handle("/metrics", promhttp.Handler())
fmt.Println("Starting service on port 8080")
http.ListenAndServe(":8080", router)
}
var (
httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_duration_seconds",
Help: "Duration of HTTP requests.",
}, []string{"path"})
)
// prometheusMiddleware implements mux.MiddlewareFunc.
func prometheusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
timer := prometheus.NewTimer(httpDuration.WithLabelValues(path))
next.ServeHTTP(w, r)
timer.ObserveDuration()
})
}