Skip to content

Commit

Permalink
feat: Adding route tagging
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jan 7, 2024
1 parent d010738 commit 3b615a3
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 38 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ require (
github.com/ViBiOh/auth/v2 v2.18.1
github.com/ViBiOh/exas v0.7.1
github.com/ViBiOh/flags v1.5.0
github.com/ViBiOh/httputils/v4 v4.70.4
github.com/ViBiOh/httputils/v4 v4.71.0
github.com/ViBiOh/vith v0.6.0
github.com/rabbitmq/amqp091-go v1.9.0
github.com/redis/go-redis/v9 v9.3.1
github.com/rs/xid v1.5.0
github.com/zeebo/xxh3 v1.0.2
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1
go.opentelemetry.io/otel v1.21.0
go.opentelemetry.io/otel/metric v1.21.0
go.opentelemetry.io/otel/trace v1.21.0
Expand Down Expand Up @@ -49,6 +48,7 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tdewolff/minify/v2 v2.20.10 // indirect
github.com/tdewolff/parse/v2 v2.7.7 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.46.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.44.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/ViBiOh/exas v0.7.1 h1:nzh6E1668OFRgoTIJZJa1RybCW64Gd918qi0Z+0Q4WA=
github.com/ViBiOh/exas v0.7.1/go.mod h1:MvIWQZXpWgg45Vsde9bMV48Kb0J8frya8vimmrkQ04E=
github.com/ViBiOh/flags v1.5.0 h1:nwuFS8tAwtV6rTPpv2pCB+r12WjZYLjluW7yT+SeVpQ=
github.com/ViBiOh/flags v1.5.0/go.mod h1:39UMuTnKsIp6walgD8dK99KRCb4DJt9vPtbWehHh1T0=
github.com/ViBiOh/httputils/v4 v4.70.4 h1:SMhk+wKNmQaf0jIinme0UErt4M/G4b7TLxOyUlkAi2w=
github.com/ViBiOh/httputils/v4 v4.70.4/go.mod h1:YQcY6qKO4SS5BSwkfpaA0xtTXlmuKOm7q4ie4UgKHMs=
github.com/ViBiOh/httputils/v4 v4.71.0 h1:FKLqcFV/STEbS3MU3epO8wMfeiDTSMd8TIcbgqfBTYc=
github.com/ViBiOh/httputils/v4 v4.71.0/go.mod h1:YQcY6qKO4SS5BSwkfpaA0xtTXlmuKOm7q4ie4UgKHMs=
github.com/ViBiOh/vith v0.6.0 h1:bdLmwgqUEWi3ivShtAhtM9gOZlWQzvZi2Z5b0BRSB1U=
github.com/ViBiOh/vith v0.6.0/go.mod h1:eRqtDU2uB8g85vll4spbnCV93DHyHNAYwXCMIBH1GuQ=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
Expand Down
4 changes: 2 additions & 2 deletions pkg/crud/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"path"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/fibr"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/telemetry"
)

func (s Service) Create(w http.ResponseWriter, r *http.Request, request provider.Request) {
fibr.SetRouteTag(r.Context(), "/mkdir")
telemetry.SetRouteTag(r.Context(), "/mkdir")

if !request.CanEdit {
s.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
Expand Down
13 changes: 6 additions & 7 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/fibr"
"github.com/ViBiOh/fibr/pkg/geo"
"github.com/ViBiOh/fibr/pkg/metadata"
"github.com/ViBiOh/fibr/pkg/provider"
Expand Down Expand Up @@ -119,38 +118,38 @@ func (s Service) handleDir(w http.ResponseWriter, r *http.Request, request provi
}

if query.GetBool(r, "geojson") {
fibr.SetRouteTag(r.Context(), "/geojson")
telemetry.SetRouteTag(r.Context(), "/geojson")
s.serveGeoJSON(w, r, request, item, items)
return renderer.Page{}, nil
}

if query.GetBool(r, "thumbnail") {
fibr.SetRouteTag(r.Context(), "/thumbnail")
telemetry.SetRouteTag(r.Context(), "/thumbnail")
s.thumbnail.List(w, r, item, items)
return renderer.Page{}, nil
}

if query.GetBool(r, "download") {
fibr.SetRouteTag(r.Context(), "/download")
telemetry.SetRouteTag(r.Context(), "/download")
s.Download(w, r, request, items)
return errorReturn(request, err)
}

go s.pushEvent(cntxt.WithoutDeadline(r.Context()), provider.NewAccessEvent(r.Context(), item, r))

if query.GetBool(r, "search") {
fibr.SetRouteTag(r.Context(), "/search")
telemetry.SetRouteTag(r.Context(), "/search")
return s.search(r, request, item, items)
}

provider.SetPrefsCookie(w, request)

if request.IsStory() {
fibr.SetRouteTag(r.Context(), "/story")
telemetry.SetRouteTag(r.Context(), "/story")
return s.story(r, request, item, items)
}

fibr.SetRouteTag(r.Context(), "/directory")
telemetry.SetRouteTag(r.Context(), "/directory")
return s.list(r.Context(), request, message, item, items)
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/crud/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"strconv"
"strings"

"github.com/ViBiOh/fibr/pkg/fibr"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/cntxt"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/telemetry"
)

func parseMultipart(r *http.Request) (map[string]string, *multipart.Part, error) {
Expand Down Expand Up @@ -72,15 +72,15 @@ func (s Service) handleFormURLEncoded(w http.ResponseWriter, r *http.Request, re

switch r.FormValue("type") {
case "share":
fibr.SetRouteTag(r.Context(), "/share")
telemetry.SetRouteTag(r.Context(), "/share")
s.handlePostShare(w, r, request, method)

case "webhook":
fibr.SetRouteTag(r.Context(), "/webhook")
telemetry.SetRouteTag(r.Context(), "/webhook")
s.handlePostWebhook(w, r, request, method)

case "description":
fibr.SetRouteTag(r.Context(), "/description")
telemetry.SetRouteTag(r.Context(), "/description")
s.handlePostDescription(w, r, request)

default:
Expand Down Expand Up @@ -114,14 +114,14 @@ func (s Service) handleMultipart(w http.ResponseWriter, r *http.Request, request

chunkNumber = fmt.Sprintf("%010d", chunkNumberValue)

fibr.SetRouteTag(r.Context(), "/chunk")
telemetry.SetRouteTag(r.Context(), "/chunk")
s.uploadChunk(w, r, request, values["filename"], chunkNumber, file)
} else {
fibr.SetRouteTag(r.Context(), "/merge")
telemetry.SetRouteTag(r.Context(), "/merge")
s.mergeChunk(w, r, request, values)
}
} else {
fibr.SetRouteTag(r.Context(), "/upload")
telemetry.SetRouteTag(r.Context(), "/upload")
s.upload(w, r, request, values, file)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crud/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"strings"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/fibr"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/cntxt"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/telemetry"
)

func (s Service) DoRename(ctx context.Context, oldPath, newPath string, oldItem absto.Item) (absto.Item, error) {
Expand Down Expand Up @@ -62,7 +62,7 @@ func parseRenameParams(r *http.Request, request provider.Request) (string, strin
}

func (s Service) Rename(w http.ResponseWriter, r *http.Request, request provider.Request) {
fibr.SetRouteTag(r.Context(), "/rename")
telemetry.SetRouteTag(r.Context(), "/rename")

if !request.CanEdit {
s.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
Expand Down
3 changes: 2 additions & 1 deletion pkg/fibr/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/query"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/telemetry"
)

var FuncMap = template.FuncMap{
Expand Down Expand Up @@ -68,7 +69,7 @@ func (s Service) TemplateFunc(w http.ResponseWriter, r *http.Request) (renderer.
}

if r.URL.Path == "/sitemap.xml" {
SetRouteTag(r.Context(), "/sitemap.xml")
telemetry.SetRouteTag(r.Context(), "/sitemap.xml")
return renderer.NewPage("sitemap", http.StatusOK, nil), nil
}

Expand Down
15 changes: 0 additions & 15 deletions pkg/fibr/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package fibr

import (
"context"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
)

func isMethodAllowed(r *http.Request) bool {
Expand All @@ -17,13 +12,3 @@ func isMethodAllowed(r *http.Request) bool {
return false
}
}

func SetRouteTag(ctx context.Context, route string) {
attr := semconv.HTTPRouteKey.String(route)

span := trace.SpanFromContext(ctx)
span.SetAttributes(attr)

labeler, _ := otelhttp.LabelerFromContext(ctx)
labeler.Add(attr)
}

0 comments on commit 3b615a3

Please sign in to comment.