From 8519915919080d361377b1f17439b426bb4acd24 Mon Sep 17 00:00:00 2001 From: Brad Heller Date: Wed, 22 Jul 2020 13:02:16 -0700 Subject: [PATCH] Add a Segment delegate for activity reporting. --- go.mod | 4 ++ go.sum | 8 ++++ instrumentation/activities/delegate.go | 6 +++ .../activities/delegates/segment.go | 39 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 instrumentation/activities/delegates/segment.go diff --git a/go.mod b/go.mod index 0259331..d0bc62c 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( cloud.google.com/go/storage v1.9.0 github.com/DATA-DOG/go-sqlmock v1.3.3 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect + github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/certifi/gocertifi v0.0.0-20180118203423-deb3ae2ef261 // indirect github.com/getsentry/raven-go v0.2.0 github.com/go-stack/stack v1.8.0 // indirect @@ -24,9 +25,12 @@ require ( github.com/puppetlabs/errawr-gen v1.0.1 github.com/puppetlabs/errawr-go/v2 v2.2.0 github.com/reflect/xparse v0.0.0-20171128034418-ab29bdc5e11c + github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect github.com/stretchr/testify v1.4.0 + github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect golang.org/x/text v0.3.2 google.golang.org/api v0.26.0 gopkg.in/intercom/intercom-go.v2 v2.0.0-20200217143803-6ffc0627261a + gopkg.in/segmentio/analytics-go.v3 v3.1.0 gopkg.in/yaml.v2 v2.2.7 // indirect ) diff --git a/go.sum b/go.sum index 240a1ae..c356fcc 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFD github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/certifi/gocertifi v0.0.0-20180118203423-deb3ae2ef261 h1:6/yVvBsKeAw05IUj4AzvrxaCnDjN4nUqKjW9+w5wixg= github.com/certifi/gocertifi v0.0.0-20180118203423-deb3ae2ef261/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4= @@ -174,6 +176,8 @@ github.com/reflect/raymond v0.0.0-20190227215356-5fa3955f4a50/go.mod h1:Bmc/S4QV github.com/reflect/xparse v0.0.0-20171128034418-ab29bdc5e11c h1:7SXtAYmgHR2aLWFlZ2dLqjPpOonfoytadtmkWDob/Bs= github.com/reflect/xparse v0.0.0-20171128034418-ab29bdc5e11c/go.mod h1:tqAg+37u8qXKZZ1QaXWnJ18evc8C6euGUAa4dQpC1gk= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 h1:ZuhckGJ10ulaKkdvJtiAqsLTiPrLaXSdnVgXJKJkTxE= +github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3/go.mod h1:9/Rh6yILuLysoQnZ2oNooD2g7aBnvM7r/fNVxRNWfBc= github.com/serenize/snaker v0.0.0-20171002133257-c7a77c38c398 h1:BbvM3zbEZXBScbJawRAPLkmc44D1KUi/zR5NIBPOWMI= github.com/serenize/snaker v0.0.0-20171002133257-c7a77c38c398/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs= github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b h1:4kg1wyftSKxLtnPAvcRWakIPpokB9w780/KwrNLnfPA= @@ -192,6 +196,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20150808065054-e02fc20de94c h1:XZWnr3b github.com/xeipuuv/gojsonreference v0.0.0-20150808065054-e02fc20de94c/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v0.0.0-20171025060643-212d8a0df7ac h1:4VBKAdTNqxLs00+bB+9Lnosfg6keGxPEXZ28e7hZV3A= github.com/xeipuuv/gojsonschema v0.0.0-20171025060643-212d8a0df7ac/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= +github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g= +github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= @@ -436,6 +442,8 @@ gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/intercom/intercom-go.v2 v2.0.0-20200217143803-6ffc0627261a h1:llOLIlb++Wl+JPeRLoZ43u8/Ufm7p4v345QFdM5Wjso= gopkg.in/intercom/intercom-go.v2 v2.0.0-20200217143803-6ffc0627261a/go.mod h1:k7NO4r+VF6eXR9VY+U32m99wFGNudcwcXCeFSKrMwes= +gopkg.in/segmentio/analytics-go.v3 v3.1.0 h1:UzxH1uaGZRpMKDhJyBz0pexz6yUoBU3x8bJsRk/HV6U= +gopkg.in/segmentio/analytics-go.v3 v3.1.0/go.mod h1:4QqqlTlSSpVlWA9/9nDcPw+FkM2yv1NQoYjUbL9/JAw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/instrumentation/activities/delegate.go b/instrumentation/activities/delegate.go index 289d2c1..1c4e868 100644 --- a/instrumentation/activities/delegate.go +++ b/instrumentation/activities/delegate.go @@ -15,3 +15,9 @@ type Delegate interface { func NewIntercomDelegate(accessToken string) Delegate { return delegates.NewIntercom(accessToken) } + +// NewSegmentDelegate instantiates a delegate for reporting activities to +// Segment, which will then send them to other integrations. +func NewSegmentDelegate(writeKey string) Delegate { + return delegates.NewSegment(writeKey) +} diff --git a/instrumentation/activities/delegates/segment.go b/instrumentation/activities/delegates/segment.go new file mode 100644 index 0000000..12bea83 --- /dev/null +++ b/instrumentation/activities/delegates/segment.go @@ -0,0 +1,39 @@ +package delegates + +import ( + "gopkg.in/segmentio/analytics-go.v3" + + "github.com/puppetlabs/horsehead/v2/instrumentation/activities/activity" +) + +// Segment reports activity data to Segment, which in turn reports it to other +// activity providers. +type Segment struct { + client analytics.Client +} + +func (d *Segment) Report(act activity.Activity) error { + track := analytics.Track{ + UserId: act.UserID, + Event: act.Name, + Timestamp: act.OccuredAt, + Properties: convertActivityMetadataForSegment(act.Metadata), + } + + return d.client.Enqueue(track) +} + +func convertActivityMetadataForSegment(am activity.ActivityMetadata) analytics.Properties { + p := analytics.NewProperties() + + for k, v := range am { + p.Set(k, v) + } + + return p +} + +func NewSegment(writeKey string) *Segment { + client := analytics.New(writeKey) + return &Segment{client} +}