-
Notifications
You must be signed in to change notification settings - Fork 18
🌱 Add contextual logging based recorder #170
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
Merged
openshift-merge-bot
merged 1 commit into
open-cluster-management-io:main
from
qiujian16:recorder
Nov 24, 2025
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package events | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "k8s.io/apimachinery/pkg/runtime" | ||
| eventsv1 "k8s.io/client-go/kubernetes/typed/events/v1" | ||
| kevents "k8s.io/client-go/tools/events" | ||
| ) | ||
|
|
||
| // NewEventRecorder creates a new event recorder for the given controller, it will also log the events | ||
| func NewEventRecorder(ctx context.Context, scheme *runtime.Scheme, | ||
| eventsClient eventsv1.EventsV1Interface, controllerName string) (kevents.EventRecorder, error) { | ||
| broadcaster := kevents.NewBroadcaster(&kevents.EventSinkImpl{Interface: eventsClient}) | ||
| err := broadcaster.StartRecordingToSinkWithContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| broadcaster.StartStructuredLogging(0) | ||
| recorder := broadcaster.NewRecorder(scheme, controllerName) | ||
| return recorder, nil | ||
| } |
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,54 @@ | ||
| package events | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| // ContextualLoggingEventRecorder implements a recorder with contextual logging | ||
| type ContextualLoggingEventRecorder struct { | ||
| component string | ||
| } | ||
|
|
||
| // NewContextualLoggingEventRecorder provides event recorder that will log all recorded events via klog. | ||
| func NewContextualLoggingEventRecorder(component string) Recorder { | ||
| return &ContextualLoggingEventRecorder{ | ||
| component: component, | ||
| } | ||
| } | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) ComponentName() string { | ||
| return r.component | ||
| } | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) ForComponent(component string) Recorder { | ||
| newRecorder := *r | ||
| newRecorder.component = component | ||
| return &newRecorder | ||
| } | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) Shutdown() {} | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) WithComponentSuffix(suffix string) Recorder { | ||
| return r.ForComponent(fmt.Sprintf("%s-%s", r.ComponentName(), suffix)) | ||
| } | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) Event(ctx context.Context, reason, message string) { | ||
| logger := klog.FromContext(ctx) | ||
| logger.Info(fmt.Sprintf("INFO: %s", message), "component", r.component, "reason", reason) | ||
| } | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) Eventf(ctx context.Context, reason, messageFmt string, args ...interface{}) { | ||
| r.Event(ctx, reason, fmt.Sprintf(messageFmt, args...)) | ||
| } | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) Warning(ctx context.Context, reason, message string) { | ||
| logger := klog.FromContext(ctx) | ||
| logger.Info(fmt.Sprintf("WARNING: %s", message), "component", r.component, "reason", reason) | ||
| } | ||
|
|
||
| func (r *ContextualLoggingEventRecorder) Warningf(ctx context.Context, reason, messageFmt string, args ...interface{}) { | ||
| r.Warning(ctx, reason, fmt.Sprintf(messageFmt, args...)) | ||
| } | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Warning events should use a distinct log level.
The
Warningmethod currently useslogger.Info, which logs warnings at the same severity as normal events. This prevents users from filtering warnings separately using log level flags (e.g.,-vflag in klog).Consider using a distinct log level for warnings:
func (r *ContextualLoggingEventRecorder) Warning(ctx context.Context, reason, message string) { logger := klog.FromContext(ctx) - logger.Info(fmt.Sprintf("WARNING: %s", message), "component", r.component, "reason", reason) + logger.V(1).Info(message, "component", r.component, "reason", reason, "eventType", "Warning") }Or use the error level if these represent actual problems:
func (r *ContextualLoggingEventRecorder) Warning(ctx context.Context, reason, message string) { logger := klog.FromContext(ctx) - logger.Info(fmt.Sprintf("WARNING: %s", message), "component", r.component, "reason", reason) + logger.Error(nil, message, "component", r.component, "reason", reason) }The choice depends on your logging strategy and how you want warnings filtered.
📝 Committable suggestion
🤖 Prompt for AI Agents