Skip to content
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

feat: Add @sentry/gatsby #2652

Merged
merged 2 commits into from Jun 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
- [react] feat: Add useProfiler hook (#2659)
- [core] feat: Export `makeMain` (#2665)
- [core] fix: Call `bindClient` when creating new `Hub` to make integrations work automatically (#2665)
- [gatsby] feat: Add @sentry/gatsby package (#2652)

## 5.17.0

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"packages/apm",
"packages/browser",
"packages/core",
"packages/gatsby",
"packages/hub",
"packages/integrations",
"packages/minimal",
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/.npmignore
@@ -0,0 +1,4 @@
*
!/dist/**/*
!/esm/**/*
*.tsbuildinfo
29 changes: 29 additions & 0 deletions packages/gatsby/LICENSE
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2020, Sentry
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 changes: 44 additions & 0 deletions packages/gatsby/README.md
@@ -0,0 +1,44 @@
<p align="center">
<a href="https://sentry.io" target="_blank" align="center">
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
</a>
<br />
</p>

# Official Sentry SDK for GatsbyJS

Register the package as a plugin in `gastby-config.js`:

```javascript
{
// ...
plugins: [
{
resolve: "@sentry/gatsby",
options: {
dsn: process.env.SENTRY_DSN, // this is the default
}
},
// ...
]
}
```

Options will be passed directly to `Sentry.init`. The `environment` value defaults to `NODE_ENV` (or `development` if not set).

## GitHub Actions

The `release` value is inferred from `GITHUB_SHA`.

## Netlify

The `release` value is inferred from `COMMIT_REF`.

## Vercel

To automatically capture the `release` value on Vercel you will need to register appropriate [system environment variable](https://vercel.com/docs/v2/build-step#system-environment-variables) (e.g. `VERCEL_GITHUB_COMMIT_SHA`) in your project.

## Links

- [Official SDK Docs](https://docs.sentry.io/quickstart/)
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)
78 changes: 78 additions & 0 deletions packages/gatsby/package.json
@@ -0,0 +1,78 @@
{
"name": "@sentry/gatsby",
"version": "5.17.0",
"description": "Offical Sentry SDK for Gatsby.js",
"repository": "git://github.com/getsentry/sentry-javascript.git",
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/gatsby",
"author": "Sentry",
"license": "BSD-3-Clause",
"keywords": ["gatsby", "gatsby-plugin"],
"engines": {
"node": ">=6"
},
dcramer marked this conversation as resolved.
Show resolved Hide resolved
"main": "dist/index.js",
"module": "esm/index.js",
"types": "dist/index.d.ts",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sentry/react": "5.17.0",
"@sentry/types": "5.17.0"
},
"peerDependencies": {
"gatsby": "*"
Copy link
Member

Choose a reason for hiding this comment

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

This config will work for all gatsby versions?

Copy link
Member Author

Choose a reason for hiding this comment

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

I dont know what versions itll work for, and I don't really want to dig through Gatsby history to figure out when specific APIs were created/etc.

},
"devDependencies": {
"jest": "^24.7.1",
"npm-run-all": "^4.1.2",
"prettier": "^1.17.0",
"prettier-check": "^2.0.0",
"rimraf": "^2.6.3",
"tslint": "^5.16.0",
dcramer marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^3.5.1"
},
"scripts": {
"build": "run-p build:es5 build:esm",
"build:es5": "tsc -p tsconfig.build.json",
"build:esm": "tsc -p tsconfig.esm.json",
"build:watch": "run-p build:watch:es5 build:watch:esm",
"build:watch:es5": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
"build:watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput",
"clean": "rimraf dist coverage build esm",
"link:yarn": "yarn link",
"lint": "run-s lint:prettier lint:tslint",
"lint:prettier": "prettier-check \"{src,test}/**/*.{ts,tsx}\"",
"lint:tslint": "tslint -t stylish -p .",
"lint:tslint:json": "tslint --format json -p . | tee lint-results.json",
"fix": "run-s fix:tslint fix:prettier",
"fix:prettier": "prettier --write \"{src,test}/**/*.{ts,tsx}\"",
"fix:tslint": "tslint --fix -t stylish -p .",
"test": "jest",
"test:watch": "jest --watch"
},
"jest": {
"collectCoverage": true,
"transform": {
"^.+\\.ts$": "ts-jest",
"^.+\\.tsx$": "ts-jest"
},
"moduleFileExtensions": [
"js",
"ts",
"tsx"
],
"testEnvironment": "jsdom",
"testMatch": [
"**/*.test.ts",
"**/*.test.tsx"
],
"globals": {
"ts-jest": {
"tsConfig": "./tsconfig.json",
"diagnostics": false
}
}
},
"sideEffects": false
}
20 changes: 20 additions & 0 deletions packages/gatsby/src/gatsby-browser.js
@@ -0,0 +1,20 @@
exports.onClientEntry = function(_, pluginParams) {
require.ensure(['@sentry/react', '@sentry/apm'], function(require) {
const Sentry = require('@sentry/browser');
const TracingIntegration = require('@sentry/apm').Integrations.Tracing;
const tracesSampleRate = pluginParams.tracesSampleRate !== undefined ? pluginParams.tracesSampleRate : 0;
const integrations = [...(pluginParams.integrations || [])];
if (tracesSampleRate) {
integrations.push(new TracingIntegration());
}
Sentry.init({
environment: process.env.NODE_ENV || 'development',
release: __SENTRY_RELEASE__,
dcramer marked this conversation as resolved.
Show resolved Hide resolved
dsn: __SENTRY_DSN__,
...pluginParams,
tracesSampleRate,
integrations,
});
window.Sentry = Sentry;
});
};
24 changes: 24 additions & 0 deletions packages/gatsby/src/gatsby-node.js
@@ -0,0 +1,24 @@
exports.onCreateWebpackConfig = ({ plugins, actions }) => {
actions.setWebpackConfig({
plugins: [
plugins.define({
__SENTRY_RELEASE__: JSON.stringify(
dcramer marked this conversation as resolved.
Show resolved Hide resolved
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
process.env.GITHUB_SHA ||
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
process.env.COMMIT_REF ||
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
process.env.VERCEL_GITHUB_COMMIT_SHA ||
dcramer marked this conversation as resolved.
Show resolved Hide resolved
process.env.VERCEL_GITLAB_COMMIT_SHA ||
process.env.VERCEL_BITBUCKET_COMMIT_SHA ||
// Zeit (now known as Vercel)
process.env.ZEIT_GITHUB_COMMIT_SHA ||
process.env.ZEIT_GITLAB_COMMIT_SHA ||
process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
'',
Copy link
Member

Choose a reason for hiding this comment

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

Is there no way for us to make this a user set option without relying on environmental variables? Can we provide some kind of pluginOptions like they can with onClientEntry and pluginParams?

Copy link
Member Author

Choose a reason for hiding this comment

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

this is still able to be overwritten from pluginOptions, but we should not make a user set anything unless they absolutely have to. frictionless has always been the goal.

),
__SENTRY_DSN__: JSON.stringify(process.env.SENTRY_DSN || ''),
Copy link
Member

Choose a reason for hiding this comment

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

@dcramer I hope this doesn't pick the wrong DSN with a Vercel project. They have some hack in the next JS Sentry example because I presume they have an internal DSN for Sentry that conflicts: https://github.com/vercel/next.js/blob/canary/examples/with-sentry-simple/next.config.js#L8

}),
],
});
};
1 change: 1 addition & 0 deletions packages/gatsby/src/index.ts
@@ -0,0 +1 @@
export * from '@sentry/react';
18 changes: 18 additions & 0 deletions packages/gatsby/test/index.test.ts
@@ -0,0 +1,18 @@
import * as GatsbyIntegration from '../src';

describe('package', () => {
it('exports init', () => {
expect(GatsbyIntegration.init).toBeDefined();
});

it('exports ErrorBoundary', () => {
expect(GatsbyIntegration.ErrorBoundary).toBeDefined();
expect(GatsbyIntegration.withErrorBoundary).toBeDefined();
});

it('exports Profiler', () => {
expect(GatsbyIntegration.Profiler).toBeDefined();
expect(GatsbyIntegration.withProfiler).toBeDefined();
expect(GatsbyIntegration.useProfiler).toBeDefined();
});
});
8 changes: 8 additions & 0 deletions packages/gatsby/tsconfig.build.json
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist"
},
"include": ["src/**/*"]
}
8 changes: 8 additions & 0 deletions packages/gatsby/tsconfig.esm.json
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "esm",
},
"include": ["src/**/*"]
}
9 changes: 9 additions & 0 deletions packages/gatsby/tsconfig.json
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.build.json",
"include": ["src/**/*.ts", "test/**/*.ts"],
"exclude": ["dist"],
"compilerOptions": {
"rootDir": ".",
"types": ["node", "jest"]
}
}
3 changes: 3 additions & 0 deletions packages/gatsby/tslint.json
@@ -0,0 +1,3 @@
{
"extends": "@sentry/typescript/tslint"
}