Skip to content

Add metrics hooks to the listener#72

Merged
nikola-jokic merged 4 commits intomainfrom
nikola-jokic/metrics
Feb 17, 2026
Merged

Add metrics hooks to the listener#72
nikola-jokic merged 4 commits intomainfrom
nikola-jokic/metrics

Conversation

@nikola-jokic
Copy link
Collaborator

No description provided.

@nikola-jokic nikola-jokic requested a review from a team as a code owner February 13, 2026 17:55
Copilot AI review requested due to automatic review settings February 13, 2026 17:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds extensibility points to the listener package by introducing a metrics hook interface that is invoked during session/message handling, and updates tests/mocks to validate the new behavior.

Changes:

  • Introduce MetricsRecorder interface + WithMetricsRecorder option with a default discard implementation.
  • Emit metrics hooks for statistics, job started/completed, and desired runner count decisions.
  • Update listener unit tests and add an autogenerated MockMetricsRecorder.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
listener/listener.go Adds MetricsRecorder hooks, option wiring, and records metrics during Run/message handling.
listener/listener_test.go Updates tests to pass a metrics recorder and assert metrics hooks are called.
listener/mocks_test.go Adds autogenerated mock implementation for MetricsRecorder.
Comments suppressed due to low confidence (1)

listener/listener_test.go:178

  • assert.ErrorIs has the arguments reversed here. The signature is assert.ErrorIs(t, err, target); as written it will treat context.Canceled as the error under test and err as the target, which can cause the assertion to behave incorrectly.
		err = l.Run(ctx, handler)
		assert.ErrorIs(t, context.Canceled, err)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +95 to +99
func WithMetricsRecorder(recorder MetricsRecorder) Option {
return func(l *Listener) {
l.metricsRecorder = recorder
}
}
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithMetricsRecorder will happily accept nil and overwrite the default recorder, which will panic later when the listener calls l.metricsRecorder.*. Consider ignoring nil recorders (keep the discard implementation) or defaulting nil to &discardMetricsRecorder{} inside this option.

Copilot uses AI. Check for mistakes.
Comment on lines 201 to 223
func (l *Listener) handleMessage(ctx context.Context, handler Scaler, msg *scaleset.RunnerScaleSetMessage) error {
l.handleStatistics(ctx, msg.Statistics)

if err := l.client.DeleteMessage(ctx, msg.MessageID); err != nil {
return fmt.Errorf("failed to delete message: %w", err)
}

for _, jobStarted := range msg.JobStartedMessages {
l.metricsRecorder.RecordJobStarted(jobStarted)
if err := handler.HandleJobStarted(ctx, jobStarted); err != nil {
return fmt.Errorf("failed to handle job started: %w", err)
}
}
for _, jobCompleted := range msg.JobCompletedMessages {
l.metricsRecorder.RecordJobCompleted(jobCompleted)
if err := handler.HandleJobCompleted(ctx, jobCompleted); err != nil {
return fmt.Errorf("failed to handle job completed: %w", err)
}
}

if _, err := handler.HandleDesiredRunnerCount(ctx, msg.Statistics.TotalAssignedJobs); err != nil {
desiredCount, err := handler.HandleDesiredRunnerCount(ctx, msg.Statistics.TotalAssignedJobs)
if err != nil {
return fmt.Errorf("failed to handle desired runner count: %w", err)
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleMessage assumes msg.Statistics is non-nil (handleStatistics assigns it and later msg.Statistics.TotalAssignedJobs is dereferenced). Since RunnerScaleSetMessage.Statistics is a pointer, a nil value here will cause a panic and can also set l.latestStatistics to nil (breaking the msg == nil path). Add a nil check and return an error (similar to the initial session statistics check) before using it.

Copilot uses AI. Check for mistakes.
@nikola-jokic nikola-jokic merged commit f9f801f into main Feb 17, 2026
8 checks passed
@nikola-jokic nikola-jokic deleted the nikola-jokic/metrics branch February 17, 2026 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants