2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
module Integrations
class Datadog < Integration
2021-10-27 15:23:28 +05:30
include ActionView :: Helpers :: UrlHelper
2021-09-30 23:02:18 +05:30
include HasWebHook
extend Gitlab :: Utils :: Override
DEFAULT_DOMAIN = 'datadoghq.com'
2021-11-11 11:23:49 +05:30
URL_TEMPLATE = 'https://webhook-intake.%{datadog_domain}/api/v2/webhook'
2021-09-30 23:02:18 +05:30
URL_API_KEYS_DOCS = " https://docs. #{ DEFAULT_DOMAIN } /account_management/api-app-keys/ "
2021-06-08 01:23:25 +05:30
SUPPORTED_EVENTS = %w[
pipeline job
] . freeze
prop_accessor :datadog_site , :api_url , :api_key , :datadog_service , :datadog_env
with_options if : :activated? do
validates :api_key , presence : true , format : { with : / \ A \ w+ \ z / }
validates :datadog_site , format : { with : / \ A[ \ w \ .]+ \ z / , allow_blank : true }
validates :api_url , public_url : { allow_blank : true }
validates :datadog_site , presence : true , unless : - > ( obj ) { obj . api_url . present? }
validates :api_url , presence : true , unless : - > ( obj ) { obj . datadog_site . present? }
end
def initialize_properties
super
2021-09-30 23:02:18 +05:30
self . datadog_site || = DEFAULT_DOMAIN
2021-06-08 01:23:25 +05:30
end
def self . supported_events
SUPPORTED_EVENTS
end
def self . default_test_event
'pipeline'
end
def configurable_events
[ ] # do not allow to opt out of required hooks
end
def title
'Datadog'
end
def description
2021-10-27 15:23:28 +05:30
s_ ( 'DatadogIntegration|Trace your GitLab pipelines with Datadog.' )
2021-06-08 01:23:25 +05:30
end
def help
2021-10-27 15:23:28 +05:30
docs_link = 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 }
2021-06-08 01:23:25 +05:30
end
def self . to_param
'datadog'
end
def fields
[
{
type : 'text' ,
name : 'datadog_site' ,
2021-09-30 23:02:18 +05:30
placeholder : DEFAULT_DOMAIN ,
2021-10-27 15:23:28 +05:30
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
} ,
2021-06-08 01:23:25 +05:30
required : false
} ,
{
type : 'text' ,
name : 'api_url' ,
2021-10-27 15:23:28 +05:30
title : s_ ( 'DatadogIntegration|API URL' ) ,
help : s_ ( 'DatadogIntegration|(Advanced) The full URL for your Datadog site.' ) ,
2021-06-08 01:23:25 +05:30
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' ) ,
2021-10-27 15:23:28 +05:30
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
} ,
2021-06-08 01:23:25 +05:30
required : true
} ,
{
type : 'text' ,
name : 'datadog_service' ,
2021-10-27 15:23:28 +05:30
title : s_ ( 'DatadogIntegration|Service' ) ,
2021-06-08 01:23:25 +05:30
placeholder : 'gitlab-ci' ,
2021-10-27 15:23:28 +05:30
help : s_ ( 'DatadogIntegration|Tag all data from this GitLab instance in Datadog. Useful when managing several self-managed deployments.' )
2021-06-08 01:23:25 +05:30
} ,
{
type : 'text' ,
name : 'datadog_env' ,
2021-10-27 15:23:28 +05:30
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
}
2021-06-08 01:23:25 +05:30
}
]
end
2021-09-30 23:02:18 +05:30
override :hook_url
2021-06-08 01:23:25 +05:30
def hook_url
2021-09-30 23:02:18 +05:30
url = api_url . presence || sprintf ( URL_TEMPLATE , datadog_domain : datadog_domain )
2021-06-08 01:23:25 +05:30
url = URI . parse ( url )
2021-09-30 23:02:18 +05:30
query = {
" dd-api-key " = > api_key ,
service : datadog_service . presence ,
env : datadog_env . presence
} . compact
url . query = query . to_query
2021-06-08 01:23:25 +05:30
url . to_s
end
def execute ( data )
object_kind = data [ :object_kind ]
object_kind = 'job' if object_kind == 'build'
return unless supported_events . include? ( object_kind )
2021-10-27 15:23:28 +05:30
data = data . with_retried_builds if data . respond_to? ( :with_retried_builds )
2021-09-30 23:02:18 +05:30
execute_web_hook! ( data , " #{ object_kind } hook " )
2021-06-08 01:23:25 +05:30
end
def test ( data )
2021-10-27 15:23:28 +05:30
result = execute ( data )
{
success : ( 200 .. 299 ) . cover? ( result [ :http_status ] ) ,
result : result [ :message ]
}
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
private
def datadog_domain
# Transparently ignore "app" prefix from datadog_site as the official docs table in
# https://docs.datadoghq.com/getting_started/site/ is confusing for internal URLs.
# US3 needs to keep a prefix but other datacenters cannot have the listed "app" prefix
datadog_site . delete_prefix ( " app. " )
end
2021-06-08 01:23:25 +05:30
end
end