Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
skip verbose endpoints for API logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorVin committed Feb 15, 2024
1 parent 3291979 commit 8d49738
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ var (
readTimeout = 10 * time.Second
writeTimeout = 20 * time.Second

livenessEndpoint = "/_health/liveness"
versionEndpoint = "/api/version"

authMiddleWare *ginauth.MultiTokenMiddleware
ginNoOp = func(_ *gin.Context) {}
)

// apiHandler is a function that performs real work for this API.
type apiHandler func(map[string]any) (map[string]any, error)

func composeAppLogging(l *zap.Logger) gin.HandlerFunc {
func composeAppLogging(l *zap.Logger, skippedPaths ...string) gin.HandlerFunc {
skipMap := map[string]struct{}{}
for _, path := range skippedPaths {
skipMap[path] = struct{}{}
}
return func(c *gin.Context) {
start := time.Now()
// some evil middlewares modify this values
Expand All @@ -35,6 +42,11 @@ func composeAppLogging(l *zap.Logger) gin.HandlerFunc {
code := c.Writer.Status()
metrics.APICallEpilog(start, path, code)

// only log if we're not skipping this path
if _, ok := skipMap[path]; ok {
return
}

fields := []zap.Field{
zap.String("path", path),
zap.String("query", query),
Expand Down Expand Up @@ -74,7 +86,7 @@ func ComposeHTTPServer(theApp *app.App) *http.Server {
}

// set up common middleware for logging and metrics
g.Use(composeAppLogging(theApp.Log), gin.Recovery())
g.Use(composeAppLogging(theApp.Log, livenessEndpoint), gin.Recovery())

// some boilerplate setup
g.NoRoute(func(c *gin.Context) {
Expand All @@ -86,11 +98,11 @@ func ComposeHTTPServer(theApp *app.App) *http.Server {
})

// a liveness endpoint
g.GET("/_health/liveness", func(c *gin.Context) {
g.GET(livenessEndpoint, func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"time": time.Now()})
})

g.GET("/api/version", func(c *gin.Context) {
g.GET(versionEndpoint, func(c *gin.Context) {
c.JSON(http.StatusOK, version.Current())
})

Expand Down

0 comments on commit 8d49738

Please sign in to comment.