diff --git a/.task/checksum/generate b/.task/checksum/generate index 9e85368..db3ce01 100644 --- a/.task/checksum/generate +++ b/.task/checksum/generate @@ -1 +1 @@ -3cf236a901d03e42352790df844d58c5 +de22f926e2940e47830ef3a33736db71 diff --git a/cmd/kuriweb/http.go b/cmd/kuriweb/http.go index 6814419..faf2f90 100644 --- a/cmd/kuriweb/http.go +++ b/cmd/kuriweb/http.go @@ -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") } } }) diff --git a/cmd/kuriweb/trace.go b/cmd/kuriweb/trace.go index c1ac2e9..b9202de 100644 --- a/cmd/kuriweb/trace.go +++ b/cmd/kuriweb/trace.go @@ -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) } diff --git a/htmlexamples/index.html b/htmlexamples/index.html index c67f8a1..3597734 100644 --- a/htmlexamples/index.html +++ b/htmlexamples/index.html @@ -44,7 +44,7 @@
Here you can find course for any taste
-In this category you can find courses of types such as
web-development, backend development, frontend developent
+This category contains 128 courses.
-Category 1
-Category 2
-Category 3
-