|
1 |
| -jest.mock('dns'); |
| 1 | +import { vi, describe, beforeEach, test, expect } from 'vitest'; |
| 2 | +import { NodeClient } from '@sentry/node'; |
| 3 | +import { createTransport } from '@sentry/core'; |
| 4 | +import { setCurrentClient, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core'; |
| 5 | +import { googleCloudGrpcIntegration, fillGrpcFunction } from '../../src/integrations/google-cloud-grpc'; |
| 6 | +import type { GrpcFunctionObject, Stub, GrpcFunction } from '../../src/integrations/google-cloud-grpc'; |
2 | 7 |
|
3 |
| -import * as dns from 'dns'; |
4 |
| -import { EventEmitter } from 'events'; |
5 |
| -import * as fs from 'fs'; |
6 |
| -import * as path from 'path'; |
7 |
| -import { PubSub } from '@google-cloud/pubsub'; |
8 |
| -import * as http2 from 'http2'; |
9 |
| -import * as nock from 'nock'; |
| 8 | +const mockSpanEnd = vi.fn(); |
| 9 | +const mockStartInactiveSpan = vi.fn(); |
| 10 | +const mockFill = vi.fn(); |
10 | 11 |
|
11 |
| -import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core'; |
12 |
| -import { NodeClient, createTransport, setCurrentClient } from '@sentry/node'; |
13 |
| -import { googleCloudGrpcIntegration } from '../../src/integrations/google-cloud-grpc'; |
| 12 | +let mockClient: NodeClient; |
14 | 13 |
|
15 |
| -const spyConnect = jest.spyOn(http2, 'connect'); |
16 |
| - |
17 |
| -const mockSpanEnd = jest.fn(); |
18 |
| -const mockStartInactiveSpan = jest.fn(spanArgs => ({ ...spanArgs })); |
| 14 | +vi.mock('@sentry/core', async () => { |
| 15 | + const original = await vi.importActual('@sentry/core'); |
| 16 | + return { |
| 17 | + ...original, |
| 18 | + fill: (obj: any, name: string, replacement: any) => { |
| 19 | + mockFill(obj, name, replacement); |
| 20 | + obj[name] = replacement(obj[name]); |
| 21 | + }, |
| 22 | + getClient: () => mockClient, |
| 23 | + }; |
| 24 | +}); |
19 | 25 |
|
20 |
| -jest.mock('@sentry/node', () => { |
| 26 | +vi.mock('@sentry/node', async () => { |
| 27 | + const original = await vi.importActual('@sentry/node'); |
21 | 28 | return {
|
22 |
| - ...jest.requireActual('@sentry/node'), |
| 29 | + ...original, |
23 | 30 | startInactiveSpan: (ctx: unknown) => {
|
24 | 31 | mockStartInactiveSpan(ctx);
|
25 | 32 | return { end: mockSpanEnd };
|
26 | 33 | },
|
27 | 34 | };
|
28 | 35 | });
|
29 | 36 |
|
30 |
| -/** Fake HTTP2 stream */ |
31 |
| -class FakeStream extends EventEmitter { |
32 |
| - public rstCode: number = 0; |
33 |
| - close() { |
34 |
| - this.emit('end'); |
35 |
| - this.emit('close'); |
36 |
| - } |
37 |
| - end() {} |
38 |
| - pause() {} |
39 |
| - resume() {} |
40 |
| - write(_data: Buffer, cb: CallableFunction) { |
41 |
| - process.nextTick(cb, null); |
42 |
| - } |
43 |
| -} |
44 |
| - |
45 |
| -/** Fake HTTP2 session for GRPC */ |
46 |
| -class FakeSession extends EventEmitter { |
47 |
| - public socket: EventEmitter = new EventEmitter(); |
48 |
| - public request: jest.Mock = jest.fn(); |
49 |
| - ping() {} |
50 |
| - mockRequest(fn: (stream: FakeStream) => void): FakeStream { |
51 |
| - const stream = new FakeStream(); |
52 |
| - this.request.mockImplementationOnce(() => { |
53 |
| - process.nextTick(fn, stream); |
54 |
| - return stream; |
55 |
| - }); |
56 |
| - return stream; |
57 |
| - } |
58 |
| - mockUnaryRequest(responseData: Buffer) { |
59 |
| - this.mockRequest(stream => { |
60 |
| - stream.emit( |
61 |
| - 'response', |
62 |
| - { ':status': 200, 'content-type': 'application/grpc', 'content-disposition': 'attachment' }, |
63 |
| - 4, |
64 |
| - ); |
65 |
| - stream.emit('data', responseData); |
66 |
| - stream.emit('trailers', { 'grpc-status': '0', 'content-disposition': 'attachment' }); |
67 |
| - }); |
68 |
| - } |
69 |
| - close() { |
70 |
| - this.emit('close'); |
71 |
| - this.socket.emit('close'); |
72 |
| - } |
73 |
| - ref() {} |
74 |
| - unref() {} |
| 37 | +// Need to override mock because the integration loads google-gax as a CJS file |
| 38 | +async function mock(mockedUri: string, stub: any) { |
| 39 | + // @ts-expect-error we are using import on purpose |
| 40 | + const { Module } = await import('module'); |
| 41 | + |
| 42 | + // @ts-expect-error test |
| 43 | + Module._load_original = Module._load; |
| 44 | + // @ts-expect-error test |
| 45 | + Module._load = (uri, parent) => { |
| 46 | + if (uri === mockedUri) return stub; |
| 47 | + // @ts-expect-error test |
| 48 | + return Module._load_original(uri, parent); |
| 49 | + }; |
75 | 50 | }
|
76 | 51 |
|
77 |
| -function mockHttp2Session(): FakeSession { |
78 |
| - const session = new FakeSession(); |
79 |
| - spyConnect.mockImplementationOnce(() => { |
80 |
| - process.nextTick(() => session.emit('connect')); |
81 |
| - return session as unknown as http2.ClientHttp2Session; |
82 |
| - }); |
83 |
| - return session; |
84 |
| -} |
| 52 | +vi.hoisted( |
| 53 | + () => |
| 54 | + void mock('google-gax', { |
| 55 | + GrpcClient: { |
| 56 | + prototype: { |
| 57 | + createStub: vi.fn(), |
| 58 | + }, |
| 59 | + }, |
| 60 | + }), |
| 61 | +); |
85 | 62 |
|
86 | 63 | describe('GoogleCloudGrpc tracing', () => {
|
87 |
| - const mockClient = new NodeClient({ |
88 |
| - tracesSampleRate: 1.0, |
89 |
| - integrations: [], |
90 |
| - dsn: 'https://withAWSServices@domain/123', |
91 |
| - transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => Promise.resolve({})), |
92 |
| - stackParser: () => [], |
93 |
| - }); |
| 64 | + beforeEach(() => { |
| 65 | + mockClient = new NodeClient({ |
| 66 | + tracesSampleRate: 1.0, |
| 67 | + integrations: [], |
| 68 | + dsn: 'https://withAWSServices@domain/123', |
| 69 | + transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => Promise.resolve({})), |
| 70 | + stackParser: () => [], |
| 71 | + }); |
94 | 72 |
|
95 |
| - const integration = googleCloudGrpcIntegration(); |
96 |
| - mockClient.addIntegration(integration); |
| 73 | + const integration = googleCloudGrpcIntegration(); |
| 74 | + mockClient.addIntegration(integration); |
| 75 | + integration.setup?.(mockClient); |
97 | 76 |
|
98 |
| - beforeEach(() => { |
99 |
| - nock('https://www.googleapis.com').post('/oauth2/v4/token').reply(200, []); |
100 | 77 | setCurrentClient(mockClient);
|
101 | 78 | mockSpanEnd.mockClear();
|
102 | 79 | mockStartInactiveSpan.mockClear();
|
| 80 | + mockFill.mockClear(); |
103 | 81 | });
|
104 | 82 |
|
105 |
| - afterAll(() => { |
106 |
| - nock.restore(); |
107 |
| - spyConnect.mockRestore(); |
108 |
| - }); |
109 |
| - |
110 |
| - // We use google cloud pubsub as an example of grpc service for which we can trace requests. |
111 |
| - describe('pubsub', () => { |
112 |
| - // @ts-expect-error see "Why @ts-expect-error" note |
113 |
| - const dnsLookup = dns.lookup as jest.Mock; |
114 |
| - // @ts-expect-error see "Why @ts-expect-error" note |
115 |
| - const resolveTxt = dns.resolveTxt as jest.Mock; |
116 |
| - dnsLookup.mockImplementation((hostname, ...args) => { |
117 |
| - expect(hostname).toEqual('pubsub.googleapis.com'); |
118 |
| - process.nextTick(args[args.length - 1], null, [{ address: '0.0.0.0', family: 4 }]); |
119 |
| - }); |
120 |
| - resolveTxt.mockImplementation((hostname, cb) => { |
121 |
| - expect(hostname).toEqual('pubsub.googleapis.com'); |
122 |
| - process.nextTick(cb, null, []); |
| 83 | + describe('setup', () => { |
| 84 | + test('integration name is correct', () => { |
| 85 | + const integration = googleCloudGrpcIntegration(); |
| 86 | + expect(integration.name).toBe('GoogleCloudGrpc'); |
123 | 87 | });
|
124 | 88 |
|
125 |
| - const pubsub = new PubSub({ |
126 |
| - credentials: { |
127 |
| - client_email: 'client@email', |
128 |
| - private_key: fs.readFileSync(path.resolve(__dirname, 'private.pem')).toString(), |
129 |
| - }, |
130 |
| - projectId: 'project-id', |
| 89 | + test('setupOnce patches GrpcClient.createStub', () => { |
| 90 | + const mockCreateStub = vi.fn(); |
| 91 | + const mockGrpcClient = { |
| 92 | + prototype: { |
| 93 | + createStub: mockCreateStub, |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 98 | + require('google-gax').GrpcClient = mockGrpcClient; |
| 99 | + |
| 100 | + const integration = googleCloudGrpcIntegration(); |
| 101 | + integration.setupOnce?.(); |
| 102 | + expect(mockCreateStub).toBeDefined(); |
131 | 103 | });
|
132 | 104 |
|
133 |
| - afterEach(() => { |
134 |
| - dnsLookup.mockReset(); |
135 |
| - resolveTxt.mockReset(); |
| 105 | + test('setupOnce throws when google-gax is not available and not optional', () => { |
| 106 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 107 | + require('google-gax').GrpcClient = undefined; |
| 108 | + |
| 109 | + const integration = googleCloudGrpcIntegration(); |
| 110 | + expect(() => integration.setupOnce?.()).toThrow(); |
136 | 111 | });
|
137 | 112 |
|
138 |
| - afterAll(async () => { |
139 |
| - await pubsub.close(); |
| 113 | + test('setupOnce does not throw when google-gax is not available and optional', () => { |
| 114 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 115 | + require('google-gax').GrpcClient = undefined; |
| 116 | + |
| 117 | + const optionalIntegration = googleCloudGrpcIntegration({ optional: true }); |
| 118 | + expect(() => optionalIntegration.setupOnce?.()).not.toThrow(); |
140 | 119 | });
|
| 120 | + }); |
141 | 121 |
|
142 |
| - test('publish', async () => { |
143 |
| - mockHttp2Session().mockUnaryRequest(Buffer.from('00000000120a1031363337303834313536363233383630', 'hex')); |
144 |
| - const resp = await pubsub.topic('nicetopic').publish(Buffer.from('data')); |
145 |
| - expect(resp).toEqual('1637084156623860'); |
146 |
| - expect(mockStartInactiveSpan).toBeCalledWith({ |
147 |
| - op: 'grpc.pubsub', |
| 122 | + describe('fillGrpcFunction', () => { |
| 123 | + test('patches unary call methods with tracing', () => { |
| 124 | + const mockStub: Stub = { |
| 125 | + unaryMethod: Object.assign(vi.fn(), { |
| 126 | + requestStream: false, |
| 127 | + responseStream: false, |
| 128 | + originalName: 'unaryMethod', |
| 129 | + } as GrpcFunctionObject), |
| 130 | + }; |
| 131 | + |
| 132 | + const mockEventEmitter = { |
| 133 | + on: vi.fn(), |
| 134 | + }; |
| 135 | + |
| 136 | + (mockStub.unaryMethod as any).apply = vi.fn().mockReturnValue(mockEventEmitter); |
| 137 | + |
| 138 | + fillGrpcFunction(mockStub, 'test-service', 'unaryMethod'); |
| 139 | + |
| 140 | + const result = (mockStub.unaryMethod as GrpcFunction)(); |
| 141 | + expect(result).toBe(mockEventEmitter); |
| 142 | + expect(mockEventEmitter.on).toHaveBeenCalledWith('status', expect.any(Function)); |
| 143 | + expect(mockStartInactiveSpan).toHaveBeenCalledWith({ |
| 144 | + name: 'unary call unaryMethod', |
| 145 | + onlyIfParent: true, |
| 146 | + op: 'grpc.test-service', |
148 | 147 | attributes: {
|
149 | 148 | [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.grpc.serverless',
|
150 | 149 | },
|
151 |
| - name: 'unary call publish', |
152 |
| - onlyIfParent: true, |
153 | 150 | });
|
154 | 151 | });
|
| 152 | + |
| 153 | + test('does not patch non-unary call methods', () => { |
| 154 | + const mockStub: Stub = { |
| 155 | + clientStreamMethod: Object.assign(vi.fn(), { |
| 156 | + requestStream: true, |
| 157 | + responseStream: false, |
| 158 | + originalName: 'clientStreamMethod', |
| 159 | + } as GrpcFunctionObject), |
| 160 | + serverStreamMethod: Object.assign(vi.fn(), { |
| 161 | + requestStream: false, |
| 162 | + responseStream: true, |
| 163 | + originalName: 'serverStreamMethod', |
| 164 | + } as GrpcFunctionObject), |
| 165 | + bidiStreamMethod: Object.assign(vi.fn(), { |
| 166 | + requestStream: true, |
| 167 | + responseStream: true, |
| 168 | + originalName: 'bidiStreamMethod', |
| 169 | + } as GrpcFunctionObject), |
| 170 | + }; |
| 171 | + |
| 172 | + fillGrpcFunction(mockStub, 'test-service', 'clientStreamMethod'); |
| 173 | + fillGrpcFunction(mockStub, 'test-service', 'serverStreamMethod'); |
| 174 | + fillGrpcFunction(mockStub, 'test-service', 'bidiStreamMethod'); |
| 175 | + |
| 176 | + expect(mockStartInactiveSpan).not.toHaveBeenCalled(); |
| 177 | + }); |
| 178 | + |
| 179 | + test('does not patch non-function properties', () => { |
| 180 | + const mockStub: Stub = { |
| 181 | + nonFunction: Object.assign(vi.fn(), { |
| 182 | + requestStream: false, |
| 183 | + responseStream: false, |
| 184 | + originalName: 'nonFunction', |
| 185 | + } as GrpcFunctionObject), |
| 186 | + }; |
| 187 | + |
| 188 | + fillGrpcFunction(mockStub, 'test-service', 'nonFunction'); |
| 189 | + expect(mockStartInactiveSpan).not.toHaveBeenCalled(); |
| 190 | + }); |
| 191 | + |
| 192 | + test('does not patch methods when return value is not an EventEmitter', () => { |
| 193 | + const mockStub: Stub = { |
| 194 | + unaryMethod: Object.assign(vi.fn(), { |
| 195 | + requestStream: false, |
| 196 | + responseStream: false, |
| 197 | + originalName: 'unaryMethod', |
| 198 | + } as GrpcFunctionObject), |
| 199 | + }; |
| 200 | + |
| 201 | + (mockStub.unaryMethod as any).apply = vi.fn().mockReturnValue({ notAnEventEmitter: true }); |
| 202 | + |
| 203 | + fillGrpcFunction(mockStub, 'test-service', 'unaryMethod'); |
| 204 | + |
| 205 | + const result = (mockStub.unaryMethod as GrpcFunction)(); |
| 206 | + expect(result).toEqual({ notAnEventEmitter: true }); |
| 207 | + expect(mockStartInactiveSpan).not.toHaveBeenCalled(); |
| 208 | + }); |
155 | 209 | });
|
156 | 210 | });
|
0 commit comments