forked from keboola/keboola-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (148 loc) · 5.47 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
// nolint: gocritic
package main
import (
"context"
"net/http"
"os"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
"github.com/keboola/keboola-as-code/internal/pkg/log"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/configmap"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/entrypoint"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/httpserver"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/httpserver/middleware"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/servicectx"
"github.com/keboola/keboola-as-code/internal/pkg/service/templates/api/config"
templatesGenSvr "github.com/keboola/keboola-as-code/internal/pkg/service/templates/api/gen/http/templates/server"
templatesGen "github.com/keboola/keboola-as-code/internal/pkg/service/templates/api/gen/templates"
"github.com/keboola/keboola-as-code/internal/pkg/service/templates/api/openapi"
"github.com/keboola/keboola-as-code/internal/pkg/service/templates/api/service"
"github.com/keboola/keboola-as-code/internal/pkg/service/templates/dependencies"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry/datadog"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry/metric/prometheus"
"github.com/keboola/keboola-as-code/internal/pkg/telemetry/pprof"
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
swaggerui "github.com/keboola/keboola-as-code/third_party"
)
const (
ServiceName = "templates-api"
ENVPrefix = "TEMPLATES_"
ErrorNamePrefix = "templates."
ExceptionIdPrefix = "keboola-templates-"
)
func main() {
entrypoint.Run(run, config.New(), entrypoint.Config{ENVPrefix: ENVPrefix})
}
func run(ctx context.Context, cfg config.Config, _ []string) error {
// Create logger
logger := log.NewServiceLogger(os.Stdout, cfg.DebugLog) // nolint:forbidigo
// Dump configuration, sensitive values are masked
dump, err := configmap.NewDumper().Dump(cfg).AsJSON(false)
if err == nil {
logger.Infof(ctx, "configuration: %s", string(dump))
} else {
return err
}
// Create process abstraction
proc := servicectx.New(servicectx.WithLogger(logger))
// PProf profiler
if cfg.PProf.Enabled {
logger.Infof(ctx, `PProf profiler enabled, listening on %q`, cfg.PProf.Listen)
srv := pprof.NewHTTPServer(cfg.PProf.Listen)
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Errorf(ctx, `PProf HTTP server error: %s`, err)
}
}()
defer func() {
if err := srv.Close(); err != nil {
logger.Errorf(ctx, `cannot stop PProf HTTP server: %s`, err)
}
}()
}
// Datadog profiler
if cfg.Datadog.Enabled && cfg.Datadog.Profiler.Enabled {
logger.Infof(ctx, "Datadog profiler enabled")
if err := profiler.Start(profiler.WithProfileTypes(cfg.Datadog.Profiler.ProfilerTypes()...)); err != nil {
return err
}
defer profiler.Stop()
}
// Setup telemetry
tel, err := telemetry.New(
func() (trace.TracerProvider, error) {
if cfg.Datadog.Enabled {
return datadog.NewTracerProvider(
logger, proc,
tracer.WithRuntimeMetrics(),
tracer.WithSamplingRules([]tracer.SamplingRule{tracer.RateRule(1.0)}),
tracer.WithAnalyticsRate(1.0),
tracer.WithDebugMode(cfg.Datadog.Debug),
), nil
}
return nil, nil
},
func() (metric.MeterProvider, error) {
return prometheus.ServeMetrics(ctx, cfg.Metrics, logger, proc, ServiceName)
},
)
if err != nil {
return err
}
// Create dependencies
apiScp, err := dependencies.NewAPIScope(ctx, cfg, proc, logger, tel, os.Stdout, os.Stderr) // nolint:forbidigo
if err != nil {
return err
}
// Create service
svc, err := service.New(ctx, apiScp)
if err != nil {
return err
}
// Start HTTP server
logger.Infof(ctx, "starting Templates API HTTP server, listen-address=%s", cfg.API.Listen)
err = httpserver.Start(ctx, apiScp, httpserver.Config{
ListenAddress: cfg.API.Listen,
ErrorNamePrefix: ErrorNamePrefix,
ExceptionIDPrefix: ExceptionIdPrefix,
MiddlewareOptions: []middleware.Option{
middleware.WithRedactedHeader("X-StorageAPI-Token"),
middleware.WithPropagators(propagation.TraceContext{}),
middleware.WithFilter(func(req *http.Request) bool {
return req.URL.Path != "/health-check"
}),
},
Mount: func(c httpserver.Components) {
// Create public request deps for each request
c.Muxer.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
next.ServeHTTP(w, req.WithContext(context.WithValue(
req.Context(),
dependencies.PublicRequestScopeCtxKey, dependencies.NewPublicRequestScope(apiScp, req),
)))
})
})
// Create server with endpoints
docsFs := http.FS(openapi.Fs)
swaggerUiFs := http.FS(swaggerui.SwaggerFS)
endpoints := templatesGen.NewEndpoints(svc)
endpoints.Use(middleware.OpenTelemetryExtractEndpoint())
server := templatesGenSvr.New(endpoints, c.Muxer, c.Decoder, c.Encoder, c.ErrorHandler, c.ErrorFormatter, docsFs, docsFs, docsFs, docsFs, swaggerUiFs)
// Mount endpoints
server.Mount(c.Muxer)
for _, m := range server.Mounts {
logger.Debugf(ctx, "HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}
},
})
if err != nil {
return err
}
// Wait for the service shutdown
proc.WaitForShutdown()
return nil
}