Class: Gitlab::Tracking::StandardContext

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/tracking/standard_context.rb

Constant Summary collapse

GITLAB_STANDARD_SCHEMA_URL =
'iglu:com.gitlab/gitlab_standard/jsonschema/1-1-7'
GITLAB_RAILS_SOURCE =
'gitlab-rails'
GITLAB_REALM_SELF_MANAGED =
'self-managed'

Instance Method Summary collapse

Constructor Details

#initialize(namespace: nil, project_id: nil, user: nil, feature_enabled_by_namespace_ids: nil, **extra) ⇒ StandardContext

Returns a new instance of StandardContext.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/tracking/standard_context.rb', line 10

def initialize(
  namespace: nil, project_id: nil, user: nil,
  feature_enabled_by_namespace_ids: nil, **extra)
  check_argument_type(:namespace, namespace, Namespace)
  check_argument_type(:project_id, project_id, Integer)
  check_argument_type(:user, user, User)

  plan_name = get_plan_name(namespace)
  check_argument_type(:plan_name, plan_name, String)

  @namespace = namespace
  @plan_name = plan_name
  @project_id = project_id
  @user = user
  @extra = extra
  @feature_enabled_by_namespace_ids = feature_enabled_by_namespace_ids
end

Instance Method Details

#environmentObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/tracking/standard_context.rb', line 32

def environment
  return 'staging' if Gitlab.staging?

  return 'production' if Gitlab.com?

  return 'org' if Gitlab.org?

  return 'self-managed' if Rails.env.production?

  'development'
end

#sourceObject



44
45
46
# File 'lib/gitlab/tracking/standard_context.rb', line 44

def source
  GITLAB_RAILS_SOURCE
end

#to_contextObject



28
29
30
# File 'lib/gitlab/tracking/standard_context.rb', line 28

def to_context
  SnowplowTracker::SelfDescribingJson.new(GITLAB_STANDARD_SCHEMA_URL, to_h)
end

#to_hObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gitlab/tracking/standard_context.rb', line 48

def to_h
  {
    environment: environment,
    source: source,
    correlation_id: Labkit::Correlation::CorrelationId.current_or_new_id,
    plan: plan_name,
    extra: extra,
    user_id: tracked_user_id,
    global_user_id: global_user_id,
    user_type: tracked_user_type,
    is_gitlab_team_member: gitlab_team_member?(user&.id),
    namespace_id: namespace&.id,
    ultimate_parent_namespace_id: namespace&.root_ancestor&.id,
    project_id: project_id,
    feature_enabled_by_namespace_ids: feature_enabled_by_namespace_ids,
    realm: realm,
    deployment_type: deployment_type,
    instance_id: ::Gitlab::GlobalAnonymousId.instance_id,
    unique_instance_id: Gitlab::GlobalAnonymousId.instance_uuid,
    host_name: Gitlab.config.gitlab.host,
    instance_version: Gitlab.version_info.to_s,
    context_generated_at: Time.current
  }
end