bc3da007db
Step 1 — OTel SDK: cmd/api and cmd/worker initialize a tracer provider that exports OTLP/HTTP to obs.88oakapps.com (Jaeger all-in-one). Sampling is AlwaysSample in dev (DEBUG=true) and TraceIDRatioBased(0.1) in prod, overridable via OTEL_TRACES_SAMPLER_ARG. Service names are honeydue-api and honeydue-worker. otelecho.Middleware opens a span per HTTP request. Step 2 — Manual spans: storage_service.Upload now takes ctx and emits storage.upload + b2.PutObject spans (size_bytes, key, mime_type, bucket, result attrs). APNs Send/SendWithCategory and FCM sendOne emit per-token spans with topic, status_code, reason. Asynq middleware emits asynq.handle:<task_type> per job with retry/payload attrs and records asynq_job_duration_seconds. Step 3 — Database: otelgorm plugin registered in database.Connect, so any SQL emitted via db.WithContext(ctx) attaches to the request span. Every repository now exposes WithContext(ctx) *XRepository as the migration helper. TaskService.ListTasks and GetTasksByResidence are migrated end-to-end (ctx threaded through handler → service → repo); remaining services adopt the same pattern incrementally — pre-migration methods still emit untraced SQL via the unchanged db field. OBS_TRACES_URL and OBS_INGEST_TOKEN flow from deploy/prod.env → honeydue-secrets → api+worker Deployments via secretKeyRef (optional). 02-setup-secrets.sh sources them from prod.env on next run; manifests mark both env vars optional so the deployment rolls without traces if the secret is absent. ch15 observability doc now lists what produces spans today vs the remaining migration work, with the explicit per-method pattern. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// WebhookEvent represents a processed webhook event for deduplication
|
|
type WebhookEvent struct {
|
|
ID uint `gorm:"primaryKey"`
|
|
EventID string `gorm:"column:event_id;size:255;not null;uniqueIndex:idx_provider_event_id"`
|
|
Provider string `gorm:"column:provider;size:20;not null;uniqueIndex:idx_provider_event_id"`
|
|
EventType string `gorm:"column:event_type;size:100;not null"`
|
|
ProcessedAt time.Time `gorm:"column:processed_at;autoCreateTime"`
|
|
PayloadHash string `gorm:"column:payload_hash;size:64"`
|
|
}
|
|
|
|
func (WebhookEvent) TableName() string {
|
|
return "webhook_event_log"
|
|
}
|
|
|
|
// WebhookEventRepository handles webhook event deduplication
|
|
type WebhookEventRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewWebhookEventRepository creates a new webhook event repository
|
|
func NewWebhookEventRepository(db *gorm.DB) *WebhookEventRepository {
|
|
return &WebhookEventRepository{db: db}
|
|
}
|
|
|
|
// HasProcessed checks if an event has already been processed
|
|
func (r *WebhookEventRepository) HasProcessed(provider, eventID string) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&WebhookEvent{}).
|
|
Where("provider = ? AND event_id = ?", provider, eventID).
|
|
Count(&count).Error
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
// RecordEvent records a processed webhook event
|
|
func (r *WebhookEventRepository) RecordEvent(provider, eventID, eventType, payloadHash string) error {
|
|
event := &WebhookEvent{
|
|
EventID: eventID,
|
|
Provider: provider,
|
|
EventType: eventType,
|
|
PayloadHash: payloadHash,
|
|
}
|
|
return r.db.Create(event).Error
|
|
}
|
|
|
|
// WithContext returns a copy of the repository whose underlying *gorm.DB carries
|
|
// the supplied context. SQL emitted via this copy gets attached to ctx's trace span
|
|
// (when otelgorm is registered) and respects ctx cancellation/deadlines.
|
|
func (r *WebhookEventRepository) WithContext(ctx context.Context) *WebhookEventRepository {
|
|
return &WebhookEventRepository{db: r.db.WithContext(ctx)}
|
|
}
|