Skip to content

Commit

Permalink
feat(otel): Adding rouge 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 3, 2024
1 parent 0209bb4 commit 7464731
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/crud/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ 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"
)

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

if !request.CanEdit {
s.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
return
Expand Down
3 changes: 3 additions & 0 deletions pkg/crud/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"fmt"
"net/http"

"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"
)

func (s Service) Delete(w http.ResponseWriter, r *http.Request, request provider.Request) {
fibr.SetRouteTag(r.Context(), "DELETE")

if !request.CanEdit {
s.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
return
Expand Down
7 changes: 7 additions & 0 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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 @@ -118,32 +119,38 @@ func (s Service) handleDir(w http.ResponseWriter, r *http.Request, request provi
}

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

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

if query.GetBool(r, "download") {
fibr.SetRouteTag(r.Context(), "GET 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(), "GET search")
return s.search(r, request, item, items)
}

provider.SetPrefsCookie(w, request)

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

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

Expand Down
12 changes: 12 additions & 0 deletions pkg/crud/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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"
Expand Down Expand Up @@ -53,6 +54,8 @@ func parseMultipart(r *http.Request) (map[string]string, *multipart.Part, error)
func (s Service) Post(w http.ResponseWriter, r *http.Request, request provider.Request) {
contentType := r.Header.Get("Content-Type")

fibr.SetRouteTag(r.Context(), "POST")

if contentType == "application/x-www-form-urlencoded" {
s.handleFormURLEncoded(w, r, request)
return
Expand All @@ -71,11 +74,17 @@ func (s Service) handleFormURLEncoded(w http.ResponseWriter, r *http.Request, re

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

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

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

default:
s.handlePost(w, r, request, method)
}
Expand Down Expand Up @@ -107,11 +116,14 @@ func (s Service) handleMultipart(w http.ResponseWriter, r *http.Request, request

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

fibr.SetRouteTag(r.Context(), "POST chunk")
s.uploadChunk(w, r, request, values["filename"], chunkNumber, file)
} else {
fibr.SetRouteTag(r.Context(), "POST merge")
s.mergeChunk(w, r, request, values)
}
} else {
fibr.SetRouteTag(r.Context(), "POST upload")
s.upload(w, r, request, values, file)
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/crud/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
Expand Down Expand Up @@ -61,6 +62,8 @@ 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(), "PATCH rename")

if !request.CanEdit {
s.error(w, r, request, model.WrapForbidden(ErrNotAuthorized))
return
Expand Down
15 changes: 15 additions & 0 deletions pkg/fibr/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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 @@ -12,3 +17,13 @@ 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 7464731

Please sign in to comment.