A continuation of HomeBox the inventory and organization system built for the Home User
chore: further cleanup and add span to attachments
Matthew Kilgorebc6c696a09ae
diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.goindex b8dd6466..b2941f2c 100644--- a/backend/internal/data/repo/repo_item_attachments.go+++ b/backend/internal/data/repo/repo_item_attachments.go@@ -14,23 +14,24 @@ import ("strings""time"+ "github.com/sysadminsmedia/homebox/backend/internal/data/ent"+ "github.com/sysadminsmedia/homebox/backend/internal/data/ent/attachment"+ "github.com/sysadminsmedia/homebox/backend/internal/data/ent/group"+ "github.com/sysadminsmedia/homebox/backend/internal/data/ent/item"+ "github.com/sysadminsmedia/homebox/backend/internal/sys/config"+ "github.com/sysadminsmedia/homebox/backend/pkgs/utils"+"github.com/evanoberholster/imagemeta""github.com/gen2brain/avif""github.com/gen2brain/heic""github.com/gen2brain/jpegxl""github.com/gen2brain/webp"+ "github.com/google/uuid""github.com/rs/zerolog/log"- "github.com/sysadminsmedia/homebox/backend/internal/data/ent/group"- "github.com/sysadminsmedia/homebox/backend/internal/sys/config"- "github.com/sysadminsmedia/homebox/backend/pkgs/utils""github.com/zeebo/blake3"+ "go.opentelemetry.io/otel""golang.org/x/image/draw"- "github.com/google/uuid"- "github.com/sysadminsmedia/homebox/backend/internal/data/ent"- "github.com/sysadminsmedia/homebox/backend/internal/data/ent/attachment"- "github.com/sysadminsmedia/homebox/backend/internal/data/ent/item"-"gocloud.dev/blob"_ "gocloud.dev/blob/azureblob"_ "gocloud.dev/blob/fileblob"@@ -162,6 +163,9 @@ func (r *AttachmentRepo) GetConnString() string {}func (r *AttachmentRepo) Create(ctx context.Context, itemID uuid.UUID, doc ItemCreateAttachment, typ attachment.Type, primary bool) (*ent.Attachment, error) {+ ctx, span := otel.Tracer("data").Start(ctx, "repo.AttachmentRepo.Create")+ defer span.End()+tx, err := r.db.Tx(ctx)if err != nil {return nil, err@@ -377,6 +381,9 @@ func (r *AttachmentRepo) Update(ctx context.Context, gid uuid.UUID, id uuid.UUID}func (r *AttachmentRepo) Delete(ctx context.Context, gid uuid.UUID, id uuid.UUID) error {+ ctx, span := otel.Tracer("data").Start(ctx, "repo.AttachmentRepo.Delete")+ defer span.End()+// Validate that the attachment belongs to the specified groupdoc, err := r.db.Attachment.Query().Where(@@ -453,6 +460,9 @@ func (r *AttachmentRepo) Rename(ctx context.Context, gid uuid.UUID, id uuid.UUID//nolint:gocyclofunc (r *AttachmentRepo) CreateThumbnail(ctx context.Context, groupId, attachmentId uuid.UUID, title string, path string) error {+ ctx, span := otel.Tracer("data").Start(ctx, "repo.AttachmentRepo.CreateThumbnail")+ defer span.End()+log.Debug().Msg("starting thumbnail creation")tx, err := r.db.Tx(ctx)if err != nil {@@ -696,6 +706,9 @@ func (r *AttachmentRepo) CreateThumbnail(ctx context.Context, groupId, attachmen}func (r *AttachmentRepo) CreateMissingThumbnails(ctx context.Context, groupId uuid.UUID) (int, error) {+ ctx, span := otel.Tracer("data").Start(ctx, "repo.AttachmentRepo.CreateMissingThumbnails")+ defer span.End()+attachments, err := r.db.Attachment.Query().Where(attachment.HasItemWith(item.HasGroupWith(group.ID(groupId))),@@ -751,6 +764,9 @@ type UploadResult struct {}func (r *AttachmentRepo) UploadFile(ctx context.Context, itemGroup *ent.Group, doc ItemCreateAttachment) (UploadResult, error) {+ ctx, span := otel.Tracer("data").Start(ctx, "repo.AttachmentRepo.UploadFile")+ defer span.End()+// Prepare for the hashing of the file contentshashOut := make([]byte, 32)@@ -849,16 +865,24 @@ func calculateThumbnailDimensions(origWidth, origHeight, maxWidth, maxHeight int// processThumbnailFromImage handles the common thumbnail processing logic after image decoding// Returns the thumbnail file path or an errorfunc (r *AttachmentRepo) processThumbnailFromImage(ctx context.Context, groupId uuid.UUID, img image.Image, title string, orientation uint16) (UploadResult, error) {+ ctx, span := otel.Tracer("data").Start(ctx, "repo.AttachmentRepo.processThumbnailFromImage")+ defer span.End()+bounds := img.Bounds()++ span.AddEvent("applying EXIF orientation")// Apply EXIF orientation if neededif orientation > 1 {img = utils.ApplyOrientation(img, orientation)bounds = img.Bounds()}++ span.AddEvent("resizing image")newWidth, newHeight := calculateThumbnailDimensions(bounds.Dx(), bounds.Dy(), r.thumbnail.Width, r.thumbnail.Height)dst := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight))draw.CatmullRom.Scale(dst, dst.Rect, img, img.Bounds(), draw.Over, nil)+ span.AddEvent("encoding thumbnail as webp")buf := new(bytes.Buffer)err := webp.Encode(buf, dst, webp.Options{Quality: 80, Lossless: false})if err != nil {diff --git a/backend/internal/sys/otel/otel.go b/backend/internal/sys/otel/otel.goindex 7b535c86..95e1c3f6 100644--- a/backend/internal/sys/otel/otel.go+++ b/backend/internal/sys/otel/otel.go@@ -148,9 +148,17 @@ func NewProvider(ctx context.Context, cfg *config.OTelConfig, buildVersion strinsampler = sdktrace.TraceIDRatioBased(cfg.SampleRate)}+ // Create batch processor with span filtering+ batchProcessor := sdktrace.NewBatchSpanProcessor(traceExporter)+ // We ignore these spans because they create unneeded noise and extra data we don't need+ ignoredSpans := []string{+ "gocloud.dev/pubsub.driver.Subscription.ReceiveBatch",+ }+ filteredProcessor := newSpanFilterProcessor(batchProcessor, ignoredSpans)+// Create trace providertp = sdktrace.NewTracerProvider(- sdktrace.WithBatcher(traceExporter),+ sdktrace.WithSpanProcessor(filteredProcessor),sdktrace.WithResource(res),sdktrace.WithSampler(sdktrace.ParentBased(sampler)),)@@ -502,3 +510,41 @@ func (e *noopLogExporter) ForceFlush(_ context.Context) error {func (e *noopLogExporter) Shutdown(_ context.Context) error {return nil}++// spanFilterProcessor wraps a SpanProcessor and filters spans based on their name.+type spanFilterProcessor struct {+ next sdktrace.SpanProcessor+ ignoredSpans map[string]bool+}++// newSpanFilterProcessor creates a new span filter processor that wraps another processor.+func newSpanFilterProcessor(next sdktrace.SpanProcessor, ignoredSpans []string) sdktrace.SpanProcessor {+ ignored := make(map[string]bool, len(ignoredSpans))+ for _, span := range ignoredSpans {+ ignored[span] = true+ }+ return &spanFilterProcessor{+ next: next,+ ignoredSpans: ignored,+ }+}++func (s *spanFilterProcessor) OnStart(parent context.Context, span sdktrace.ReadWriteSpan) {+ if !s.ignoredSpans[span.Name()] {+ s.next.OnStart(parent, span)+ }+}++func (s *spanFilterProcessor) OnEnd(span sdktrace.ReadOnlySpan) {+ if !s.ignoredSpans[span.Name()] {+ s.next.OnEnd(span)+ }+}++func (s *spanFilterProcessor) Shutdown(ctx context.Context) error {+ return s.next.Shutdown(ctx)+}++func (s *spanFilterProcessor) ForceFlush(ctx context.Context) error {+ return s.next.ForceFlush(ctx)+}