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

Add protocol as a configuration option for the dd-trace-agent URL #416

Merged
merged 6 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ Options can be configured as a parameter to the [init()](https://datadog.github.
| enabled | DD_TRACE_ENABLED | true | Whether to enable the tracer. |
| debug | DD_TRACE_DEBUG | false | Enable debug logging in the tracer. |
| service | DD_SERVICE_NAME | | The service name to be used for this program. |
| protocol | DD_TRACE_AGENT_PROTOCOL | http | The protocol of the trace agent that the tracer will submit to. |
| hostname | DD_TRACE_AGENT_HOSTNAME | localhost | The address of the trace agent that the tracer will submit to. |
| port | DD_TRACE_AGENT_PORT | 8126 | The port of the trace agent that the tracer will submit to. |
| env | DD_ENV | | Set an application’s environment e.g. `prod`, `pre-prod`, `stage`. |
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Config {
const enabled = coalesce(options.enabled, platform.env('DD_TRACE_ENABLED'), true)
const debug = coalesce(options.debug, platform.env('DD_TRACE_DEBUG'), false)
const env = coalesce(options.env, platform.env('DD_ENV'))
const protocol = 'http'
const protocol = coalesce(options.protocol, platform.env('DD_TRACE_AGENT_PROTOCOL'), 'http')
const hostname = coalesce(
options.hostname,
platform.env('DD_AGENT_HOST'),
Expand Down
4 changes: 3 additions & 1 deletion src/platform/node/request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const http = require('http')
const https = require('https')

function request (options, callback) {
options = Object.assign({
Expand All @@ -14,7 +15,8 @@ function request (options, callback) {
options.headers['Content-Length'] = byteLength(data)

return new Promise((resolve, reject) => {
const req = http.request(options, res => {
const client = options.protocol === 'https:' ? https : http
const req = client.request(options, res => {
let data = ''

res.on('data', chunk => { data += chunk })
Expand Down
1 change: 1 addition & 0 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Tracer extends BaseTracer {
* @param {boolean} [options.enabled=true] Whether to enable the tracer.
* @param {boolean} [options.debug=false] Enable debug logging in the tracer.
* @param {string} [options.service] The service name to be used for this program.
* @param {string} [options.protocol=http] The protocol of the trace agent that the tracer will submit to.
* @param {string} [options.hostname=localhost] The address of the trace agent that the tracer will submit to.
* @param {number|string} [options.port=8126] The port of the trace agent that the tracer will submit to.
* @param {number} [options.sampleRate=1] Percentage of spans to sample as a float between 0 and 1.
Expand Down
7 changes: 7 additions & 0 deletions test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Config', () => {
})

it('should initialize from environment variables', () => {
platform.env.withArgs('DD_TRACE_AGENT_PROTOCOL').returns('https')
platform.env.withArgs('DD_TRACE_AGENT_HOSTNAME').returns('agent')
platform.env.withArgs('DD_TRACE_AGENT_PORT').returns('6218')
platform.env.withArgs('DD_TRACE_ENABLED').returns('false')
Expand All @@ -49,6 +50,7 @@ describe('Config', () => {

expect(config).to.have.property('enabled', false)
expect(config).to.have.property('debug', true)
expect(config).to.have.nested.property('url.protocol', 'https:')
expect(config).to.have.nested.property('url.hostname', 'agent')
expect(config).to.have.nested.property('url.port', '6218')
expect(config).to.have.property('service', 'service')
Expand All @@ -61,6 +63,7 @@ describe('Config', () => {
const config = new Config({
enabled: false,
debug: true,
protocol: 'https',
hostname: 'agent',
port: 6218,
service: 'service',
Expand All @@ -74,6 +77,7 @@ describe('Config', () => {

expect(config).to.have.property('enabled', false)
expect(config).to.have.property('debug', true)
expect(config).to.have.nested.property('url.protocol', 'https:')
expect(config).to.have.nested.property('url.hostname', 'agent')
expect(config).to.have.nested.property('url.port', '6218')
expect(config).to.have.property('service', 'service')
Expand All @@ -95,6 +99,7 @@ describe('Config', () => {
})

it('should give priority to the options', () => {
platform.env.withArgs('DD_TRACE_AGENT_PROTOCOL').returns('fakeprotocol')
platform.env.withArgs('DD_TRACE_AGENT_HOSTNAME').returns('agent')
platform.env.withArgs('DD_TRACE_AGENT_PORT').returns('6218')
platform.env.withArgs('DD_TRACE_ENABLED').returns('false')
Expand All @@ -105,6 +110,7 @@ describe('Config', () => {
const config = new Config({
enabled: true,
debug: false,
protocol: 'https',
hostname: 'server',
port: 7777,
service: 'test',
Expand All @@ -113,6 +119,7 @@ describe('Config', () => {

expect(config).to.have.property('enabled', true)
expect(config).to.have.property('debug', false)
expect(config).to.have.nested.property('url.protocol', 'https:')
expect(config).to.have.nested.property('url.hostname', 'server')
expect(config).to.have.nested.property('url.port', '7777')
expect(config).to.have.property('service', 'test')
Expand Down