Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func InitLogger(config Config) *zap.Logger {
return logger
}

func RequestTimer() fiber.Handler {
return func(c *fiber.Ctx) error {
c.Locals("start", time.Now())
return c.Next()
}
}

func Min(handler func(*fiber.Ctx, bool) error) fiber.Handler {
return func(c *fiber.Ctx) error {
return handler(c, true)
Expand Down Expand Up @@ -92,21 +99,26 @@ func NewApiServer(config Config) *ApiServer {

app.Use(recover.New())
app.Use(cors.New())
app.Use(RequestTimer())

app.Use(fiberzap.New(fiberzap.Config{
Logger: logger,
FieldsFunc: func(c *fiber.Ctx) []zap.Field {
fields := []zap.Field{}

if startTime, ok := c.Locals("start").(time.Time); ok {
latencyMs := float64(time.Since(startTime).Nanoseconds()) / float64(time.Millisecond)
fields = append(fields, zap.Float64("latency_ms", latencyMs))
}

// Add upstream server to logs, if found
if upstream, ok := c.Locals("upstream").(string); ok && upstream != "" {
fields = append(fields,
zap.String("upstream", upstream),
)
fields = append(fields, zap.String("upstream", upstream))
}

return fields
},
Fields: []string{"status", "method", "url", "route"},
}))

app.Get("/", app.home)
Expand Down
Loading