|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { diag, SpanContext, SpanKind } from '@opentelemetry/api'; |
| 4 | +import { ExportResultCode } from '@opentelemetry/core'; |
| 5 | +import { Resource } from '@opentelemetry/resources'; |
| 6 | +import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer'; |
| 7 | +import { OTLPUdpSpanExporter, UdpExporter } from '../src/otlp-udp-exporter'; |
| 8 | +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; |
| 9 | +import * as sinon from 'sinon'; |
| 10 | +import expect from 'expect'; |
| 11 | +import { Socket } from 'dgram'; |
| 12 | + |
| 13 | +describe('UdpExporterTest', () => { |
| 14 | + const endpoint = '127.0.0.1:3000'; |
| 15 | + const host = '127.0.0.1'; |
| 16 | + const port = 3000; |
| 17 | + let udpExporter: UdpExporter; |
| 18 | + let socketSend: sinon.SinonStub<any[], any>; |
| 19 | + let socketClose: sinon.SinonStub<[callback?: (() => void) | undefined], Socket>; |
| 20 | + let diagErrorSpy: sinon.SinonSpy<[message: string, ...args: unknown[]], void>; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + udpExporter = new UdpExporter(endpoint); |
| 24 | + |
| 25 | + // Stub the _socket methods |
| 26 | + socketSend = sinon.stub(udpExporter['_socket'], 'send'); |
| 27 | + socketClose = sinon.stub(udpExporter['_socket'], 'close'); |
| 28 | + |
| 29 | + // Spy on diag.error using sinon |
| 30 | + diagErrorSpy = sinon.spy(diag, 'error'); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + sinon.restore(); // Restore the original dgram behavior |
| 35 | + }); |
| 36 | + |
| 37 | + it('should parse the endpoint correctly', () => { |
| 38 | + expect(udpExporter['_host']).toBe(host); |
| 39 | + expect(udpExporter['_port']).toBe(port); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should send UDP data correctly', () => { |
| 43 | + const data = new Uint8Array([1, 2, 3]); |
| 44 | + const prefix = 'T1'; |
| 45 | + const encodedData = '{"format":"json","version":1}\nT1AQID'; |
| 46 | + const protbufBinary = Buffer.from(encodedData, 'utf-8'); |
| 47 | + udpExporter.sendData(data, prefix); |
| 48 | + sinon.assert.calledOnce(socketSend); |
| 49 | + expect(socketSend.getCall(0).args[0]).toEqual(protbufBinary); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle errors when sending UDP data', () => { |
| 53 | + const errorMessage = 'UDP send error'; |
| 54 | + socketSend.yields(new Error(errorMessage)); // Simulate an error |
| 55 | + |
| 56 | + const data = new Uint8Array([1, 2, 3]); |
| 57 | + // Expect the sendData method to throw the error |
| 58 | + expect(() => udpExporter.sendData(data, 'T1')).toThrow(errorMessage); |
| 59 | + // Assert that diag.error was called with the correct error message |
| 60 | + expect(diagErrorSpy.calledOnce).toBe(true); |
| 61 | + expect(diagErrorSpy.calledWith('Error sending UDP data: %s', sinon.match.instanceOf(Error))).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should close the socket on shutdown', () => { |
| 65 | + udpExporter.shutdown(); |
| 66 | + expect(socketClose.calledOnce).toBe(true); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('OTLPUdpSpanExporterTest', () => { |
| 71 | + let otlpUdpSpanExporter: OTLPUdpSpanExporter; |
| 72 | + let udpExporterMock: { sendData: any; shutdown: any }; |
| 73 | + let diagErrorSpy: sinon.SinonSpy<[message: string, ...args: unknown[]], void>; |
| 74 | + const endpoint = '127.0.0.1:3000'; |
| 75 | + const prefix = 'T1'; |
| 76 | + const serializedData = new Uint8Array([1, 2, 3]); // Mock serialized data |
| 77 | + // Mock ReadableSpan object |
| 78 | + const mockSpanData: ReadableSpan = { |
| 79 | + name: 'spanName', |
| 80 | + kind: SpanKind.SERVER, |
| 81 | + spanContext: () => { |
| 82 | + const spanContext: SpanContext = { |
| 83 | + traceId: '00000000000000000000000000000008', |
| 84 | + spanId: '0000000000000009', |
| 85 | + traceFlags: 0, |
| 86 | + }; |
| 87 | + return spanContext; |
| 88 | + }, |
| 89 | + startTime: [0, 0], |
| 90 | + endTime: [0, 1], |
| 91 | + status: { code: 0 }, |
| 92 | + attributes: {}, |
| 93 | + links: [], |
| 94 | + events: [], |
| 95 | + duration: [0, 1], |
| 96 | + ended: true, |
| 97 | + resource: new Resource({}), |
| 98 | + instrumentationLibrary: { name: 'mockedLibrary' }, |
| 99 | + droppedAttributesCount: 0, |
| 100 | + droppedEventsCount: 0, |
| 101 | + droppedLinksCount: 0, |
| 102 | + }; |
| 103 | + const spans: ReadableSpan[] = [mockSpanData]; // Mock span data |
| 104 | + |
| 105 | + beforeEach(() => { |
| 106 | + // Mock UdpExporter methods |
| 107 | + udpExporterMock = { |
| 108 | + sendData: sinon.stub(), |
| 109 | + shutdown: sinon.stub().resolves(), |
| 110 | + }; |
| 111 | + |
| 112 | + // Stub the UdpExporter constructor to return our mock |
| 113 | + sinon.stub(UdpExporter.prototype, 'sendData').callsFake(udpExporterMock.sendData); |
| 114 | + sinon.stub(UdpExporter.prototype, 'shutdown').callsFake(udpExporterMock.shutdown); |
| 115 | + |
| 116 | + // Stub the diag.error method |
| 117 | + diagErrorSpy = sinon.spy(diag, 'error'); |
| 118 | + |
| 119 | + // Create an instance of OTLPUdpSpanExporter |
| 120 | + otlpUdpSpanExporter = new OTLPUdpSpanExporter(endpoint, prefix); |
| 121 | + }); |
| 122 | + |
| 123 | + afterEach(() => { |
| 124 | + // Restore the original functionality after each test |
| 125 | + sinon.restore(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should export spans successfully', () => { |
| 129 | + const callback = sinon.stub(); |
| 130 | + // Stub ProtobufTraceSerializer.serializeRequest |
| 131 | + sinon.stub(ProtobufTraceSerializer, 'serializeRequest').returns(serializedData); |
| 132 | + |
| 133 | + otlpUdpSpanExporter.export(spans, callback); |
| 134 | + |
| 135 | + expect(udpExporterMock.sendData.calledOnceWith(serializedData, 'T1')).toBe(true); |
| 136 | + expect(callback.calledOnceWith({ code: ExportResultCode.SUCCESS })).toBe(true); |
| 137 | + expect(diagErrorSpy.notCalled).toBe(true); // Ensure no error was logged |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle serialization failure', () => { |
| 141 | + // Make serializeRequest return null |
| 142 | + sinon.stub(ProtobufTraceSerializer, 'serializeRequest').returns(undefined); |
| 143 | + const callback = sinon.stub(); |
| 144 | + |
| 145 | + otlpUdpSpanExporter.export(spans, callback); |
| 146 | + |
| 147 | + expect(callback.notCalled).toBe(true); |
| 148 | + expect(udpExporterMock.sendData.notCalled).toBe(true); |
| 149 | + expect(diagErrorSpy.notCalled).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should handle errors during export', () => { |
| 153 | + const error = new Error('Export error'); |
| 154 | + udpExporterMock.sendData.throws(error); |
| 155 | + |
| 156 | + const callback = sinon.stub(); |
| 157 | + |
| 158 | + otlpUdpSpanExporter.export(spans, callback); |
| 159 | + |
| 160 | + expect(diagErrorSpy.calledOnceWith('Error exporting spans: %s', sinon.match.instanceOf(Error))).toBe(true); |
| 161 | + expect(callback.calledOnceWith({ code: ExportResultCode.FAILED })).toBe(true); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should shutdown the UDP exporter successfully', async () => { |
| 165 | + await otlpUdpSpanExporter.shutdown(); |
| 166 | + expect(udpExporterMock.shutdown.calledOnce).toBe(true); |
| 167 | + }); |
| 168 | +}); |
0 commit comments