Skip to content

Commit

Permalink
overrides timestamps coming from frontend agents, fixes elastic#723
Browse files Browse the repository at this point in the history
  • Loading branch information
jalvz committed Apr 3, 2018
1 parent edb22ae commit c0ba7ba
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions beater/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type Config struct {
LibraryPattern *regexp.Regexp
ExcludeFromGrouping *regexp.Regexp
SmapMapper sourcemap.Mapper
IsFrontend bool
}
12 changes: 11 additions & 1 deletion processor/error/payload.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -62,14 +64,22 @@ 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{
"processor": processorEntry,
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}
Expand Down
14 changes: 14 additions & 0 deletions processor/error/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
14 changes: 12 additions & 2 deletions processor/transaction/payload.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}
Expand Down
14 changes: 14 additions & 0 deletions processor/transaction/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit c0ba7ba

Please sign in to comment.