Skip to content

Commit

Permalink
Add third_party/collectorpayload module
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeemster committed Jan 22, 2023
1 parent 72e9298 commit 7124e85
Show file tree
Hide file tree
Showing 9 changed files with 1,054 additions and 50 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
)

require (
github.com/apache/thrift v0.17.0
github.com/davecgh/go-spew v1.1.1
github.com/dop251/goja v0.0.0-20220722151623-4765a9872229
github.com/hashicorp/hcl/v2 v2.13.0
Expand Down
52 changes: 2 additions & 50 deletions go.sum

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions third_party/snowplow/collectorpayload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# collectorpayload

This module is forked from the [Scala equivalent](https://github.com/snowplow/snowplow/tree/master/2-collectors/thrift-schemas/collector-payload-1).

To generate the `gen-go/model1` directory:

1. Install `thrift`: https://thrift.apache.org/download
2. From this directory run `thrift -r --gen go collector_payload_1.thrift`

_NOTE_: Running `make format` will fix formatting issues in the auto-generated code.

To see how to use the library have a look at `collector_payload_test.go` which contains helper functions for seralizing and deserializing payloads.
62 changes: 62 additions & 0 deletions third_party/snowplow/collectorpayload/collector_payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Snowplow Community License Version 1.0,
// and you may not use this file except in compliance with the Snowplow Community License Version 1.0.
// You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0

package collectorpayload

import (
"context"
"encoding/base64"
"encoding/json"

thrift "github.com/apache/thrift/lib/go/thrift"

model1 "github.com/snowplow/snowbridge/third_party/snowplow/collectorpayload/gen-go/model1"
)

// BinarySerializer serializes a CollectorPayload into a byte array ready for transport
func BinarySerializer(ctx context.Context, collectorPayload *model1.CollectorPayload) ([]byte, error) {
t := thrift.NewTMemoryBufferLen(1024)
p := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(t)

serializer := &thrift.TSerializer{
Transport: t,
Protocol: p,
}

return serializer.Write(ctx, collectorPayload)
}

// BinaryDeserializer deserializes a CollectorPayload byte array back to a struct
func BinaryDeserializer(ctx context.Context, collectorPayloadBytes []byte) (*model1.CollectorPayload, error) {
var inputBytes []byte

// Attempt to decode from base64 as most payloads will arrive with the thrift string re-encoded
base64DecodedCollectorPayload, base64Err := base64.StdEncoding.DecodeString(string(collectorPayloadBytes))
if base64Err != nil {
inputBytes = collectorPayloadBytes
} else {
inputBytes = []byte(base64DecodedCollectorPayload)
}

t := thrift.NewTMemoryBufferLen(1024)
p := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(t)

deserializer := &thrift.TDeserializer{
Transport: t,
Protocol: p,
}

collectorPayload := model1.NewCollectorPayload()
err := deserializer.Read(ctx, collectorPayload, inputBytes)

return collectorPayload, err
}

// ToJSON converts the collector payload struct to a JSON representation for simpler portability
func ToJSON(collectorPayload *model1.CollectorPayload) ([]byte, error) {
return json.Marshal(collectorPayload)
}
33 changes: 33 additions & 0 deletions third_party/snowplow/collectorpayload/collector_payload_1.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Snowplow Community License Version 1.0,
// and you may not use this file except in compliance with the Snowplow Community License Version 1.0.
// You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0

namespace go model1

struct CollectorPayload {
31337: string schema

// Required fields which are intrinsic properties of HTTP
100: string ipAddress

// Required fields which are Snowplow-specific
200: i64 timestamp
210: string encoding
220: string collector

// Optional fields which are intrinsic properties of HTTP
300: optional string userAgent
310: optional string refererUri
320: optional string path
330: optional string querystring
340: optional string body
350: optional list<string> headers
360: optional string contentType

// Optional fields which are Snowplow-specific
400: optional string hostname
410: optional string networkUserId
}
36 changes: 36 additions & 0 deletions third_party/snowplow/collectorpayload/collector_payload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Snowplow Community License Version 1.0,
// and you may not use this file except in compliance with the Snowplow Community License Version 1.0.
// You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0

package collectorpayload

import (
"context"
"testing"

model1 "github.com/snowplow/snowbridge/third_party/snowplow/collectorpayload/gen-go/model1"

"github.com/stretchr/testify/assert"
)

// TestBinarySerializer
func TestBinarySerializerAndDeserializer(t *testing.T) {
ctx := context.Background()

assert := assert.New(t)

payload := model1.NewCollectorPayload()
payload.IpAddress = "192.168.0.1"

res, err := BinarySerializer(ctx, payload)
assert.Nil(err)
assert.NotNil(res)

res1, err1 := BinaryDeserializer(ctx, res)
assert.Nil(err1)
assert.NotNil(res1)
assert.Equal("192.168.0.1", res1.IpAddress)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7124e85

Please sign in to comment.