Skip to content

Commit

Permalink
add factory method to construct agentless telemetry transport
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jul 16, 2024
1 parent c46f299 commit 608757b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/datadog/core/environment/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ module Ext
TAG_SERVICE = 'service'
TAG_VERSION = 'version'

DD_SITE_STAGING = 'datad0g.com'

GEM_DATADOG_VERSION = Datadog::VERSION::STRING
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/core/telemetry/http/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ module Ext
CONTENT_TYPE_APPLICATION_JSON = 'application/json'
API_VERSION = 'v2'

AGENTLESS_HOST_PREFIX = 'instrumentation-telemetry-intake'

AGENT_ENDPOINT = '/telemetry/proxy/api/v2/apmtelemetry'
AGENTLESS_ENDPOINT = 'api/v2/apmtelemetry'
end
end
end
Expand Down
31 changes: 28 additions & 3 deletions lib/datadog/core/telemetry/http/transport.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../../configuration/settings'
require_relative '../../environment/ext'
require_relative '../../transport/ext'
require_relative 'env'
require_relative 'ext'
Expand All @@ -23,17 +24,37 @@ def self.build_agent_transport
)
end

def self.build_agentless_transport(api_key:, dd_site:)
host =
if dd_site == Environment::Ext::DD_SITE_STAGING
# special case for staging - the url is constructed differently
'all-http-intake.logs.datad0g.com'
else
"#{Http::Ext::AGENTLESS_HOST_PREFIX}.#{dd_site}"
end

Transport.new(
host: host,
port: 443,
path: Http::Ext::AGENTLESS_ENDPOINT,
ssl: true,
api_key: api_key
)
end

attr_reader \
:host,
:port,
:ssl,
:path
:path,
:api_key

def initialize(host:, port:, path:, ssl: false)
def initialize(host:, port:, path:, ssl: false, api_key: nil)
@host = host
@port = port
@ssl = ssl
@path = path
@api_key = api_key
end

def request(request_type:, payload:)
Expand All @@ -47,7 +68,7 @@ def request(request_type:, payload:)
private

def headers(request_type:, api_version: Http::Ext::API_VERSION)
{
result = {
Core::Transport::Ext::HTTP::HEADER_DD_INTERNAL_UNTRACED_REQUEST => '1',
Ext::HEADER_CONTENT_TYPE => Http::Ext::CONTENT_TYPE_APPLICATION_JSON,
Ext::HEADER_DD_TELEMETRY_API_VERSION => api_version,
Expand All @@ -58,6 +79,10 @@ def headers(request_type:, api_version: Http::Ext::API_VERSION)
# Enable debug mode for telemetry
# HEADER_TELEMETRY_DEBUG_ENABLED => 'true',
}

result[Ext::HEADER_DD_API_KEY] = api_key unless api_key.nil?

result
end

def adapter
Expand Down
18 changes: 10 additions & 8 deletions sig/datadog/core/environment/ext.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ module Datadog
module Core
module Environment
module Ext
LANG: untyped
LANG: String

LANG_ENGINE: untyped
LANG_ENGINE: String

LANG_INTERPRETER: untyped
LANG_INTERPRETER: String

LANG_PLATFORM: untyped
LANG_PLATFORM: String

LANG_VERSION: untyped
LANG_VERSION: String

RUBY_ENGINE: untyped
RUBY_ENGINE: String

GEM_DATADOG_VERSION: untyped
GEM_DATADOG_VERSION: String

ENGINE_VERSION: untyped
ENGINE_VERSION: String

DD_SITE_STAGING: String
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions sig/datadog/core/telemetry/http/ext.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ module Datadog

API_VERSION: "v2"

AGENTLESS_HOST_PREFIX: "instrumentation-telemetry-intake"

AGENT_ENDPOINT: "/telemetry/proxy/api/v2/apmtelemetry"

AGENTLESS_ENDPOINT: "api/v2/apmtelemetry"
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion sig/datadog/core/telemetry/http/transport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module Datadog

attr_reader path: String

def initialize: (host: String, port: Integer, path: String, ?ssl: bool) -> void
attr_reader api_key: String?

def initialize: (host: String, port: Integer, path: String, ?ssl: bool, ?api_key: String?) -> void

def request: (request_type: String, payload: String) -> Datadog::Core::Telemetry::Http::Adapters::Net::response

Expand Down
50 changes: 50 additions & 0 deletions spec/datadog/core/telemetry/http/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@
it { expect(transport.port).to eq(port) }
it { expect(transport.ssl).to eq(false) }
it { expect(transport.path).to eq(Datadog::Core::Telemetry::Http::Ext::AGENT_ENDPOINT) }
it { expect(transport.api_key).to be_nil }
end

describe '.build_agentless_transport' do
subject(:transport) { described_class.build_agentless_transport(api_key: api_key, dd_site: dd_site) }

let(:api_key) { 'dd_api_key' }
let(:dd_site) { 'datadoghq.com' }

it { expect(transport.host).to eq('instrumentation-telemetry-intake.datadoghq.com') }
it { expect(transport.port).to eq(443) }
it { expect(transport.ssl).to eq(true) }
it { expect(transport.path).to eq(Datadog::Core::Telemetry::Http::Ext::AGENTLESS_ENDPOINT) }
it { expect(transport.api_key).to eq(api_key) }

context 'when dd_site is staging' do
let(:dd_site) { Datadog::Core::Environment::Ext::DD_SITE_STAGING }

it { expect(transport.host).to eq('all-http-intake.logs.datad0g.com') }
end

context 'when dd_site is eu' do
let(:dd_site) { 'datadoghq.eu' }

it { expect(transport.host).to eq('instrumentation-telemetry-intake.datadoghq.eu') }
end
end

describe '#request' do
Expand Down Expand Up @@ -62,5 +88,29 @@
end

it { is_expected.to be(response) }

context 'expect api key in headers when provided' do
let(:transport) { described_class.build_agentless_transport(api_key: api_key, dd_site: dd_site) }

let(:api_key) { 'dd_api_key' }
let(:dd_site) { 'datadoghq.com' }
let(:hostname) { 'instrumentation-telemetry-intake.datadoghq.com' }
let(:ssl) { true }
let(:port) { 443 }
let(:path) { Datadog::Core::Telemetry::Http::Ext::AGENTLESS_ENDPOINT }
let(:headers) do
{
'Content-Type' => 'application/json',
'DD-Telemetry-API-Version' => 'v2',
'DD-Telemetry-Request-Type' => 'app-started',
'DD-Internal-Untraced-Request' => '1',
'DD-Client-Library-Language' => 'ruby',
'DD-Client-Library-Version' => Datadog::Core::Environment::Identity.gem_datadog_version_semver2,
'DD-API-KEY' => api_key
}
end

it { is_expected.to be(response) }
end
end
end
5 changes: 4 additions & 1 deletion static-analysis.datadog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ rulesets:
rules:
percent-w:
ignore:
- '**'
- "**"
symbols-as-keys:
ignore:
- "**"

0 comments on commit 608757b

Please sign in to comment.