|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { collectorTypes } from '@opentelemetry/exporter-collector'; |
| 18 | +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; |
| 19 | + |
| 20 | +import * as assert from 'assert'; |
| 21 | +import { CollectorExporterNodeBase } from '../src/CollectorExporterNodeBase'; |
| 22 | +import { CollectorExporterConfigNode, ServiceClientType } from '../src/types'; |
| 23 | +import { mockedReadableSpan } from './helper'; |
| 24 | + |
| 25 | +class MockCollectorExporter extends CollectorExporterNodeBase< |
| 26 | + ReadableSpan, |
| 27 | + ReadableSpan[] |
| 28 | +> { |
| 29 | + /** |
| 30 | + * Callbacks passed to _send() |
| 31 | + */ |
| 32 | + sendCallbacks: { |
| 33 | + onSuccess: () => void; |
| 34 | + onError: (error: collectorTypes.CollectorExporterError) => void; |
| 35 | + }[] = []; |
| 36 | + |
| 37 | + getDefaultUrl(config: CollectorExporterConfigNode): string { |
| 38 | + return ''; |
| 39 | + } |
| 40 | + |
| 41 | + getDefaultServiceName(config: CollectorExporterConfigNode): string { |
| 42 | + return ''; |
| 43 | + } |
| 44 | + |
| 45 | + convert(spans: ReadableSpan[]): ReadableSpan[] { |
| 46 | + return spans; |
| 47 | + } |
| 48 | + |
| 49 | + getServiceClientType() { |
| 50 | + return ServiceClientType.SPANS; |
| 51 | + } |
| 52 | + |
| 53 | + getServiceProtoPath(): string { |
| 54 | + return 'opentelemetry/proto/collector/trace/v1/trace_service.proto'; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Mocked _send which just saves the callbacks for later |
| 59 | +MockCollectorExporter.prototype['_send'] = function _sendMock( |
| 60 | + self: MockCollectorExporter, |
| 61 | + objects: ReadableSpan[], |
| 62 | + onSuccess: () => void, |
| 63 | + onError: (error: collectorTypes.CollectorExporterError) => void |
| 64 | +): void { |
| 65 | + self.sendCallbacks.push({ onSuccess, onError }); |
| 66 | +}; |
| 67 | + |
| 68 | +describe('CollectorExporterNodeBase', () => { |
| 69 | + let exporter: MockCollectorExporter; |
| 70 | + const concurrencyLimit = 5; |
| 71 | + |
| 72 | + beforeEach(done => { |
| 73 | + exporter = new MockCollectorExporter({ concurrencyLimit }); |
| 74 | + done(); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('export', () => { |
| 78 | + it('should export requests concurrently', async () => { |
| 79 | + const spans = [Object.assign({}, mockedReadableSpan)]; |
| 80 | + const numToExport = concurrencyLimit; |
| 81 | + |
| 82 | + for (let i = 0; i < numToExport; ++i) { |
| 83 | + exporter.export(spans, () => {}); |
| 84 | + } |
| 85 | + |
| 86 | + assert.strictEqual(exporter['_sendingPromises'].length, numToExport); |
| 87 | + const promisesAllDone = Promise.all(exporter['_sendingPromises']); |
| 88 | + // Mock that all requests finish sending |
| 89 | + exporter.sendCallbacks.forEach(({ onSuccess }) => onSuccess()); |
| 90 | + |
| 91 | + // All finished promises should be popped off |
| 92 | + await promisesAllDone; |
| 93 | + assert.strictEqual(exporter['_sendingPromises'].length, 0); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should drop new export requests when already sending at concurrencyLimit', async () => { |
| 97 | + const spans = [Object.assign({}, mockedReadableSpan)]; |
| 98 | + const numToExport = concurrencyLimit + 5; |
| 99 | + |
| 100 | + for (let i = 0; i < numToExport; ++i) { |
| 101 | + exporter.export(spans, () => {}); |
| 102 | + } |
| 103 | + |
| 104 | + assert.strictEqual(exporter['_sendingPromises'].length, concurrencyLimit); |
| 105 | + const promisesAllDone = Promise.all(exporter['_sendingPromises']); |
| 106 | + // Mock that all requests finish sending |
| 107 | + exporter.sendCallbacks.forEach(({ onSuccess }) => onSuccess()); |
| 108 | + |
| 109 | + // All finished promises should be popped off |
| 110 | + await promisesAllDone; |
| 111 | + assert.strictEqual(exporter['_sendingPromises'].length, 0); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should pop export request promises even if they failed', async () => { |
| 115 | + const spans = [Object.assign({}, mockedReadableSpan)]; |
| 116 | + |
| 117 | + exporter.export(spans, () => {}); |
| 118 | + assert.strictEqual(exporter['_sendingPromises'].length, 1); |
| 119 | + const promisesAllDone = Promise.all(exporter['_sendingPromises']); |
| 120 | + // Mock that all requests fail sending |
| 121 | + exporter.sendCallbacks.forEach(({ onError }) => |
| 122 | + onError(new Error('Failed to send!!')) |
| 123 | + ); |
| 124 | + |
| 125 | + // All finished promises should be popped off |
| 126 | + await promisesAllDone; |
| 127 | + assert.strictEqual(exporter['_sendingPromises'].length, 0); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should pop export request promises even if success callback throws error', async () => { |
| 131 | + const spans = [Object.assign({}, mockedReadableSpan)]; |
| 132 | + |
| 133 | + exporter['_sendPromise']( |
| 134 | + spans, |
| 135 | + () => { |
| 136 | + throw new Error('Oops'); |
| 137 | + }, |
| 138 | + () => {} |
| 139 | + ); |
| 140 | + |
| 141 | + assert.strictEqual(exporter['_sendingPromises'].length, 1); |
| 142 | + const promisesAllDone = Promise.all(exporter['_sendingPromises']) |
| 143 | + // catch expected unhandled exception |
| 144 | + .catch(() => {}); |
| 145 | + |
| 146 | + // Mock that the request finishes sending |
| 147 | + exporter.sendCallbacks.forEach(({ onSuccess }) => { |
| 148 | + onSuccess(); |
| 149 | + }); |
| 150 | + |
| 151 | + // All finished promises should be popped off |
| 152 | + await promisesAllDone; |
| 153 | + assert.strictEqual(exporter['_sendingPromises'].length, 0); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments