|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const assert = require('assert') |
4 | | -const http = require('http') |
| 4 | +const http = require('node:http') |
| 5 | +const https = require('node:https') |
5 | 6 |
|
6 | 7 | const { describe, it, beforeEach, afterEach } = require('mocha') |
7 | 8 | const sinon = require('sinon') |
@@ -844,6 +845,18 @@ describe('OpenTelemetry Traces', () => { |
844 | 845 | }) |
845 | 846 |
|
846 | 847 | describe('Telemetry Metrics', () => { |
| 848 | + it('sets protocol:http tag for http:// endpoint', () => { |
| 849 | + const exporter = new OtlpHttpTraceExporter('http://collector.example/v1/traces', {}, 1000, {}) |
| 850 | + |
| 851 | + assert.ok(exporter.telemetryTags.includes('protocol:http')) |
| 852 | + }) |
| 853 | + |
| 854 | + it('sets protocol:https tag for https:// endpoint', () => { |
| 855 | + const exporter = new OtlpHttpTraceExporter('https://collector.example/v1/traces', {}, 1000, {}) |
| 856 | + |
| 857 | + assert.ok(exporter.telemetryTags.includes('protocol:https')) |
| 858 | + }) |
| 859 | + |
847 | 860 | it('tracks telemetry metrics for exported traces', () => { |
848 | 861 | const telemetryMetrics = { |
849 | 862 | manager: { namespace: sinon.stub().returns({ count: sinon.stub().returns({ inc: sinon.spy() }) }) }, |
@@ -887,5 +900,39 @@ describe('OpenTelemetry Traces', () => { |
887 | 900 |
|
888 | 901 | assert.strictEqual(exporter.options.path, '/v1/traces?token=abc') |
889 | 902 | }) |
| 903 | + |
| 904 | + it('selects http transport for http:// URLs', () => { |
| 905 | + const exporter = new OtlpHttpTraceExporter('http://collector.example/v1/traces', {}, 1000, {}) |
| 906 | + const mockReq = { write: () => {}, end: () => {}, on: () => mockReq, once: () => mockReq } |
| 907 | + const httpStub = sinon.stub(http, 'request').returns(mockReq) |
| 908 | + sinon.stub(https, 'request').returns(mockReq) |
| 909 | + |
| 910 | + exporter.sendPayload(Buffer.from('{}'), () => {}) |
| 911 | + |
| 912 | + assert.ok(httpStub.calledOnce, 'http.request should have been called') |
| 913 | + }) |
| 914 | + |
| 915 | + it('selects https transport for https:// URLs', () => { |
| 916 | + const exporter = new OtlpHttpTraceExporter('https://collector.example/v1/traces', {}, 1000, {}) |
| 917 | + const mockReq = { write: () => {}, end: () => {}, on: () => mockReq, once: () => mockReq } |
| 918 | + sinon.stub(http, 'request').returns(mockReq) |
| 919 | + const httpsStub = sinon.stub(https, 'request').returns(mockReq) |
| 920 | + |
| 921 | + exporter.sendPayload(Buffer.from('{}'), () => {}) |
| 922 | + |
| 923 | + assert.ok(httpsStub.calledOnce, 'https.request should have been called') |
| 924 | + }) |
| 925 | + |
| 926 | + it('switches transport when setUrl is called with a different scheme', () => { |
| 927 | + const exporter = new OtlpHttpTraceExporter('http://collector.example/v1/traces', {}, 1000, {}) |
| 928 | + const mockReq = { write: () => {}, end: () => {}, on: () => mockReq, once: () => mockReq } |
| 929 | + sinon.stub(http, 'request').returns(mockReq) |
| 930 | + const httpsStub = sinon.stub(https, 'request').returns(mockReq) |
| 931 | + |
| 932 | + exporter.setUrl('https://secure-collector.example/v1/traces') |
| 933 | + exporter.sendPayload(Buffer.from('{}'), () => {}) |
| 934 | + |
| 935 | + assert.ok(httpsStub.calledOnce, 'https.request should have been called after switching to https') |
| 936 | + }) |
890 | 937 | }) |
891 | 938 | }) |
0 commit comments