File: snowplow_event_tracking_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (63 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
#
# Requires a context containing:
# - subject
# - feature_flag_name
# - category
# - action
# - namespace
# Optionally, the context can contain:
# - project
# - property
# - user
# - label
# - **extra

RSpec.shared_examples 'Snowplow event tracking' do |overrides: {}|
  let(:extra) { {} }

  if try(:feature_flag_name)
    it 'is not emitted if FF is disabled' do
      stub_feature_flags(feature_flag_name => false)

      subject

      expect_no_snowplow_event(category: category, action: action)
    end
  end

  it 'is emitted' do
    params = {
      category: category,
      action: action,
      namespace: namespace,
      user: try(:user),
      project: try(:project),
      label: try(:label),
      property: try(:property),
      context: try(:context)
    }.merge(overrides).compact.merge(extra)

    subject

    expect_snowplow_event(**params)
  end
end

RSpec.shared_examples 'Snowplow event tracking with RedisHLL context' do |overrides: {}|
  it_behaves_like 'Snowplow event tracking', overrides: overrides do
    let(:context) do
      event = try(:property) || action
      [Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll, event: event).to_context.to_json]
    end
  end
end

RSpec.shared_examples 'Snowplow event tracking with Redis context' do |overrides: {}|
  it_behaves_like 'Snowplow event tracking', overrides: overrides do
    let(:context) do
      key_path = try(:label) || action
      [Gitlab::Usage::MetricDefinition.context_for(key_path).to_context.to_json]
    end
  end
end