Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const http = require('http')
const { URL } = require('url')
const http = require('node:http')
const https = require('node:https')
const { URL } = require('node:url')
const log = require('../../log')
const telemetryMetrics = require('../../telemetry/metrics')

Expand All @@ -16,6 +17,8 @@ const tracerMetrics = telemetryMetrics.manager.namespace('tracers')
* @class OtlpHttpExporterBase
*/
class OtlpHttpExporterBase {
#transport = https

/**
* Creates a new OtlpHttpExporterBase instance.
*
Expand Down Expand Up @@ -45,7 +48,7 @@ class OtlpHttpExporterBase {
this.setUrl(url)

this.telemetryTags = [
'protocol:http',
`protocol:${this.#transport === https ? 'https' : 'http'}`,
`encoding:${isJson ? 'json' : 'protobuf'}`,
]
}
Expand Down Expand Up @@ -81,7 +84,7 @@ class OtlpHttpExporterBase {
},
}

const req = http.request(options, (res) => {
const req = this.#transport.request(options, (res) => {
let data = ''

res.on('data', (chunk) => {
Expand Down Expand Up @@ -124,6 +127,10 @@ class OtlpHttpExporterBase {
this.options.hostname = parsedUrl.hostname
this.options.port = parsedUrl.port
this.options.path = parsedUrl.pathname + parsedUrl.search
this.#transport = parsedUrl.protocol === 'http:' ? http : https
Comment thread
ida613 marked this conversation as resolved.
if (this.telemetryTags !== undefined) {
this.telemetryTags[0] = `protocol:${this.#transport === https ? 'https' : 'http'}`
}
}

/**
Expand Down
49 changes: 48 additions & 1 deletion packages/dd-trace/test/opentelemetry/traces.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const assert = require('assert')
const http = require('http')
const http = require('node:http')
const https = require('node:https')

const { describe, it, beforeEach, afterEach } = require('mocha')
const sinon = require('sinon')
Expand Down Expand Up @@ -844,6 +845,18 @@ describe('OpenTelemetry Traces', () => {
})

describe('Telemetry Metrics', () => {
it('sets protocol:http tag for http:// endpoint', () => {
const exporter = new OtlpHttpTraceExporter('http://collector.example/v1/traces', {}, 1000, {})

assert.ok(exporter.telemetryTags.includes('protocol:http'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: calling setUrl here with https afterwards would be great. Everything would be tested in that case :)

Because that was the last functionality that was missing with my earlier comment

})

it('sets protocol:https tag for https:// endpoint', () => {
const exporter = new OtlpHttpTraceExporter('https://collector.example/v1/traces', {}, 1000, {})

assert.ok(exporter.telemetryTags.includes('protocol:https'))
})

it('tracks telemetry metrics for exported traces', () => {
const telemetryMetrics = {
manager: { namespace: sinon.stub().returns({ count: sinon.stub().returns({ inc: sinon.spy() }) }) },
Expand Down Expand Up @@ -887,5 +900,39 @@ describe('OpenTelemetry Traces', () => {

assert.strictEqual(exporter.options.path, '/v1/traces?token=abc')
})

it('selects http transport for http:// URLs', () => {
const exporter = new OtlpHttpTraceExporter('http://collector.example/v1/traces', {}, 1000, {})
const mockReq = { write: () => {}, end: () => {}, on: () => mockReq, once: () => mockReq }
const httpStub = sinon.stub(http, 'request').returns(mockReq)
sinon.stub(https, 'request').returns(mockReq)

exporter.sendPayload(Buffer.from('{}'), () => {})

assert.ok(httpStub.calledOnce, 'http.request should have been called')
})

it('selects https transport for https:// URLs', () => {
const exporter = new OtlpHttpTraceExporter('https://collector.example/v1/traces', {}, 1000, {})
const mockReq = { write: () => {}, end: () => {}, on: () => mockReq, once: () => mockReq }
sinon.stub(http, 'request').returns(mockReq)
const httpsStub = sinon.stub(https, 'request').returns(mockReq)

exporter.sendPayload(Buffer.from('{}'), () => {})

assert.ok(httpsStub.calledOnce, 'https.request should have been called')
})

it('switches transport when setUrl is called with a different scheme', () => {
const exporter = new OtlpHttpTraceExporter('http://collector.example/v1/traces', {}, 1000, {})
const mockReq = { write: () => {}, end: () => {}, on: () => mockReq, once: () => mockReq }
sinon.stub(http, 'request').returns(mockReq)
const httpsStub = sinon.stub(https, 'request').returns(mockReq)

exporter.setUrl('https://secure-collector.example/v1/traces')
exporter.sendPayload(Buffer.from('{}'), () => {})

assert.ok(httpsStub.calledOnce, 'https.request should have been called after switching to https')
})
})
})
Loading