Skip to content

Commit

Permalink
feat: Add support for gzipped event payloads (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 authored May 15, 2024
1 parent 78be599 commit 91de9cb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/service_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ This means that the SDK has the ability to construct and compare two contexts fo

This means that the SDK supports caching of the e-tag header between client restarts. Typical SDKs track the polling e-tag header and send it between subsequent requests. SDKs supporting this capability are able to persist that e-tag header for use even between complete client restarts.

#### Capability `"event-gzip"`

This means that the SDK supports gzip compression of event payloads.

#### Capability `"event-sampling"`

This means that the SDK supports event sampling; the SDK can limit the number of certain events based on payloads received from upstream services.
Expand Down
24 changes: 23 additions & 1 deletion mockld/events_service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package mockld

import (
"compress/gzip"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -32,7 +34,7 @@ type EventsService struct {
lock sync.Mutex
}

func NewEventsService(sdkKind SDKKind, logger framework.Logger) *EventsService {
func NewEventsService(sdkKind SDKKind, logger framework.Logger, requireGzip bool) *EventsService {
s := &EventsService{
AnalyticsEventPayloads: make(chan Events, eventsChannelBufferSize),
sdkKind: sdkKind,
Expand All @@ -42,6 +44,10 @@ func NewEventsService(sdkKind SDKKind, logger framework.Logger) *EventsService {
}

router := mux.NewRouter()
if requireGzip {
router.Use(gzipMiddleware)
}

switch sdkKind {
case ServerSideSDK, PHPSDK:
router.HandleFunc("/bulk", s.postEvents).Methods("POST")
Expand All @@ -62,6 +68,22 @@ func NewEventsService(sdkKind SDKKind, logger framework.Logger) *EventsService {
return s
}

func gzipMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
gr, err := gzip.NewReader(r.Body)
if err != nil {
http.Error(w, "Failed to read gzipped request body", http.StatusBadRequest)
return
}
r.Body = gr
next.ServeHTTP(w, r)
} else {
http.Error(w, "Failed to provide a gzipped payload", http.StatusBadRequest)
}
})
}

func (s *EventsService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.logger.Printf("Received %s %s", r.Method, r.URL)
s.handler.ServeHTTP(w, r)
Expand Down
4 changes: 4 additions & 0 deletions sdktests/common_tests_events_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (c CommonEventTests) RequestMethodAndHeaders(t *ldtest.T, credential string
headersMatcher,
c.authorizationHeaderMatcher(credential),
))

if t.Capabilities().Has(servicedef.CapabilityEventGzip) {
m.In(t).For("request headers").Assert(request.Headers, Header("Content-Encoding").Should(m.StringContains("gzip")))
}
})
}
})
Expand Down
5 changes: 4 additions & 1 deletion sdktests/testapi_sdk_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ type SDKEventSink struct {
// sink will be attached to this test scope, and also to any of its subtests that are active when the
// output is generated.
func NewSDKEventSink(t *ldtest.T) *SDKEventSink {
eventsService := mockld.NewEventsService(requireContext(t).sdkKind, t.DebugLogger())
eventsService := mockld.NewEventsService(
requireContext(t).sdkKind,
t.DebugLogger(),
t.Capabilities().Has(servicedef.CapabilityEventGzip))
eventsEndpoint := requireContext(t).harness.NewMockEndpoint(eventsService, t.DebugLogger(),
harness.MockEndpointDescription("events service"))

Expand Down
1 change: 1 addition & 0 deletions servicedef/service_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
CapabilityAutoEnvAttributes = "auto-env-attributes"
CapabilityMigrations = "migrations"
CapabilityEventSampling = "event-sampling"
CapabilityEventGzip = "event-gzip"
CapabilityETagCaching = "etag-caching"
CapabilityInlineContext = "inline-context"
CapabilityAnonymousRedaction = "anonymous-redaction"
Expand Down

0 comments on commit 91de9cb

Please sign in to comment.