feat(v2/events/snowplow) add custom context support to v2 Snowplow

Related to:

This adds support for adding custom contexts to Snowplow payloads.

Similar to !450 (closed), but does some groundwork to support validating contexts and includes validating gitlab_standard per its iglu schema out of the box for Artifact Registry and other projects using Snowplow in LabKit v2.

How to test

cd v2 && go test -race ./events/snowplow/...

Verify on local with GDK's Snowplow Micro

  1. Run gdk config set snowplow_micro.enabled true and gdk reconfigure.
  2. Make sure snowplow-micro runs (requires Docker).
  3. Run this minimal Go program:
package main

import (
	"log"
	"time"

	"gitlab.com/gitlab-org/labkit/v2/events/snowplow"
)

func main() {
	emitter, err := snowplow.NewEmitter("http://localhost:9091/com.snowplowanalytics.snowplow/tp2")
	if err != nil {
		log.Fatal(err)
	}
	tracker, _ := snowplow.NewTracker("verify-labkit-498", emitter)

	if err := tracker.TrackEventWithContexts(
		"labkit_498_verification",
		nil,
		snowplow.GitLabStandardContext{
			Environment:    "development",
			OrganizationID: 1,
			Realm:          snowplow.RealmSelfManaged,
		},
	); err != nil {
		log.Fatal(err)
	}

	tracker.Flush()
	time.Sleep(2 * time.Second) // emitter's send loop is async; wait briefly
	tracker.Stop()
}
go mod init verify
go mod edit -replace gitlab.com/gitlab-org/labkit/v2=/path/to/your/labkit/v2
go mod tidy
go run main.go
  1. Verify the event landed in Snowplow Micro:
  curl -s http://localhost:9091/micro/good | jq '.[] | select(.event.app_id == "verify-labkit-498")'

Or just visit http://localhost:9091/micro/good

You should see one event whose contexts array contains an entry with schema = "iglu:com.gitlab/gitlab_standard/jsonschema/1-1-8" and data.environment = "development", data.organization_id = 1, data.realm = "self-managed".

Edited by João Pereira

Merge request reports

Loading