Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions packages/serverless/src/awsservices.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentScope } from '@sentry/node';
import { startInactiveSpan } from '@sentry/node';
import type { Integration, Span } from '@sentry/types';
import { fill } from '@sentry/utils';
// 'aws-sdk/global' import is expected to be type-only so it's erased in the final .js file.
Expand Down Expand Up @@ -57,19 +57,13 @@ function wrapMakeRequest<TService extends AWSService, TResult>(
): MakeRequestFunction<GenericParams, TResult> {
return function (this: TService, operation: string, params?: GenericParams, callback?: MakeRequestCallback<TResult>) {
let span: Span | undefined;
const scope = getCurrentScope();
// eslint-disable-next-line deprecation/deprecation
const transaction = scope.getTransaction();
const req = orig.call(this, operation, params);
req.on('afterBuild', () => {
if (transaction) {
// eslint-disable-next-line deprecation/deprecation
span = transaction.startChild({
description: describe(this, operation, params),
op: 'http.client',
origin: 'auto.http.serverless',
});
}
span = startInactiveSpan({
name: describe(this, operation, params),
op: 'http.client',
origin: 'auto.http.serverless',
});
});
req.on('complete', () => {
if (span) {
Expand Down
21 changes: 7 additions & 14 deletions packages/serverless/src/google-cloud-grpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EventEmitter } from 'events';
import { getCurrentScope } from '@sentry/node';
import type { Integration, Span } from '@sentry/types';
import { startInactiveSpan } from '@sentry/node';
import type { Integration } from '@sentry/types';
import { fill } from '@sentry/utils';

interface GrpcFunction extends CallableFunction {
Expand Down Expand Up @@ -107,18 +107,11 @@ function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: str
if (typeof ret?.on !== 'function') {
return ret;
}
let span: Span | undefined;
const scope = getCurrentScope();
// eslint-disable-next-line deprecation/deprecation
const transaction = scope.getTransaction();
if (transaction) {
// eslint-disable-next-line deprecation/deprecation
span = transaction.startChild({
description: `${callType} ${methodName}`,
op: `grpc.${serviceIdentifier}`,
origin: 'auto.grpc.serverless',
});
}
const span = startInactiveSpan({
name: `${callType} ${methodName}`,
op: `grpc.${serviceIdentifier}`,
origin: 'auto.grpc.serverless',
});
ret.on('status', () => {
if (span) {
span.end();
Expand Down
23 changes: 8 additions & 15 deletions packages/serverless/src/google-cloud-http.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// '@google-cloud/common' import is expected to be type-only so it's erased in the final .js file.
// When TypeScript compiler is upgraded, use `import type` syntax to explicitly assert that we don't want to load a module here.
import type * as common from '@google-cloud/common';
import { getCurrentScope } from '@sentry/node';
import type { Integration, Span } from '@sentry/types';
import { startInactiveSpan } from '@sentry/node';
import type { Integration } from '@sentry/types';
import { fill } from '@sentry/utils';

type RequestOptions = common.DecorateRequestOptions;
Expand Down Expand Up @@ -51,19 +51,12 @@ export class GoogleCloudHttp implements Integration {
/** Returns a wrapped function that makes a request with tracing enabled */
function wrapRequestFunction(orig: RequestFunction): RequestFunction {
return function (this: common.Service, reqOpts: RequestOptions, callback: ResponseCallback): void {
let span: Span | undefined;
const scope = getCurrentScope();
// eslint-disable-next-line deprecation/deprecation
const transaction = scope.getTransaction();
if (transaction) {
const httpMethod = reqOpts.method || 'GET';
// eslint-disable-next-line deprecation/deprecation
span = transaction.startChild({
description: `${httpMethod} ${reqOpts.uri}`,
op: `http.client.${identifyService(this.apiEndpoint)}`,
origin: 'auto.http.serverless',
});
}
const httpMethod = reqOpts.method || 'GET';
const span = startInactiveSpan({
name: `${httpMethod} ${reqOpts.uri}`,
op: `http.client.${identifyService(this.apiEndpoint)}`,
origin: 'auto.http.serverless',
});
orig.call(this, reqOpts, (...args: Parameters<ResponseCallback>) => {
if (span) {
span.end();
Expand Down
11 changes: 1 addition & 10 deletions packages/serverless/test/__mocks__/@sentry/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ export const fakeScope = {
setTag: jest.fn(),
setContext: jest.fn(),
setSpan: jest.fn(),
getTransaction: jest.fn(() => fakeTransaction),
setSDKProcessingMetadata: jest.fn(),
setPropagationContext: jest.fn(),
};
export const fakeSpan = {
end: jest.fn(),
setHttpStatus: jest.fn(),
};
export const fakeTransaction = {
end: jest.fn(),
setHttpStatus: jest.fn(),
startChild: jest.fn(() => fakeSpan),
};
export const init = jest.fn();
export const addGlobalEventProcessor = jest.fn();
export const getCurrentScope = jest.fn(() => fakeScope);
Expand All @@ -36,19 +30,16 @@ export const withScope = jest.fn(cb => cb(fakeScope));
export const flush = jest.fn(() => Promise.resolve());
export const getClient = jest.fn(() => ({}));
export const startSpanManual = jest.fn((ctx, callback: (span: any) => any) => callback(fakeSpan));
export const startInactiveSpan = jest.fn(() => fakeSpan);

export const resetMocks = (): void => {
fakeTransaction.setHttpStatus.mockClear();
fakeTransaction.end.mockClear();
fakeTransaction.startChild.mockClear();
fakeSpan.end.mockClear();
fakeSpan.setHttpStatus.mockClear();

fakeScope.addEventProcessor.mockClear();
fakeScope.setTag.mockClear();
fakeScope.setContext.mockClear();
fakeScope.setSpan.mockClear();
fakeScope.getTransaction.mockClear();

init.mockClear();
addGlobalEventProcessor.mockClear();
Expand Down
7 changes: 0 additions & 7 deletions packages/serverless/test/awslambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import * as Sentry from '../src';

const { wrapHandler } = Sentry.AWSLambda;

/**
* Why @ts-expect-error some Sentry.X calls
*
* A hack-ish way to contain everything related to mocks in the same __mocks__ file.
* Thanks to this, we don't have to do more magic than necessary. Just add and export desired method and assert on it.
*/

// Default `timeoutWarningLimit` is 500ms so leaving some space for it to trigger when necessary
const DEFAULT_EXECUTION_TIME = 100;
let fakeEvent: { [key: string]: unknown };
Expand Down
22 changes: 6 additions & 16 deletions packages/serverless/test/awsservices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import * as nock from 'nock';

import { AWSServices } from '../src/awsservices';

/**
* Why @ts-expect-error some Sentry.X calls
*
* A hack-ish way to contain everything related to mocks in the same __mocks__ file.
* Thanks to this, we don't have to do more magic than necessary. Just add and export desired method and assert on it.
*/

describe('AWSServices', () => {
beforeAll(() => {
new AWSServices().setupOnce();
Expand All @@ -30,11 +23,10 @@ describe('AWSServices', () => {
nock('https://foo.s3.amazonaws.com').get('/bar').reply(200, 'contents');
const data = await s3.getObject({ Bucket: 'foo', Key: 'bar' }).promise();
expect(data.Body?.toString('utf-8')).toEqual('contents');
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.startInactiveSpan).toBeCalledWith({
op: 'http.client',
origin: 'auto.http.serverless',
description: 'aws.s3.getObject foo',
name: 'aws.s3.getObject foo',
});
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeSpan.end).toBeCalled();
Expand All @@ -48,11 +40,10 @@ describe('AWSServices', () => {
expect(data.Body?.toString('utf-8')).toEqual('contents');
done();
});
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.startInactiveSpan).toBeCalledWith({
op: 'http.client',
origin: 'auto.http.serverless',
description: 'aws.s3.getObject foo',
name: 'aws.s3.getObject foo',
});
});
});
Expand All @@ -64,11 +55,10 @@ describe('AWSServices', () => {
nock('https://lambda.eu-north-1.amazonaws.com').post('/2015-03-31/functions/foo/invocations').reply(201, 'reply');
const data = await lambda.invoke({ FunctionName: 'foo' }).promise();
expect(data.Payload?.toString('utf-8')).toEqual('reply');
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.startInactiveSpan).toBeCalledWith({
op: 'http.client',
origin: 'auto.http.serverless',
description: 'aws.lambda.invoke foo',
name: 'aws.lambda.invoke foo',
});
});
});
Expand Down
6 changes: 0 additions & 6 deletions packages/serverless/test/gcpfunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import type {
Request,
Response,
} from '../src/gcpfunction/general';
/**
* Why @ts-expect-error some Sentry.X calls
*
* A hack-ish way to contain everything related to mocks in the same __mocks__ file.
* Thanks to this, we don't have to do more magic than necessary. Just add and export desired method and assert on it.
*/

describe('GCPFunction', () => {
afterEach(() => {
Expand Down
12 changes: 2 additions & 10 deletions packages/serverless/test/google-cloud-grpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import * as nock from 'nock';

import { GoogleCloudGrpc } from '../src/google-cloud-grpc';

/**
* Why @ts-expect-error some Sentry.X calls
*
* A hack-ish way to contain everything related to mocks in the same __mocks__ file.
* Thanks to this, we don't have to do more magic than necessary. Just add and export desired method and assert on it.
*/

const spyConnect = jest.spyOn(http2, 'connect');

/** Fake HTTP2 stream */
Expand Down Expand Up @@ -126,11 +119,10 @@ describe('GoogleCloudGrpc tracing', () => {
mockHttp2Session().mockUnaryRequest(Buffer.from('00000000120a1031363337303834313536363233383630', 'hex'));
const resp = await pubsub.topic('nicetopic').publish(Buffer.from('data'));
expect(resp).toEqual('1637084156623860');
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.startInactiveSpan).toBeCalledWith({
op: 'grpc.pubsub',
origin: 'auto.grpc.serverless',
description: 'unary call publish',
name: 'unary call publish',
});
await pubsub.close();
});
Expand Down
17 changes: 4 additions & 13 deletions packages/serverless/test/google-cloud-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import * as nock from 'nock';

import { GoogleCloudHttp } from '../src/google-cloud-http';

/**
* Why @ts-expect-error some Sentry.X calls
*
* A hack-ish way to contain everything related to mocks in the same __mocks__ file.
* Thanks to this, we don't have to do more magic than necessary. Just add and export desired method and assert on it.
*/

describe('GoogleCloudHttp tracing', () => {
beforeAll(() => {
new GoogleCloudHttp().setupOnce();
Expand Down Expand Up @@ -57,17 +50,15 @@ describe('GoogleCloudHttp tracing', () => {
);
const resp = await bigquery.query('SELECT true AS foo');
expect(resp).toEqual([[{ foo: true }]]);
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.startInactiveSpan).toBeCalledWith({
op: 'http.client.bigquery',
origin: 'auto.http.serverless',
description: 'POST /jobs',
name: 'POST /jobs',
});
// @ts-expect-error see "Why @ts-expect-error" note
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.startInactiveSpan).toBeCalledWith({
op: 'http.client.bigquery',
origin: 'auto.http.serverless',
description: expect.stringMatching(/^GET \/queries\/.+/),
name: expect.stringMatching(/^GET \/queries\/.+/),
});
});
});
Expand Down