Class: Integrations::Datadog

Inherits:
Integration show all
Extended by:
Gitlab::Utils::Override
Includes:
HasWebHook
Defined in:
app/models/integrations/datadog.rb

Constant Summary collapse

DEFAULT_DOMAIN =
'datadoghq.com'
URL_TEMPLATE =
'https://webhook-intake.%{datadog_domain}/api/v2/webhook'
URL_API_KEYS_DOCS =
"https://docs.#{DEFAULT_DOMAIN}/account_management/api-app-keys/"
SUPPORTED_EVENTS =
%w[
  pipeline job
].freeze
TAG_KEY_VALUE_RE =
%r{\A [\w-]+ : .*\S.* \z}x.freeze

Constants inherited from Integration

Integration::BASE_CLASSES, Integration::DEV_INTEGRATION_NAMES, Integration::INTEGRATION_NAMES, Integration::PROJECT_SPECIFIC_INTEGRATION_NAMES, Integration::SECTION_TYPE_CONNECTION, Integration::UnknownType

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from HasWebHook

#execute_web_hook!, #hook_ssl_verification, #update_web_hook!

Methods inherited from Integration

#activate_disabled_reason, #activated?, #api_field_names, #async_execute, #attributes, available_integration_names, available_integration_types, boolean_accessor, build_from_integration, #category, create_from_active_default_integrations, default_integration, #default_test_event, dev_integration_names, #dup, #editable?, #event_channel_names, event_description, #event_field, #event_names, event_names, field, fields, find_or_initialize_all_non_project_specific, find_or_initialize_non_project_specific_integration, #global_fields, #group_level?, #inheritable?, inherited_descendants_from_self_or_ancestors_from, instance_exists_for?, #instance_level?, integration_name_to_model, integration_name_to_type, integration_names, #json_fields, #operating?, #parent, #project_level?, project_specific_integration_names, prop_accessor, #properties=, #reencrypt_properties, #reset_updated_properties, #secret_fields, #sections, #show_active_box?, #supports_data_fields?, #testable?, #to_data_fields_hash, #to_integration_hash, #to_param, #updated_properties

Methods included from ResetSecretFields

#exposing_secrets_fields

Methods included from Loggable

#build_message, #log_error, #log_exception, #log_info, #logger

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.default_test_eventObject


49
50
51
# File 'app/models/integrations/datadog.rb', line 49

def self.default_test_event
  'pipeline'
end

.supported_eventsObject


37
38
39
# File 'app/models/integrations/datadog.rb', line 37

def self.supported_events
  SUPPORTED_EVENTS
end

.to_paramObject


75
76
77
# File 'app/models/integrations/datadog.rb', line 75

def self.to_param
  'datadog'
end

Instance Method Details

#configurable_eventsObject


53
54
55
56
# File 'app/models/integrations/datadog.rb', line 53

def configurable_events
  [] # do not allow to opt out of required hooks
  # archive_trace is opt-in but we handle it with a more detailed field below
end

#descriptionObject


62
63
64
# File 'app/models/integrations/datadog.rb', line 62

def description
  s_('DatadogIntegration|Trace your GitLab pipelines with Datadog.')
end

#execute(data) ⇒ Object


182
183
184
185
186
187
188
189
# File 'app/models/integrations/datadog.rb', line 182

def execute(data)
  object_kind = data[:object_kind]
  object_kind = 'job' if object_kind == 'build'
  return unless supported_events.include?(object_kind)

  data = hook_data(data, object_kind)
  execute_web_hook!(data, "#{object_kind} hook")
end

#fieldsObject


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/integrations/datadog.rb', line 79

def fields
  f = [
    {
      type: 'text',
      name: 'datadog_site',
      placeholder: DEFAULT_DOMAIN,
      help: ERB::Util.html_escape(
        s_('DatadogIntegration|The Datadog site to send data to. To send data to the EU site, use %{codeOpen}datadoghq.eu%{codeClose}.')
      ) % {
        codeOpen: '<code>'.html_safe,
        codeClose: '</code>'.html_safe
      },
      required: false
    },
    {
      type: 'text',
      name: 'api_url',
      title: s_('DatadogIntegration|API URL'),
      help: s_('DatadogIntegration|(Advanced) The full URL for your Datadog site.'),
      required: false
    },
    {
      type: 'password',
      name: 'api_key',
      title: _('API key'),
      non_empty_password_title: s_('ProjectService|Enter new API key'),
      non_empty_password_help: s_('ProjectService|Leave blank to use your current API key'),
      help: ERB::Util.html_escape(
        s_('DatadogIntegration|%{linkOpen}API key%{linkClose} used for authentication with Datadog.')
      ) % {
        linkOpen: %Q{<a href="#{URL_API_KEYS_DOCS}" target="_blank" rel="noopener noreferrer">}.html_safe,
        linkClose: '</a>'.html_safe
      },
      required: true
    }
  ]

  if Feature.enabled?(:datadog_integration_logs_collection, parent)
    f.append({
      type: 'checkbox',
      name: 'archive_trace_events',
      title: s_('Logs'),
      checkbox_label: s_('Enable logs collection'),
      help: s_('When enabled, job logs are collected by Datadog and displayed along with pipeline execution traces.'),
      required: false
    })
  end

  f += [
    {
      type: 'text',
      name: 'datadog_service',
      title: s_('DatadogIntegration|Service'),
      placeholder: 'gitlab-ci',
      help: s_('DatadogIntegration|Tag all data from this GitLab instance in Datadog. Useful when managing several self-managed deployments.')
    },
    {
      type: 'text',
      name: 'datadog_env',
      title: s_('DatadogIntegration|Environment'),
      placeholder: 'ci',
      help: ERB::Util.html_escape(
        s_('DatadogIntegration|For self-managed deployments, set the %{codeOpen}env%{codeClose} tag for all the data sent to Datadog. %{linkOpen}How do I use tags?%{linkClose}')
      ) % {
        codeOpen: '<code>'.html_safe,
        codeClose: '</code>'.html_safe,
        linkOpen: '<a href="https://docs.datadoghq.com/getting_started/tagging/#using-tags" target="_blank" rel="noopener noreferrer">'.html_safe,
        linkClose: '</a>'.html_safe
      }
    },
    {
      type: 'textarea',
      name: 'datadog_tags',
      title: s_('DatadogIntegration|Tags'),
      placeholder: "tag:value\nanother_tag:value",
      help: ERB::Util.html_escape(
        s_('DatadogIntegration|Custom tags in Datadog. Enter one tag per line in the %{codeOpen}key:value%{codeClose} format. %{linkOpen}How do I use tags?%{linkClose}')
      ) % {
        codeOpen: '<code>'.html_safe,
        codeClose: '</code>'.html_safe,
        linkOpen: '<a href="https://docs.datadoghq.com/getting_started/tagging/#using-tags" target="_blank" rel="noopener noreferrer">'.html_safe,
        linkClose: '</a>'.html_safe
      }
    }
  ]

  f
end

#helpObject


66
67
68
69
70
71
72
73
# File 'app/models/integrations/datadog.rb', line 66

def help
  docs_link = ActionController::Base.helpers.link_to(
    s_('DatadogIntegration|How do I set up this integration?'),
    Rails.application.routes.url_helpers.help_page_url('integration/datadog'),
    target: '_blank', rel: 'noopener noreferrer'
  )
  s_('DatadogIntegration|Send CI/CD pipeline information to Datadog to monitor for job failures and troubleshoot performance issues. %{docs_link}').html_safe % { docs_link: docs_link.html_safe }
end

#hook_urlObject


169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/integrations/datadog.rb', line 169

def hook_url
  url = api_url.presence || sprintf(URL_TEMPLATE, datadog_domain: datadog_domain)
  url = URI.parse(url)
  query = {
    "dd-api-key" => api_key,
    service: datadog_service.presence,
    env: datadog_env.presence,
    tags: datadog_tags_query_param.presence
  }.compact
  url.query = query.to_query
  url.to_s
end

#initialize_propertiesObject


31
32
33
34
35
# File 'app/models/integrations/datadog.rb', line 31

def initialize_properties
  super

  self.datadog_site ||= DEFAULT_DOMAIN
end

#supported_eventsObject


41
42
43
44
45
46
47
# File 'app/models/integrations/datadog.rb', line 41

def supported_events
  events = super

  return events + ['archive_trace'] if Feature.enabled?(:datadog_integration_logs_collection, parent)

  events
end

#test(data) ⇒ Object


191
192
193
194
195
196
197
198
# File 'app/models/integrations/datadog.rb', line 191

def test(data)
  result = execute(data)

  {
    success: (200..299).cover?(result[:http_status]),
    result: result[:message]
  }
end

#titleObject


58
59
60
# File 'app/models/integrations/datadog.rb', line 58

def title
  'Datadog'
end