Nik Afiq 657b6aeb22 feat: implement initial application structure with health and hello endpoints
- Add bootstrap package to initialize application components including logger, tracer, and HTTP server.
- Create config package to load runtime settings from environment variables.
- Implement observability features including logging, metrics, and tracing.
- Add health check and hello use cases with corresponding HTTP handlers.
- Introduce middleware for request ID, access logging, metrics, and recovery.
- Set up HTTP router with defined routes for health and hello endpoints.
- Include tests for health and hello endpoints to ensure proper functionality.
- Add OpenTelemetry collector configuration for trace exporting.
2026-03-05 21:22:43 +09:00

44 lines
971 B
Go

package middleware
import (
"log/slog"
"time"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel/trace"
"switchbot-api/internal/transport/http/contextkeys"
)
func AccessLog(logger *slog.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
route := c.FullPath()
if route == "" {
route = "/unknown"
}
spanCtx := trace.SpanContextFromContext(c.Request.Context())
traceID := ""
spanID := ""
if spanCtx.IsValid() {
traceID = spanCtx.TraceID().String()
spanID = spanCtx.SpanID().String()
}
logger.Info(
"http_request",
slog.String("method", c.Request.Method),
slog.String("path", c.Request.URL.Path),
slog.String("route", route),
slog.Int("status", c.Writer.Status()),
slog.Duration("duration", time.Since(start)),
slog.String("request_id", contextkeys.RequestIDFromContext(c.Request.Context())),
slog.String("trace_id", traceID),
slog.String("span_id", spanID),
)
}
}