diff --git a/packages/opentelemetry-exporter-collector-proto/src/CollectorExporterNodeBase.ts b/packages/opentelemetry-exporter-collector-proto/src/CollectorExporterNodeBase.ts index ea6701ae78..92bc64b67c 100644 --- a/packages/opentelemetry-exporter-collector-proto/src/CollectorExporterNodeBase.ts +++ b/packages/opentelemetry-exporter-collector-proto/src/CollectorExporterNodeBase.ts @@ -17,6 +17,7 @@ import { CollectorExporterNodeBase as CollectorExporterBaseMain, collectorTypes, + CollectorExporterNodeConfigBase, } from '@opentelemetry/exporter-collector'; import { ServiceClientType } from './types'; @@ -55,7 +56,7 @@ export abstract class CollectorExporterNodeBase< this._sendingPromises.push(promise); } - onInit(config: collectorTypes.CollectorExporterConfigBase): void { + onInit(config: CollectorExporterNodeConfigBase): void { this._isShutdown = false; // defer to next tick and lazy load to avoid loading protobufjs too early // and making this impossible to be instrumented diff --git a/packages/opentelemetry-exporter-collector-proto/src/CollectorMetricExporter.ts b/packages/opentelemetry-exporter-collector-proto/src/CollectorMetricExporter.ts index d10611b3a9..43618f5c06 100644 --- a/packages/opentelemetry-exporter-collector-proto/src/CollectorMetricExporter.ts +++ b/packages/opentelemetry-exporter-collector-proto/src/CollectorMetricExporter.ts @@ -17,6 +17,7 @@ import { collectorTypes, toCollectorExportMetricServiceRequest, + CollectorExporterNodeConfigBase, } from '@opentelemetry/exporter-collector'; import { MetricRecord, MetricExporter } from '@opentelemetry/metrics'; import { ServiceClientType } from './types'; @@ -47,16 +48,14 @@ export class CollectorMetricExporter ); } - getDefaultUrl(config: collectorTypes.CollectorExporterConfigBase): string { + getDefaultUrl(config: CollectorExporterNodeConfigBase): string { if (!config.url) { return DEFAULT_COLLECTOR_URL; } return config.url; } - getDefaultServiceName( - config: collectorTypes.CollectorExporterConfigBase - ): string { + getDefaultServiceName(config: CollectorExporterNodeConfigBase): string { return config.serviceName || DEFAULT_SERVICE_NAME; } diff --git a/packages/opentelemetry-exporter-collector-proto/src/CollectorTraceExporter.ts b/packages/opentelemetry-exporter-collector-proto/src/CollectorTraceExporter.ts index 79ca82049c..49d29c5d60 100644 --- a/packages/opentelemetry-exporter-collector-proto/src/CollectorTraceExporter.ts +++ b/packages/opentelemetry-exporter-collector-proto/src/CollectorTraceExporter.ts @@ -19,6 +19,7 @@ import { CollectorExporterNodeBase } from './CollectorExporterNodeBase'; import { collectorTypes, toCollectorExportTraceServiceRequest, + CollectorExporterNodeConfigBase, } from '@opentelemetry/exporter-collector'; import { ServiceClientType } from './types'; @@ -40,16 +41,14 @@ export class CollectorTraceExporter return toCollectorExportTraceServiceRequest(spans, this); } - getDefaultUrl(config: collectorTypes.CollectorExporterConfigBase): string { + getDefaultUrl(config: CollectorExporterNodeConfigBase): string { if (!config.url) { return DEFAULT_COLLECTOR_URL; } return config.url; } - getDefaultServiceName( - config: collectorTypes.CollectorExporterConfigBase - ): string { + getDefaultServiceName(config: CollectorExporterNodeConfigBase): string { return config.serviceName || DEFAULT_SERVICE_NAME; } diff --git a/packages/opentelemetry-exporter-collector-proto/src/util.ts b/packages/opentelemetry-exporter-collector-proto/src/util.ts index 05e4ec00fc..f944d91512 100644 --- a/packages/opentelemetry-exporter-collector-proto/src/util.ts +++ b/packages/opentelemetry-exporter-collector-proto/src/util.ts @@ -17,6 +17,7 @@ import { collectorTypes, sendWithHttp, + CollectorExporterNodeConfigBase, } from '@opentelemetry/exporter-collector'; import * as path from 'path'; @@ -33,7 +34,7 @@ export function getExportRequestProto(): Type | undefined { export function onInit( collector: CollectorExporterNodeBase, - _config: collectorTypes.CollectorExporterConfigBase + _config: CollectorExporterNodeConfigBase ): void { const dir = path.resolve(__dirname, '..', 'protos'); const root = new protobufjs.Root(); diff --git a/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts index 0a943a737f..572947bd12 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts @@ -14,9 +14,12 @@ * limitations under the License. */ +import { + collectorTypes, + CollectorExporterNodeConfigBase, +} from '@opentelemetry/exporter-collector'; import * as api from '@opentelemetry/api'; import * as metrics from '@opentelemetry/metrics'; -import { collectorTypes } from '@opentelemetry/exporter-collector'; import * as core from '@opentelemetry/core'; import * as http from 'http'; import * as assert from 'assert'; @@ -48,7 +51,7 @@ const waitTimeMS = 20; describe('CollectorMetricExporter - node with proto over http', () => { let collectorExporter: CollectorMetricExporter; - let collectorExporterConfig: collectorTypes.CollectorExporterConfigBase; + let collectorExporterConfig: CollectorExporterNodeConfigBase; let spyRequest: sinon.SinonSpy; let spyWrite: sinon.SinonSpy; let metrics: metrics.MetricRecord[]; @@ -65,6 +68,8 @@ describe('CollectorMetricExporter - node with proto over http', () => { serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', + keepAlive: true, + httpAgentOptions: { keepAliveMsecs: 2000 }, }; collectorExporter = new CollectorMetricExporter(collectorExporterConfig); // Overwrites the start time to make tests consistent @@ -120,6 +125,19 @@ describe('CollectorMetricExporter - node with proto over http', () => { }, waitTimeMS); }); + it('should have keep alive and keepAliveMsecs option set', done => { + collectorExporter.export(metrics, () => {}); + + setTimeout(() => { + const args = spyRequest.args[0]; + const options = args[0]; + const agent = options.agent; + assert.strictEqual(agent.keepAlive, true); + assert.strictEqual(agent.options.keepAliveMsecs, 2000); + done(); + }); + }); + it('should successfully send metrics', done => { collectorExporter.export(metrics, () => {}); diff --git a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts index 6f534d9558..6120493c93 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts @@ -14,7 +14,10 @@ * limitations under the License. */ -import { collectorTypes } from '@opentelemetry/exporter-collector'; +import { + collectorTypes, + CollectorExporterNodeConfigBase, +} from '@opentelemetry/exporter-collector'; import * as core from '@opentelemetry/core'; import { ReadableSpan } from '@opentelemetry/tracing'; @@ -43,7 +46,7 @@ const waitTimeMS = 20; describe('CollectorTraceExporter - node with proto over http', () => { let collectorExporter: CollectorTraceExporter; - let collectorExporterConfig: collectorTypes.CollectorExporterConfigBase; + let collectorExporterConfig: CollectorExporterNodeConfigBase; let spyRequest: sinon.SinonSpy; let spyWrite: sinon.SinonSpy; let spans: ReadableSpan[]; @@ -60,6 +63,8 @@ describe('CollectorTraceExporter - node with proto over http', () => { serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', + keepAlive: true, + httpAgentOptions: { keepAliveMsecs: 2000 }, }; collectorExporter = new CollectorTraceExporter(collectorExporterConfig); spans = []; @@ -95,6 +100,19 @@ describe('CollectorTraceExporter - node with proto over http', () => { }, waitTimeMS); }); + it('should have keep alive and keepAliveMsecs option set', done => { + collectorExporter.export(spans, () => {}); + + setTimeout(() => { + const args = spyRequest.args[0]; + const options = args[0]; + const agent = options.agent; + assert.strictEqual(agent.keepAlive, true); + assert.strictEqual(agent.options.keepAliveMsecs, 2000); + done(); + }); + }); + it('should successfully send the spans', done => { collectorExporter.export(spans, () => {}); diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/CollectorExporterNodeBase.ts b/packages/opentelemetry-exporter-collector/src/platform/node/CollectorExporterNodeBase.ts index c086210850..2f3d2977de 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/CollectorExporterNodeBase.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/CollectorExporterNodeBase.ts @@ -14,8 +14,11 @@ * limitations under the License. */ +import type * as http from 'http'; +import type * as https from 'https'; + import { CollectorExporterBase } from '../../CollectorExporterBase'; -import { CollectorExporterConfigBase } from '../../types'; +import { CollectorExporterNodeConfigBase } from './types'; import * as collectorTypes from '../../types'; import { parseHeaders } from '../../util'; import { sendWithHttp } from './util'; @@ -27,22 +30,35 @@ export abstract class CollectorExporterNodeBase< ExportItem, ServiceRequest > extends CollectorExporterBase< - CollectorExporterConfigBase, + CollectorExporterNodeConfigBase, ExportItem, ServiceRequest > { DEFAULT_HEADERS: Record = {}; headers: Record; - constructor(config: CollectorExporterConfigBase = {}) { + keepAlive: boolean = true; + httpAgentOptions: http.AgentOptions | https.AgentOptions = {}; + constructor(config: CollectorExporterNodeConfigBase = {}) { super(config); if ((config as any).metadata) { this.logger.warn('Metadata cannot be set when using http'); } this.headers = parseHeaders(config.headers, this.logger) || this.DEFAULT_HEADERS; + if (typeof config.keepAlive === 'boolean') { + this.keepAlive = config.keepAlive; + } + if (config.httpAgentOptions) { + if (!this.keepAlive) { + this.logger.warn( + 'httpAgentOptions is used only when keepAlive is true' + ); + } + this.httpAgentOptions = Object.assign({}, config.httpAgentOptions); + } } - onInit(_config: CollectorExporterConfigBase): void { + onInit(_config: CollectorExporterNodeConfigBase): void { this._isShutdown = false; } diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/CollectorMetricExporter.ts b/packages/opentelemetry-exporter-collector/src/platform/node/CollectorMetricExporter.ts index f86c397fbe..30c70e9e07 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/CollectorMetricExporter.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/CollectorMetricExporter.ts @@ -15,8 +15,8 @@ */ import { MetricRecord, MetricExporter } from '@opentelemetry/metrics'; -import { CollectorExporterConfigBase } from '../../types'; import * as collectorTypes from '../../types'; +import { CollectorExporterNodeConfigBase } from './types'; import { CollectorExporterNodeBase } from './CollectorExporterNodeBase'; import { toCollectorExportMetricServiceRequest } from '../../transformMetrics'; @@ -45,14 +45,14 @@ export class CollectorMetricExporter ); } - getDefaultUrl(config: CollectorExporterConfigBase): string { + getDefaultUrl(config: CollectorExporterNodeConfigBase): string { if (!config.url) { return DEFAULT_COLLECTOR_URL; } return config.url; } - getDefaultServiceName(config: CollectorExporterConfigBase): string { + getDefaultServiceName(config: CollectorExporterNodeConfigBase): string { return config.serviceName || DEFAULT_SERVICE_NAME; } } diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/CollectorTraceExporter.ts b/packages/opentelemetry-exporter-collector/src/platform/node/CollectorTraceExporter.ts index 0d5eea66dd..4d6e4c03ff 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/CollectorTraceExporter.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/CollectorTraceExporter.ts @@ -15,8 +15,8 @@ */ import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing'; -import { CollectorExporterConfigBase } from '../../types'; import { CollectorExporterNodeBase } from './CollectorExporterNodeBase'; +import { CollectorExporterNodeConfigBase } from './types'; import * as collectorTypes from '../../types'; import { toCollectorExportTraceServiceRequest } from '../../transform'; @@ -38,14 +38,14 @@ export class CollectorTraceExporter return toCollectorExportTraceServiceRequest(spans, this, true); } - getDefaultUrl(config: CollectorExporterConfigBase): string { + getDefaultUrl(config: CollectorExporterNodeConfigBase): string { if (!config.url) { return DEFAULT_COLLECTOR_URL; } return config.url; } - getDefaultServiceName(config: CollectorExporterConfigBase): string { + getDefaultServiceName(config: CollectorExporterNodeConfigBase): string { return config.serviceName || DEFAULT_SERVICE_NAME; } } diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/index.ts b/packages/opentelemetry-exporter-collector/src/platform/node/index.ts index 6b647adc24..d2f9fa5223 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/index.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/index.ts @@ -18,3 +18,4 @@ export * from './CollectorTraceExporter'; export * from './CollectorMetricExporter'; export * from './CollectorExporterNodeBase'; export * from './util'; +export * from './types'; diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/types.ts b/packages/opentelemetry-exporter-collector/src/platform/node/types.ts new file mode 100644 index 0000000000..cee0f477a5 --- /dev/null +++ b/packages/opentelemetry-exporter-collector/src/platform/node/types.ts @@ -0,0 +1,28 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import type * as http from 'http'; +import type * as https from 'https'; + +import { CollectorExporterConfigBase } from '../../types'; + +/** + * Collector Exporter node base config + */ +export interface CollectorExporterNodeConfigBase + extends CollectorExporterConfigBase { + keepAlive?: boolean; + httpAgentOptions?: http.AgentOptions | https.AgentOptions; +} diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/util.ts b/packages/opentelemetry-exporter-collector/src/platform/node/util.ts index 29d46e2943..edab1f2227 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/util.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/util.ts @@ -36,7 +36,7 @@ export function sendWithHttp( ): void { const parsedUrl = new url.URL(collector.url); - const options = { + const options: http.RequestOptions | https.RequestOptions = { hostname: parsedUrl.hostname, port: parsedUrl.port, path: parsedUrl.pathname, @@ -49,6 +49,14 @@ export function sendWithHttp( }; const request = parsedUrl.protocol === 'http:' ? http.request : https.request; + const Agent = parsedUrl.protocol === 'http:' ? http.Agent : https.Agent; + if (collector.keepAlive) { + options.agent = new Agent({ + ...collector.httpAgentOptions, + keepAlive: true, + }); + } + const req = request(options, (res: http.IncomingMessage) => { let data = ''; res.on('data', chunk => (data += chunk)); diff --git a/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts index ecf87d3983..1d65447481 100644 --- a/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts @@ -19,8 +19,10 @@ import * as core from '@opentelemetry/core'; import * as http from 'http'; import * as assert from 'assert'; import * as sinon from 'sinon'; -import { CollectorMetricExporter } from '../../src/platform/node'; -import { CollectorExporterConfigBase } from '../../src/types'; +import { + CollectorMetricExporter, + CollectorExporterNodeConfigBase, +} from '../../src/platform/node'; import * as collectorTypes from '../../src/types'; import { MockedResponse } from './nodeHelpers'; import { @@ -50,7 +52,7 @@ const address = 'localhost:1501'; describe('CollectorMetricExporter - node with json over http', () => { let collectorExporter: CollectorMetricExporter; - let collectorExporterConfig: CollectorExporterConfigBase; + let collectorExporterConfig: CollectorExporterNodeConfigBase; let spyRequest: sinon.SinonSpy; let spyWrite: sinon.SinonSpy; let metrics: MetricRecord[]; @@ -83,6 +85,8 @@ describe('CollectorMetricExporter - node with json over http', () => { serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', + keepAlive: true, + httpAgentOptions: { keepAliveMsecs: 2000 }, }; collectorExporter = new CollectorMetricExporter(collectorExporterConfig); // Overwrites the start time to make tests consistent @@ -138,6 +142,19 @@ describe('CollectorMetricExporter - node with json over http', () => { }); }); + it('should have keep alive and keepAliveMsecs option set', done => { + collectorExporter.export(metrics, () => {}); + + setTimeout(() => { + const args = spyRequest.args[0]; + const options = args[0]; + const agent = options.agent; + assert.strictEqual(agent.keepAlive, true); + assert.strictEqual(agent.options.keepAliveMsecs, 2000); + done(); + }); + }); + it('should successfully send metrics', done => { collectorExporter.export(metrics, () => {}); diff --git a/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts index 86b65faa12..e9c2de8fb5 100644 --- a/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts @@ -19,8 +19,10 @@ import { ReadableSpan } from '@opentelemetry/tracing'; import * as http from 'http'; import * as assert from 'assert'; import * as sinon from 'sinon'; -import { CollectorTraceExporter } from '../../src/platform/node'; -import { CollectorExporterConfigBase } from '../../src/types'; +import { + CollectorTraceExporter, + CollectorExporterNodeConfigBase, +} from '../../src/platform/node'; import * as collectorTypes from '../../src/types'; import { MockedResponse } from './nodeHelpers'; @@ -40,7 +42,7 @@ const address = 'localhost:1501'; describe('CollectorTraceExporter - node with json over http', () => { let collectorExporter: CollectorTraceExporter; - let collectorExporterConfig: CollectorExporterConfigBase; + let collectorExporterConfig: CollectorExporterNodeConfigBase; let spyRequest: sinon.SinonSpy; let spyWrite: sinon.SinonSpy; let spans: ReadableSpan[]; @@ -73,6 +75,8 @@ describe('CollectorTraceExporter - node with json over http', () => { serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', + keepAlive: true, + httpAgentOptions: { keepAliveMsecs: 2000 }, }; collectorExporter = new CollectorTraceExporter(collectorExporterConfig); spans = []; @@ -108,6 +112,19 @@ describe('CollectorTraceExporter - node with json over http', () => { }); }); + it('should have keep alive and keepAliveMsecs option set', done => { + collectorExporter.export(spans, () => {}); + + setTimeout(() => { + const args = spyRequest.args[0]; + const options = args[0]; + const agent = options.agent; + assert.strictEqual(agent.keepAlive, true); + assert.strictEqual(agent.options.keepAliveMsecs, 2000); + done(); + }); + }); + it('should successfully send the spans', done => { collectorExporter.export(spans, () => {}); diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index 73d37ea79f..1bcd42616f 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -55,7 +55,7 @@ const applyCustomAttributesOnSpanErrorMessage = 'bad applyCustomAttributesOnSpan function'; let server: http.Server; -const serverPort = 22345; +const serverPort = 22346; const protocol = 'http'; const hostname = 'localhost'; const pathname = '/test'; diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts index 44f6468ec5..f98c24baf6 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts @@ -102,13 +102,7 @@ describe('Packages', () => { } const urlparsed = url.parse( - name === 'got' && process.versions.node.startsWith('12') - ? // there is an issue with got 9.6 version and node 12 when redirecting so url above will not work - // https://github.com/nock/nock/pull/1551 - // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 - // TODO: check if this is still the case when new version - `${protocol}://info.cern.ch/` - : `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` + `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` ); const result = await httpPackage.get(urlparsed.href!); if (!resHeaders) { diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts index 4a775482a5..e1d3cb72a4 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts @@ -93,13 +93,7 @@ describe('Packages', () => { } const urlparsed = url.parse( - name === 'got' && process.versions.node.startsWith('12') - ? // there is an issue with got 9.6 version and node 12 when redirecting so url above will not work - // https://github.com/nock/nock/pull/1551 - // https://github.com/sindresorhus/got/commit/bf1aa5492ae2bc78cbbec6b7d764906fb156e6c2#diff-707a4781d57c42085155dcb27edb9ccbR258 - // TODO: check if this is still the case when new version - `${protocol}://info.cern.ch/` - : `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` + `${protocol}://www.google.com/search?q=axios&oq=axios&aqs=chrome.0.69i59l2j0l3j69i60.811j0j7&sourceid=chrome&ie=UTF-8` ); const result = await httpPackage.get(urlparsed.href!); if (!resHeaders) {