forked from mtusi2026/mtusi2026lab7
108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"mtusi-messenger/internal/platform/objectstore"
|
|
)
|
|
|
|
type tracedStore struct {
|
|
inner objectstore.Store
|
|
}
|
|
|
|
// WrapStore adds S3 client spans without recording object keys (keys contain
|
|
// chat_id) or presigned URLs.
|
|
func WrapStore(inner objectstore.Store) objectstore.Store {
|
|
if inner == nil {
|
|
return nil
|
|
}
|
|
return &tracedStore{inner: inner}
|
|
}
|
|
|
|
func (s *tracedStore) start(ctx context.Context, operation string) (context.Context, trace.Span) {
|
|
return otel.Tracer("messenger/s3").Start(ctx, "S3."+operation,
|
|
trace.WithSpanKind(trace.SpanKindClient),
|
|
trace.WithAttributes(
|
|
attribute.String("rpc.system", "aws.s3"),
|
|
attribute.String("rpc.service", "S3"),
|
|
attribute.String("rpc.method", operation),
|
|
attribute.String("aws.s3.operation", operation),
|
|
),
|
|
)
|
|
}
|
|
|
|
func (s *tracedStore) Ping(ctx context.Context) error {
|
|
ctx, span := s.start(ctx, "Ping")
|
|
defer span.End()
|
|
return s.inner.Ping(ctx)
|
|
}
|
|
|
|
func (s *tracedStore) Put(ctx context.Context, key string, body io.ReadSeeker, size int64, contentType string, metadata map[string]string) error {
|
|
ctx, span := s.start(ctx, "PutObject")
|
|
defer span.End()
|
|
span.SetAttributes(attribute.Int64("aws.s3.object.size", size))
|
|
return s.inner.Put(ctx, key, body, size, contentType, metadata)
|
|
}
|
|
|
|
type spanReadCloser struct {
|
|
inner io.ReadCloser
|
|
span trace.Span
|
|
}
|
|
|
|
func (r *spanReadCloser) Read(p []byte) (int, error) { return r.inner.Read(p) }
|
|
func (r *spanReadCloser) Close() error {
|
|
err := r.inner.Close()
|
|
r.span.End()
|
|
return err
|
|
}
|
|
|
|
func (s *tracedStore) Open(ctx context.Context, key string) (io.ReadCloser, objectstore.Object, error) {
|
|
ctx, span := s.start(ctx, "GetObject")
|
|
body, object, err := s.inner.Open(ctx, key)
|
|
if err != nil {
|
|
span.RecordError(errors.New(Redact(err.Error())))
|
|
span.End()
|
|
return nil, object, err
|
|
}
|
|
span.SetAttributes(attribute.Int64("aws.s3.object.size", object.Size))
|
|
return &spanReadCloser{inner: body, span: span}, object, nil
|
|
}
|
|
|
|
func (s *tracedStore) Head(ctx context.Context, key string) (objectstore.Object, error) {
|
|
ctx, span := s.start(ctx, "HeadObject")
|
|
defer span.End()
|
|
return s.inner.Head(ctx, key)
|
|
}
|
|
|
|
func (s *tracedStore) Copy(ctx context.Context, src, dst, contentType string, metadata map[string]string) error {
|
|
ctx, span := s.start(ctx, "CopyObject")
|
|
defer span.End()
|
|
return s.inner.Copy(ctx, src, dst, contentType, metadata)
|
|
}
|
|
|
|
func (s *tracedStore) Delete(ctx context.Context, key string) error {
|
|
ctx, span := s.start(ctx, "DeleteObject")
|
|
defer span.End()
|
|
return s.inner.Delete(ctx, key)
|
|
}
|
|
|
|
func (s *tracedStore) List(ctx context.Context, prefix string) ([]objectstore.Object, error) {
|
|
ctx, span := s.start(ctx, "ListObjects")
|
|
defer span.End()
|
|
return s.inner.List(ctx, prefix)
|
|
}
|
|
|
|
func (s *tracedStore) PresignPut(ctx context.Context, key string, size int64, ttl time.Duration) (string, time.Time, error) {
|
|
ctx, span := s.start(ctx, "PresignPut")
|
|
defer span.End()
|
|
span.SetAttributes(attribute.Int64("aws.s3.object.size", size), attribute.String("aws.s3.presign.ttl", ttl.String()))
|
|
return s.inner.PresignPut(ctx, key, size, ttl)
|
|
}
|