|
| 1 | +package instrumented |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + |
| 10 | + "github.com/Prescott-Data/nexus-framework/nexus-broker/internal/domain" |
| 11 | + "github.com/Prescott-Data/nexus-framework/nexus-broker/internal/repository" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + dbOpDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 16 | + Name: "nexus_db_operation_duration_seconds", |
| 17 | + Help: "Duration of database operations by repository and method", |
| 18 | + Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0}, |
| 19 | + }, []string{"repo", "method"}) |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + prometheus.MustRegister(dbOpDuration) |
| 24 | +} |
| 25 | + |
| 26 | +func observe(repo, method string, start time.Time) { |
| 27 | + dbOpDuration.WithLabelValues(repo, method).Observe(time.Since(start).Seconds()) |
| 28 | +} |
| 29 | + |
| 30 | +// --- ConnectionRepository decorator --- |
| 31 | + |
| 32 | +// ConnectionRepository wraps repository.ConnectionRepository with latency instrumentation. |
| 33 | +type ConnectionRepository struct { |
| 34 | + inner repository.ConnectionRepository |
| 35 | +} |
| 36 | + |
| 37 | +// NewConnectionRepository wraps a ConnectionRepository with Prometheus latency histograms. |
| 38 | +func NewConnectionRepository(inner repository.ConnectionRepository) repository.ConnectionRepository { |
| 39 | + return &ConnectionRepository{inner: inner} |
| 40 | +} |
| 41 | + |
| 42 | +func (r *ConnectionRepository) Create(ctx context.Context, conn *domain.Connection) error { |
| 43 | + defer observe("connection", "Create", time.Now()) |
| 44 | + return r.inner.Create(ctx, conn) |
| 45 | +} |
| 46 | + |
| 47 | +func (r *ConnectionRepository) GetPending(ctx context.Context, id uuid.UUID) (*domain.Connection, error) { |
| 48 | + defer observe("connection", "GetPending", time.Now()) |
| 49 | + return r.inner.GetPending(ctx, id) |
| 50 | +} |
| 51 | + |
| 52 | +func (r *ConnectionRepository) GetWithProvider(ctx context.Context, id uuid.UUID) (*domain.ConnectionWithProvider, error) { |
| 53 | + defer observe("connection", "GetWithProvider", time.Now()) |
| 54 | + return r.inner.GetWithProvider(ctx, id) |
| 55 | +} |
| 56 | + |
| 57 | +func (r *ConnectionRepository) GetReturnURL(ctx context.Context, id uuid.UUID) (string, error) { |
| 58 | + defer observe("connection", "GetReturnURL", time.Now()) |
| 59 | + return r.inner.GetReturnURL(ctx, id) |
| 60 | +} |
| 61 | + |
| 62 | +func (r *ConnectionRepository) UpdateStatus(ctx context.Context, id uuid.UUID, status string) error { |
| 63 | + defer observe("connection", "UpdateStatus", time.Now()) |
| 64 | + return r.inner.UpdateStatus(ctx, id, status) |
| 65 | +} |
| 66 | + |
| 67 | +func (r *ConnectionRepository) CountByStatus(ctx context.Context) (map[string]int64, error) { |
| 68 | + defer observe("connection", "CountByStatus", time.Now()) |
| 69 | + return r.inner.CountByStatus(ctx) |
| 70 | +} |
| 71 | + |
| 72 | +// --- TokenRepository decorator --- |
| 73 | + |
| 74 | +// TokenRepository wraps repository.TokenRepository with latency instrumentation. |
| 75 | +type TokenRepository struct { |
| 76 | + inner repository.TokenRepository |
| 77 | +} |
| 78 | + |
| 79 | +// NewTokenRepository wraps a TokenRepository with Prometheus latency histograms. |
| 80 | +func NewTokenRepository(inner repository.TokenRepository) repository.TokenRepository { |
| 81 | + return &TokenRepository{inner: inner} |
| 82 | +} |
| 83 | + |
| 84 | +func (r *TokenRepository) Upsert(ctx context.Context, token *domain.Token) error { |
| 85 | + defer observe("token", "Upsert", time.Now()) |
| 86 | + return r.inner.Upsert(ctx, token) |
| 87 | +} |
| 88 | + |
| 89 | +func (r *TokenRepository) Get(ctx context.Context, connectionID uuid.UUID) (*domain.Token, error) { |
| 90 | + defer observe("token", "Get", time.Now()) |
| 91 | + return r.inner.Get(ctx, connectionID) |
| 92 | +} |
0 commit comments