Skip to content

[8.19](backport #44442) metricbeat: ensure event.duration field is not discarded on Windows #44454

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
merged 2 commits into from
May 23, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ otherwise no tag is added. {issue}42208[42208] {pull}42403[42403]
- Add GCP organization and project details to ECS cloud fields. {pull}40461[40461]
- Fix the function to determine CPU cores on windows {issue}42593[42593] {pull}43409[43409]
- Handle permission errors while collecting data from Windows services and don't interrupt the overall collection by skipping affected services {issue}40765[40765] {pull}43665[43665]
- Fixed a bug where `event.duration` could be missing from an event on Windows systems due to low-resolution clock. {pull}44440[44440]

*Osquerybeat*

Expand Down
13 changes: 7 additions & 6 deletions metricbeat/mb/module/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"math/rand/v2"
"sync"
"time"

Expand Down Expand Up @@ -205,7 +205,7 @@ func (msw *metricSetWrapper) run(done <-chan struct{}, out chan<- beat.Event) {

// Start each metricset randomly over a period of MaxDelayPeriod.
if msw.module.maxStartDelay > 0 {
delay := time.Duration(rand.Int63n(int64(msw.module.maxStartDelay)))
delay := rand.N(msw.module.maxStartDelay)
debugf("%v/%v will start after %v", msw.module.Name(), msw.Name(), delay)
select {
case <-done:
Expand Down Expand Up @@ -320,15 +320,15 @@ func (msw *metricSetWrapper) handleFetchError(err error, reporter mb.PushReporte
reporter.Error(err)
msw.stats.consecutiveFailures.Set(0)
// mark module as running if metrics are partially available and display the error message
msw.module.UpdateStatus(status.Running, fmt.Sprintf("Error fetching data for metricset %s.%s: %v", msw.module.Name(), msw.MetricSet.Name(), err))
msw.module.UpdateStatus(status.Running, fmt.Sprintf("Error fetching data for metricset %s.%s: %v", msw.module.Name(), msw.Name(), err))
logp.Err("Error fetching data for metricset %s.%s: %s", msw.module.Name(), msw.Name(), err)

default:
reporter.Error(err)
msw.stats.consecutiveFailures.Inc()
if msw.failureThreshold > 0 && msw.stats.consecutiveFailures != nil && uint(msw.stats.consecutiveFailures.Get()) >= msw.failureThreshold {
// mark it as degraded for any other issue encountered
msw.module.UpdateStatus(status.Degraded, fmt.Sprintf("Error fetching data for metricset %s.%s: %v", msw.module.Name(), msw.MetricSet.Name(), err))
msw.module.UpdateStatus(status.Degraded, fmt.Sprintf("Error fetching data for metricset %s.%s: %v", msw.module.Name(), msw.Name(), err))
}
logp.Err("Error fetching data for metricset %s.%s: %s", msw.module.Name(), msw.Name(), err)

Expand Down Expand Up @@ -401,7 +401,8 @@ func (r reporterV2) Done() <-chan struct{} { return r.done }
func (r reporterV2) Error(err error) bool { return r.Event(mb.Event{Error: err}) }
func (r reporterV2) Event(event mb.Event) bool {
if event.Took == 0 && !r.start.IsZero() {
event.Took = time.Since(r.start)
// ensure elapsed time is always > 0
event.Took = max(time.Since(r.start), time.Microsecond)
}
if r.msw.periodic {
event.Period = r.msw.Module().Config().Period
Expand All @@ -428,7 +429,7 @@ func (r reporterV2) Event(event mb.Event) bool {
if event.Namespace == "" {
event.Namespace = r.msw.Registration().Namespace
}
beatEvent := event.BeatEvent(r.msw.module.Name(), r.msw.MetricSet.Name(), r.msw.module.eventModifiers...)
beatEvent := event.BeatEvent(r.msw.module.Name(), r.msw.Name(), r.msw.module.eventModifiers...)
if !writeEvent(r.done, r.out, beatEvent) {
return false
}
Expand Down
24 changes: 24 additions & 0 deletions metricbeat/mb/module/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ func TestPeriodIsAddedToEvent(t *testing.T) {
}
}

func TestDurationIsAddedToEvent(t *testing.T) {
hosts := []string{"alpha"}
config := newConfig(t, map[string]interface{}{
"module": moduleName,
"metricsets": []string{reportingFetcherName},
"hosts": hosts,
})

registry := newTestRegistry(t)
m, err := module.NewWrapper(config, registry, module.WithMetricSetInfo())
require.NoError(t, err)

done := make(chan struct{})
defer close(done)

output := m.Start(done)

event := <-output

fields := event.Fields.Flatten()
assert.Contains(t, fields, "event.duration", "event.duration should be present in event")
assert.Greater(t, fields["event.duration"], time.Duration(0), "event.duration should be greater than 0")
}

func TestNewWrapperForMetricSet(t *testing.T) {
hosts := []string{"alpha"}
c := newConfig(t, map[string]interface{}{
Expand Down