-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
322 lines (289 loc) · 8.41 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/motemen/go-loghttp"
"github.com/rs/zerolog"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
oteltrace "go.opentelemetry.io/otel/trace"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var log *zerolog.Logger
var tracer = otel.Tracer("echo-server")
var httpClient *http.Client
func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
logger := zerolog.New(output).With().Timestamp().Caller().Logger()
log = &logger
transport := &loghttp.Transport{
LogRequest: func(req *http.Request) {
log.Debug().
Interface("headers", req.Header).
Msg("calling " + req.Method + " " + req.URL.String())
},
LogResponse: func(res *http.Response) {
req := res.Request
log.Debug().
Str("status", res.Status).
Interface("headers", res.Header).
Msg("call " + req.Method + " " + req.URL.String() + " answered")
},
}
httpClient = &http.Client{Transport: otelhttp.NewTransport(transport)}
}
func initTracer() (*sdktrace.TracerProvider, error) {
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(os.Getenv("JAEGER_API"))))
if err != nil {
return nil, err
}
tp := sdktrace.NewTracerProvider(
// Always be sure to batch in production.
sdktrace.WithBatcher(exp),
// Record information about this application in a Resource.
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("bet"),
)),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, b3.New()))
return tp, nil
}
func main() {
start := time.Now()
e := echo.New()
e.Logger.SetOutput(ioutil.Discard)
// Middleware
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
start := time.Now()
log.Debug().
Interface("headers", req.Header).
Msg(">>> " + req.Method + " " + req.RequestURI)
if err = next(c); err != nil {
c.Error(err)
}
log.Debug().
Str("latency", time.Now().Sub(start).String()).
Int("status", res.Status).
Interface("headers", res.Header()).
Msg("<<< " + req.Method + " " + req.RequestURI)
return
}
})
e.Use(middleware.Recover())
//CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}))
e.Static("/static", "assets/api-docs")
tp, err := initTracer()
if err != nil {
log.Panic()
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
skipper := otelecho.WithSkipper(func(c echo.Context) bool {
return !strings.Contains(c.Request().RequestURI, "/health")
})
e.Use(otelecho.Middleware("bet", skipper))
e.HTTPErrorHandler = func(err error, c echo.Context) {
ctx := c.Request().Context()
oteltrace.SpanFromContext(ctx).RecordError(err)
e.DefaultHTTPErrorHandler(err, c)
}
// Server
e.POST("/api/bets", CreateBet)
e.GET("/health", Health)
elapsed := time.Now().Sub(start)
log.Debug().Msg("Bets app initialized in " + elapsed.String())
e.Logger.Fatal(e.Start(":9999"))
}
func Health(c echo.Context) error {
return c.JSON(200, &HealthData{Status: "UP"})
}
type HealthData struct {
Status string `json:"status,omitempty"`
}
func CreateBet(c echo.Context) error {
defer c.Request().Body.Close()
bet := &Bet{}
if err := json.NewDecoder(c.Request().Body).Decode(bet); err != nil {
log.Error().Err(err).Msg("Failed reading the request body")
return echo.NewHTTPError(http.StatusInternalServerError, err.Error)
}
match, matchStatus, matchErr := match(c)
player, playerStatus, playerErr := player(c)
champ, champStatus, champErr := championship(c)
if hasError(matchErr, playerErr, champErr) {
return c.JSON(http.StatusServiceUnavailable, &Error{Errors: map[string]int{
"players": playerStatus,
"matches": matchStatus,
"championships": champStatus,
}})
}
b := &Bet{
HomeTeamScore: strconv.Itoa(2),
AwayTeamScore: strconv.Itoa(3),
Championship: champ,
Match: match.String(),
Email: player,
}
return c.JSON(http.StatusCreated, b)
}
func hasError(errs ...error) bool {
r := false
for _, err := range errs {
if err != nil {
r = true
}
}
return r
}
func match(ctx echo.Context) (*Match, int, error) {
req, _ := http.NewRequest("GET", os.Getenv("MATCH_SVC"), nil)
forwardHeaders(ctx, req)
res, err := httpClient.Do(req)
if err != nil {
log.Error().Err(err).Msg("failed to call matches")
return nil, 0, err
}
status := res.StatusCode
if !is2xx(status) {
return nil, status, errors.New(res.Status)
}
data := &Match{}
if jsonErr := json.NewDecoder(res.Body).Decode(data); jsonErr != nil {
log.Error().Err(jsonErr).Msg("failed to read matches response body")
return nil, 0, jsonErr
}
return data, status, nil
}
func forwardHeaders(ctx echo.Context, r *http.Request) {
incomingHeaders := []string{
"Authorization",
"x-version",
// open tracing
"x-request-id",
"x-b3-traceid",
"x-b3-spanid",
"x-b3-parentspanid",
"x-b3-sampled",
"x-b3-flags",
// open telemetry
"ot-tracer-spanid",
"ot-tracer-traceid",
"ot-tracer-sampled",
}
for _, th := range incomingHeaders {
h := ctx.Request().Header.Get(th)
if h != "" {
r.Header.Set(th, h)
}
}
}
func championship(ctx echo.Context) (string, int, error) {
req, _ := http.NewRequest("GET", os.Getenv("CHAMPIONSHIP_SVC"), nil)
forwardHeaders(ctx, req)
res, err := httpClient.Do(req)
if err != nil {
log.Error().Err(err).Msg("failed to call championships")
return "", 0, err
}
status := res.StatusCode
if !is2xx(status) {
return "", status, errors.New(res.Status)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Error().Err(err).Msg("failed to read matches response body")
return "", status, readErr
}
var data map[string]string
if jsonErr := json.Unmarshal(body, &data); jsonErr != nil {
log.Error().Err(err).Msg("failed to read matches response body")
return "", status, jsonErr
}
return data["title"], status, nil
}
func player(ctx echo.Context) (string, int, error) {
req, _ := http.NewRequest("GET", os.Getenv("PLAYER_SVC"), nil)
forwardHeaders(ctx, req)
res, err := httpClient.Do(req)
if err != nil {
log.Error().Err(err).Msg("failed to call players")
return "", 0, err
}
status := res.StatusCode
if !is2xx(status) {
return "", status, errors.New(res.Status)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Error().Err(err).Msg("failed to read players response body")
return "", status, readErr
}
var data map[string]string
if jsonErr := json.Unmarshal(body, &data); jsonErr != nil {
log.Error().Err(err).Msg("failed to read players response body")
return "", status, jsonErr
}
return data["email"], status, nil
}
func is2xx(status int) bool {
return status >= 200 && status < 300
}
type Bet struct {
HomeTeamScore string `json:"homeTeamScore,omitempty"`
AwayTeamScore string `json:"awayTeamScore,omitempty"`
Championship string `json:"championship,omitempty"`
Match string `json:"match,omitempty"`
Email string `json:"email,omitempty"`
}
type Error struct {
Errors map[string]int `json:"errors,omitempty"`
}
type Match struct {
Date time.Time `json:"date"`
Championship string `json:"championship"`
Teams struct {
Home struct {
Name string `json:"name"`
Score int `json:"score"`
} `json:"home"`
Away struct {
Name string `json:"name"`
Score int `json:"score"`
} `json:"Away"`
} `json:"teams"`
}
func (m *Match) String() string {
h := m.Teams.Home
a := m.Teams.Away
return fmt.Sprintf("%s - %s %dx%d %s (%s)", m.Date.Format("2006-01-02"), h.Name, h.Score, a.Score, a.Name, m.Championship)
}