-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathaggregator.go
More file actions
251 lines (215 loc) · 6.59 KB
/
aggregator.go
File metadata and controls
251 lines (215 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package telemetry
import (
"context"
"sync"
"time"
"github.com/databricks/databricks-sql-go/logger"
)
// metricsAggregator aggregates metrics by statement and batches for export.
type metricsAggregator struct {
mu sync.RWMutex
statements map[string]*statementMetrics
batch []*telemetryMetric
exporter *telemetryExporter
batchSize int
flushInterval time.Duration
stopCh chan struct{}
flushTimer *time.Ticker
closeOnce sync.Once
ctx context.Context // Cancellable context for in-flight exports
cancel context.CancelFunc // Cancels ctx on close
exportSem chan struct{} // Bounds concurrent export goroutines
}
// statementMetrics holds aggregated metrics for a statement.
type statementMetrics struct {
statementID string
sessionID string
totalLatency time.Duration
chunkCount int
bytesDownloaded int64
pollCount int
errors []string
tags map[string]interface{}
}
// newMetricsAggregator creates a new metrics aggregator.
func newMetricsAggregator(exporter *telemetryExporter, cfg *Config) *metricsAggregator {
ctx, cancel := context.WithCancel(context.Background())
agg := &metricsAggregator{
statements: make(map[string]*statementMetrics),
batch: make([]*telemetryMetric, 0, cfg.BatchSize),
exporter: exporter,
batchSize: cfg.BatchSize,
flushInterval: cfg.FlushInterval,
stopCh: make(chan struct{}),
ctx: ctx,
cancel: cancel,
exportSem: make(chan struct{}, 8), // Bound to 8 concurrent exports
}
// Start background flush timer
go agg.flushLoop()
return agg
}
// recordMetric records a metric for aggregation.
func (agg *metricsAggregator) recordMetric(ctx context.Context, metric *telemetryMetric) {
// Swallow all errors
defer func() {
if r := recover(); r != nil {
logger.Debug().Msgf("telemetry: recordMetric panic: %v", r)
}
}()
agg.mu.Lock()
defer agg.mu.Unlock()
switch metric.metricType {
case "connection":
// Emit connection events immediately: connection lifecycle events must be captured
// before the connection closes, as we won't have another opportunity to flush
agg.batch = append(agg.batch, metric)
if len(agg.batch) >= agg.batchSize {
agg.flushUnlocked(ctx)
}
case "statement":
// Aggregate by statement ID
stmt, exists := agg.statements[metric.statementID]
if !exists {
stmt = &statementMetrics{
statementID: metric.statementID,
sessionID: metric.sessionID,
tags: make(map[string]interface{}),
}
agg.statements[metric.statementID] = stmt
}
// Update aggregated values
stmt.totalLatency += time.Duration(metric.latencyMs) * time.Millisecond
if chunkCount, ok := metric.tags["chunk_count"].(int); ok {
stmt.chunkCount += chunkCount
}
if bytes, ok := metric.tags["bytes_downloaded"].(int64); ok {
stmt.bytesDownloaded += bytes
}
if pollCount, ok := metric.tags["poll_count"].(int); ok {
stmt.pollCount += pollCount
}
// Store error if present
if metric.errorType != "" {
stmt.errors = append(stmt.errors, metric.errorType)
}
// Merge tags
for k, v := range metric.tags {
stmt.tags[k] = v
}
case "error":
// Check if terminal error
if metric.errorType != "" && isTerminalError(&simpleError{msg: metric.errorType}) {
// Flush terminal errors immediately: terminal errors often lead to connection
// termination. If we wait for the next batch/timer flush, this data may be lost
agg.batch = append(agg.batch, metric)
agg.flushUnlocked(ctx)
} else {
// Buffer non-terminal errors with statement
if stmt, exists := agg.statements[metric.statementID]; exists {
stmt.errors = append(stmt.errors, metric.errorType)
}
}
}
}
// completeStatement marks a statement as complete and emits aggregated metric.
func (agg *metricsAggregator) completeStatement(ctx context.Context, statementID string, failed bool) {
defer func() {
if r := recover(); r != nil {
logger.Debug().Msgf("telemetry: completeStatement panic: %v", r)
}
}()
agg.mu.Lock()
defer agg.mu.Unlock()
stmt, exists := agg.statements[statementID]
if !exists {
return
}
delete(agg.statements, statementID)
// Create aggregated metric
metric := &telemetryMetric{
metricType: "statement",
timestamp: time.Now(),
statementID: stmt.statementID,
sessionID: stmt.sessionID,
latencyMs: stmt.totalLatency.Milliseconds(),
tags: stmt.tags,
}
// Add aggregated counts
metric.tags["chunk_count"] = stmt.chunkCount
metric.tags["bytes_downloaded"] = stmt.bytesDownloaded
metric.tags["poll_count"] = stmt.pollCount
// Add error information if failed
if failed && len(stmt.errors) > 0 {
// Use the first error as the primary error type
metric.errorType = stmt.errors[0]
}
agg.batch = append(agg.batch, metric)
// Flush if batch full
if len(agg.batch) >= agg.batchSize {
agg.flushUnlocked(ctx)
}
}
// flushLoop runs periodic flush in background.
func (agg *metricsAggregator) flushLoop() {
agg.flushTimer = time.NewTicker(agg.flushInterval)
defer agg.flushTimer.Stop()
for {
select {
case <-agg.flushTimer.C:
agg.flush(agg.ctx) // Use cancellable context so exports stop on shutdown
case <-agg.stopCh:
return
}
}
}
// flush flushes pending metrics to exporter.
func (agg *metricsAggregator) flush(ctx context.Context) {
agg.mu.Lock()
defer agg.mu.Unlock()
agg.flushUnlocked(ctx)
}
// flushUnlocked flushes without locking (caller must hold lock).
func (agg *metricsAggregator) flushUnlocked(ctx context.Context) {
if len(agg.batch) == 0 {
return
}
// Copy batch and clear
metrics := make([]*telemetryMetric, len(agg.batch))
copy(metrics, agg.batch)
agg.batch = agg.batch[:0]
// Acquire semaphore slot; skip export if already at capacity to prevent goroutine leaks
select {
case agg.exportSem <- struct{}{}:
default:
logger.Debug().Msg("telemetry: export semaphore full, dropping metrics batch")
return
}
// Export asynchronously
go func() {
defer func() {
<-agg.exportSem
if r := recover(); r != nil {
logger.Debug().Msgf("telemetry: async export panic: %v", r)
}
}()
agg.exporter.export(ctx, metrics)
}()
}
// close stops the aggregator and flushes pending metrics.
// Safe to call multiple times — subsequent calls are no-ops for the stop/cancel step.
func (agg *metricsAggregator) close(ctx context.Context) error {
agg.closeOnce.Do(func() {
close(agg.stopCh)
agg.cancel() // Cancel in-flight periodic export goroutines
})
agg.flush(ctx)
return nil
}
// simpleError is a simple error implementation for testing.
type simpleError struct {
msg string
}
func (e *simpleError) Error() string {
return e.msg
}