-
Notifications
You must be signed in to change notification settings - Fork 55
[PECOBLR-1381][PECOBLR-1382] Implement telemetry Phase 6-7: Collection, Aggregation & Driver Integration #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
samikshya-db
wants to merge
16
commits into
main
Choose a base branch
from
stack/PECOBLR-1381-1382-telemetry-phase6-7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
38e41de
[PECOBLR-1143] Implement telemetry Phase 4-5: Export infrastructure a…
samikshya-db e12933d
Refactor telemetry config to use config overlay approach
samikshya-db f5b0e91
Implement config overlay pattern and refactor feature flags for exten…
samikshya-db b2fc43c
Fix lint issues: gofmt, errcheck, and staticcheck
samikshya-db 4b53634
Merge branch 'main' into stack/PECOBLR-1143-telemetry-phase4-5
samikshya-db ab13528
Fix SA9003 lint error: Add comment to empty default case
samikshya-db 05f4ea2
Fix telemetry endpoint to match JDBC driver
samikshya-db 68f8493
Make first feature flag call blocking
samikshya-db 7fe0e54
Fix test failures in telemetry
samikshya-db 2387f20
Fix SA9003 staticcheck lint errors in exporter
samikshya-db 53b51e3
[PECOBLR-1381] Implement telemetry Phase 6: Metric collection & aggre…
samikshya-db 51d40d2
[PECOBLR-1382] Implement telemetry Phase 7: Driver integration
samikshya-db 09b9b99
Update DESIGN.md: Mark Phase 7 as completed
samikshya-db f388244
Update driver integration for config overlay pattern
samikshya-db 1593c87
Merge branch 'main' into stack/PECOBLR-1381-1382-telemetry-phase6-7
samikshya-db ae00204
Merge branch 'main' into stack/PECOBLR-1381-1382-telemetry-phase6-7
samikshya-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // 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 | ||
| } | ||
|
|
||
| // 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 { | ||
| 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{}), | ||
| } | ||
|
|
||
| // 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 { | ||
| // Log at trace level only | ||
| // logger.Trace().Msgf("telemetry: recordMetric panic: %v", r) | ||
| } | ||
| }() | ||
|
|
||
| agg.mu.Lock() | ||
| defer agg.mu.Unlock() | ||
|
|
||
| switch metric.metricType { | ||
| case "connection": | ||
| // Emit connection events immediately | ||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any reason for flushing connection or terminal error events immediately ? |
||
| 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 { | ||
| // Log at trace level only | ||
| } | ||
| }() | ||
|
|
||
| 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(context.Background()) | ||
| 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] | ||
|
|
||
| // Export asynchronously | ||
| go func() { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| // Log at trace level only | ||
| } | ||
| }() | ||
| agg.exporter.export(ctx, metrics) | ||
| }() | ||
| } | ||
|
|
||
| // close stops the aggregator and flushes pending metrics. | ||
| func (agg *metricsAggregator) close(ctx context.Context) error { | ||
| close(agg.stopCh) | ||
| 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In previous PR (#319), we have the logic of
client > server > default. If that is the case, doesn'tForceEnableTelemetryconflict with that ?