Commit 09a37d36 authored by Alper Akgun's avatar Alper Akgun

Merge branch 'ali/update-gitlab-standard' into 'master'

Allow extra data to be sent with Snowplow events

See merge request gitlab-org/gitlab!57504
parents 5e2abe50 933e1074
...@@ -314,6 +314,7 @@ Custom event tracking and instrumentation can be added by directly calling the ` ...@@ -314,6 +314,7 @@ Custom event tracking and instrumentation can be added by directly calling the `
| `project` | Project | nil | The project associated with the event. | | `project` | Project | nil | The project associated with the event. |
| `user` | User | nil | The user associated with the event. | | `user` | User | nil | The user associated with the event. |
| `namespace` | Namespace | nil | The namespace associated with the event. | | `namespace` | Namespace | nil | The namespace associated with the event. |
| `extra` | Hash | `{}` | Additional keyword arguments are collected into a hash and sent with the event. |
Tracking can be viewed as either tracking user behavior, or can be used for instrumentation to monitor and visualize performance over time in an area or aspect of code. Tracking can be viewed as either tracking user behavior, or can be used for instrumentation to monitor and visualize performance over time in an area or aspect of code.
...@@ -495,6 +496,7 @@ The [`StandardContext`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/g ...@@ -495,6 +496,7 @@ The [`StandardContext`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/g
| `namespace_id` | **{dotted-circle}** | integer | | | `namespace_id` | **{dotted-circle}** | integer | |
| `environment` | **{check-circle}** | string (max 32 chars) | Name of the source environment, such as `production` or `staging` | | `environment` | **{check-circle}** | string (max 32 chars) | Name of the source environment, such as `production` or `staging` |
| `source` | **{check-circle}** | string (max 32 chars) | Name of the source application, such as `gitlab-rails` or `gitlab-javascript` | | `source` | **{check-circle}** | string (max 32 chars) | Name of the source application, such as `gitlab-rails` or `gitlab-javascript` |
| `extra` | **{dotted-circle}** | JSON | Any additional data associated with the event, in the form of key-value pairs |
### Default Schema ### Default Schema
......
...@@ -9,8 +9,8 @@ module Gitlab ...@@ -9,8 +9,8 @@ module Gitlab
Gitlab::CurrentSettings.snowplow_enabled? Gitlab::CurrentSettings.snowplow_enabled?
end end
def event(category, action, label: nil, property: nil, value: nil, context: [], project: nil, user: nil, namespace: nil) # rubocop:disable Metrics/ParameterLists def event(category, action, label: nil, property: nil, value: nil, context: [], project: nil, user: nil, namespace: nil, **extra) # rubocop:disable Metrics/ParameterLists
contexts = [Tracking::StandardContext.new(project: project, user: user, namespace: namespace).to_context, *context] contexts = [Tracking::StandardContext.new(project: project, user: user, namespace: namespace, **extra).to_context, *context]
snowplow.event(category, action, label: label, property: property, value: value, context: contexts) snowplow.event(category, action, label: label, property: property, value: value, context: contexts)
product_analytics.event(category, action, label: label, property: property, value: value, context: contexts) product_analytics.event(category, action, label: label, property: property, value: value, context: contexts)
......
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
module Gitlab module Gitlab
module Tracking module Tracking
class StandardContext class StandardContext
GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-3' GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-4'
GITLAB_RAILS_SOURCE = 'gitlab-rails' GITLAB_RAILS_SOURCE = 'gitlab-rails'
def initialize(namespace: nil, project: nil, user: nil, **data) def initialize(namespace: nil, project: nil, user: nil, **extra)
@data = data @extra = extra
end end
def to_context def to_context
...@@ -35,8 +35,9 @@ module Gitlab ...@@ -35,8 +35,9 @@ module Gitlab
def to_h def to_h
{ {
environment: environment, environment: environment,
source: source source: source,
}.merge(@data) extra: @extra
}
end end
end end
end end
......
...@@ -58,10 +58,16 @@ RSpec.describe Gitlab::Tracking::StandardContext do ...@@ -58,10 +58,16 @@ RSpec.describe Gitlab::Tracking::StandardContext do
end end
context 'with extra data' do context 'with extra data' do
subject { described_class.new(foo: 'bar') } subject { described_class.new(extra_key_1: 'extra value 1', extra_key_2: 'extra value 2') }
it 'creates a Snowplow context with the given data' do it 'includes extra data in `extra` hash' do
expect(snowplow_context.to_json.dig(:data, :foo)).to eq('bar') expect(snowplow_context.to_json.dig(:data, :extra)).to eq(extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
end
end
context 'without extra data' do
it 'contains an empty `extra` hash' do
expect(snowplow_context.to_json.dig(:data, :extra)).to be_empty
end end
end end
......
...@@ -51,7 +51,7 @@ RSpec.describe Gitlab::Tracking do ...@@ -51,7 +51,7 @@ RSpec.describe Gitlab::Tracking do
expect(Gitlab::Tracking::StandardContext) expect(Gitlab::Tracking::StandardContext)
.to receive(:new) .to receive(:new)
.with(project: project, user: user, namespace: namespace) .with(project: project, user: user, namespace: namespace, extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
.and_call_original .and_call_original
expect_any_instance_of(klass).to receive(:event) do |_, category, action, args| expect_any_instance_of(klass).to receive(:event) do |_, category, action, args|
...@@ -66,7 +66,8 @@ RSpec.describe Gitlab::Tracking do ...@@ -66,7 +66,8 @@ RSpec.describe Gitlab::Tracking do
end end
described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5, described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5,
context: [other_context], project: project, user: user, namespace: namespace) context: [other_context], project: project, user: user, namespace: namespace,
extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment