-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
194 lines (186 loc) Β· 6 KB
/
run.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
package cloudrunner
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
"go.einride.tech/cloudrunner/cloudclient"
"go.einride.tech/cloudrunner/cloudconfig"
"go.einride.tech/cloudrunner/cloudotel"
"go.einride.tech/cloudrunner/cloudprofiler"
"go.einride.tech/cloudrunner/cloudrequestlog"
"go.einride.tech/cloudrunner/cloudruntime"
"go.einride.tech/cloudrunner/cloudserver"
"go.einride.tech/cloudrunner/cloudslog"
"go.einride.tech/cloudrunner/cloudtrace"
"go.einride.tech/cloudrunner/cloudzap"
"google.golang.org/grpc"
)
// runConfig configures the Run entrypoint from environment variables.
type runConfig struct {
// Runtime contains runtime config.
Runtime cloudruntime.Config
// Logger contains logger config.
Logger cloudzap.LoggerConfig
// Profiler contains profiler config.
Profiler cloudprofiler.Config
// TraceExporter contains trace exporter config.
TraceExporter cloudotel.TraceExporterConfig
// MetricExporter contains metric exporter config.
MetricExporter cloudotel.MetricExporterConfig
// Server contains server config.
Server cloudserver.Config
// Client contains client config.
Client cloudclient.Config
// RequestLogger contains request logging config.
RequestLogger cloudrequestlog.Config
}
// Run a service.
// Configuration of the service is loaded from the environment.
func Run(fn func(context.Context) error, options ...Option) (err error) {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
usage := flag.Bool("help", false, "show help then exit")
yamlServiceSpecificationFile := flag.String("config", "", "load environment from a YAML service specification")
validate := flag.Bool("validate", false, "validate config then exit")
flag.Parse()
flag.CommandLine.SetOutput(os.Stdout)
var run runContext
for _, option := range options {
option(&run)
}
if *yamlServiceSpecificationFile != "" {
run.configOptions = append(
run.configOptions, cloudconfig.WithYAMLServiceSpecificationFile(*yamlServiceSpecificationFile),
)
}
if *validate {
run.configOptions = append(
run.configOptions,
cloudconfig.WithOptionalSecrets(),
)
}
config, err := cloudconfig.New("cloudrunner", &run.config, run.configOptions...)
if err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
if *usage {
printUsage(flag.CommandLine.Output(), config)
return nil
}
if err := config.Load(); err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
if err := run.config.Runtime.Autodetect(); err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
if *validate {
return nil
}
run.traceMiddleware.ProjectID = run.config.Runtime.ProjectID
if run.traceMiddleware.TraceHook == nil {
run.traceMiddleware.TraceHook = cloudtrace.IDHook
}
run.serverMiddleware.Config = run.config.Server
run.requestLoggerMiddleware.Config = run.config.RequestLogger
ctx = withRunContext(ctx, &run)
ctx = cloudruntime.WithConfig(ctx, run.config.Runtime)
logger, err := cloudzap.NewLogger(run.config.Logger)
if err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
run.loggerMiddleware.Logger = logger
ctx = cloudzap.WithLogger(ctx, logger)
// Set the global default log/slog logger.
slog.SetDefault(slog.New(cloudslog.NewHandler(cloudslog.LoggerConfig{
Development: run.config.Logger.Development,
Level: cloudzap.LevelToSlog(run.config.Logger.Level),
ProtoMessageSizeLimit: run.config.RequestLogger.MessageSizeLimit,
})))
if err := cloudprofiler.Start(run.config.Profiler); err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
resource, err := cloudotel.NewResource(ctx)
if err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
stopTraceExporter, err := cloudotel.StartTraceExporter(ctx, run.config.TraceExporter, resource)
if err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
stopMetricExporter, err := cloudotel.StartMetricExporter(ctx, run.config.MetricExporter, resource)
if err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
cloudotel.RegisterErrorHandler(ctx)
buildInfo, _ := debug.ReadBuildInfo()
go func() {
<-ctx.Done()
// Cloud Run sends a SIGTERM and allows for 10 seconds before it completely shuts down
// the instance.
// See https://cloud.google.com/run/docs/container-contract#instance-shutdown for more details.
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
slog.InfoContext(ctx, "shutting down")
err := errors.Join(
stopTraceExporter(shutdownCtx),
stopMetricExporter(shutdownCtx),
)
if err != nil {
slog.WarnContext(ctx, "unable to call shutdown routines", slog.Any("error", err))
}
}()
slog.InfoContext(
ctx,
"up and running",
slog.Any("config", config),
slog.Any("resource", resource),
slog.Any("buildInfo", buildInfo),
)
defer slog.InfoContext(ctx, "goodbye")
defer func() {
if r := recover(); r != nil {
var msg slog.Attr
if err2, ok := r.(error); ok {
msg = slog.Any("error", err2)
err = err2
} else {
msg = slog.Any("msg", r)
err = fmt.Errorf("recovered panic")
}
slog.LogAttrs(
ctx,
slog.LevelError,
"recovered panic",
msg,
slog.String("stack", string(debug.Stack())),
)
}
}()
return fn(ctx)
}
type runContext struct {
config runConfig
configOptions []cloudconfig.Option
grpcServerOptions []grpc.ServerOption
loggerMiddleware cloudzap.Middleware
serverMiddleware cloudserver.Middleware
clientMiddleware cloudclient.Middleware
requestLoggerMiddleware cloudrequestlog.Middleware
traceMiddleware cloudtrace.Middleware
securityHeadersMiddleware cloudserver.SecurityHeadersMiddleware
}
type runContextKey struct{}
func withRunContext(ctx context.Context, run *runContext) context.Context {
return context.WithValue(ctx, runContextKey{}, run)
}
func getRunContext(ctx context.Context) (*runContext, bool) {
result, ok := ctx.Value(runContextKey{}).(*runContext)
return result, ok
}