add pagination

This commit is contained in:
Aleksandr Trushkin
2024-04-07 23:49:06 +03:00
parent 68810d93a7
commit 605e117586
23 changed files with 639 additions and 153 deletions

View File

@ -43,9 +43,9 @@ func setupCoursesHTTP(srv xhttp.Server, router *mux.Router, _ *slog.Logger) {
coursesListLearningOnlyPath := makePathTemplate(xhttp.LearningTypePathParam)
coursesListFullPath := makePathTemplate(xhttp.LearningTypePathParam, xhttp.ThematicTypePathParam)
muxHandleFunc(coursesRouter, "/", coursesAPI.Index).Methods(http.MethodGet)
muxHandleFunc(coursesRouter, coursesListLearningOnlyPath, coursesAPI.List).Methods(http.MethodGet)
muxHandleFunc(coursesRouter, coursesListFullPath, coursesAPI.List).Methods(http.MethodGet)
muxHandleFunc(coursesRouter, "index", "/", coursesAPI.Index).Methods(http.MethodGet)
muxHandleFunc(coursesRouter, "list_learning", coursesListLearningOnlyPath, coursesAPI.List).Methods(http.MethodGet)
muxHandleFunc(coursesRouter, "list_full", coursesListFullPath, coursesAPI.List).Methods(http.MethodGet)
}
func setupHTTP(cfg config.HTTP, srv xhttp.Server, log *slog.Logger) *http.Server {
@ -111,6 +111,7 @@ func middlewareTrace() mux.MiddlewareFunc {
reqidAttr := attribute.Key("http.request_id")
statusAttr := attribute.Key("http.status_code")
payloadAttr := attribute.Key("http.payload_size")
pathAttr := attribute.Key("http.template_path")
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -118,7 +119,16 @@ func middlewareTrace() mux.MiddlewareFunc {
reqid := xcontext.GetRequestID(ctx)
var span trace.Span
ctx, span = webtracer.Start(ctx, r.URL.String(), trace.WithAttributes(reqidAttr.String(reqid)))
route := mux.CurrentRoute(r)
hname := route.GetName()
hpath, _ := route.GetPathTemplate()
ctx, span = webtracer.Start(
ctx, "http."+hname,
trace.WithAttributes(
reqidAttr.String(reqid),
pathAttr.String(hpath),
),
)
defer span.End()
next.ServeHTTP(w, r.WithContext(ctx))
@ -131,6 +141,8 @@ func middlewareTrace() mux.MiddlewareFunc {
)
if statusCode > 399 {
span.SetStatus(codes.Error, "error during request")
} else {
span.SetStatus(codes.Ok, "request completed")
}
}
})

View File

@ -22,7 +22,7 @@ import (
"go.opentelemetry.io/otel/sdk/trace"
)
var webtracer = otel.Tracer("kuriweb")
var webtracer = otel.Tracer("kuriweb.http")
type shutdownFunc func(context.Context) error
@ -126,7 +126,7 @@ func newMeterProvider() (*metric.MeterProvider, error) {
return meterProvider, nil
}
func muxHandleFunc(router *mux.Router, path string, hf http.HandlerFunc) *mux.Route {
func muxHandleFunc(router *mux.Router, name, path string, hf http.HandlerFunc) *mux.Route {
h := otelhttp.WithRouteTag(path, hf)
return router.Handle(path, h)
return router.Handle(path, h).Name(name)
}