-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathinit.ts
More file actions
115 lines (100 loc) · 4.63 KB
/
init.ts
File metadata and controls
115 lines (100 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { Integration, Options } from '@sentry/core';
import { applySdkMetadata, debug, getSDKSource } from '@sentry/core';
import type { NodeClient, NodeOptions } from '@sentry/node';
import { getDefaultIntegrationsWithoutPerformance, initWithoutDefaultIntegrations } from '@sentry/node';
import { envToBool } from '@sentry/node-core';
import { DEBUG_BUILD } from './debug-build';
import { awsIntegration } from './integration/aws';
import { awsLambdaIntegration } from './integration/awslambda';
/**
* Checks if proxy environment variables would interfere with the layer extension.
* The layer extension uses localhost:9000, so we need to check if proxy settings would prevent this.
*/
function shouldDisableLayerExtensionForProxy(): boolean {
const { http_proxy, no_proxy } = process.env;
// If no http proxy is configured, no interference (https_proxy doesn't affect HTTP requests)
if (!http_proxy) {
return false;
}
// Check if localhost is exempted by no_proxy
if (no_proxy) {
const exemptions = no_proxy.split(',').map(exemption => exemption.trim().toLowerCase());
// Handle common localhost exemption patterns explicitly
// If localhost is exempted, requests to the layer extension will not be proxied
const localhostExemptions = ['*', 'localhost', '127.0.0.1', '::1'];
if (exemptions.some(exemption => localhostExemptions.includes(exemption))) {
return false;
}
}
// If http_proxy is set and no localhost exemption, it would interfere
// The layer extension uses HTTP to localhost:9000, so only http_proxy matters
if (http_proxy) {
DEBUG_BUILD &&
debug.log(
'Disabling useLayerExtension due to http_proxy environment variable. Consider adding localhost to no_proxy to re-enable.',
);
return true;
}
return false;
}
/**
* Get the default integrations for the AWSLambda SDK.
*/
// NOTE: in awslambda-auto.ts, we also call the original `getDefaultIntegrations` from `@sentry/node` to load performance integrations.
// If at some point we need to filter a node integration out for good, we need to make sure to also filter it out there.
export function getDefaultIntegrations(options: Options): Integration[] {
return [...getDefaultIntegrationsWithoutPerformance(options), awsIntegration(), awsLambdaIntegration()];
}
export interface AwsServerlessOptions extends NodeOptions {
/**
* If Sentry events should be proxied through the Lambda extension when using the Lambda layer.
* Defaults to `true` when using the Lambda layer.
*
* Can also be configured via the `SENTRY_LAYER_EXTENSION` environment variable.
*/
useLayerExtension?: boolean;
}
/**
* Initializes the Sentry AWS Lambda SDK.
*
* @param options Configuration options for the SDK, @see {@link AWSLambdaOptions}.
*/
export function init(options: AwsServerlessOptions = {}): NodeClient | undefined {
const sdkSource = getSDKSource();
const proxyWouldInterfere = shouldDisableLayerExtensionForProxy();
// Determine useLayerExtension value with the following priority:
// 1. Explicit option value (if provided)
// 2. Environment variable SENTRY_LAYER_EXTENSION (if set)
// 3. Default logic based on sdkSource, tunnel, and proxy settings
const useLayerExtensionFromEnv = envToBool(process.env.SENTRY_LAYER_EXTENSION, { strict: true });
const defaultUseLayerExtension = sdkSource === 'aws-lambda-layer' && !options.tunnel && !proxyWouldInterfere;
const useLayerExtension = options.useLayerExtension ?? useLayerExtensionFromEnv ?? defaultUseLayerExtension;
const opts = {
defaultIntegrations: getDefaultIntegrations(options),
useLayerExtension,
...options,
};
if (opts.useLayerExtension) {
if (sdkSource !== 'aws-lambda-layer') {
DEBUG_BUILD && debug.warn('The Sentry Lambda extension is only supported when using the AWS Lambda layer.');
} else if (opts.tunnel || proxyWouldInterfere) {
if (opts.tunnel) {
DEBUG_BUILD &&
debug.warn(
`Using a custom tunnel with the Sentry Lambda extension is not supported. Events will be tunnelled to ${opts.tunnel} and not through the extension.`,
);
}
if (proxyWouldInterfere) {
DEBUG_BUILD &&
debug.warn(
'Sentry Lambda extension is disabled due to proxy environment variables (http_proxy/https_proxy). Consider adding localhost to no_proxy to re-enable.',
);
}
} else {
DEBUG_BUILD && debug.log('Proxying Sentry events through the Sentry Lambda extension');
opts.tunnel = 'http://localhost:9000/envelope';
}
}
applySdkMetadata(opts, 'aws-serverless', ['aws-serverless'], sdkSource);
return initWithoutDefaultIntegrations(opts);
}