Skip to content

Commit

Permalink
Fix GTMSS preview expired message handling
Browse files Browse the repository at this point in the history
So that only messages with provided preview context can expire. If there
is no context, message never expires and is simply passed through.
  • Loading branch information
pondzix committed Jan 23, 2025
1 parent b843db5 commit 4239acc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
23 changes: 12 additions & 11 deletions pkg/transform/snowplow_gtmss_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,26 @@ func gtmssPreviewTransformation(ctx, property, headerKey string, expiry time.Dur
return nil, nil, message, nil
}

tstamp, err := parsedEvent.GetValue("collector_tstamp")
headerVal, err := extractHeaderValue(parsedEvent, ctx, property)
if err != nil {
message.SetError(err)
return nil, nil, message, nil
}

if collectorTstamp, ok := tstamp.(time.Time); ok {
if time.Now().UTC().After(collectorTstamp.Add(expiry)) {
message.SetError(errors.New("Message has expired"))
if headerVal != nil {
tstamp, err := parsedEvent.GetValue("collector_tstamp")
if err != nil {
message.SetError(err)
return nil, nil, message, nil
}
}

headerVal, err := extractHeaderValue(parsedEvent, ctx, property)
if err != nil {
message.SetError(err)
return nil, nil, message, nil
}
if headerVal != nil {
if collectorTstamp, ok := tstamp.(time.Time); ok {
if time.Now().UTC().After(collectorTstamp.Add(expiry)) {
message.SetError(errors.New("Message has expired"))
return nil, nil, message, nil
}
}

if message.HTTPHeaders == nil {
message.HTTPHeaders = make(map[string]string)
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/transform/snowplow_gtmss_preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,29 @@ func TestGTMSSPreview(t *testing.T) {
ExpInterState: nil,
Error: errors.New("Message has expired"),
},
{
Scenario: "no_gtmss_no_expire",
Ctx: "contexts_com_google_tag-manager_server-side_preview_mode_1",
Property: "x-gtm-server-preview",
HeaderKey: "x-gtm-server-preview",
Expiry: 1 * time.Hour,
InputMsg: &models.Message{
Data: spTsvNoGtmss,
PartitionKey: "pk",
},
InputInterState: nil,
Expected: map[string]*models.Message{
"success": {
Data: spTsvNoGtmss,
PartitionKey: "pk",
HTTPHeaders: nil,
},
"filtered": nil,
"failed": nil,
},
ExpInterState: spTsvNoGtmssParsed,
Error: nil,
},
}

for _, tt := range testCases {
Expand Down

0 comments on commit 4239acc

Please sign in to comment.