-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
205 lines (187 loc) 路 7.02 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"goa.design/clue/clue"
"goa.design/clue/debug"
"goa.design/clue/health"
"goa.design/clue/log"
goahttp "goa.design/goa/v3/http"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"goa.design/clue/example/middleware"
"goa.design/clue/example/weather/services/front"
"goa.design/clue/example/weather/services/front/clients/forecaster"
"goa.design/clue/example/weather/services/front/clients/locator"
"goa.design/clue/example/weather/services/front/clients/tester"
genfront "goa.design/clue/example/weather/services/front/gen/front"
genhttp "goa.design/clue/example/weather/services/front/gen/http/front/server"
)
func main() {
var (
httpListenAddr = flag.String("http-addr", ":8084", "HTTP listen address")
metricsListenAddr = flag.String("metrics-addr", ":8085", "metrics listen address")
forecasterAddr = flag.String("forecaster-addr", ":8080", "Forecaster service address")
forecasterHealthAddr = flag.String("forecaster-health-addr", ":8081", "Forecaster service health-check address")
locatorAddr = flag.String("locator-addr", ":8082", "Locator service address")
locatorHealthAddr = flag.String("locator-health-addr", ":8083", "Locator service health-check address")
coladdr = flag.String("otel-addr", ":4317", "OpenTelemtry collector listen address")
debugf = flag.Bool("debug", false, "Enable debug logs")
testerAddr = flag.String("tester-addr", ":8090", "Tester service address")
)
flag.Parse()
// 1. Create logger
format := log.FormatJSON
if log.IsTerminal() {
format = log.FormatTerminal
}
ctx := log.Context(context.Background(), log.WithFormat(format), log.WithFunc(log.Span))
if *debugf {
ctx = log.Context(ctx, log.WithDebug())
log.Debugf(ctx, "debug logs enabled")
}
// 2. Setup instrumentation
spanExporter, err := otlptracegrpc.New(ctx,
otlptracegrpc.WithEndpoint(*coladdr),
otlptracegrpc.WithTLSCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf(ctx, err, "failed to initialize tracing")
}
defer func() {
// Create new context in case the parent context has been canceled.
ctx := log.Context(context.Background(), log.WithFormat(format))
if err := spanExporter.Shutdown(ctx); err != nil {
log.Errorf(ctx, err, "failed to shutdown tracing")
}
}()
metricExporter, err := otlpmetricgrpc.New(ctx,
otlpmetricgrpc.WithEndpoint(*coladdr),
otlpmetricgrpc.WithTLSCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf(ctx, err, "failed to initialize metrics")
}
defer func() {
// Create new context in case the parent context has been canceled.
ctx := log.Context(context.Background(), log.WithFormat(format))
if err := metricExporter.Shutdown(ctx); err != nil {
log.Errorf(ctx, err, "failed to shutdown metrics")
}
}()
cfg, err := clue.NewConfig(ctx,
genfront.ServiceName,
genfront.APIVersion,
metricExporter,
spanExporter,
)
if err != nil {
log.Fatalf(ctx, err, "failed to initialize instrumentation")
}
clue.ConfigureOpenTelemetry(ctx, cfg)
// 3. Create clients
lcc, err := grpc.DialContext(ctx,
*locatorAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(log.UnaryClientInterceptor()),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()))
if err != nil {
log.Fatalf(ctx, err, "failed to connect to locator")
}
lc := locator.New(lcc)
fcc, err := grpc.DialContext(ctx, *forecasterAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(log.UnaryClientInterceptor()),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()))
if err != nil {
log.Fatalf(ctx, err, "failed to connect to forecast")
}
fc := forecaster.New(fcc)
tcc, err := grpc.DialContext(ctx, *testerAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(log.UnaryClientInterceptor()),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()))
if err != nil {
log.Errorf(ctx, err, "failed to connect to tester")
os.Exit(1)
}
tc := tester.New(tcc)
// 4. Create service & endpoints
svc := front.New(fc, lc, tc)
endpoints := genfront.NewEndpoints(svc)
endpoints.Use(debug.LogPayloads())
endpoints.Use(log.Endpoint)
endpoints.Use(middleware.ErrorReporter())
// 5. Create transport
mux := goahttp.NewMuxer()
debug.MountDebugLogEnabler(debug.Adapt(mux))
debug.MountPprofHandlers(debug.Adapt(mux))
handler := otelhttp.NewHandler(mux, genfront.ServiceName) // 3. Add OpenTelemetry instrumentation
handler = debug.HTTP()(handler) // 2. Add debug endpoints
handler = log.HTTP(ctx)(handler) // 1. Add logger to request context
server := genhttp.New(endpoints, mux, goahttp.RequestDecoder, goahttp.ResponseEncoder, nil, nil)
genhttp.Mount(mux, server)
for _, m := range server.Mounts {
log.Print(ctx, log.KV{K: "method", V: m.Method}, log.KV{K: "endpoint", V: m.Verb + " " + m.Pattern})
}
httpServer := &http.Server{Addr: *httpListenAddr, Handler: handler}
// 6. Mount health check & metrics on separate HTTP server (different listen port)
// No testerHealthAddr pinger because we don't want the whole system to die just because
// tester isn't healthy for some reason
check := health.Handler(health.NewChecker(
health.NewPinger("locator", *locatorHealthAddr),
health.NewPinger("forecaster", *forecasterHealthAddr)))
check = log.HTTP(ctx)(check).(http.HandlerFunc) // Log health-check errors
http.Handle("/healthz", check)
http.Handle("/livez", check)
metricsServer := &http.Server{Addr: *metricsListenAddr}
// 7. Start HTTP server
errc := make(chan error)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("signal: %s", <-c)
}()
ctx, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
go func() {
log.Printf(ctx, "HTTP server listening on %s", *httpListenAddr)
errc <- httpServer.ListenAndServe()
}()
go func() {
log.Printf(ctx, "Metrics server listening on %s", *metricsListenAddr)
errc <- metricsServer.ListenAndServe()
}()
<-ctx.Done()
log.Printf(ctx, "shutting down HTTP servers")
// Shutdown gracefully with a 30s timeout.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Errorf(ctx, err, "failed to shutdown HTTP server")
}
if err := metricsServer.Shutdown(ctx); err != nil {
log.Errorf(ctx, err, "failed to shutdown metrics server")
}
}()
// Cleanup
if err := <-errc; err != nil && !strings.HasPrefix(err.Error(), "signal:") {
log.Errorf(ctx, err, "exiting")
}
cancel()
wg.Wait()
log.Printf(ctx, "exited")
}