Skip to content
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

fix: adapt kubernetes event #29591

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions libbeat/common/kubernetes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,17 @@ func ContainerIDWithRuntime(s PodContainerStatus) (string, string) {
}
return "", ""
}

func EventLastTimestamp(eve *Event) metav1.Time {
lastTimestamp := eve.LastTimestamp
if lastTimestamp.IsZero() {
if eve.Series != nil && !eve.Series.LastObservedTime.IsZero() {
lastTimestamp = metav1.Time(eve.Series.LastObservedTime)
} else if !eve.EventTime.IsZero() {
lastTimestamp = metav1.Time(eve.EventTime)
Copy link
Member

Choose a reason for hiding this comment

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

Here we fallback to a value that is described as Time when this Event was first observed, is this something that is actually correct? EventTime is mostly a good fallback if FirstTimestamp is not present.

} else {
lastTimestamp = eve.CreationTimestamp
Copy link
Member

Choose a reason for hiding this comment

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

Does this field actually exist? I cannot find on the version of the client library we use (v1.21): https://github.com/kubernetes/api/blob/release-1.21/core/v1/types.go#L5463

}
}
return lastTimestamp
}
24 changes: 21 additions & 3 deletions metricbeat/module/kubernetes/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ func (m *MetricSet) Run(reporter mb.PushReporter) {
return
}

func getEventCount(eve *kubernetes.Event) int32 {
count := eve.Count
if count == 0 {
if eve.Series != nil && eve.Series.Count != 0 {
count = eve.Series.Count
} else {
count = 1
}
}
return count
}

func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig) common.MapStr {
eventMeta := common.MapStr{
"timestamp": common.MapStr{
Expand Down Expand Up @@ -176,7 +188,7 @@ func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig) com
"message": eve.Message,
"reason": eve.Reason,
"type": eve.Type,
"count": eve.Count,
"count": getEventCount(eve),
"source": common.MapStr{
"host": eve.Source.Host,
"component": eve.Source.Component,
Expand All @@ -193,8 +205,14 @@ func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig) com

tsMap := make(common.MapStr)

tsMap["first_occurrence"] = kubernetes.Time(&eve.FirstTimestamp).UTC()
tsMap["last_occurrence"] = kubernetes.Time(&eve.LastTimestamp).UTC()
lastTimestamp := kubernetes.EventLastTimestamp(eve)
tsMap["first_occurrence"] = func() time.Time {
if eve.FirstTimestamp.IsZero() {
return kubernetes.Time(&lastTimestamp).UTC()
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this is correct. If an event has no FirstTimestamp attached then we fallback to lastTimestamp so this means that each time the event occurs we emit data with different first_occurrence which is always equal to the current lastTimestamp. Is this something that is intentional, and if so why?

Maybe it's better here to use EventTime as a fallback?

}
return kubernetes.Time(&eve.FirstTimestamp).UTC()
}()
tsMap["last_occurrence"] = kubernetes.Time(&lastTimestamp).UTC()

if len(tsMap) != 0 {
output["timestamp"] = tsMap
Expand Down