-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement tracing of google cloud requests #2981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kamilogorek
merged 2 commits into
getsentry:master
from
marshall-lee:serverless/gcpservices
Oct 20, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be falsy anyway, but we can leave it as is to be more explicit.