Skip to content

Commit

Permalink
Merge pull request #23 from foomo/tracify
Browse files Browse the repository at this point in the history
feat: tracify & cookiebot
  • Loading branch information
franklinkim authored Oct 1, 2024
2 parents 7249491 + a95ea01 commit dae9b3e
Show file tree
Hide file tree
Showing 40 changed files with 1,040 additions and 116 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ linters:
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false]
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false]
- exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false]
- exportloopref # checks for pointers to enclosing loop variables [fast: false, auto-fix: false]
#- forbidigo # Forbids identifiers [fast: false, auto-fix: false]
- forcetypeassert # finds forced type assertions [fast: true, auto-fix: false]
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false]
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.23.0

require (
github.com/ThreeDotsLabs/watermill v1.3.5
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/foomo/go v0.0.3
github.com/foomo/gostandards v0.1.0
github.com/gogo/protobuf v1.3.2
Expand All @@ -16,9 +17,11 @@ require (
github.com/json-iterator/go v1.1.12
github.com/mitchellh/mapstructure v1.5.0
github.com/pkg/errors v0.9.1
github.com/pperaltaisern/watermillzap v1.0.0
github.com/prometheus/common v0.55.0
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand All @@ -40,7 +43,6 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand Down Expand Up @@ -140,7 +142,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.29.2 // indirect
k8s.io/client-go v0.29.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/pperaltaisern/watermillzap v1.0.0 h1:TtlI/WW6VHgkwgyXOtcMd/Utvw/2ZBm22o4bJ9IaoD4=
github.com/pperaltaisern/watermillzap v1.0.0/go.mod h1:tc6T7N3R5pKS7RAG2RUoscJI33yVk8aUPrfzHTGNR3c=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
Expand Down
19 changes: 10 additions & 9 deletions integration/loki/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
)

type Line struct {
Name sesamy.EventName `json:"name"`
Params any `json:"params"`
ClientID string `json:"client_id"`
UserID string `json:"user_id,omitempty"`
UserProperties map[string]any `json:"user_properties,omitempty"`
Consent *mpv2.Consent `json:"consent,omitempty"`
NonPersonalizedAds bool `json:"non_personalized_ads,omitempty"`
UserData *mpv2.UserData `json:"user_data,omitempty"`
DebugMode bool `json:"debug_mode,omitempty"`
Name sesamy.EventName `json:"name"`
Params any `json:"params"`
ClientID string `json:"client_id"`
UserID string `json:"user_id,omitempty"`
UserProperties map[string]any `json:"user_properties,omitempty"`
Consent *mpv2.ConsentData `json:"consent,omitempty"`
UserData *mpv2.UserData `json:"user_data,omitempty"`
DebugMode bool `json:"debug_mode,omitempty"`
SessionID string `json:"session_id,omitempty"`
EngagementTimeMSec int64 `json:"engagement_time_msec,omitempty"`
}

func (l *Line) Marshal() ([]byte, error) {
Expand Down
3 changes: 2 additions & 1 deletion integration/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ func (l *Loki) Write(payload mpv2.Payload[any]) {
UserData: payload.UserData,
ClientID: payload.ClientID,
UserProperties: payload.UserProperties,
NonPersonalizedAds: payload.NonPersonalizedAds,
DebugMode: payload.DebugMode,
SessionID: payload.SessionID,
EngagementTimeMSec: payload.EngagementTimeMSec,
}

lineBytes, err := line.Marshal()
Expand Down
23 changes: 23 additions & 0 deletions integration/loki/messagehandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package loki

import (
"encoding/json"

"github.com/ThreeDotsLabs/watermill/message"
"github.com/foomo/sesamy-go/pkg/encoding/mpv2"
"github.com/pkg/errors"
)

func MPv2MessageHandler(loki *Loki) message.NoPublishHandlerFunc {
return func(msg *message.Message) error {
var payload mpv2.Payload[any]

// unmarshal payload
if err := json.Unmarshal(msg.Payload, &payload); err != nil {
return errors.Wrap(err, "failed to unmarshal payload")
}

loki.Write(payload)
return nil
}
}
29 changes: 29 additions & 0 deletions integration/loki/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package loki

import (
"context"
)

type Service struct {
loki *Loki
}

func NewService(loki *Loki) *Service {
return &Service{loki: loki}
}

func (s *Service) Name() string {
return "loki"
}

// Start pulls lines out of the channel and sends them to Loki
func (s *Service) Start(ctx context.Context) error {
s.loki.Start(ctx)
return nil
}

// Close will cancel any ongoing requests and stop the goroutine listening for requests
func (s *Service) Close(ctx context.Context) error {
s.loki.Stop()
return nil
}
26 changes: 26 additions & 0 deletions integration/watermill/gtag/messagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/ThreeDotsLabs/watermill/message"
"github.com/foomo/sesamy-go/pkg/encoding/gtag"
"github.com/foomo/sesamy-go/pkg/encoding/gtagencode"
"github.com/foomo/sesamy-go/pkg/encoding/mpv2"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -32,3 +34,27 @@ func MessageHandler(handler func(payload *gtag.Payload, msg *message.Message) er
return []*message.Message{msg}, nil
}
}

func MPv2MessageHandler(msg *message.Message) ([]*message.Message, error) {
var payload gtag.Payload

// unmarshal payload
if err := json.Unmarshal(msg.Payload, &payload); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal payload")
}

// encode to mpv2
var mpv2Payload *mpv2.Payload[any]
if err := gtagencode.MPv2(payload, &mpv2Payload); err != nil {
return nil, errors.Wrap(err, "failed to encode gtag to mpv2")
}

// marshal payload
b, err := json.Marshal(mpv2Payload)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal payload")
}
msg.Payload = b

return []*message.Message{msg}, nil
}
148 changes: 148 additions & 0 deletions integration/watermill/gtag/messagehandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package gtag_test

import (
"context"
"encoding/json"
"fmt"
"sync/atomic"
"testing"
"time"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
"github.com/foomo/sesamy-go/integration/watermill/gtag"
encoding "github.com/foomo/sesamy-go/pkg/encoding/gtag"
"github.com/foomo/sesamy-go/pkg/sesamy"
"github.com/pperaltaisern/watermillzap"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestMessageHandler(t *testing.T) {
l := zaptest.NewLogger(t)

router, err := message.NewRouter(message.RouterConfig{}, watermillzap.NewLogger(l))
require.NoError(t, err)
defer router.Close()

// Create pubSub
pubSub := gochannel.NewGoChannel(
gochannel.Config{},
watermillzap.NewLogger(l),
)

var done atomic.Bool
router.AddHandler("gtag", "in", pubSub, "out", pubSub, gtag.MessageHandler(func(payload *encoding.Payload, msg *message.Message) error {
expected := `{"consent":{},"campaign":{},"ecommerce":{},"client_hints":{},"protocol_version":"2","client_id":"C123456","richsstsse":"1","document_location":"https://foomo.org","document_title":"Home","is_debug":"1","event_name":"add_to_cart"}`
if !assert.JSONEq(t, expected, string(msg.Payload)) {
fmt.Println(string(msg.Payload))
}
done.Store(true)
return nil
}))

go func() {
assert.NoError(t, router.Run(context.TODO()))
}()
assert.Eventually(t, router.IsRunning, time.Second, 50*time.Millisecond)

payload := encoding.Payload{
Consent: encoding.Consent{},
Campaign: encoding.Campaign{},
ECommerce: encoding.ECommerce{},
ClientHints: encoding.ClientHints{},
ProtocolVersion: encoding.Set("2"),
TrackingID: nil,
GTMHashInfo: nil,
ClientID: encoding.Set("C123456"),
Richsstsse: encoding.Set("1"),
DocumentLocation: encoding.Set("https://foomo.org"),
DocumentTitle: encoding.Set("Home"),
DocumentReferrer: nil,
IsDebug: encoding.Set("1"),
EventName: encoding.Set(sesamy.EventNameAddToCart),
EventParameter: nil,
EventParameterNumber: nil,
UserID: nil,
SessionID: nil,
UserProperty: nil,
UserPropertyNumber: nil,
NonPersonalizedAds: nil,
SST: nil,
Remain: nil,
}
jsonPayload, err := json.Marshal(payload)
require.NoError(t, err)

msg := message.NewMessage(watermill.NewUUID(), jsonPayload)

require.NoError(t, pubSub.Publish("in", msg))

assert.Eventually(t, done.Load, time.Second, 50*time.Millisecond)
}

func TestMPv2MessageHandler(t *testing.T) {
l := zaptest.NewLogger(t)

router, err := message.NewRouter(message.RouterConfig{}, watermillzap.NewLogger(l))
require.NoError(t, err)
defer router.Close()

// Create pubSub
pubSub := gochannel.NewGoChannel(
gochannel.Config{},
watermillzap.NewLogger(l),
)

var done atomic.Bool
router.AddHandler("gtag", "in", pubSub, "out", pubSub, gtag.MPv2MessageHandler)
router.AddNoPublisherHandler("mpv2", "out", pubSub, func(msg *message.Message) error {
expected := `{"client_id":"C123456","events":[{"name":"add_to_cart","params":{}}],"debug_mode":true}`
if !assert.JSONEq(t, expected, string(msg.Payload)) {
fmt.Println(string(msg.Payload))
}
done.Store(true)
return nil
})

go func() {
assert.NoError(t, router.Run(context.TODO()))
}()
assert.Eventually(t, router.IsRunning, time.Second, 50*time.Millisecond)

payload := encoding.Payload{
Consent: encoding.Consent{},
Campaign: encoding.Campaign{},
ECommerce: encoding.ECommerce{},
ClientHints: encoding.ClientHints{},
ProtocolVersion: encoding.Set("2"),
TrackingID: nil,
GTMHashInfo: nil,
ClientID: encoding.Set("C123456"),
Richsstsse: encoding.Set("1"),
DocumentLocation: encoding.Set("https://foomo.org"),
DocumentTitle: encoding.Set("Home"),
DocumentReferrer: nil,
IsDebug: encoding.Set("1"),
EventName: encoding.Set(sesamy.EventNameAddToCart),
EventParameter: nil,
EventParameterNumber: nil,
UserID: nil,
SessionID: nil,
UserProperty: nil,
UserPropertyNumber: nil,
NonPersonalizedAds: nil,
SST: nil,
Remain: nil,
}
jsonPayload, err := json.Marshal(payload)
require.NoError(t, err)

msg := message.NewMessage(watermill.NewUUID(), jsonPayload)

require.NoError(t, pubSub.Publish("in", msg))

assert.Eventually(t, done.Load, time.Second, 50*time.Millisecond)
}
Loading

0 comments on commit dae9b3e

Please sign in to comment.