Skip to content

feat(aws): Add OTEL based integrations #11548

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 4 commits into from
Apr 22, 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
8 changes: 3 additions & 5 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,10 @@ The `Transaction` integration has been removed from `@sentry/integrations`. Ther
#### @sentry/serverless

`@sentry/serverless` has been removed and will no longer be published. The serverless package has been split into two
different packages, `@sentry/aws-serverless` and `@sentry/google-cloud-serverless`. These new packages have smaller
bundle size than `@sentry/serverless`, which should improve your serverless cold-start times.
different packages, `@sentry/aws-serverless` and `@sentry/google-cloud-serverless`.

`@sentry/aws-serverless` and `@sentry/google-cloud-serverless` has also been changed to only emit CJS builds. The ESM
build for the `@sentry/serverless` package was always broken and we decided to remove it entirely. ESM support will be
re-added at a later date.
The `@sentry/google-cloud-serverless` package has also been changed to only emit CJS builds because it can only
instrument CJS. ESM support will be re-added at a later date.

In `@sentry/serverless` you had to use a namespace import to initialize the SDK. This has been removed so that you can
directly import from the SDK instead.
Expand Down
1 change: 1 addition & 0 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"mysql2": "^3.7.1",
"nock": "^13.1.0",
"pg": "^8.7.3",
"@aws-sdk/client-s3": "^3.552.0",
"proxy": "^2.1.1",
"reflect-metadata": "0.2.1",
"rxjs": "^7.8.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const Sentry = require('@sentry/aws-serverless');

Sentry.init({
dsn: 'https://[email protected]/1337',
tracesSampleRate: 1.0,
debug: true,
transport: loggingTransport,
});

const { S3 } = require('@aws-sdk/client-s3');
const nock = require('nock');

async function run() {
const bucketName = 'aws-test-bucket';
const keyName = 'aws-test-object.txt';

nock(`https://${bucketName}.s3.amazonaws.com`).get(`/${keyName}`).reply(200, 'contents');
nock(`https://${bucketName}.s3.amazonaws.com`).put(`/${keyName}`).reply(200, 'contents');

await Sentry.startSpan({ name: 'Test Transaction' }, async () => {
const region = 'us-east-1';
const s3Client = new S3({ region });
nock(`https://ot-demo-test.s3.${region}.amazonaws.com/`)
.put('/aws-ot-s3-test-object.txt?x-id=PutObject')
.reply(200, 'test');

const params = {
Bucket: 'ot-demo-test',
Key: 'aws-ot-s3-test-object.txt',
};
await s3Client.putObject(params);
});
}

run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';

const EXPECTED_TRANSCATION = {
transaction: 'Test Transaction',
spans: expect.arrayContaining([
expect.objectContaining({
description: 'S3.PutObject',
op: 'rpc',
origin: 'auto.otel.aws',
data: {
'sentry.origin': 'auto.otel.aws',
'sentry.op': 'rpc',
'rpc.system': 'aws-api',
'rpc.method': 'PutObject',
'rpc.service': 'S3',
'aws.region': 'us-east-1',
'otel.kind': 'CLIENT',
},
}),
]),
};

describe('awsIntegration', () => {
afterAll(() => {
cleanupChildProcesses();
});

test('should auto-instrument aws-sdk v2 package.', done => {
createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSCATION }).start(done);
});
});
8 changes: 2 additions & 6 deletions docs/v8-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,18 @@ working on this during the v8 alpha/beta cycle.
### Using Custom OpenTelemetry Instrumentation

While we include some vetted OpenTelemetry instrumentation out of the box, you can also add your own instrumentation on
top of that. You can do that by installing an instrumentation package (as well as `@opentelemetry/instrumentation`) and
setting it up like this:
top of that. You can do that by installing an instrumentation package and setting it up like this:

```js
const Sentry = require('@sentry/node');
const { GenericPoolInstrumentation } = require('@opentelemetry/instrumentation-generic-pool');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

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

// Afterwards, you can add additional instrumentation:
registerInsturmentations({
instrumentations: [new GenericPoolInstrumentation()],
});
Sentry.addOpenTelemetryInstrumentation(new GenericPoolInstrumentation());
```

### Using a Custom OpenTelemetry Setup
Expand Down
15 changes: 8 additions & 7 deletions packages/aws-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./build/npm/types/index.d.ts",
"default": "./build/npm/esm/index.js"
},
"require": {
"types": "./build/npm/types/index.d.ts",
"default": "./build/npm/cjs/index.js"
Expand Down Expand Up @@ -47,19 +51,16 @@
"access": "public"
},
"dependencies": {
"@opentelemetry/instrumentation-aws-lambda": "0.40.0",
"@opentelemetry/instrumentation-aws-sdk": "0.40.0",
"@sentry/core": "8.0.0-beta.3",
"@sentry/node": "8.0.0-beta.3",
"@sentry/types": "8.0.0-beta.3",
"@sentry/utils": "8.0.0-beta.3",
"@types/aws-lambda": "^8.10.62",
"@types/express": "^4.17.14"
"@types/aws-lambda": "^8.10.62"
},
"devDependencies": {
"@types/node": "^14.18.0",
"aws-sdk": "^2.765.0",
"find-up": "^5.0.0",
"nock": "^13.0.4",
"npm-packlist": "^2.1.4"
"@types/node": "^14.18.0"
},
"scripts": {
"build": "run-p build:transpile build:types build:bundle",
Expand Down
1 change: 0 additions & 1 deletion packages/aws-serverless/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default [
// packages with bundles have a different build directory structure
hasBundles: true,
}),
{ emitEsm: false },
),
...makeOtelLoaders('./build', 'sentry-node'),
];
2 changes: 1 addition & 1 deletion packages/aws-serverless/src/awslambda-auto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init, tryPatchHandler } from './awslambda';
import { init, tryPatchHandler } from './sdk';

const lambdaTaskRoot = process.env.LAMBDA_TASK_ROOT;
if (lambdaTaskRoot) {
Expand Down
115 changes: 0 additions & 115 deletions packages/aws-serverless/src/awsservices.ts

This file was deleted.

7 changes: 4 additions & 3 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export {
sessionTimingIntegration,
} from '@sentry/core';

export { getDefaultIntegrations, init, tryPatchHandler, wrapHandler } from './awslambda';
export type { WrapperOptions } from './awslambda';
export { awsIntegration } from './integration/aws';
export { awsLambdaIntegration } from './integration/awslambda';

export { awsServicesIntegration } from './awsservices';
export { getDefaultIntegrations, init, tryPatchHandler, wrapHandler } from './sdk';
export type { WrapperOptions } from './sdk';
24 changes: 24 additions & 0 deletions packages/aws-serverless/src/integration/aws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/node';
import type { IntegrationFn } from '@sentry/types';

const _awsIntegration = (() => {
return {
name: 'Aws',
setupOnce() {
addOpenTelemetryInstrumentation(
new AwsInstrumentation({
preRequestHook(span) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.otel.aws');
},
}),
);
},
};
}) satisfies IntegrationFn;

/**
* Instrumentation for aws-sdk package
*/
export const awsIntegration = defineIntegration(_awsIntegration);
24 changes: 24 additions & 0 deletions packages/aws-serverless/src/integration/awslambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/node';
import type { IntegrationFn } from '@sentry/types';

const _awsLambdaIntegration = (() => {
return {
name: 'AwsLambda',
setupOnce() {
addOpenTelemetryInstrumentation(
new AwsLambdaInstrumentation({
requestHook(span) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.otel.aws-lambda');
},
}),
);
},
};
}) satisfies IntegrationFn;

/**
* Instrumentation for aws-sdk package
*/
export const awsLambdaIntegration = defineIntegration(_awsLambdaIntegration);
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import type { Context, Handler } from 'aws-lambda';
import { performance } from 'perf_hooks';

import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import { awsServicesIntegration } from './awsservices';

import { DEBUG_BUILD } from './debug-build';
import { awsIntegration } from './integration/aws';
import { awsLambdaIntegration } from './integration/awslambda';
import { markEventUnhandled } from './utils';

const { isPromise } = types;
Expand Down Expand Up @@ -61,7 +62,7 @@ export interface WrapperOptions {

/** Get the default integrations for the AWSLambda SDK. */
export function getDefaultIntegrations(options: Options): Integration[] {
return [...getNodeDefaultIntegrations(options), awsServicesIntegration({ optional: true })];
return [...getNodeDefaultIntegrations(options), awsIntegration(), awsLambdaIntegration()];
}

/**
Expand Down
Loading
Loading