forked from Equanox/petasos-rewriter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
162 lines (139 loc) · 4.26 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
package main
import (
"fmt"
"net/url"
"os"
"time"
"github.com/avast/retry-go"
"github.com/benchkram/errz"
"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
"go.opentelemetry.io/otel/propagation"
)
const (
applicationName = "petasos-rewriter"
serverPort = "server.port"
petasosEndpoint = "petasos.endpoint"
talariaInternal = "talaria.internal"
talariaExternal = "talaria.external"
talariaDomain = "talaria.domain"
zipkinName = "zipkin"
jaegarName = "jaegar"
noopName = "noop"
traceProviderType = "type"
traceProviderEndpoint = "endpoint"
traceProviderSkipTraceExport = "skipTraceExport"
spanIdHeader = "span-id"
traceIdHeader = "trace-id"
remoteUpdateEndpoint = "remoteUpdate.url"
metricsServerPort = "metricsOptions.port"
)
func init() {
err := ConfigureViper(applicationName)
if err != nil {
errz.Fatal(err, "Could not read configuration")
}
}
var (
petasosURL *url.URL
sentryEnabled = false
remoteUpdateAddressEnabled = false
resourceURL *url.URL
isAuthHeaderCheckEnabled = false
authHeaderCheckRequestPath = ""
)
var rootCmd = &cobra.Command{
Use: "petasos-rewriter",
Short: "Request middleware implemented as `gateway`",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
printConfig()
logging(viper.Sub("log"))
isAuthHeaderCheckEnabled = viper.GetBool("server.authHeader.check.enabled")
authHeaderCheckRequestPath = viper.GetString("server.authHeader.check.requestPath")
var err error
petasosURL, err = url.Parse(viper.GetString(petasosEndpoint))
if err != nil {
log.Error().Msg(err.Error())
os.Exit(1)
}
fixedScheme := viper.GetString("server.fixedScheme")
if !(fixedScheme == "" || fixedScheme == "http" || fixedScheme == "https") {
log.Error().Msg(fmt.Errorf("Invalid Scheme [%s]", fixedScheme).Error())
os.Exit(1)
}
ConfigureSentry(viper.Sub("sentry"))
tp, err := configureTracerProvider(viper.Sub("traceProvider"), applicationName)
if err != nil {
errz.Fatal(err, "Configuration is missing for trace provider, shutting down")
}
// Initial health check
log.Info().Msg("Checking if petasos is reachable")
attempt := 1
err = retry.Do(
func() error {
log.Debug().Msgf("Trying to reach petasos: [attempt: %d]", attempt)
attempt++
err = petasosHealth(petasosURL)
if err != nil {
sentry.CaptureException(err)
sentry.Flush(2 * time.Second)
return fmt.Errorf("unhealthy")
}
return nil
},
retry.Attempts(10),
retry.Delay(1*time.Second),
)
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelFatal)
sentry.CaptureMessage("Could not reach petasos, shutting down")
})
errz.Fatal(err, "Could not reach petasos, shutting down")
prop := propagation.TraceContext{}
otelEchoOptions := []otelecho.Option{
otelecho.WithPropagators(prop),
otelecho.WithTracerProvider(tp),
}
remoteUpdateAddressEnabled = viper.GetBool("remoteUpdate.enable")
if remoteUpdateAddressEnabled {
resourceURL, err = url.Parse(viper.GetString(remoteUpdateEndpoint))
if err != nil {
log.Error().Msg(err.Error())
remoteUpdateAddressEnabled = false
}
}
client := configureClient(prop, tp)
// Setup & Start Server
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(otelecho.Middleware(applicationName, otelEchoOptions...))
e.Use(Middleware())
if sentryEnabled {
e.Use(sentryecho.New(sentryecho.Options{
Repanic: true,
}))
}
// Setup prometheus
provideMetrics(e)
requestHandlerFunc := func(ctx echo.Context) error {
return forwarder(ctx, client)
}
e.GET("/api/*", requestHandlerFunc)
e.Logger.Fatal(e.Start(":" + viper.GetString(serverPort)))
},
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Error().Msg(err.Error())
os.Exit(1)
}
os.Exit(0)
}