Skip to content

Commit 64adcf7

Browse files
committed
chore(admin): Delete unused code from fxa-shared
Because: - Cleaning up code duplication, in favor of code ported to libs This Commit: - Deletes unreferenced code
1 parent fe76afd commit 64adcf7

43 files changed

Lines changed: 114 additions & 3088 deletions

Some content is hidden

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

libs/shared/monitoring/src/index.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,72 @@ export type MonitoringConfig = {
1515

1616
let initialized = false;
1717

18-
// IMPORTANT! This initialization function must be called first thing when a server starts. If it's called after server
19-
// frameworks initialized instrumentation might not work properly.
2018
/**
2119
* Initializes modules related to error monitoring, performance monitoring, and tracing.
22-
* @param opts - Initialization options. See underlying implementations for more details.
20+
*
21+
* IMPORTANT: This function must be called as early as possible during server startup, before
22+
* any server framework (e.g. Hapi, Express) registers its own instrumentation. Calling it
23+
* after framework initialization may result in incomplete or broken tracing.
24+
*
25+
* Initialization is guarded so that calling this function more than once is a no-op (a
26+
* warning is emitted on subsequent calls).
27+
*
28+
* @param opts - Monitoring initialization options, including an optional logger and a config
29+
* object that may contain both Sentry and tracing settings.
2330
*/
2431
export function initMonitoring(opts: MonitoringConfig) {
25-
const { log, config } = opts;
32+
const { log: logger, config } = opts;
33+
const log = logger || console;
34+
2635
if (initialized) {
27-
opts.log?.warn('monitoring', 'Monitoring can only be initialized once');
36+
log.warn('monitoring', 'Monitoring can only be initialized once');
2837
return;
2938
}
3039
initialized = true;
3140

41+
/**
42+
* IMPORTANT!
43+
*
44+
* Sentry also uses OTEL under the hood. Which means:
45+
* - Mind the order of initialization. Otel should be first, if configured!
46+
* - Mind the config.tracing.sentry.enabled flag
47+
* - Mind the config.sentry.skipOpenTelemetrySetup flag
48+
*
49+
* If the order or flags aren't correct the following could happen:
50+
* - Traces disappear
51+
* - Sentry errors don't get recorded
52+
* - Sentry context bleeds between requests (ie breadcrumbs, stack traces, etc. seem off)
53+
*/
54+
55+
let nodeTracingInitialized = false;
3256
if (config.tracing) {
33-
initTracing(config.tracing, log || console);
57+
// Important! Sentry also uses OTEL under the hood. Flip this flag so the two can co-exist!
58+
// If you start seeing funny stack traces, or cross pollinating bread crumbs, something went
59+
// sideways here.
60+
if (config.sentry?.dsn) {
61+
config.tracing.sentry = { enabled: true };
62+
}
63+
64+
log.info('monitoring', {
65+
msg: `Initialize Tracing with: ${JSON.stringify(config.tracing)}`,
66+
});
67+
nodeTracingInitialized = !!initTracing(config.tracing, log);
3468
}
69+
70+
// Important! Order matters here. If OTEL is configured, Sentry must be initialized after OTEL.
3571
if (config && config.sentry) {
36-
initSentry(config, log || console);
72+
if (nodeTracingInitialized) {
73+
config.sentry.skipOpenTelemetrySetup = true;
74+
}
75+
76+
log.info('monitoring', {
77+
msg: `Initializing Sentry: ${JSON.stringify({
78+
env: config.sentry?.env,
79+
clientName: config.sentry.clientName,
80+
serverName: config.sentry.serverName,
81+
hasDsn: !!config.sentry?.dsn,
82+
})}`,
83+
});
84+
initSentry(config, log);
3785
}
3886
}

libs/shared/sentry-utils/src/lib/models/sentry-config-opts.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import { ErrorEvent } from '@sentry/core';
66

77
export type SentryConfigOpts = {
8-
/** Name of release */
8+
/** The release name/version string passed to Sentry (e.g. a semver tag or git SHA). */
99
release?: string;
1010

11-
/** Fall back for name of release */
11+
/** Fallback release identifier used when `release` is not provided. */
1212
version?: string;
1313

1414
/** Sentry specific settings */
@@ -24,12 +24,15 @@ export type SentryConfigOpts = {
2424
/** The name of the active server. */
2525
serverName?: string;
2626

27-
/** When set to true, building a configuration will throw an error critical fields are missing. */
27+
/** When true, `buildSentryConfig` will throw a `SentryConfigurationBuildError` if any required fields are missing, rather than emitting a warning. */
2828
strict?: boolean;
2929

30-
/** The tracing sample rate. Setting this above 0 will aso result in performance metrics being captured. */
30+
/** The tracing sample rate (0–1). Setting this above 0 also enables performance metrics capture. */
3131
tracesSampleRate?: number;
3232

33+
/** Flag indicating that otel should not be setup. */
34+
skipOpenTelemetrySetup?: boolean;
35+
3336
/** A function that determines the sample rate for a given event. Setting this will override tracesSampleRate. */
3437
tracesSampler?: (context: { name?: string }) => number;
3538

@@ -38,10 +41,25 @@ export type SentryConfigOpts = {
3841
};
3942
};
4043

44+
/** Additional runtime options that extend the base Sentry configuration. */
4145
export type ExtraOpts = {
46+
/**
47+
* A predicate called with each captured error. Return `true` to suppress the
48+
* error (prevent it from being sent to Sentry), or `undefined`/`false` to
49+
* allow it through.
50+
*/
4251
ignoreErrors?: (error: any) => boolean | undefined;
52+
53+
/** Additional Sentry integrations to register beyond the defaults. */
4354
integrations?: any[];
55+
56+
/**
57+
* An ordered list of event-filter functions applied in `beforeSend`. Each
58+
* filter receives the current event and the Sentry hint, and must return the
59+
* (possibly mutated) event.
60+
*/
4461
eventFilters?: Array<(event: ErrorEvent, hint: any) => ErrorEvent>;
4562
};
4663

64+
/** Combined options accepted by `initSentry` functions across all platforms. */
4765
export type InitSentryOpts = SentryConfigOpts & ExtraOpts;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@
329329
"@fxa/vendored/crypto-relier/esm": "./dist/libs/vendored/crypto-relier/main.js",
330330
"@fxa/shared/pem-jwk": "./dist/libs/shared/pem-jwk/main.cjs",
331331
"@fxa/shared/l10n": "./dist/libs/shared/l10n/main.cjs",
332-
"@fxa/shared/otel": "./dist/libs/shared/otel/main.cjs"
332+
"@fxa/shared/otel": "./dist/libs/shared/otel/main.cjs",
333+
"@fxa/shared/monitoring": "./dist/libs/shared/monitoring/main.cjs"
333334
}
334335
}

packages/fxa-admin-panel/server/jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ module.exports = {
2020
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
2121
prefix: '<rootDir>/../../../',
2222
}),
23+
moduleNameMapper: {
24+
...pathsToModuleNameMapper(compilerOptions.paths, {
25+
prefix: '<rootDir>/../../../',
26+
}),
27+
'^@opentelemetry/otlp-exporter-base/node-http$':
28+
'@opentelemetry/otlp-exporter-base/build/src/index-node-http.js',
29+
'^@opentelemetry/otlp-exporter-base/browser-http$':
30+
'@opentelemetry/otlp-exporter-base/build/src/index-browser-http.js',
31+
},
2332
coveragePathIgnorePatterns: ['<rootDir>'],
2433
coverageThreshold: {
2534
global: {

packages/fxa-auth-server/bin/key_server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const {
2121
ProductConfigurationManager,
2222
StrapiClient,
2323
} = require('@fxa/shared/cms');
24-
const { TracingProvider } = require('@fxa/shared/otel');
24+
const { getCurrent: getCurrentTracingProvider } = require('@fxa/shared/otel');
2525

2626
const { AppError: error } = require('@fxa/accounts/errors');
2727
const { JWTool } = require('@fxa/vendored/jwtool');
@@ -93,7 +93,7 @@ async function run(config) {
9393
const log = require('../lib/log')({
9494
...config.log,
9595
statsd,
96-
nodeTracer: TracingProvider.getCurrent(),
96+
nodeTracer: getCurrentTracingProvider(),
9797
});
9898
Container.set(AuthLogger, log);
9999

packages/fxa-auth-server/jest.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
const { pathsToModuleNameMapper } = require('ts-jest');
6+
const { compilerOptions } = require('../../tsconfig.base.json');
7+
58
module.exports = {
69
preset: 'ts-jest',
710
testEnvironment: 'node',
@@ -19,11 +22,9 @@ module.exports = {
1922
'/node_modules/(?!(@fxa|fxa-shared|p-queue|p-timeout|eventemitter3)/)',
2023
],
2124
moduleNameMapper: {
22-
'^@fxa/shared/(.*)$': '<rootDir>/../../libs/shared/$1/src',
23-
'^@fxa/accounts/(.*)$': '<rootDir>/../../libs/accounts/$1/src',
24-
'^@fxa/payments/(.*)$': '<rootDir>/../../libs/payments/$1/src',
25-
'^@fxa/profile/(.*)$': '<rootDir>/../../libs/profile/$1/src',
26-
'^fxa-shared/(.*)$': '<rootDir>/../fxa-shared/$1',
25+
...pathsToModuleNameMapper(compilerOptions.paths, {
26+
prefix: '<rootDir>/../../',
27+
}),
2728
},
2829
testTimeout: 10000,
2930
maxWorkers: 4,

packages/fxa-auth-server/lib/server.in.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import sinon from 'sinon';
66
import { Account } from 'fxa-shared/db/models/auth/account';
77

88
const mockReportValidationError = jest.fn();
9-
jest.mock('fxa-shared/sentry/report-validation-error', () => ({
9+
jest.mock('@fxa/shared/sentry-node', () => ({
1010
reportValidationError: mockReportValidationError,
1111
}));
1212

packages/fxa-customs-server/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
"audit": "npm audit --json | audit-filter --nsp-config=.nsprc --audit=-",
2222
"lint": "eslint .",
2323
"test": "scripts/test-local.sh",
24-
"test-unit": "yarn make-artifacts-dir && tap test/local --node-arg=-r --node-arg=esbuild-register --jobs=1 | tap-xunit > ../../artifacts/tests/$npm_package_name/fxa-customs-server-tap-unit-results.xml",
25-
"test-integration": "yarn make-artifacts-dir && tap test/remote --jobs=1 | tap-xunit > ../../artifacts/tests/$npm_package_name/fxa-customs-server-tap-integration-results.xml",
2624
"format": "prettier --write --config ../../_dev/.prettierrc '**'",
2725
"make-artifacts-dir": "mkdir -p ../../artifacts/tests/$npm_package_name"
2826
},

packages/fxa-profile-server/scripts/mocha-coverage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const argv = [
2727
'--reporter=lcov',
2828
'--reporter=text',
2929
'--report-dir=coverage',
30+
'--require',
31+
'module-alias/register',
3032
MOCHA_BIN,
3133
'--require',
3234
'module-alias/register',

packages/fxa-settings/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
"^@fxa/shared/assets(.*)$": "<rootDir>/../../libs/shared/assets/src$1",
8383
"@fxa/accounts/errors": "<rootDir>/../../libs/accounts/errors/src/index.ts",
8484
"@fxa/accounts/oauth": "<rootDir>/../../libs/accounts/oauth/src/index.ts",
85+
"@fxa/shared/sentry-browser": "<rootDir>/../../libs/shared/sentry-browser/src/index.ts",
86+
"@fxa/shared/sentry-utils": "<rootDir>/../../libs/shared/sentry-utils/src/index.ts",
8587
"@fxa/vendored/common-password-list": "<rootDir>/../../libs/vendored/common-password-list/src/index.ts",
8688
"@fxa/vendored/incremental-encoder": "<rootDir>/../../libs/vendored/incremental-encoder/src/index.ts"
8789
},

0 commit comments

Comments
 (0)