|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "go.opentelemetry.io/otel/attribute" |
| 8 | + "go.opentelemetry.io/otel/metric" |
| 9 | +) |
| 10 | + |
| 11 | +// BatchWriterMetrics holds all metrics instruments for the BatchWriter |
| 12 | +type BatchWriterMetrics struct { |
| 13 | + // Counters |
| 14 | + BatchesProcessedTotal metric.Int64Counter |
| 15 | + RecordsProcessedTotal metric.Int64Counter |
| 16 | + ChannelOverflowsTotal metric.Int64Counter |
| 17 | + NonBlockingFallbacksTotal metric.Int64Counter |
| 18 | + |
| 19 | + // Histograms |
| 20 | + BatchSizeHistogram metric.Int64Histogram |
| 21 | + FlushDurationMs metric.Float64Histogram |
| 22 | + WorkerQueueWaitTimeMs metric.Float64Histogram |
| 23 | + |
| 24 | + // Gauges |
| 25 | + QueueDepth metric.Int64UpDownCounter |
| 26 | + WorkerBufferSizes []metric.Int64UpDownCounter |
| 27 | + ActiveWorkers metric.Int64UpDownCounter |
| 28 | +} |
| 29 | + |
| 30 | +// InitializeBatchWriterMetrics creates and registers all BatchWriter metrics |
| 31 | +func InitializeBatchWriterMetrics(ctx context.Context, meter metric.Meter) (*BatchWriterMetrics, error) { |
| 32 | + m := &BatchWriterMetrics{} |
| 33 | + var err error |
| 34 | + |
| 35 | + // Initialize counters |
| 36 | + m.BatchesProcessedTotal, err = meter.Int64Counter( |
| 37 | + "fdb_batch_writer_batches_processed_total", |
| 38 | + metric.WithDescription("Total number of batches processed by BatchWriter"), |
| 39 | + ) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + m.RecordsProcessedTotal, err = meter.Int64Counter( |
| 45 | + "fdb_batch_writer_records_processed_total", |
| 46 | + metric.WithDescription("Total number of records processed by BatchWriter"), |
| 47 | + ) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + m.ChannelOverflowsTotal, err = meter.Int64Counter( |
| 53 | + "fdb_batch_writer_channel_overflows_total", |
| 54 | + metric.WithDescription("Number of times worker channels reached capacity"), |
| 55 | + ) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + m.NonBlockingFallbacksTotal, err = meter.Int64Counter( |
| 61 | + "fdb_batch_writer_non_blocking_fallbacks_total", |
| 62 | + metric.WithDescription("Number of times non-blocking fallback was used"), |
| 63 | + ) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + // Initialize histograms |
| 69 | + m.BatchSizeHistogram, err = meter.Int64Histogram( |
| 70 | + "fdb_batch_writer_batch_size", |
| 71 | + metric.WithDescription("Distribution of batch sizes processed by BatchWriter"), |
| 72 | + ) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + m.FlushDurationMs, err = meter.Float64Histogram( |
| 78 | + "fdb_batch_writer_flush_duration_milliseconds", |
| 79 | + metric.WithDescription("Time taken to flush batches to disk in milliseconds"), |
| 80 | + ) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + m.WorkerQueueWaitTimeMs, err = meter.Float64Histogram( |
| 86 | + "fdb_batch_writer_queue_wait_milliseconds", |
| 87 | + metric.WithDescription("Time requests spend waiting in worker queues in milliseconds"), |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + // Initialize gauges |
| 94 | + m.QueueDepth, err = meter.Int64UpDownCounter( |
| 95 | + "fdb_batch_writer_queue_depth", |
| 96 | + metric.WithDescription("Current depth of all BatchWriter queues combined"), |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + m.ActiveWorkers, err = meter.Int64UpDownCounter( |
| 103 | + "fdb_batch_writer_active_workers", |
| 104 | + metric.WithDescription("Number of currently active worker goroutines"), |
| 105 | + ) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + return m, nil |
| 111 | +} |
| 112 | + |
| 113 | +// RecordBatchProcessed records metrics for a processed batch |
| 114 | +func (m *BatchWriterMetrics) RecordBatchProcessed(ctx context.Context, workerID int, batchSize int, duration time.Duration) { |
| 115 | + attrs := attribute.NewSet(attribute.Int("worker_id", workerID)) |
| 116 | + |
| 117 | + m.BatchesProcessedTotal.Add(ctx, 1, metric.WithAttributeSet(attrs)) |
| 118 | + m.RecordsProcessedTotal.Add(ctx, int64(batchSize), metric.WithAttributeSet(attrs)) |
| 119 | + m.BatchSizeHistogram.Record(ctx, int64(batchSize), metric.WithAttributeSet(attrs)) |
| 120 | + m.FlushDurationMs.Record(ctx, float64(duration.Milliseconds()), metric.WithAttributeSet(attrs)) |
| 121 | +} |
| 122 | + |
| 123 | +// RecordWorkerQueueWait records the time a request spends waiting in a worker queue |
| 124 | +func (m *BatchWriterMetrics) RecordWorkerQueueWait(ctx context.Context, workerID int, duration time.Duration) { |
| 125 | + attrs := attribute.NewSet(attribute.Int("worker_id", workerID)) |
| 126 | + m.WorkerQueueWaitTimeMs.Record(ctx, float64(duration.Milliseconds()), metric.WithAttributeSet(attrs)) |
| 127 | +} |
| 128 | + |
| 129 | +// RecordChannelOverflow records a channel overflow event |
| 130 | +func (m *BatchWriterMetrics) RecordChannelOverflow(ctx context.Context, workerID int) { |
| 131 | + attrs := attribute.NewSet(attribute.Int("worker_id", workerID)) |
| 132 | + m.ChannelOverflowsTotal.Add(ctx, 1, metric.WithAttributeSet(attrs)) |
| 133 | +} |
| 134 | + |
| 135 | +// RecordNonBlockingFallback records when the non-blocking fallback is used |
| 136 | +func (m *BatchWriterMetrics) RecordNonBlockingFallback(ctx context.Context, workerID int) { |
| 137 | + attrs := attribute.NewSet(attribute.Int("worker_id", workerID)) |
| 138 | + m.NonBlockingFallbacksTotal.Add(ctx, 1, metric.WithAttributeSet(attrs)) |
| 139 | +} |
| 140 | + |
| 141 | +// UpdateQueueDepth updates the queue depth counter |
| 142 | +func (m *BatchWriterMetrics) UpdateQueueDepth(ctx context.Context, delta int64) { |
| 143 | + m.QueueDepth.Add(ctx, delta) |
| 144 | +} |
| 145 | + |
| 146 | +// UpdateWorkerBuffer updates the buffer size for a specific worker |
| 147 | +func (m *BatchWriterMetrics) UpdateWorkerBuffer(ctx context.Context, workerID int, size int64) { |
| 148 | + if workerID < len(m.WorkerBufferSizes) { |
| 149 | + attrs := attribute.NewSet(attribute.Int("worker_id", workerID)) |
| 150 | + m.WorkerBufferSizes[workerID].Add(ctx, size, metric.WithAttributeSet(attrs)) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// InitializeWorkerBufferMetrics creates separate buffer size metrics for each worker |
| 155 | +func (m *BatchWriterMetrics) InitializeWorkerBufferMetrics(ctx context.Context, meter metric.Meter, workerCount int) error { |
| 156 | + m.WorkerBufferSizes = make([]metric.Int64UpDownCounter, workerCount) |
| 157 | + |
| 158 | + for i := 0; i < workerCount; i++ { |
| 159 | + var err error |
| 160 | + m.WorkerBufferSizes[i], err = meter.Int64UpDownCounter( |
| 161 | + "fdb_batch_writer_worker_buffer_size", |
| 162 | + metric.WithDescription("Current size of worker buffer"), |
| 163 | + ) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments