feat(v2/events/snowplow) add custom context support to v2 Snowplow
Related to:
- Add support for custom contexts to Labkit v2 Sn... (#103 - closed)
- ADR-012: Usage Data Collection, MR
- Iglu
artifact_registry_context/1-0-0 - Iglu
gitlab_standard/1-1-8 - !450 (closed) —
TrackEventWithContextsandGitLabStandardContext(previous version of this change)
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
- Run
gdk config set snowplow_micro.enabled trueandgdk reconfigure. - Make sure
snowplow-microruns (requires Docker). - 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- 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