Skip to content

Commit

Permalink
refactor: Using startspan and fixing some span
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed May 22, 2022
1 parent 38f344f commit b1eb3d5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 51 deletions.
17 changes: 7 additions & 10 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/query"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"go.opentelemetry.io/otel/trace"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

func (a App) getWithMessage(w http.ResponseWriter, r *http.Request, request provider.Request, message renderer.Message) (renderer.Page, error) {
Expand Down Expand Up @@ -139,12 +139,8 @@ func (a App) handleDir(w http.ResponseWriter, r *http.Request, request provider.
}

func (a App) listFiles(r *http.Request, request provider.Request, item absto.Item) (items []absto.Item, err error) {
ctx := r.Context()
if a.tracer != nil {
var span trace.Span
ctx, span = a.tracer.Start(r.Context(), "files")
defer span.End()
}
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "files")
defer end()

if query.GetBool(r, "search") {
items, err = a.searchFiles(r, request)
Expand Down Expand Up @@ -178,7 +174,8 @@ func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provid
return
}

ctx := r.Context()
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "geojson")
defer end()

var hash string
if query.GetBool(r, "search") {
Expand All @@ -199,7 +196,7 @@ func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provid
w.Header().Add("Etag", etag)
w.WriteHeader(http.StatusOK)

done := r.Context().Done()
done := ctx.Done()
isDone := func() bool {
select {
case <-done:
Expand All @@ -222,7 +219,7 @@ func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provid
return
}

exif, err := a.exifApp.GetExifFor(r.Context(), item)
exif, err := a.exifApp.GetExifFor(ctx, item)
if err != nil {
logger.WithField("item", item.Pathname).Error("unable to get exif: %s", err)
continue
Expand Down
9 changes: 3 additions & 6 deletions pkg/crud/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/concurrent"
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"go.opentelemetry.io/otel/trace"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

const (
Expand All @@ -38,11 +38,8 @@ func (a App) getCover(ctx context.Context, request provider.Request, files []abs

// List render directory web view of given dirPath
func (a App) List(ctx context.Context, request provider.Request, message renderer.Message, item absto.Item, files []absto.Item) (renderer.Page, error) {
if a.tracer != nil {
var span trace.Span
ctx, span = a.tracer.Start(ctx, "list")
defer span.End()
}
ctx, end := tracer.StartSpan(ctx, a.tracer, "list")
defer end()

items := make([]provider.RenderItem, len(files))
wg := concurrent.NewLimited(6)
Expand Down
10 changes: 3 additions & 7 deletions pkg/crud/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ViBiOh/fibr/pkg/thumbnail"
httpModel "github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"go.opentelemetry.io/otel/trace"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

const (
Expand Down Expand Up @@ -138,12 +138,8 @@ func (a App) searchFiles(r *http.Request, request provider.Request) (items []abs
}

func (a App) search(r *http.Request, request provider.Request, files []absto.Item) (renderer.Page, error) {
ctx := r.Context()
if a.tracer != nil {
var span trace.Span
ctx, span = a.tracer.Start(ctx, "search")
defer span.End()
}
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "search")
defer end()

items := make([]provider.RenderItem, len(files))
var hasMap bool
Expand Down
10 changes: 3 additions & 7 deletions pkg/crud/story.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ import (
"github.com/ViBiOh/fibr/pkg/thumbnail"
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"go.opentelemetry.io/otel/trace"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

func (a App) story(r *http.Request, request provider.Request, item absto.Item, files []absto.Item) (renderer.Page, error) {
ctx := r.Context()
if a.tracer != nil {
var span trace.Span
ctx, span = a.tracer.Start(ctx, "story")
defer span.End()
}
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "story")
defer end()

items := make([]provider.StoryItem, 0, len(files))
var cover map[string]any
Expand Down
16 changes: 5 additions & 11 deletions pkg/exif/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"go.opentelemetry.io/otel/trace"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
)

var (
Expand All @@ -23,11 +23,8 @@ func (a App) GetExifFor(ctx context.Context, item absto.Item) (exas.Exif, error)
return exas.Exif{}, nil
}

if a.tracer != nil {
var span trace.Span
ctx, span = a.tracer.Start(ctx, "aggregate")
defer span.End()
}
ctx, end := tracer.StartSpan(ctx, a.tracer, "get_exif")
defer end()

exif, err := a.loadExif(ctx, item)
if err != nil && !absto.IsNotExist(err) {
Expand All @@ -48,11 +45,8 @@ func (a App) GetAggregateFor(ctx context.Context, item absto.Item) (provider.Agg
return provider.Aggregate{}, nil
}

if a.tracer != nil {
var span trace.Span
ctx, span = a.tracer.Start(ctx, "aggregate")
defer span.End()
}
ctx, end := tracer.StartSpan(ctx, a.tracer, "aggregate")
defer end()

aggregate, err := a.loadAggregate(ctx, item)
if err != nil && !absto.IsNotExist(err) {
Expand Down
12 changes: 2 additions & 10 deletions pkg/provider/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,7 @@ func (e EventBus) Start(done <-chan struct{}, storageApp absto.Storage, renamers

go func() {
for event := range e.bus {
var span trace.Span
ctx := context.Background()

if e.tracer != nil {
ctx, span = e.tracer.Start(ctx, "event")
span.SetAttributes(attribute.String("type", event.Type.String()))
}
ctx, end := tracer.StartSpan(context.Background(), e.tracer, "event", trace.WithAttributes(attribute.String("type", event.Type.String())))

if event.Type == RenameEvent && event.Item.IsDir {
RenameDirectory(ctx, storageApp, renamers, event.Item, *event.New)
Expand All @@ -344,9 +338,7 @@ func (e EventBus) Start(done <-chan struct{}, storageApp absto.Storage, renamers
consumer(ctx, event)
}

if span != nil {
span.End()
}
end()
e.increaseMetric(event, "done")
}
}()
Expand Down

0 comments on commit b1eb3d5

Please sign in to comment.