Commit f6532332 authored by Markus Koller's avatar Markus Koller

Merge branch 'datadog_integration_update_urls' into 'master'

Update URLs in the datadog integration

See merge request gitlab-org/gitlab!66093
parents a1767c8e 05e7f98c
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
module Integrations module Integrations
class Datadog < Integration class Datadog < Integration
DEFAULT_SITE = 'datadoghq.com' DEFAULT_DOMAIN = 'datadoghq.com'
URL_TEMPLATE = 'https://webhooks-http-intake.logs.%{datadog_site}/v1/input/' URL_TEMPLATE = 'https://webhooks-http-intake.logs.%{datadog_domain}/api/v2/webhook'
URL_TEMPLATE_API_KEYS = 'https://app.%{datadog_site}/account/settings#api' URL_TEMPLATE_API_KEYS = 'https://app.%{datadog_domain}/account/settings#api'
URL_API_KEYS_DOCS = "https://docs.#{DEFAULT_SITE}/account_management/api-app-keys/" URL_API_KEYS_DOCS = "https://docs.#{DEFAULT_DOMAIN}/account_management/api-app-keys/"
SUPPORTED_EVENTS = %w[ SUPPORTED_EVENTS = %w[
pipeline job pipeline job
...@@ -26,7 +26,7 @@ module Integrations ...@@ -26,7 +26,7 @@ module Integrations
def initialize_properties def initialize_properties
super super
self.datadog_site ||= DEFAULT_SITE self.datadog_site ||= DEFAULT_DOMAIN
end end
def self.supported_events def self.supported_events
...@@ -62,7 +62,7 @@ module Integrations ...@@ -62,7 +62,7 @@ module Integrations
{ {
type: 'text', type: 'text',
name: 'datadog_site', name: 'datadog_site',
placeholder: DEFAULT_SITE, placeholder: DEFAULT_DOMAIN,
help: 'Choose the Datadog site to send data to. Set to "datadoghq.eu" to send data to the EU site', help: 'Choose the Datadog site to send data to. Set to "datadoghq.eu" to send data to the EU site',
required: false required: false
}, },
...@@ -105,18 +105,21 @@ module Integrations ...@@ -105,18 +105,21 @@ module Integrations
end end
def hook_url def hook_url
url = api_url.presence || sprintf(URL_TEMPLATE, datadog_site: datadog_site) url = api_url.presence || sprintf(URL_TEMPLATE, datadog_domain: datadog_domain)
url = URI.parse(url) url = URI.parse(url)
url.path = File.join(url.path || '/', api_key) query = {
query = { service: datadog_service.presence, env: datadog_env.presence }.compact "dd-api-key" => api_key,
url.query = query.to_query unless query.empty? service: datadog_service.presence,
env: datadog_env.presence
}.compact
url.query = query.to_query
url.to_s url.to_s
end end
def api_keys_url def api_keys_url
return URL_API_KEYS_DOCS unless datadog_site.presence return URL_API_KEYS_DOCS unless datadog_site.presence
sprintf(URL_TEMPLATE_API_KEYS, datadog_site: datadog_site) sprintf(URL_TEMPLATE_API_KEYS, datadog_domain: datadog_domain)
end end
def execute(data) def execute(data)
...@@ -137,5 +140,14 @@ module Integrations ...@@ -137,5 +140,14 @@ module Integrations
{ success: true, result: result[:message] } { success: true, result: result[:message] }
end end
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
end end
end end
...@@ -10,13 +10,13 @@ RSpec.describe Integrations::Datadog do ...@@ -10,13 +10,13 @@ RSpec.describe Integrations::Datadog do
let(:active) { true } let(:active) { true }
let(:dd_site) { 'datadoghq.com' } let(:dd_site) { 'datadoghq.com' }
let(:default_url) { 'https://webhooks-http-intake.logs.datadoghq.com/v1/input/' } let(:default_url) { 'https://webhooks-http-intake.logs.datadoghq.com/api/v2/webhook' }
let(:api_url) { '' } let(:api_url) { '' }
let(:api_key) { SecureRandom.hex(32) } let(:api_key) { SecureRandom.hex(32) }
let(:dd_env) { 'ci' } let(:dd_env) { 'ci' }
let(:dd_service) { 'awesome-gitlab' } let(:dd_service) { 'awesome-gitlab' }
let(:expected_hook_url) { default_url + api_key + "?env=#{dd_env}&service=#{dd_service}" } let(:expected_hook_url) { default_url + "?dd-api-key=#{api_key}&env=#{dd_env}&service=#{dd_service}" }
let(:instance) do let(:instance) do
described_class.new( described_class.new(
...@@ -65,7 +65,7 @@ RSpec.describe Integrations::Datadog do ...@@ -65,7 +65,7 @@ RSpec.describe Integrations::Datadog do
context 'with custom api_url' do context 'with custom api_url' do
let(:dd_site) { '' } let(:dd_site) { '' }
let(:api_url) { 'https://webhooks-http-intake.logs.datad0g.com/v1/input/' } let(:api_url) { 'https://webhooks-http-intake.logs.datad0g.com/api/v2/webhook' }
it { is_expected.not_to validate_presence_of(:datadog_site) } it { is_expected.not_to validate_presence_of(:datadog_site) }
it { is_expected.to validate_presence_of(:api_url) } it { is_expected.to validate_presence_of(:api_url) }
...@@ -107,9 +107,9 @@ RSpec.describe Integrations::Datadog do ...@@ -107,9 +107,9 @@ RSpec.describe Integrations::Datadog do
end end
context 'with custom URL' do context 'with custom URL' do
let(:api_url) { 'https://webhooks-http-intake.logs.datad0g.com/v1/input/' } let(:api_url) { 'https://webhooks-http-intake.logs.datad0g.com/api/v2/webhook' }
it { is_expected.to eq(api_url + api_key + "?env=#{dd_env}&service=#{dd_service}") } it { is_expected.to eq(api_url + "?dd-api-key=#{api_key}&env=#{dd_env}&service=#{dd_service}") }
context 'blank' do context 'blank' do
let(:api_url) { '' } let(:api_url) { '' }
...@@ -122,7 +122,7 @@ RSpec.describe Integrations::Datadog do ...@@ -122,7 +122,7 @@ RSpec.describe Integrations::Datadog do
let(:dd_service) { '' } let(:dd_service) { '' }
let(:dd_env) { '' } let(:dd_env) { '' }
it { is_expected.to eq(default_url + api_key) } it { is_expected.to eq(default_url + "?dd-api-key=#{api_key}") }
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment