This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (105 loc) · 3.33 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"context"
"errors"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
debugsvc "github.com/lvjp/dummy/internal/pkg/service/debug"
stringsvc "github.com/lvjp/dummy/internal/pkg/service/string"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/sourcegraph/conc/pool"
)
func main() {
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
// Check for local debugging
if os.Getenv("SCW_APPLICATION_NAME") == "" {
logger = logger.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
})
}
mainCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
s := newServer(logger)
p := pool.New().WithErrors().WithContext(mainCtx)
p.Go(func(ctx context.Context) error {
s.BaseContext = func(l net.Listener) context.Context {
return ctx
}
logger.Info().Str("addr", s.Addr).Msg("Start HTTP server")
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error().Err(err).Msg("Cannot server listen and serve")
return err
}
logger.Info().Msg("HTTP server gracefully shutdown")
return nil
})
p.Go(func(ctx context.Context) error {
<-ctx.Done()
logger.Info().Msg("Shutdown sequence started")
return s.Shutdown(context.Background())
})
if err := p.Wait(); err != nil {
logger.Error().Err(err).Msg("Unexpected error on shutdown")
return
}
logger.Info().Msg("Shutdown sequence finished")
}
func newServer(logger zerolog.Logger) *http.Server {
mux := http.NewServeMux()
mux.Handle("/string/", http.StripPrefix("/string", initStringService(logger)))
mux.Handle("/debug/", http.StripPrefix("/debug", initDebugService(logger)))
mux.Handle("/metrics", promhttp.Handler())
// PORT is definied by Scaleway serverless containers
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
return &http.Server{
Addr: ":" + port,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
}
func initStringService(logger zerolog.Logger) http.Handler {
svc := stringsvc.NewService()
svc = stringsvc.NewLoggingService(logger.With().Str("service", "string").Logger(), svc)
svc = stringsvc.NewInstrumentingService(newPromMetrics("string", svc))
return stringsvc.MakeHandler(svc)
}
func initDebugService(logger zerolog.Logger) http.Handler {
svc := debugsvc.NewService()
svc = debugsvc.NewLoggingservice(logger.With().Str("service", "debug").Logger(), svc)
svc = debugsvc.NewInstrumentingService(newPromMetrics("debug", svc))
return debugsvc.MakeHandler(svc)
}
func newPromMetrics[T any](subsystem string, svc T) (*prometheus.CounterVec, *prometheus.HistogramVec, T) {
fieldKeys := []string{"method"}
counter := promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "api",
Subsystem: subsystem + "_service",
Name: "request_count",
Help: "Number of requests received.",
},
fieldKeys,
)
histogram := promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "api",
Subsystem: subsystem + "_service",
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
},
fieldKeys,
)
return counter, histogram, svc
}