-
Couldn't load subscription status.
- Fork 289
WIP introduce an event metrics plugin with a general schema #4781
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||||
| package metrics | ||||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| "sync" | ||||||||||||||
| "time" | ||||||||||||||
|
|
||||||||||||||
| "github.com/sirupsen/logrus" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| const ( | ||||||||||||||
| EventsPluginName = "events" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| type EventLevel string | ||||||||||||||
|
|
||||||||||||||
| const ( | ||||||||||||||
| EventLevelInfo EventLevel = "Info" | ||||||||||||||
| EventLevelWarning EventLevel = "Warning" | ||||||||||||||
| EventLevelError EventLevel = "Error" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| type Event struct { | ||||||||||||||
| Level EventLevel `json:"level"` | ||||||||||||||
| Source string `json:"source"` | ||||||||||||||
| Locator EventLocator `json:"locator"` | ||||||||||||||
| Message EventMessage `json:"message"` | ||||||||||||||
| From time.Time `json:"from"` | ||||||||||||||
| To time.Time `json:"to"` | ||||||||||||||
| Timestamp time.Time `json:"timestamp"` | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| type EventLocator struct { | ||||||||||||||
| Type string `json:"type"` | ||||||||||||||
| Name string `json:"name"` | ||||||||||||||
| Container *string `json:"container,omitempty"` | ||||||||||||||
| Keys map[string]any `json:"keys,omitempty"` | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| type EventMessage struct { | ||||||||||||||
| Reason string `json:"reason"` | ||||||||||||||
| Cause string `json:"cause"` | ||||||||||||||
| HumanMessage string `json:"humanMessage"` | ||||||||||||||
| Annotations map[string]any `json:"annotations,omitempty"` | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (e *Event) SetTimestamp(t time.Time) { | ||||||||||||||
| e.Timestamp = t | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func NewEvent(level EventLevel, locator EventLocator, message EventMessage, source string, from, to time.Time) *Event { | ||||||||||||||
| return &Event{Level: level, Source: source, From: from, To: to, Locator: locator, Message: message} | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| type eventsPlugin struct { | ||||||||||||||
| mu sync.Mutex | ||||||||||||||
| logger *logrus.Entry | ||||||||||||||
| events []MetricsEvent | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func newEventsPlugin(logger *logrus.Entry) *eventsPlugin { | ||||||||||||||
| return &eventsPlugin{logger: logger.WithField("plugin", EventsPluginName)} | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (p *eventsPlugin) Name() string { return EventsPluginName } | ||||||||||||||
|
|
||||||||||||||
| func (p *eventsPlugin) Record(ev MetricsEvent) { | ||||||||||||||
| if _, ok := ev.(*Event); !ok { | ||||||||||||||
| return | ||||||||||||||
| } | ||||||||||||||
| p.mu.Lock() | ||||||||||||||
| p.events = append(p.events, ev) | ||||||||||||||
| p.mu.Unlock() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func (p *eventsPlugin) Events() []MetricsEvent { | ||||||||||||||
| return p.events | ||||||||||||||
|
||||||||||||||
| return p.events | |
| p.mu.Lock() | |
| defer p.mu.Unlock() | |
| eventsCopy := make([]MetricsEvent, len(p.events)) | |
| copy(eventsCopy, p.events) | |
| return eventsCopy |
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.
The
Timestampfield is not set in theNewEventconstructor. TheEventstruct includes aTimestampfield (line 29) which remains zero-valued when events are created through this constructor. Consider either setting it in the constructor or documenting that callers must useSetTimestampseparately.