forked from mtusi2026/mtusi2026lab6
127 lines
3.5 KiB
Go
127 lines
3.5 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"backend/internal/domain/port"
|
|
"context"
|
|
"errors"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/codes"
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"io"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type DBTracer struct{}
|
|
|
|
func (DBTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryStartData) context.Context {
|
|
ctx, _ = otel.Tracer("messenger").Start(ctx, "postgres.query", trace.WithSpanKind(trace.SpanKindClient))
|
|
return ctx
|
|
}
|
|
func (DBTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, d pgx.TraceQueryEndData) {
|
|
s := trace.SpanFromContext(ctx)
|
|
if d.Err != nil {
|
|
s.SetStatus(codes.Error, "database_error")
|
|
}
|
|
s.End()
|
|
}
|
|
|
|
type Objects struct{ port.Objects }
|
|
|
|
func (s Objects) Put(ctx context.Context, k string, r io.Reader, n int64, m string) error {
|
|
ctx, span := otel.Tracer("messenger").Start(ctx, "s3.put", trace.WithSpanKind(trace.SpanKindClient))
|
|
defer span.End()
|
|
e := s.Objects.Put(ctx, k, r, n, m)
|
|
if e != nil {
|
|
span.SetStatus(codes.Error, "storage_error")
|
|
}
|
|
return e
|
|
}
|
|
func (s Objects) Get(ctx context.Context, k string) (io.ReadCloser, error) {
|
|
ctx, span := otel.Tracer("messenger").Start(ctx, "s3.get", trace.WithSpanKind(trace.SpanKindClient))
|
|
r, e := s.Objects.Get(ctx, k)
|
|
if e != nil {
|
|
span.SetStatus(codes.Error, "storage_error")
|
|
span.End()
|
|
return nil, e
|
|
}
|
|
return &tracedReader{reader: r, span: span}, nil
|
|
}
|
|
|
|
// The caller owns Close, as for every io.ReadCloser. Keeping the span open until
|
|
// Close includes streaming body reads and late Close errors in the same boundary.
|
|
type tracedReader struct {
|
|
reader io.ReadCloser
|
|
span trace.Span
|
|
once sync.Once
|
|
closed atomic.Bool
|
|
closeError error
|
|
}
|
|
|
|
func (r *tracedReader) Read(p []byte) (int, error) {
|
|
if r.closed.Load() {
|
|
return 0, io.ErrClosedPipe
|
|
}
|
|
n, e := r.reader.Read(p)
|
|
if e != nil && !errors.Is(e, io.EOF) {
|
|
r.span.SetStatus(codes.Error, "storage_error")
|
|
}
|
|
return n, e
|
|
}
|
|
func (r *tracedReader) Close() error {
|
|
r.once.Do(func() {
|
|
r.closed.Store(true)
|
|
r.closeError = r.reader.Close()
|
|
if r.closeError != nil {
|
|
r.span.SetStatus(codes.Error, "storage_error")
|
|
}
|
|
r.span.End()
|
|
})
|
|
return r.closeError
|
|
}
|
|
func (s Objects) Stat(ctx context.Context, k string) (port.Object, error) {
|
|
ctx, span := otel.Tracer("messenger").Start(ctx, "s3.stat", trace.WithSpanKind(trace.SpanKindClient))
|
|
defer span.End()
|
|
r, e := s.Objects.Stat(ctx, k)
|
|
if e != nil {
|
|
span.SetStatus(codes.Error, "storage_error")
|
|
}
|
|
return r, e
|
|
}
|
|
|
|
type acquireKey struct{}
|
|
type acquireState struct {
|
|
telemetry *Telemetry
|
|
started time.Time
|
|
span trace.Span
|
|
}
|
|
|
|
func (DBTracer) TraceAcquireStart(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireStartData) context.Context {
|
|
t := current.Load()
|
|
if t == nil {
|
|
return ctx
|
|
}
|
|
t.poolPending.Add(1)
|
|
ctx, span := otel.Tracer("messenger").Start(ctx, "postgres.acquire", trace.WithSpanKind(trace.SpanKindClient))
|
|
return context.WithValue(ctx, acquireKey{}, acquireState{t, time.Now(), span})
|
|
}
|
|
func (DBTracer) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, data pgxpool.TraceAcquireEndData) {
|
|
s, ok := ctx.Value(acquireKey{}).(acquireState)
|
|
if !ok {
|
|
return
|
|
}
|
|
s.telemetry.poolPending.Add(-1)
|
|
outcome := "success"
|
|
if data.Err != nil {
|
|
outcome = "error"
|
|
s.span.SetStatus(codes.Error, "database_acquire_error")
|
|
}
|
|
s.telemetry.dbAcquire.Record(ctx, time.Since(s.started).Seconds(), metric.WithAttributes(attribute.String("outcome", outcome)))
|
|
s.span.End()
|
|
}
|