Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve user-agent header to include telemetry information #235

Merged
merged 3 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'cgi'
require 'net/https'
require 'rbconfig'
require 'socket'
require 'uri'
require 'English'
Expand All @@ -13,7 +14,16 @@
require 'set'
require 'open3'

require 'dogapi/version'

module Dogapi
USER_AGENT = format(
'dogapi-rb/%<version>s (ruby %<ruver>s; os %<os>s; arch %<arch>s)',
version: VERSION,
ruver: RUBY_VERSION,
os: RbConfig::CONFIG['host_os'].downcase,
arch: RbConfig::CONFIG['host_cpu']
)

# Metadata class to hold the scope of an API call
class Scope
Expand Down Expand Up @@ -138,11 +148,11 @@ def request(method, url, extra_params, body, send_json, with_app_key=true)
def prepare_request(method, url, params, body, send_json, with_app_key)
url_with_params = url + params
req = method.new(url_with_params)
req['User-Agent'] = USER_AGENT
unless should_set_api_and_app_keys_in_params?(url)
req['DD-API-KEY'] = @api_key
req['DD-APPLICATION-KEY'] = @application_key if with_app_key
end

if send_json
req.content_type = 'application/json'
req.body = MultiJson.dump(body)
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@
expect(req['DD-APPLICATION-KEY']).to eq service.application_key
end
end

it 'properly sets User-Agent header' do
service = Dogapi::APIService.new('api_key', 'app_key', true, nil, 'https://app.example.com')
params = service.prepare_params(nil, '/api/v1/validate', true)
req = service.prepare_request(Net::HTTP::Get, '/api/v1/validate', params, nil, false, true)

expect(req.key?('User-Agent')).to be true
expect(req['User-Agent']).to match(%r{dogapi-rb\/[^\s]+ \(ruby [^\s]+; os [^\s]+; arch [^\s]+\)})
end
end
end

Expand Down