From 3e6f8c7c1f224606f31d77a24b27cbe039d1b2e6 Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Sat, 1 Jul 2023 22:30:12 +0000 Subject: [PATCH] Add segment support --- tracker/backend/ingestor/go.mod | 4 ++++ tracker/backend/ingestor/go.sum | 6 +++++ tracker/backend/ingestor/handler/sample.go | 26 ++++++++++++++++++++++ tracker/backend/ingestor/main.go | 14 +++++++----- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/tracker/backend/ingestor/go.mod b/tracker/backend/ingestor/go.mod index e9cbd90..9abc024 100644 --- a/tracker/backend/ingestor/go.mod +++ b/tracker/backend/ingestor/go.mod @@ -12,6 +12,7 @@ require ( github.com/bufbuild/connect-go v1.9.0 github.com/bufbuild/connect-grpcreflect-go v1.1.0 github.com/gitpod-io/leeway v0.0.0-00010101000000-000000000000 + github.com/segmentio/analytics-go/v3 v3.2.1 github.com/sirupsen/logrus v1.9.3 ) @@ -29,10 +30,12 @@ require ( github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.12 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.19.2 // indirect github.com/aws/smithy-go v1.13.5 // indirect + github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/goccy/go-json v0.9.11 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/flatbuffers v2.0.8+incompatible // indirect + github.com/google/uuid v1.3.0 // indirect github.com/influxdata/line-protocol/v2 v2.2.1 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/klauspost/asmfmt v1.3.2 // indirect @@ -41,6 +44,7 @@ require ( github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect github.com/pierrec/lz4/v4 v4.1.15 // indirect + github.com/segmentio/backo-go v1.0.0 // indirect github.com/zeebo/xxh3 v1.0.2 // indirect golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect golang.org/x/mod v0.10.0 // indirect diff --git a/tracker/backend/ingestor/go.sum b/tracker/backend/ingestor/go.sum index e9f507f..92122fb 100644 --- a/tracker/backend/ingestor/go.sum +++ b/tracker/backend/ingestor/go.sum @@ -99,6 +99,8 @@ github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +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/bufbuild/connect-go v1.9.0 h1:JIgAeNuFpo+SUPfU19Yt5TcWlznsN5Bv10/gI/6Pjoc= github.com/bufbuild/connect-go v1.9.0/go.mod h1:CAIePUgkDR5pAFaylSMtNK45ANQjp9JvpluG20rhpV8= github.com/bufbuild/connect-grpcreflect-go v1.1.0 h1:T0FKu1y9zZW4cjHuF+Q7jIN6ek8HTpCxOP8ZsORZICg= @@ -418,6 +420,10 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/segmentio/analytics-go/v3 v3.2.1 h1:G+f90zxtc1p9G+WigVyTR0xNfOghOGs/PYAlljLOyeg= +github.com/segmentio/analytics-go/v3 v3.2.1/go.mod h1:p8owAF8X+5o27jmvUognuXxdtqvSGtD0ZrfY2kcS9bE= +github.com/segmentio/backo-go v1.0.0 h1:kbOAtGJY2DqOR0jfRkYEorx/b18RgtepGtY3+Cpe6qA= +github.com/segmentio/backo-go v1.0.0/go.mod h1:kJ9mm9YmoWSkk+oQ+5Cj8DEoRCX2JT6As4kEtIIOp1M= github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shirou/gopsutil/v3 v3.22.2/go.mod h1:WapW1AOOPlHyXr+yOyw3uYx36enocrtSoSBy0L5vUHY= github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk= diff --git a/tracker/backend/ingestor/handler/sample.go b/tracker/backend/ingestor/handler/sample.go index d4ede97..885f2f2 100644 --- a/tracker/backend/ingestor/handler/sample.go +++ b/tracker/backend/ingestor/handler/sample.go @@ -10,6 +10,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/cloudwatch" "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" v1 "github.com/gitpod-io/leeway/pkg/remotereporter/api/gen/v1" + segment "github.com/segmentio/analytics-go/v3" "github.com/sirupsen/logrus" ) @@ -73,3 +74,28 @@ func WriteToInfluxDB(client *influx.Client, database string) SampleStorageFunc { ) } } + +func WriteToSegment(client segment.Client) SampleStorageFunc { + return func(ctx context.Context, sample *v1.PackageBuildFinishedRequest) error { + eventName := "package_build_" + if sample.Error == "" { + eventName += "succeeded" + } else { + eventName += "failed" + } + + client.Enqueue(segment.Track{ + AnonymousId: sample.SessionId, + Event: eventName, + Timestamp: time.Now(), + Properties: segment.Properties{ + "name": sample.Package.Name, + "durationMS": sample.DurationMs, + "repo": sample.Package.Git.Origin, + "dirtyWorkingCopy": sample.Package.Git.DirtyWorkingCopy, + "commit": sample.Package.Git.Commit, + }, + }) + return nil + } +} diff --git a/tracker/backend/ingestor/main.go b/tracker/backend/ingestor/main.go index 04339e8..97a5d11 100644 --- a/tracker/backend/ingestor/main.go +++ b/tracker/backend/ingestor/main.go @@ -14,6 +14,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/cloudwatch" "github.com/awslabs/aws-lambda-go-api-proxy/httpadapter" grpcreflect "github.com/bufbuild/connect-grpcreflect-go" + segment "github.com/segmentio/analytics-go/v3" "github.com/sirupsen/logrus" "github.com/gitpod-io/leeway/pkg/remotereporter/api/gen/v1/v1connect" @@ -21,9 +22,9 @@ import ( ) var ( - listen = flag.String("listen", ":8080", "address to listen on when not running as lambda") - verbose = flag.Bool("verbose", false, "enable verbose logging") - sampleSink = flag.String("sample-sink", "console", "where to write samples to. Valid values are: console, cloudwatch") + listen = flag.String("listen", ":8080", "address to listen on when not running as lambda") + verbose = flag.Bool("verbose", false, "enable verbose logging") + sink = flag.String("sink", "console", "where to write samples to. Valid values are: console, cloudwatch, influxdb, segment") ) func main() { @@ -41,7 +42,7 @@ func main() { mux := http.NewServeMux() var store handler.SampleStorageFunc - switch *sampleSink { + switch *sink { case "console": store = handler.PrintSample case "cloudwatch": @@ -57,8 +58,11 @@ func main() { log.Fatalf("cannot create InfluxDB client: %v", err) } store = handler.WriteToInfluxDB(client, os.Getenv("INFLUXDB_DATABASE")) + case "segment": + client := segment.New(os.Getenv("SEGMENT_KEY")) + store = handler.WriteToSegment(client) default: - logrus.Fatalf("unsupported --sample-sink: %s", *sampleSink) + logrus.Fatalf("unsupported --sample-sink: %s", *sink) } mux.Handle(v1connect.NewReporterServiceHandler(handler.NewBuildReportHandler(store)))