mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit introduces a comprehensive observability stack to the application, addressing a key objective from the TODO list. The following features have been implemented: - **Centralized Logging:** Replaced the existing custom logger with `zerolog` for structured, leveled, and performant logging. The logger is configured to output human-friendly console logs in development and JSON logs in production. - **Distributed Tracing:** Integrated OpenTelemetry to provide distributed tracing capabilities. A new middleware has been added to create spans for all incoming HTTP requests, and the trace context is propagated. - **Prometheus Metrics:** Added Prometheus metrics to monitor HTTP request latency and total request counts. A new `/metrics` endpoint is exposed on port 9090 to serve these metrics. - **Request ID:** Implemented a middleware to add a unique request ID to every incoming request and response, improving traceability. The new observability components are encapsulated in the `internal/observability` package, and the existing `internal/platform/log` package has been refactored to be a backward-compatible wrapper around the new logger. The main application entry point (`cmd/api/main.go`) has been updated to initialize and gracefully shut down the new observability components.
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"tercul/internal/adapters/graphql"
|
|
"tercul/internal/observability"
|
|
"tercul/internal/platform/auth"
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
)
|
|
|
|
// NewServer creates a new GraphQL server with the given resolver
|
|
func NewServer(resolver *graphql.Resolver) http.Handler {
|
|
c := graphql.Config{Resolvers: resolver}
|
|
c.Directives.Binding = graphql.Binding
|
|
srv := handler.NewDefaultServer(graphql.NewExecutableSchema(c))
|
|
|
|
// Create a mux to handle GraphQL endpoint only (no playground here; served separately in production)
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/query", srv)
|
|
|
|
return mux
|
|
}
|
|
|
|
// NewServerWithAuth creates a new GraphQL server with authentication and observability middleware
|
|
func NewServerWithAuth(resolver *graphql.Resolver, jwtManager *auth.JWTManager, metrics *observability.Metrics) http.Handler {
|
|
c := graphql.Config{Resolvers: resolver}
|
|
c.Directives.Binding = graphql.Binding
|
|
srv := handler.NewDefaultServer(graphql.NewExecutableSchema(c))
|
|
|
|
// Create a middleware chain
|
|
var chain http.Handler
|
|
chain = srv
|
|
chain = auth.GraphQLAuthMiddleware(jwtManager)(chain)
|
|
chain = metrics.PrometheusMiddleware(chain)
|
|
chain = observability.TracingMiddleware(chain)
|
|
chain = observability.RequestIDMiddleware(chain)
|
|
|
|
// Create a mux to handle GraphQL endpoint
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/query", chain)
|
|
|
|
return mux
|
|
} |