Skip to content

feat(google-cloud): Add @sentry/google-cloud package #10993

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
merged 8 commits into from
Mar 11, 2024
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
3 changes: 3 additions & 0 deletions .craft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ targets:
- name: npm
id: '@sentry/serverless'
includeNames: /^sentry-serverless-\d.*\.tgz$/
- name: npm
id: '@sentry/google-cloud'
includeNames: /^sentry-google-cloud-\d.*\.tgz$/
- name: npm
id: '@sentry/bun'
includeNames: /^sentry-bun-\d.*\.tgz$/
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ body:
- '@sentry/react'
- '@sentry/remix'
- '@sentry/serverless'
- '@sentry/google-cloud'
- '@sentry/svelte'
- '@sentry/sveltekit'
- '@sentry/vue'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@sentry/astro": "latest || *",
"@sentry/nextjs": "latest || *",
"@sentry/serverless": "latest || *",
"@sentry/google-cloud": "latest || *",
"@sentry/bun": "latest || *",
"@sentry/types": "latest || *",
"@types/node": "18.15.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as SentryAstro from '@sentry/astro';
import * as SentryBun from '@sentry/bun';
import * as SentryGoogleCloud from '@sentry/google-cloud';
import * as SentryNextJs from '@sentry/nextjs';
import * as SentryNode from '@sentry/node';
import * as SentryNodeExperimental from '@sentry/node-experimental';
Expand Down Expand Up @@ -81,6 +82,12 @@ const DEPENDENTS: Dependent[] = [
exports: Object.keys(SentryServerless),
ignoreExports: ['cron', 'hapiErrorPlugin'],
},
{
package: '@sentry/google-cloud',
compareWith: nodeExports,
exports: Object.keys(SentryGoogleCloud),
ignoreExports: ['makeMain'],
},
{
package: '@sentry/sveltekit',
compareWith: nodeExperimentalExports,
Expand Down
6 changes: 6 additions & 0 deletions dev-packages/e2e-tests/verdaccio-config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ packages:
unpublish: $all
# proxy: npmjs # Don't proxy for E2E tests!

'@sentry/google-cloud':
access: $all
publish: $all
unpublish: $all
# proxy: npmjs # Don't proxy for E2E tests!

'@sentry/svelte':
access: $all
publish: $all
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"postpublish": "lerna run --stream --concurrency 1 postpublish",
"test": "lerna run --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\" test",
"test:unit": "lerna run --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\" test:unit",
"test-ci-browser": "lerna run test --ignore \"@sentry/{bun,deno,node,node-experimental,profiling-node,serverless,nextjs,remix,gatsby,sveltekit,vercel-edge}\" --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\"",
"test-ci-browser": "lerna run test --ignore \"@sentry/{bun,deno,node,node-experimental,profiling-node,serverless,google-cloud,nextjs,remix,gatsby,sveltekit,vercel-edge}\" --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\"",
"test-ci-node": "ts-node ./scripts/node-unit-tests.ts",
"test-ci-bun": "lerna run test --scope @sentry/bun",
"test:update-snapshots": "lerna run test:update-snapshots",
Expand All @@ -56,6 +56,7 @@
"packages/eslint-plugin-sdk",
"packages/feedback",
"packages/gatsby",
"packages/google-cloud",
"packages/integration-shims",
"packages/nextjs",
"packages/node",
Expand Down
23 changes: 23 additions & 0 deletions packages/google-cloud/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
env: {
node: true,
},
extends: ['../../.eslintrc.js'],
rules: {
'@sentry-internal/sdk/no-optional-chaining': 'off',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: I think this is by now disabled by default, so we don't need to overwrite this anymore.

},
overrides: [
{
files: ['scripts/**/*.ts'],
parserOptions: {
project: ['../../tsconfig.dev.json'],
},
},
{
files: ['test/**'],
parserOptions: {
sourceType: 'module',
},
},
],
};
14 changes: 14 additions & 0 deletions packages/google-cloud/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2024 Sentry (https://sentry.io) and individual contributors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 changes: 47 additions & 0 deletions packages/google-cloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<p align="center">
<a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank">
<img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84">
</a>
</p>

# Official Sentry SDK for Google Cloud Functions

## Links

- [Official SDK Docs](https://docs.sentry.io/)
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)

## General

This package is a wrapper around `@sentry/node`, with added functionality related to various Serverless solutions. All
methods available in `@sentry/node` can be imported from `@sentry/google-cloud`.

To use this SDK, call `Sentry.init(options)` at the very beginning of your JavaScript file.

```javascript
import * as Sentry from '@sentry/google-cloud';

Sentry.init({
dsn: '__DSN__',
tracesSampleRate: 1.0,
// ...
});

// For HTTP Functions:

exports.helloHttp = Sentry.wrapHttpFunction((req, res) => {
throw new Error('oh, hello there!');
});

// For Background Functions:

exports.helloEvents = Sentry.wrapEventFunction((data, context, callback) => {
throw new Error('oh, hello there!');
});

// For CloudEvents:

exports.helloEvents = Sentry.wrapCloudEventFunction((context, callback) => {
throw new Error('oh, hello there!');
});
```
1 change: 1 addition & 0 deletions packages/google-cloud/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../jest/jest.config.js');
86 changes: 86 additions & 0 deletions packages/google-cloud/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"name": "@sentry/google-cloud",
"version": "8.0.0-alpha.2",
"description": "Official Sentry SDK for Google Cloud Functions",
"repository": "git://github.com/getsentry/sentry-javascript.git",
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/google-cloud",
"author": "Sentry",
"license": "MIT",
"engines": {
"node": ">=14.18"
},
"files": [
"cjs",
"esm",
"types",
"types-ts3.8"
],
"main": "build/cjs/index.js",
"module": "build/esm/index.js",
"types": "build/types/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./build/types/index.d.ts",
"default": "./build/esm/index.js"
},
"require": {
"types": "./build/types/index.d.ts",
"default": "./build/cjs/index.js"
}
}
},
"typesVersions": {
"<4.9": {
"build/types/index.d.ts": [
"build/types-ts3.8/index.d.ts"
]
}
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sentry/core": "8.0.0-alpha.2",
"@sentry/node": "8.0.0-alpha.2",
"@sentry/types": "8.0.0-alpha.2",
"@sentry/utils": "8.0.0-alpha.2",
"@types/express": "^4.17.14"
},
"devDependencies": {
"@google-cloud/bigquery": "^5.3.0",
"@google-cloud/common": "^3.4.1",
"@google-cloud/functions-framework": "^1.7.1",
"@google-cloud/pubsub": "^2.5.0",
"@types/node": "^14.6.4",
"find-up": "^5.0.0",
"google-gax": "^2.9.0",
"nock": "^13.0.4",
"npm-packlist": "^2.1.4"
},
"scripts": {
"build": "run-p build:transpile build:types",
"build:dev": "yarn build",
"build:transpile": "rollup -c rollup.npm.config.mjs",
"build:types": "run-s build:types:core build:types:downlevel",
"build:types:core": "tsc -p tsconfig.types.json",
"build:types:downlevel": "yarn downlevel-dts build/types build/types-ts3.8 --to ts3.8",
"build:watch": "run-p build:transpile:watch build:types:watch",
"build:dev:watch": "yarn build:watch",
"build:transpile:watch": "rollup -c rollup.npm.config.mjs --watch",
"build:types:watch": "tsc -p tsconfig.types.json --watch",
"build:tarball": "ts-node ../../scripts/prepack.ts && npm pack ./build",
"circularDepCheck": "madge --circular src/index.ts",
"clean": "rimraf build coverage sentry-google-cloud-*.tgz",
"fix": "eslint . --format stylish --fix",
"lint": "eslint . --format stylish",
"test": "jest",
"test:watch": "jest --watch",
"yalc:publish": "ts-node ../../scripts/prepack.ts --bundles && yalc publish ./build/npm --push --sig"
},
"volta": {
"extends": "../../package.json"
},
"sideEffects": false
}
3 changes: 3 additions & 0 deletions packages/google-cloud/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';

export default makeNPMConfigVariants(makeBaseNPMConfig());
8 changes: 8 additions & 0 deletions packages/google-cloud/src/debug-build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare const __DEBUG_BUILD__: boolean;

/**
* This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
*
* ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
*/
export const DEBUG_BUILD = __DEBUG_BUILD__;
81 changes: 81 additions & 0 deletions packages/google-cloud/src/gcpfunction/cloud_events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, handleCallbackErrors } from '@sentry/core';
import { captureException, flush, getCurrentScope, startSpanManual } from '@sentry/node';
import { logger } from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
import { domainify, markEventUnhandled, proxyFunction } from '../utils';
import type { CloudEventFunction, CloudEventFunctionWithCallback, WrapperOptions } from './general';

export type CloudEventFunctionWrapperOptions = WrapperOptions;

/**
* Wraps an event function handler adding it error capture and tracing capabilities.
*
* @param fn Event handler
* @param options Options
* @returns Event handler
*/
export function wrapCloudEventFunction(
fn: CloudEventFunction | CloudEventFunctionWithCallback,
wrapOptions: Partial<CloudEventFunctionWrapperOptions> = {},
): CloudEventFunctionWithCallback {
return proxyFunction(fn, f => domainify(_wrapCloudEventFunction(f, wrapOptions)));
}

function _wrapCloudEventFunction(
fn: CloudEventFunction | CloudEventFunctionWithCallback,
wrapOptions: Partial<CloudEventFunctionWrapperOptions> = {},
): CloudEventFunctionWithCallback {
const options: CloudEventFunctionWrapperOptions = {
flushTimeout: 2000,
...wrapOptions,
};
return (context, callback) => {
return startSpanManual(
{
name: context.type || '<unknown>',
op: 'function.gcp.cloud_event',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless.gcp_cloud_event',
},
},
span => {
const scope = getCurrentScope();
scope.setContext('gcp.function.context', { ...context });

const newCallback = domainify((...args: unknown[]) => {
if (args[0] !== null && args[0] !== undefined) {
captureException(args[0], scope => markEventUnhandled(scope));
}
span.end();

// eslint-disable-next-line @typescript-eslint/no-floating-promises
flush(options.flushTimeout)
.then(null, e => {
DEBUG_BUILD && logger.error(e);
})
.then(() => {
callback(...args);
});
});

if (fn.length > 1) {
return handleCallbackErrors(
() => (fn as CloudEventFunctionWithCallback)(context, newCallback),
err => {
captureException(err, scope => markEventUnhandled(scope));
},
);
}

return Promise.resolve()
.then(() => (fn as CloudEventFunction)(context))
.then(
result => newCallback(null, result),
err => newCallback(err, undefined),
);
},
);
};
}
Loading