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 609c6ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 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
2 changes: 1 addition & 1 deletion pkg/transform/snowplow_gtmss_preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGTMSSPreview(t *testing.T) {
Ctx: "contexts_com_google_tag-manager_server-side_preview_mode_1",
Property: "x-gtm-server-preview",
HeaderKey: "x-gtm-server-preview",
Expiry: fiftyYears,
Expiry: 1 * time.Hour,
InputMsg: &models.Message{
Data: spTsvNoGtmss,
PartitionKey: "pk",
Expand Down

0 comments on commit 609c6ee

Please sign in to comment.