diff --git a/beater/handlers.go b/beater/handlers.go index 6e0cbde7549..ffdcfc8345d 100644 --- a/beater/handlers.go +++ b/beater/handlers.go @@ -126,6 +126,7 @@ func frontendHandler(pf ProcessorFactory, beaterConfig *Config, report reporter) SmapMapper: smapper, LibraryPattern: regexp.MustCompile(beaterConfig.Frontend.LibraryPattern), ExcludeFromGrouping: regexp.MustCompile(beaterConfig.Frontend.ExcludeFromGrouping), + IsFrontend: true, } return logHandler( killSwitchHandler(beaterConfig.Frontend.isEnabled(), diff --git a/config/config.go b/config/config.go index 8403283b99a..27b87d15842 100644 --- a/config/config.go +++ b/config/config.go @@ -10,4 +10,5 @@ type Config struct { LibraryPattern *regexp.Regexp ExcludeFromGrouping *regexp.Regexp SmapMapper sourcemap.Mapper + IsFrontend bool } diff --git a/processor/error/payload.go b/processor/error/payload.go index e15d0d8ab98..845523b147c 100644 --- a/processor/error/payload.go +++ b/processor/error/payload.go @@ -1,6 +1,8 @@ package error import ( + "time" + "github.com/elastic/apm-server/config" m "github.com/elastic/apm-server/model" "github.com/elastic/apm-server/utility" @@ -62,6 +64,14 @@ func (pa *payload) transform(config config.Config) []beat.Event { var events []beat.Event for _, event := range pa.Events { + + var timestamp time.Time + if config.IsFrontend { + timestamp = time.Now() + } else { + timestamp = event.Timestamp + } + context := context.Transform(event.Context) ev := beat.Event{ Fields: common.MapStr{ @@ -69,7 +79,7 @@ func (pa *payload) transform(config config.Config) []beat.Event { errorDocType: event.Transform(config, pa.Service), "context": context, }, - Timestamp: event.Timestamp, + Timestamp: timestamp, } if event.Transaction != nil { ev.Fields["transaction"] = common.MapStr{"id": event.Transaction.Id} diff --git a/processor/error/payload_test.go b/processor/error/payload_test.go index 1626a959448..de61b6efa5b 100644 --- a/processor/error/payload_test.go +++ b/processor/error/payload_test.go @@ -175,3 +175,17 @@ func TestPayloadTransform(t *testing.T) { } } } + +func TestOverrideFrontendTimestamp(t *testing.T) { + timestamp := time.Now().AddDate(0, 0, -1) + + p := payload{ + Service: m.Service{Name: "myservice"}, + Events: []Event{{Timestamp: timestamp}}, + } + + frontendEvents := p.transform(config.Config{IsFrontend: true}) + for _, event := range frontendEvents { + assert.InDelta(t, time.Now().Unix(), event.Timestamp.Unix(), time.Millisecond.Seconds()*10) + } +} diff --git a/processor/transaction/payload.go b/processor/transaction/payload.go index 728d6df9674..ea373a71ecb 100644 --- a/processor/transaction/payload.go +++ b/processor/transaction/payload.go @@ -1,6 +1,8 @@ package transaction import ( + "time" + "github.com/elastic/apm-server/config" m "github.com/elastic/apm-server/model" "github.com/elastic/apm-server/utility" @@ -65,13 +67,21 @@ func (pa *payload) transform(config config.Config) []beat.Event { var events []beat.Event for _, event := range pa.Events { + + var timestamp time.Time + if config.IsFrontend { + timestamp = time.Now() + } else { + timestamp = event.Timestamp + } + ev := beat.Event{ Fields: common.MapStr{ "processor": processorTransEntry, transactionDocType: event.Transform(), "context": context.Transform(event.Context), }, - Timestamp: event.Timestamp, + Timestamp: timestamp, } events = append(events, ev) @@ -85,7 +95,7 @@ func (pa *payload) transform(config config.Config) []beat.Event { "transaction": trId, "context": spanContext.Transform(sp.Context), }, - Timestamp: event.Timestamp, + Timestamp: timestamp, } events = append(events, ev) } diff --git a/processor/transaction/payload_test.go b/processor/transaction/payload_test.go index af688ebaca1..c7dc9ac40eb 100644 --- a/processor/transaction/payload_test.go +++ b/processor/transaction/payload_test.go @@ -239,3 +239,17 @@ func TestPayloadTransform(t *testing.T) { } } } + +func TestOverrideFrontendTimestamp(t *testing.T) { + timestamp := time.Now().AddDate(0, 0, -1) + + p := payload{ + Service: m.Service{Name: "myservice"}, + Events: []Event{{Timestamp: timestamp, Spans: []*Span{{}}}}, + } + + frontendEvents := p.transform(config.Config{IsFrontend: true}) + for _, event := range frontendEvents { + assert.InDelta(t, time.Now().Unix(), event.Timestamp.Unix(), time.Millisecond.Seconds()*10) + } +}