-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tracing of google cloud requests (#2981)
Co-authored-by: Kamil Ogórek <[email protected]>
- Loading branch information
1 parent
3a7be5b
commit 9b6f448
Showing
11 changed files
with
892 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import { Integration } from '@sentry/types'; | ||
|
||
import { GoogleCloudGrpc } from '../google-cloud-grpc'; | ||
import { GoogleCloudHttp } from '../google-cloud-http'; | ||
|
||
import { serverlessEventProcessor } from '../utils'; | ||
|
||
export * from './http'; | ||
export * from './events'; | ||
export * from './cloud_events'; | ||
|
||
export const defaultIntegrations: Integration[] = [ | ||
...Sentry.defaultIntegrations, | ||
new GoogleCloudHttp({ optional: true }), // We mark this integration optional since '@google-cloud/common' module could be missing. | ||
new GoogleCloudGrpc({ optional: true }), // We mark this integration optional since 'google-gax' module could be missing. | ||
]; | ||
|
||
/** | ||
* @see {@link Sentry.init} | ||
*/ | ||
export function init(options: Sentry.NodeOptions = {}): void { | ||
if (options.defaultIntegrations === undefined) { | ||
options.defaultIntegrations = defaultIntegrations; | ||
} | ||
Sentry.init(options); | ||
Sentry.addGlobalEventProcessor(serverlessEventProcessor('GCPFunction')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { getCurrentHub } from '@sentry/node'; | ||
import { Integration, Span, Transaction } from '@sentry/types'; | ||
import { fill } from '@sentry/utils'; | ||
import { EventEmitter } from 'events'; | ||
|
||
interface GrpcFunction extends CallableFunction { | ||
(...args: unknown[]): EventEmitter; | ||
} | ||
|
||
interface GrpcFunctionObject extends GrpcFunction { | ||
requestStream: boolean; | ||
responseStream: boolean; | ||
originalName: string; | ||
} | ||
|
||
interface StubOptions { | ||
servicePath?: string; | ||
} | ||
|
||
interface CreateStubFunc extends CallableFunction { | ||
(createStub: unknown, options: StubOptions): PromiseLike<Stub>; | ||
} | ||
|
||
interface Stub { | ||
[key: string]: GrpcFunctionObject; | ||
} | ||
|
||
/** Google Cloud Platform service requests tracking for GRPC APIs */ | ||
export class GoogleCloudGrpc implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'GoogleCloudGrpc'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string = GoogleCloudGrpc.id; | ||
|
||
private readonly _optional: boolean; | ||
|
||
public constructor(options: { optional?: boolean } = {}) { | ||
this._optional = options.optional || false; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setupOnce(): void { | ||
try { | ||
const gaxModule = require('google-gax'); | ||
fill( | ||
gaxModule.GrpcClient.prototype, // eslint-disable-line @typescript-eslint/no-unsafe-member-access | ||
'createStub', | ||
wrapCreateStub, | ||
); | ||
} catch (e) { | ||
if (!this._optional) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** Returns a wrapped function that returns a stub with tracing enabled */ | ||
function wrapCreateStub(origCreate: CreateStubFunc): CreateStubFunc { | ||
return async function(this: unknown, ...args: Parameters<CreateStubFunc>) { | ||
const servicePath = args[1]?.servicePath; | ||
if (servicePath == null || servicePath == undefined) { | ||
return origCreate.apply(this, args); | ||
} | ||
const serviceIdentifier = identifyService(servicePath); | ||
const stub = await origCreate.apply(this, args); | ||
for (const methodName of Object.keys(Object.getPrototypeOf(stub))) { | ||
fillGrpcFunction(stub, serviceIdentifier, methodName); | ||
} | ||
return stub; | ||
}; | ||
} | ||
|
||
/** Patches the function in grpc stub to enable tracing */ | ||
function fillGrpcFunction(stub: Stub, serviceIdentifier: string, methodName: string): void { | ||
const funcObj = stub[methodName]; | ||
if (typeof funcObj !== 'function') { | ||
return; | ||
} | ||
const callType = | ||
!funcObj.requestStream && !funcObj.responseStream | ||
? 'unary call' | ||
: funcObj.requestStream && !funcObj.responseStream | ||
? 'client stream' | ||
: !funcObj.requestStream && funcObj.responseStream | ||
? 'server stream' | ||
: 'bidi stream'; | ||
if (callType != 'unary call') { | ||
return; | ||
} | ||
fill( | ||
stub, | ||
methodName, | ||
(orig: GrpcFunction): GrpcFunction => (...args) => { | ||
const ret = orig.apply(stub, args); | ||
if (typeof ret?.on !== 'function') { | ||
return ret; | ||
} | ||
let transaction: Transaction | undefined; | ||
let span: Span | undefined; | ||
const scope = getCurrentHub().getScope(); | ||
if (scope) { | ||
transaction = scope.getTransaction(); | ||
} | ||
if (transaction) { | ||
span = transaction.startChild({ | ||
description: `${callType} ${methodName}`, | ||
op: `gcloud.grpc.${serviceIdentifier}`, | ||
}); | ||
} | ||
ret.on('status', () => { | ||
if (span) { | ||
span.finish(); | ||
} | ||
}); | ||
return ret; | ||
}, | ||
); | ||
} | ||
|
||
/** Identifies service by its address */ | ||
function identifyService(servicePath: string): string { | ||
const match = servicePath.match(/^(\w+)\.googleapis.com$/); | ||
return match ? match[1] : servicePath; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// '@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 * as common from '@google-cloud/common'; | ||
import { getCurrentHub } from '@sentry/node'; | ||
import { Integration, Span, Transaction } from '@sentry/types'; | ||
import { fill } from '@sentry/utils'; | ||
|
||
type RequestOptions = common.DecorateRequestOptions; | ||
type ResponseCallback = common.BodyResponseCallback; | ||
// This interace could be replaced with just type alias once the `strictBindCallApply` mode is enabled. | ||
interface RequestFunction extends CallableFunction { | ||
(reqOpts: RequestOptions, callback: ResponseCallback): void; | ||
} | ||
|
||
/** Google Cloud Platform service requests tracking for RESTful APIs */ | ||
export class GoogleCloudHttp implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'GoogleCloudHttp'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string = GoogleCloudHttp.id; | ||
|
||
private readonly _optional: boolean; | ||
|
||
public constructor(options: { optional?: boolean } = {}) { | ||
this._optional = options.optional || false; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setupOnce(): void { | ||
try { | ||
const commonModule = require('@google-cloud/common') as typeof common; | ||
fill(commonModule.Service.prototype, 'request', wrapRequestFunction); | ||
} catch (e) { | ||
if (!this._optional) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** 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 transaction: Transaction | undefined; | ||
let span: Span | undefined; | ||
const scope = getCurrentHub().getScope(); | ||
if (scope) { | ||
transaction = scope.getTransaction(); | ||
} | ||
if (transaction) { | ||
const httpMethod = reqOpts.method || 'GET'; | ||
span = transaction.startChild({ | ||
description: `${httpMethod} ${reqOpts.uri}`, | ||
op: `gcloud.http.${identifyService(this.apiEndpoint)}`, | ||
}); | ||
} | ||
orig.call(this, reqOpts, (...args: Parameters<ResponseCallback>) => { | ||
if (span) { | ||
span.finish(); | ||
} | ||
callback(...args); | ||
}); | ||
}; | ||
} | ||
|
||
/** Identifies service by its base url */ | ||
function identifyService(apiEndpoint: string): string { | ||
const match = apiEndpoint.match(/^https:\/\/(\w+)\.googleapis.com$/); | ||
return match ? match[1] : apiEndpoint.replace(/^(http|https)?:\/\//, ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const lookup = jest.fn(); | ||
export const resolveTxt = jest.fn(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.