Skip to content

Commit

Permalink
feat: Support PHP update to event schema v4 (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Mar 14, 2024
1 parent 81175da commit 6d23303
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
13 changes: 9 additions & 4 deletions internal/events/event-relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@ func (r *analyticsEventEndpointDispatcher) dispatch(w http.ResponseWriter, req *
metadata := GetEventPayloadMetadata(req)

r.loggers.Debugf("Received %d events (v%d) to be proxied to %s", len(evts), metadata.SchemaVersion, r.remotePath)
if metadata.SchemaVersion >= SummaryEventsSchemaVersion {
// New-style events that have already gone through summarization - deliver them as-is
r.getVerbatimRelay().enqueue(metadata, evts)
} else {
if metadata.SchemaVersion < SummaryEventsSchemaVersion {
r.getSummarizingRelay().enqueue(metadata, evts)
return
}

if _, ok := req.Header[http.CanonicalHeaderKey(EventUnsummarizedHeader)]; ok {
r.getSummarizingRelay().enqueue(metadata, evts)
return
}

r.getVerbatimRelay().enqueue(metadata, evts)
})
}

Expand Down
4 changes: 4 additions & 0 deletions internal/events/event_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const (
// EventSchemaHeader is an HTTP header that describes the schema version for event requests.
EventSchemaHeader = "X-LaunchDarkly-Event-Schema"

// EventUnsummarizedHeader is an HTTP header that denotes events being received have not gone
// through the standard event summarization process.
EventUnsummarizedHeader = "X-LaunchDarkly-Unsummarized"

// TagsHeader is an HTTP header that may be sent by SDKs that support application metadata.
// We copy the value of this header when proxying events.
TagsHeader = "X-LaunchDarkly-Tags"
Expand Down
28 changes: 25 additions & 3 deletions internal/events/summarizing-relay-testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ func makeAllSummarizeEventsParams() []summarizeEventsParams {
}
]`,
},
{
name: "php feature event using schema 4 will be summarized",
schemaVersion: 4,
inputEventsJSON: `
[
{ "kind": "feature", "creationDate": 1000, "key": "flagkey", "user": {"key": "userkey"},
"value": "b", "default": "c", "version": 11, "variation": 1, "trackEvents": false, "samplingRatio": 1, "excludeFromSummaries": false }
]`,
expectedEventsJSON: `
[
{ "kind": "index", "creationDate": 1000, "context": {"key": "userkey"} },
{
"kind": "summary", "startDate": 1000, "endDate": 1000,
"features": {
"flagkey": {
"default": "c", "contextKinds": ["user"],
"counters": [{"variation": 1, "version": 11, "value": "b", "count": 1}]
}
}
}
]`,
},
{
name: "feature event has non-standard defaults for ratio and exclusion fields",
inputEventsJSON: `
Expand All @@ -106,13 +128,13 @@ func makeAllSummarizeEventsParams() []summarizeEventsParams {
name: "feature event includes the provided sampling ratio",
inputEventsJSON: `
[
{ "kind": "feature", "creationDate": 1000, "key": "flagkey", "user": {"key": "userkey"},
{ "kind": "feature", "creationDate": 1000, "key": "flagkey", "context": {"key": "userkey"},
"value": "b", "default": "c", "version": 11, "variation": 1, "trackEvents": true, "samplingRatio": 0 }
]`,
expectedEventsJSON: `
[
{"kind":"index","creationDate":1000,"context":{"key": "userkey"}},
{"kind":"feature","creationDate":1000,"key":"flagkey","version":11,"contextKeys":{"user":"userkey"},"variation":1,"value":"b","default":"c","samplingRatio":0},
{"kind":"feature","creationDate":1000,"key":"flagkey","version":11,"context":{"key":"userkey"},"variation":1,"value":"b","default":"c","samplingRatio":0},
{"kind":"summary","startDate":1000,"endDate":1000,"features":{"flagkey":{"default":"c","counters":[{"variation":1,"version":11,"value":"b","count":1}],"contextKinds":["user"]}}}
]`,
},
Expand All @@ -126,7 +148,7 @@ func makeAllSummarizeEventsParams() []summarizeEventsParams {
expectedEventsJSON: `
[
{"kind":"index","creationDate":1000,"context":{"key": "userkey"}},
{"kind":"feature","creationDate":1000,"key":"flagkey","version":11,"contextKeys":{"user":"userkey"},"variation":1,"value":"b","default":"c"}
{"kind":"feature","creationDate":1000,"key":"flagkey","version":11,"context":{"key":"userkey"},"variation":1,"value":"b","default":"c"}
]`,
},
{
Expand Down
5 changes: 4 additions & 1 deletion internal/events/summarizing-relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func TestSummarizeEvents(t *testing.T) {
_, _ = st.UpsertFlag(p.dataStore, ep.storedFlag)
}

req := st.BuildRequest("POST", "/", []byte(ep.inputEventsJSON), headersWithEventSchema(ep.schemaVersion))
headers := headersWithEventSchema(ep.schemaVersion)
headers.Set(EventUnsummarizedHeader, "true")

req := st.BuildRequest("POST", "/", []byte(ep.inputEventsJSON), headers)
p.dispatcher.GetHandler(basictypes.ServerSDK, ldevents.AnalyticsEventDataKind)(httptest.NewRecorder(), req)
p.dispatcher.flush()

Expand Down

0 comments on commit 6d23303

Please sign in to comment.