Skip to content

Commit

Permalink
feat: Add Bun SDK (#9029)
Browse files Browse the repository at this point in the history
Co-authored-by: Abhijeet Prasad <[email protected]>
  • Loading branch information
HazAT and AbhiPrasad authored Sep 18, 2023
1 parent 4a4366d commit d32b550
Show file tree
Hide file tree
Showing 21 changed files with 649 additions and 1 deletion.
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,34 @@ jobs:
- name: Compute test coverage
uses: codecov/codecov-action@v3

job_bun_unit_tests:
name: Bun Unit Tests
needs: [job_get_metadata, job_build]
timeout-minutes: 10
runs-on: ubuntu-20.04
strategy:
fail-fast: false
steps:
- name: Check out current commit (${{ needs.job_get_metadata.outputs.commit_label }})
uses: actions/checkout@v3
with:
ref: ${{ env.HEAD_COMMIT }}
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- name: Set up Bun
uses: oven-sh/setup-bun@v1
- name: Restore caches
uses: ./.github/actions/restore-cache
env:
DEPENDENCY_CACHE_KEY: ${{ needs.job_build.outputs.dependency_cache_key }}
- name: Run tests
run: |
yarn test-ci-bun
- name: Compute test coverage
uses: codecov/codecov-action@v3

job_node_unit_tests:
name: Node (${{ matrix.node }}) Unit Tests
needs: [job_get_metadata, job_build]
Expand Down Expand Up @@ -864,6 +892,7 @@ jobs:
job_build,
job_browser_build_tests,
job_browser_unit_tests,
job_bun_unit_tests,
job_node_unit_tests,
job_nextjs_integration_test,
job_node_integration_tests,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
"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/{node,node-experimental,opentelemetry-node,serverless,nextjs,remix,gatsby,sveltekit}\" --ignore \"@sentry-internal/{browser-integration-tests,e2e-tests,integration-shims,node-integration-tests,overhead-metrics}\"",
"test-ci-browser": "lerna run test --ignore \"@sentry/{bun,node,node-experimental,opentelemetry-node,serverless,nextjs,remix,gatsby,sveltekit}\" --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",
"yalc:publish": "lerna run yalc:publish"
},
Expand All @@ -41,6 +42,7 @@
"packages/angular-ivy",
"packages/browser",
"packages/browser-integration-tests",
"packages/bun",
"packages/core",
"packages/e2e-tests",
"packages/ember",
Expand Down
12 changes: 12 additions & 0 deletions packages/bun/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
env: {
node: true,
},
extends: ['../../.eslintrc.js'],
rules: {
'@sentry-internal/sdk/no-optional-chaining': 'off',
'@sentry-internal/sdk/no-nullish-coalescing': 'off',
'@sentry-internal/sdk/no-unsupported-es6-methods': 'off',
'@sentry-internal/sdk/no-class-field-initializers': 'off',
},
};
14 changes: 14 additions & 0 deletions packages/bun/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2023 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.
78 changes: 78 additions & 0 deletions packages/bun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<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 Bun (Beta)

[![npm version](https://img.shields.io/npm/v/@sentry/bun.svg)](https://www.npmjs.com/package/@sentry/bun)
[![npm dm](https://img.shields.io/npm/dm/@sentry/bun.svg)](https://www.npmjs.com/package/@sentry/bun)
[![npm dt](https://img.shields.io/npm/dt/@sentry/bun.svg)](https://www.npmjs.com/package/@sentry/bun)

## Links

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

The Sentry Bun SDK is in beta. Please help us improve the SDK by [reporting any issues or giving us feedback](https://github.com/getsentry/sentry-javascript/issues).

## Usage

To use this SDK, call `init(options)` as early as possible in the main entry module. This will initialize the SDK and
hook into the environment. Note that you can turn off almost all side effects using the respective options.

```javascript
// ES5 Syntax
const Sentry = require('@sentry/bun');
// ES6 Syntax
import * as Sentry from '@sentry/bun';

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

To set context information or send manual events, use the exported functions of `@sentry/bun`. Note that these
functions will not perform any action before you have called `init()`:

```javascript
// Set user information, as well as tags and further extras
Sentry.configureScope(scope => {
scope.setExtra('battery', 0.7);
scope.setTag('user_mode', 'admin');
scope.setUser({ id: '4711' });
// scope.clear();
});

// Add a breadcrumb for future events
Sentry.addBreadcrumb({
message: 'My Breadcrumb',
// ...
});

// Capture exceptions, messages or manual events
Sentry.captureMessage('Hello, world!');
Sentry.captureException(new Error('Good bye'));
Sentry.captureEvent({
message: 'Manual',
stacktrace: [
// ...
],
});
```

It's not possible to capture unhandled exceptions, unhandled promise rejections now - Bun is working on adding support for it.
[Github Issue](https://github.com/oven-sh/bun/issues/5091) follow this issue. To report errors to Sentry, you have to manually try-catch and call `Sentry.captureException` in the catch block.

```ts
import * as Sentry from '@sentry/bun';

try {
throw new Error('test');
} catch (e) {
Sentry.captureException(e);
}
```

1 change: 1 addition & 0 deletions packages/bun/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../jest/jest.config.js');
71 changes: 71 additions & 0 deletions packages/bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@sentry/bun",
"version": "7.69.0",
"description": "Official Sentry SDK for bun",
"repository": "git://github.com/getsentry/sentry-javascript.git",
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/bun",
"author": "Sentry",
"license": "MIT",
"engines": {
"node": ">=8"
},
"main": "build/esm/index.js",
"module": "build/esm/index.js",
"types": "build/types/index.d.ts",
"typesVersions": {
"<4.9": {
"build/npm/types/index.d.ts": [
"build/npm/types-ts3.8/index.d.ts"
]
}
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sentry/core": "7.69.0",
"@sentry/node": "7.69.0",
"@sentry/types": "7.69.0",
"@sentry/utils": "7.69.0"
},
"devDependencies": {
"bun-types": "latest"
},
"scripts": {
"build": "run-p build:transpile build:types",
"build:dev": "yarn build",
"build:transpile": "rollup -c rollup.npm.config.js",
"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.js --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-node-*.tgz",
"fix": "run-s fix:eslint fix:prettier",
"fix:eslint": "eslint . --format stylish --fix",
"fix:prettier": "prettier --write \"{src,test,scripts}/**/**.ts\"",
"lint": "run-s lint:prettier lint:eslint",
"lint:eslint": "eslint . --format stylish",
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
"install:bun": "node ./scripts/install-bun.js",
"test": "run-s install:bun test:bun",
"test:bun": "bun test",
"test:watch": "bun test --watch",
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push"
},
"volta": {
"extends": "../../package.json"
},
"sideEffects": false,
"madge":{
"detectiveOptions": {
"ts": {
"skipTypeImports": true
}
}
}
}
6 changes: 6 additions & 0 deletions packages/bun/rollup.npm.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';

const config = makeNPMConfigVariants(makeBaseNPMConfig());

// remove cjs from config array config[0].output.format == cjs
export default [config[1]];
49 changes: 49 additions & 0 deletions packages/bun/scripts/install-bun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable no-console */
if (process.env.CI) {
// This script is not needed in CI we install bun via GH actions
return;
}
const { exec } = require('child_process');
const https = require('https');

// Define the URL of the Bash script for bun installation
const installScriptUrl = 'https://bun.sh/install';

// Check if bun is installed
exec('bun -version', error => {
if (error) {
console.error('bun is not installed. Installing...');
// Download and execute the installation script
https
.get(installScriptUrl, res => {
if (res.statusCode !== 200) {
console.error(`Failed to download the installation script (HTTP ${res.statusCode})`);
process.exit(1);
}

res.setEncoding('utf8');
let scriptData = '';

res.on('data', chunk => {
scriptData += chunk;
});

res.on('end', () => {
// Execute the downloaded script
exec(scriptData, installError => {
if (installError) {
console.error('Failed to install bun:', installError);
process.exit(1);
}
console.log('bun has been successfully installed.');
});
});
})
.on('error', e => {
console.error('Failed to download the installation script:', e);
process.exit(1);
});
} else {
// Bun is installed
}
});
40 changes: 40 additions & 0 deletions packages/bun/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { ServerRuntimeClientOptions } from '@sentry/core';
import { SDK_VERSION, ServerRuntimeClient } from '@sentry/core';
import * as os from 'os';

import type { BunClientOptions } from './types';

/**
* The Sentry Bun SDK Client.
*
* @see BunClientOptions for documentation on configuration options.
* @see SentryClient for usage documentation.
*/
export class BunClient extends ServerRuntimeClient<BunClientOptions> {
/**
* Creates a new Bun SDK instance.
* @param options Configuration options for this SDK.
*/
public constructor(options: BunClientOptions) {
options._metadata = options._metadata || {};
options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.bun',
packages: [
{
name: 'npm:@sentry/bun',
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};

const clientOptions: ServerRuntimeClientOptions = {
...options,
platform: 'bun',
runtime: { name: 'bun', version: Bun.version },
serverName: options.serverName || global.process.env.SENTRY_NAME || os.hostname(),
};

super(clientOptions);
}
}
Loading

0 comments on commit d32b550

Please sign in to comment.