|
| 1 | +package monitoring |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "sync" |
| 6 | + |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | +) |
| 9 | + |
| 10 | +// SQS HT-FIFO partition action labels. Stable string set so |
| 11 | +// dashboards / alerts can rely on the values not changing. |
| 12 | +const ( |
| 13 | + SQSPartitionActionSend = "send" |
| 14 | + SQSPartitionActionReceive = "receive" |
| 15 | + SQSPartitionActionDelete = "delete" |
| 16 | + |
| 17 | + // sqsMaxTrackedQueues caps the number of distinct queue names |
| 18 | + // the metrics layer will emit a per-(queue, partition, action) |
| 19 | + // series for. Any queue beyond this cap collapses to the |
| 20 | + // _other label so a misbehaving caller (e.g. a script that |
| 21 | + // generates random queue names) cannot blow up the |
| 22 | + // Prometheus cardinality budget. Mirrors dynamoMaxTrackedTables. |
| 23 | + sqsMaxTrackedQueues = 512 |
| 24 | + |
| 25 | + // sqsQueueOverflow is the placeholder label used when a queue |
| 26 | + // name is not in the tracked set (cap exceeded). Operators see |
| 27 | + // the overflow as a single _other series and know to look at |
| 28 | + // the application logs for the real names. |
| 29 | + sqsQueueOverflow = "_other" |
| 30 | +) |
| 31 | + |
| 32 | +// SQSPartitionObserver records per-(queue, partition, action) |
| 33 | +// counters for HT-FIFO operations. The interface is small so |
| 34 | +// adapter call sites can pass a no-op observer in tests without |
| 35 | +// pulling in the full Prometheus registry. |
| 36 | +type SQSPartitionObserver interface { |
| 37 | + // ObservePartitionMessage increments the |
| 38 | + // sqs_partition_messages_total counter for one operation on |
| 39 | + // one (queue, partition) pair. Action must be one of |
| 40 | + // SQSPartitionActionSend / Receive / Delete; any other value |
| 41 | + // is silently dropped so a typo at a future call site cannot |
| 42 | + // crash the process. |
| 43 | + ObservePartitionMessage(queue string, partition uint32, action string) |
| 44 | +} |
| 45 | + |
| 46 | +// SQSMetrics owns the Prometheus counter for HT-FIFO partition |
| 47 | +// operations. Mirrors DynamoDBMetrics' shape: per-Registry |
| 48 | +// instance, label-cardinality-bounded by sqsMaxTrackedQueues. |
| 49 | +type SQSMetrics struct { |
| 50 | + partitionMessages *prometheus.CounterVec |
| 51 | + |
| 52 | + mu sync.Mutex |
| 53 | + trackedQueues map[string]struct{} |
| 54 | +} |
| 55 | + |
| 56 | +func newSQSMetrics(registerer prometheus.Registerer) *SQSMetrics { |
| 57 | + m := &SQSMetrics{ |
| 58 | + partitionMessages: prometheus.NewCounterVec( |
| 59 | + prometheus.CounterOpts{ |
| 60 | + Name: "elastickv_sqs_partition_messages_total", |
| 61 | + Help: "Total HT-FIFO partition operations by queue, partition, and action (send / receive / delete). Non-zero only for queues with PartitionCount > 1; use to spot uneven MessageGroupId distributions across partitions.", |
| 62 | + }, |
| 63 | + []string{"queue", "partition", "action"}, |
| 64 | + ), |
| 65 | + trackedQueues: map[string]struct{}{}, |
| 66 | + } |
| 67 | + registerer.MustRegister(m.partitionMessages) |
| 68 | + return m |
| 69 | +} |
| 70 | + |
| 71 | +// ObservePartitionMessage implements SQSPartitionObserver. The |
| 72 | +// (queue, action) pair is validated and (queue) is collapsed to |
| 73 | +// the overflow label past sqsMaxTrackedQueues distinct names. |
| 74 | +func (m *SQSMetrics) ObservePartitionMessage(queue string, partition uint32, action string) { |
| 75 | + if m == nil { |
| 76 | + return |
| 77 | + } |
| 78 | + if !sqsValidPartitionAction(action) { |
| 79 | + return |
| 80 | + } |
| 81 | + if queue == "" { |
| 82 | + // Defensive: an empty queue name would collapse all |
| 83 | + // requests onto a single series — almost certainly a bug |
| 84 | + // at the call site. Drop silently rather than emit |
| 85 | + // poisoned data. |
| 86 | + return |
| 87 | + } |
| 88 | + queueLabel := m.queueLabelForCardinalityBudget(queue) |
| 89 | + // WithLabelValues avoids the prometheus.Labels map allocation |
| 90 | + // on every observe call. Label order matches the |
| 91 | + // NewCounterVec declaration: queue, partition, action. |
| 92 | + // Mirrors DynamoDBMetrics. |
| 93 | + m.partitionMessages.WithLabelValues( |
| 94 | + queueLabel, |
| 95 | + strconv.FormatUint(uint64(partition), 10), |
| 96 | + action, |
| 97 | + ).Inc() |
| 98 | +} |
| 99 | + |
| 100 | +// queueLabelForCardinalityBudget returns queue if the metric has |
| 101 | +// already emitted a series for it OR there is room in the |
| 102 | +// tracked-queues set; returns sqsQueueOverflow otherwise. The |
| 103 | +// cap-and-collapse pattern mirrors DynamoDBMetrics.tableLabel |
| 104 | +// so a misbehaving caller cannot exhaust the Prometheus |
| 105 | +// cardinality budget. |
| 106 | +func (m *SQSMetrics) queueLabelForCardinalityBudget(queue string) string { |
| 107 | + m.mu.Lock() |
| 108 | + defer m.mu.Unlock() |
| 109 | + if _, ok := m.trackedQueues[queue]; ok { |
| 110 | + return queue |
| 111 | + } |
| 112 | + if len(m.trackedQueues) >= sqsMaxTrackedQueues { |
| 113 | + return sqsQueueOverflow |
| 114 | + } |
| 115 | + m.trackedQueues[queue] = struct{}{} |
| 116 | + return queue |
| 117 | +} |
| 118 | + |
| 119 | +// sqsValidPartitionAction returns true iff action is one of the |
| 120 | +// stable label values. Keeps a typo at the call site (e.g. |
| 121 | +// "Send" vs "send") from polluting the metric. |
| 122 | +func sqsValidPartitionAction(action string) bool { |
| 123 | + switch action { |
| 124 | + case SQSPartitionActionSend, SQSPartitionActionReceive, SQSPartitionActionDelete: |
| 125 | + return true |
| 126 | + } |
| 127 | + return false |
| 128 | +} |
0 commit comments