Class: Integrations::Datadog

Inherits:
Integration show all
Includes:
HasAvatar, 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/"
CI_LOGS_DOCS =
"https://docs.#{DEFAULT_DOMAIN}/continuous_integration/pipelines/gitlab/?tab=gitlabcom#collect-job-logs"
CI_VISIBILITY_PRICING =
"https://www.#{DEFAULT_DOMAIN}/pricing/?product=ci-pipeline-visibility#products"
SITES_DOCS =
"https://docs.#{DEFAULT_DOMAIN}/getting_started/site/"
SUPPORTED_EVENTS =
%w[
  pipeline build archive_trace push merge_request note tag_push subgroup project
].freeze
TAG_KEY_VALUE_RE =
%r{\A [\w-]+ : .*\S.* \z}x

Constants included from Base::Integration

Base::Integration::BASE_ATTRIBUTES, Base::Integration::BASE_CLASSES, Base::Integration::DEV_INTEGRATION_NAMES, Base::Integration::ENCRYPTED_PROPERTIES_MAX_SIZE, Base::Integration::INSTANCE_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::INTEGRATION_NAMES, Base::Integration::PROJECT_AND_GROUP_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::PROJECT_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::SECTION_TYPE_CONFIGURATION, Base::Integration::SECTION_TYPE_CONNECTION, Base::Integration::SECTION_TYPE_TRIGGER, Base::Integration::SNOWPLOW_EVENT_ACTION, Base::Integration::SNOWPLOW_EVENT_LABEL, Base::Integration::UnknownType

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasAvatar

#avatar_url

Methods included from HasWebHook

#execute_web_hook!, #hook_ssl_verification, #update_web_hook!

Methods included from Base::Integration

#activate!, #activate_disabled_reason, #activated?, #after_build_from_integration, #api_field_names, #async_execute, #attributes, #attribution_notice, #category, #chat?, #ci?, #deactivate!, #default_test_event, #description, #dup, #editable?, #event_channel_names, #event_names, #fields, #form_fields, #group_level?, #help, #inheritable?, #instance_level?, #json_fields, #manual_activation?, #operating?, #organization_id_from_parent, #parent, #project_level?, #reencrypt_properties, #reset_updated_properties, #secret_fields, #supported_events, #supports_data_fields?, #testable?, #title, #to_database_hash, #to_param, #toggle!, #updated_properties, #validate_encrypted_properties_size_limit

Methods included from Gitlab::Utils::Override

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

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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 Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.default_test_eventObject



179
180
181
# File 'app/models/integrations/datadog.rb', line 179

def self.default_test_event
  'push'
end

.descriptionObject



192
193
194
# File 'app/models/integrations/datadog.rb', line 192

def self.description
  s_('DatadogIntegration|Connect your projects to Datadog and trace your GitLab pipelines.')
end

.helpObject



196
197
198
199
200
201
202
# File 'app/models/integrations/datadog.rb', line 196

def self.help
  build_help_page_url(
    'integration/datadog.md',
    s_('DatadogIntegration|Connect your GitLab projects to your Datadog account to synchronize repository metadata and enrich telemetry on your Datadog account.'),
    link_text: _('How do I set up this integration?')
  )
end

.supported_eventsObject



175
176
177
# File 'app/models/integrations/datadog.rb', line 175

def self.supported_events
  SUPPORTED_EVENTS
end

.titleObject



188
189
190
# File 'app/models/integrations/datadog.rb', line 188

def self.title
  'Datadog'
end

.to_paramObject



204
205
206
# File 'app/models/integrations/datadog.rb', line 204

def self.to_param
  'datadog'
end

Instance Method Details

#configurable_eventsObject



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

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

#execute(data) ⇒ Object



226
227
228
229
230
231
232
233
# File 'app/models/integrations/datadog.rb', line 226

def execute(data)
  return unless supported_events.include?(data[:object_kind])

  object_kind = data[:object_kind]
  object_kind = 'job' if object_kind == 'build'
  data = hook_data(data, object_kind)
  execute_web_hook!(data, "#{object_kind} hook")
end

#hook_urlObject



209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/integrations/datadog.rb', line 209

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

#initialize_propertiesObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/integrations/datadog.rb', line 150

def initialize_properties
  super

  self.datadog_site ||= DEFAULT_DOMAIN

  # Previous versions of the integration don't have the datadog_ci_visibility boolean stored in the configuration.
  # Since the previous default was for this to be enabled, we want this attribute to be initialized to true
  # if the integration was previously active. Otherwise, it should default to false.
  if datadog_ci_visibility.nil?
    self.datadog_ci_visibility = active
  end
end

#sectionsObject

The config is divided in two sections:

  • General account configuration, which allows setting up a Datadog site and API key

  • CI Visibility configuration, which is specific to job & pipeline events



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/integrations/datadog.rb', line 24

def sections
  [
    {
      type: SECTION_TYPE_CONNECTION,
      title: s_('DatadogIntegration|Datadog account'),
      description: help
    },
    {
      type: SECTION_TYPE_CONFIGURATION,
      title: s_('DatadogIntegration|CI Visibility'),
      description: s_('DatadogIntegration|Additionally, enable CI Visibility to send pipeline information to Datadog to monitor for job failures and troubleshoot performance issues.')
    }
  ]
end

#test(data) ⇒ Object



235
236
237
238
239
240
241
242
# File 'app/models/integrations/datadog.rb', line 235

def test(data)
  result = execute(data)

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

#update_pipeline_eventsObject



167
168
169
170
171
172
173
# File 'app/models/integrations/datadog.rb', line 167

def update_pipeline_events
  # pipeline and job events are opt-in, controlled by a single datadog_ci_visibility checkbox
  unless datadog_ci_visibility.nil?
    self.job_events = datadog_ci_visibility
    self.pipeline_events = datadog_ci_visibility
  end
end

#url_variablesObject



222
223
224
# File 'app/models/integrations/datadog.rb', line 222

def url_variables
  { 'api_key' => api_key }
end