Skip to content

Commit aba6438

Browse files
committed
feat(node-core): Add node-core package
1 parent 797ebd1 commit aba6438

File tree

102 files changed

+13093
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+13093
-3
lines changed

dev-packages/rollup-utils/npmHelpers.mjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,14 @@ export function makeOtelLoaders(outputFolder, hookVariant) {
152152
}
153153

154154
const requiredDep = hookVariant === 'otel' ? '@opentelemetry/instrumentation' : '@sentry/node';
155-
const foundImportInTheMiddleDep = Object.keys(packageDotJSON.dependencies ?? {}).some(key => {
156-
return key === requiredDep;
157-
});
155+
const foundImportInTheMiddleDep =
156+
Object.keys(packageDotJSON.dependencies ?? {}).some(key => {
157+
return key === requiredDep;
158+
}) ||
159+
Object.keys(packageDotJSON.devDependencies ?? {}).some(key => {
160+
return key === requiredDep;
161+
});
162+
158163
if (!foundImportInTheMiddleDep) {
159164
throw new Error(
160165
`You used the makeOtelLoaders() rollup utility but didn't specify the "${requiredDep}" dependency in ${path.resolve(

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"packages/nestjs",
6969
"packages/nextjs",
7070
"packages/node",
71+
"packages/node-core",
7172
"packages/nuxt",
7273
"packages/opentelemetry",
7374
"packages/pino-transport",

packages/node-core/.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
env: {
3+
node: true,
4+
},
5+
extends: ['../../.eslintrc.js'],
6+
rules: {
7+
'@sentry-internal/sdk/no-class-field-initializers': 'off',
8+
},
9+
};

packages/node-core/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Functional Software, Inc. dba Sentry
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/node-core/README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<p align="center">
2+
<a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank">
3+
<img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84">
4+
</a>
5+
</p>
6+
7+
# Sentry JavaScript SDK Node Core
8+
9+
[![npm version](https://img.shields.io/npm/v/@sentry/node.svg)](https://www.npmjs.com/package/@sentry/node-core)
10+
[![npm dm](https://img.shields.io/npm/dm/@sentry/node.svg)](https://www.npmjs.com/package/@sentry/node-core)
11+
[![npm dt](https://img.shields.io/npm/dt/@sentry/node.svg)](https://www.npmjs.com/package/@sentry/node-core)
12+
13+
> [!CAUTION]
14+
> This package is in alpha state and may be subject to breaking changes.
15+
16+
Unlike the `@sentry/node` SDK, this SDK comes with no OpenTelemetry auto-instrumentation out of the box. It requires the following OpenTelemetry dependencies and supports both v1 and v2 of OpenTelemetry:
17+
18+
- `@opentelemetry/api`
19+
- `@opentelemetry/context-async-hooks`
20+
- `@opentelemetry/core`
21+
- `@opentelemetry/instrumentation`
22+
- `@opentelemetry/resources`
23+
- `@opentelemetry/sdk-trace-base`
24+
- `@opentelemetry/semantic-conventions`.
25+
26+
## Installation
27+
28+
```bash
29+
npm install @sentry/node-core @sentry/opentelemetry @opentelemetry/api @opentelemetry/core @opentelemetry/context-async-hooks @opentelemetry/instrumentation @opentelemetry/resources @opentelemetry/sdk-trace-base @opentelemetry/semantic-conventions
30+
31+
# Or yarn
32+
yarn add @sentry/node-core @sentry/opentelemetry @opentelemetry/api @opentelemetry/core @opentelemetry/context-async-hooks @opentelemetry/instrumentation @opentelemetry/resources @opentelemetry/sdk-trace-base @opentelemetry/semantic-conventions
33+
```
34+
35+
## Usage
36+
37+
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you
38+
require any other modules in your application, otherwise any auto-instrumentation will **not** work.
39+
You also need to set up OpenTelemetry, if you prefer not to, consider using the `@sentry/node` SDK instead.
40+
41+
You need to create a file named `instrument.js` that imports and initializes Sentry:
42+
43+
```js
44+
// CJS Syntax
45+
const { trace, propagation, context } = require('@opentelemetry/api');
46+
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
47+
const Sentry = require('@sentry/node-core');
48+
const { SentrySpanProcessor, SentryPropagator, SentrySampler } = require('@sentry/opentelemetry');
49+
// ESM Syntax
50+
import { context, propagation, trace } from '@opentelemetry/api';
51+
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
52+
import * as Sentry from '@sentry/node-core';
53+
import { SentrySpanProcessor, SentryPropagator, SentrySampler } from '@sentry/opentelemetry';
54+
55+
const sentryClient = Sentry.init({
56+
dsn: '__DSN__',
57+
// ...
58+
});
59+
60+
// Note: This could be BasicTracerProvider or any other provider depending on how you want to use the
61+
// OpenTelemetry SDK
62+
const provider = new NodeTracerProvider({
63+
// Ensure the correct subset of traces is sent to Sentry
64+
// This also ensures trace propagation works as expected
65+
sampler: sentryClient ? new SentrySampler(sentryClient) : undefined,
66+
spanProcessors: [
67+
// Ensure spans are correctly linked & sent to Sentry
68+
new SentrySpanProcessor(),
69+
// Add additional processors here
70+
],
71+
});
72+
73+
trace.setGlobalTracerProvider(provider);
74+
propagation.setGlobalPropagator(new SentryPropagator());
75+
context.setGlobalContextManager(new Sentry.SentryContextManager());
76+
77+
Sentry.validateOpenTelemetrySetup();
78+
```
79+
80+
You need to require or import the `instrument.js` file before importing any other modules in your application. This is
81+
necessary to ensure that Sentry can automatically instrument all modules in your application:
82+
83+
```js
84+
// Import this first!
85+
import './instrument';
86+
87+
// Now import other modules
88+
import http from 'http';
89+
90+
// Your application code goes here
91+
```
92+
93+
### ESM Support
94+
95+
When running your application in ESM mode, you should use the Node.js
96+
[`--import`](https://nodejs.org/api/cli.html#--importmodule) command line option to ensure that Sentry is loaded before
97+
the application code is evaluated.
98+
99+
Adjust the Node.js call for your application to use the `--import` parameter and point it at `instrument.js`, which
100+
contains your `Sentry.init`() code:
101+
102+
```bash
103+
# Note: This is only available for Node v18.19.0 onwards.
104+
node --import ./instrument.mjs app.mjs
105+
```
106+
107+
If it is not possible for you to pass the `--import` flag to the Node.js binary, you can alternatively use the
108+
`NODE_OPTIONS` environment variable as follows:
109+
110+
```bash
111+
NODE_OPTIONS="--import ./instrument.mjs" npm run start
112+
```
113+
114+
## Links
115+
116+
- [Official SDK Docs](https://docs.sentry.io/quickstart/)

packages/node-core/package.json

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"name": "@sentry/node-core",
3+
"version": "9.30.0",
4+
"description": "Sentry Node SDK using OpenTelemetry for performance instrumentation",
5+
"repository": "git://github.com/getsentry/sentry-javascript.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/node",
7+
"author": "Sentry",
8+
"license": "MIT",
9+
"engines": {
10+
"node": ">=18"
11+
},
12+
"files": [
13+
"/build"
14+
],
15+
"main": "build/cjs/index.js",
16+
"module": "build/esm/index.js",
17+
"types": "build/types/index.d.ts",
18+
"exports": {
19+
"./package.json": "./package.json",
20+
".": {
21+
"import": {
22+
"types": "./build/types/index.d.ts",
23+
"default": "./build/esm/index.js"
24+
},
25+
"require": {
26+
"types": "./build/types/index.d.ts",
27+
"default": "./build/cjs/index.js"
28+
}
29+
},
30+
"./import": {
31+
"import": {
32+
"default": "./build/import-hook.mjs"
33+
}
34+
},
35+
"./loader": {
36+
"import": {
37+
"default": "./build/loader-hook.mjs"
38+
}
39+
},
40+
"./init": {
41+
"import": {
42+
"default": "./build/esm/init.js"
43+
},
44+
"require": {
45+
"default": "./build/cjs/init.js"
46+
}
47+
}
48+
},
49+
"typesVersions": {
50+
"<5.0": {
51+
"build/types/index.d.ts": [
52+
"build/types-ts3.8/index.d.ts"
53+
]
54+
}
55+
},
56+
"publishConfig": {
57+
"access": "public"
58+
},
59+
"peerDependencies": {
60+
"@opentelemetry/api": "^1.9.0",
61+
"@opentelemetry/context-async-hooks": "^1.30.1 || ^2.0.0",
62+
"@opentelemetry/core": "^1.30.1 || ^2.0.0",
63+
"@opentelemetry/instrumentation": "^0.57.1 || ^0.200.0",
64+
"@opentelemetry/resources": "^1.30.1 || ^2.0.0",
65+
"@opentelemetry/sdk-trace-base": "^1.30.1 || ^2.0.0",
66+
"@opentelemetry/semantic-conventions": "^1.30.0"
67+
},
68+
"devDependencies": {
69+
"@opentelemetry/api": "^1.9.0",
70+
"@opentelemetry/context-async-hooks": "^1.30.1",
71+
"@opentelemetry/core": "^1.30.1",
72+
"@opentelemetry/instrumentation": "^0.57.2",
73+
"@opentelemetry/resources": "^1.30.1",
74+
"@opentelemetry/sdk-trace-base": "^1.30.1",
75+
"@opentelemetry/semantic-conventions": "^1.30.0",
76+
"@types/node": "^18.19.1"
77+
},
78+
"dependencies": {
79+
"@sentry/core": "9.30.0",
80+
"@sentry/opentelemetry": "9.30.0",
81+
"import-in-the-middle": "^1.13.1"
82+
},
83+
"scripts": {
84+
"build": "run-p build:transpile build:types",
85+
"build:dev": "yarn build",
86+
"build:transpile": "rollup -c rollup.npm.config.mjs",
87+
"build:types": "run-s build:types:core build:types:downlevel",
88+
"build:types:core": "tsc -p tsconfig.types.json",
89+
"build:types:downlevel": "yarn downlevel-dts build/types build/types-ts3.8 --to ts3.8",
90+
"build:watch": "run-p build:transpile:watch build:types:watch",
91+
"build:dev:watch": "yarn build:watch",
92+
"build:transpile:watch": "rollup -c rollup.npm.config.mjs --watch",
93+
"build:types:watch": "tsc -p tsconfig.types.json --watch",
94+
"build:tarball": "npm pack",
95+
"circularDepCheck": "madge --circular src/index.ts",
96+
"clean": "rimraf build coverage sentry-node-*.tgz",
97+
"fix": "eslint . --format stylish --fix",
98+
"lint": "eslint . --format stylish",
99+
"lint:es-compatibility": "es-check es2022 ./build/cjs/*.js && es-check es2022 ./build/esm/*.js --module",
100+
"test": "yarn test:unit",
101+
"test:unit": "vitest run",
102+
"test:watch": "vitest --watch",
103+
"yalc:publish": "yalc publish --push --sig"
104+
},
105+
"volta": {
106+
"extends": "../../package.json"
107+
},
108+
"sideEffects": false
109+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { makeBaseBundleConfig } from '@sentry-internal/rollup-utils';
2+
3+
export function createWorkerCodeBuilder(entry, outDir) {
4+
let base64Code;
5+
6+
return [
7+
makeBaseBundleConfig({
8+
bundleType: 'node-worker',
9+
entrypoints: [entry],
10+
licenseTitle: '@sentry/node',
11+
outputFileBase: () => 'worker-script.js',
12+
packageSpecificConfig: {
13+
output: {
14+
dir: outDir,
15+
sourcemap: false,
16+
},
17+
plugins: [
18+
{
19+
name: 'output-base64-worker-script',
20+
renderChunk(code) {
21+
base64Code = Buffer.from(code).toString('base64');
22+
},
23+
},
24+
],
25+
},
26+
}),
27+
() => {
28+
return base64Code;
29+
},
30+
];
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import replace from '@rollup/plugin-replace';
2+
import { makeBaseNPMConfig, makeNPMConfigVariants, makeOtelLoaders } from '@sentry-internal/rollup-utils';
3+
import { createWorkerCodeBuilder } from './rollup.anr-worker.config.mjs';
4+
5+
const [anrWorkerConfig, getAnrBase64Code] = createWorkerCodeBuilder(
6+
'src/integrations/anr/worker.ts',
7+
'build/esm/integrations/anr',
8+
);
9+
10+
const [localVariablesWorkerConfig, getLocalVariablesBase64Code] = createWorkerCodeBuilder(
11+
'src/integrations/local-variables/worker.ts',
12+
'build/esm/integrations/local-variables',
13+
);
14+
15+
export default [
16+
...makeOtelLoaders('./build', 'otel'),
17+
// The workers needs to be built first since it's their output is copied in the main bundle.
18+
anrWorkerConfig,
19+
localVariablesWorkerConfig,
20+
...makeNPMConfigVariants(
21+
makeBaseNPMConfig({
22+
entrypoints: ['src/index.ts', 'src/init.ts'],
23+
packageSpecificConfig: {
24+
output: {
25+
// set exports to 'named' or 'auto' so that rollup doesn't warn
26+
exports: 'named',
27+
preserveModules: true,
28+
},
29+
plugins: [
30+
replace({
31+
delimiters: ['###', '###'],
32+
// removes some rollup warnings
33+
preventAssignment: true,
34+
values: {
35+
AnrWorkerScript: () => getAnrBase64Code(),
36+
LocalVariablesWorkerScript: () => getLocalVariablesBase64Code(),
37+
},
38+
}),
39+
],
40+
},
41+
}),
42+
),
43+
];

0 commit comments

Comments
 (0)