@sophonz/node-sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +118 -0
- package/build/bin/opentelemetry-instrument.d.ts +3 -0
- package/build/bin/opentelemetry-instrument.d.ts.map +1 -0
- package/build/bin/opentelemetry-instrument.js +21 -0
- package/build/package.json +62 -0
- package/build/src/AbstractAsyncHooksContextManager.d.ts +18 -0
- package/build/src/AbstractAsyncHooksContextManager.d.ts.map +1 -0
- package/build/src/AbstractAsyncHooksContextManager.js +122 -0
- package/build/src/MutableAsyncLocalStorageContextManager.d.ts +14 -0
- package/build/src/MutableAsyncLocalStorageContextManager.d.ts.map +1 -0
- package/build/src/MutableAsyncLocalStorageContextManager.js +36 -0
- package/build/src/constants.d.ts +27 -0
- package/build/src/constants.d.ts.map +1 -0
- package/build/src/constants.js +41 -0
- package/build/src/gcp.d.ts +3 -0
- package/build/src/gcp.d.ts.map +1 -0
- package/build/src/gcp.js +60 -0
- package/build/src/index.d.ts +6 -0
- package/build/src/index.d.ts.map +1 -0
- package/build/src/index.js +11 -0
- package/build/src/instrumentations/console.d.ts +24 -0
- package/build/src/instrumentations/console.d.ts.map +1 -0
- package/build/src/instrumentations/console.js +146 -0
- package/build/src/instrumentations/http.d.ts +21 -0
- package/build/src/instrumentations/http.d.ts.map +1 -0
- package/build/src/instrumentations/http.js +285 -0
- package/build/src/logger.d.ts +26 -0
- package/build/src/logger.d.ts.map +1 -0
- package/build/src/logger.js +54 -0
- package/build/src/metrics.d.ts +3 -0
- package/build/src/metrics.d.ts.map +1 -0
- package/build/src/metrics.js +14 -0
- package/build/src/otel-logger/index.d.ts +36 -0
- package/build/src/otel-logger/index.d.ts.map +1 -0
- package/build/src/otel-logger/index.js +104 -0
- package/build/src/otel-logger/pino.d.ts +37 -0
- package/build/src/otel-logger/pino.d.ts.map +1 -0
- package/build/src/otel-logger/pino.js +91 -0
- package/build/src/otel-logger/winston.d.ts +29 -0
- package/build/src/otel-logger/winston.d.ts.map +1 -0
- package/build/src/otel-logger/winston.js +72 -0
- package/build/src/otel.d.ts +29 -0
- package/build/src/otel.d.ts.map +1 -0
- package/build/src/otel.js +482 -0
- package/build/src/spanProcessor.d.ts +17 -0
- package/build/src/spanProcessor.d.ts.map +1 -0
- package/build/src/spanProcessor.js +37 -0
- package/build/src/tracing.d.ts +2 -0
- package/build/src/tracing.d.ts.map +1 -0
- package/build/src/tracing.js +4 -0
- package/build/src/utils.d.ts +3 -0
- package/build/src/utils.d.ts.map +1 -0
- package/build/src/utils.js +32 -0
- package/build/src/version.d.ts +2 -0
- package/build/src/version.d.ts.map +1 -0
- package/build/src/version.js +4 -0
- package/package.json +62 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setTraceAttributes = exports.shutdown = exports.init = exports.initSDK = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const instrumentation_node_exception_1 = require("@sophonz/instrumentation-node-exception");
|
|
6
|
+
const instrumentation_node_1 = require("@sophonz/instrumentation-node");
|
|
7
|
+
const api_1 = require("@opentelemetry/api");
|
|
8
|
+
const auto_instrumentations_node_1 = require("@opentelemetry/auto-instrumentations-node");
|
|
9
|
+
const exporter_trace_otlp_proto_1 = require("@opentelemetry/exporter-trace-otlp-proto");
|
|
10
|
+
const instrumentation_runtime_node_1 = require("@opentelemetry/instrumentation-runtime-node");
|
|
11
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
12
|
+
const sdk_node_1 = require("@opentelemetry/sdk-node");
|
|
13
|
+
const cli_spinners_1 = tslib_1.__importDefault(require("cli-spinners"));
|
|
14
|
+
const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
|
|
15
|
+
const ora_1 = tslib_1.__importDefault(require("ora"));
|
|
16
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
17
|
+
const semver = tslib_1.__importStar(require("semver"));
|
|
18
|
+
const shimmer_1 = require("shimmer");
|
|
19
|
+
const package_json_1 = require("../package.json");
|
|
20
|
+
const constants_1 = require("./constants");
|
|
21
|
+
const console_1 = tslib_1.__importDefault(require("./instrumentations/console"));
|
|
22
|
+
const http_1 = require("./instrumentations/http");
|
|
23
|
+
const metrics_1 = require("./metrics");
|
|
24
|
+
const MutableAsyncLocalStorageContextManager_1 = require("./MutableAsyncLocalStorageContextManager");
|
|
25
|
+
const otel_logger_1 = require("./otel-logger");
|
|
26
|
+
const spanProcessor_1 = tslib_1.__importDefault(require("./spanProcessor"));
|
|
27
|
+
const UI_LOG_PREFIX = '[⚡Sophonz]';
|
|
28
|
+
const env = process.env;
|
|
29
|
+
const IS_LOCAL = env.NODE_ENV === 'development' || !env.NODE_ENV;
|
|
30
|
+
const setOtelEnvs = ({ apiKey, disableLogs, disableMetrics, disableTracing, service, }) => {
|
|
31
|
+
var _a;
|
|
32
|
+
env.OTEL_NODE_RESOURCE_DETECTORS = (_a = env.OTEL_NODE_RESOURCE_DETECTORS) !== null && _a !== void 0 ? _a : 'all';
|
|
33
|
+
env.OTEL_TRACES_SAMPLER = constants_1.DEFAULT_OTEL_TRACES_SAMPLER;
|
|
34
|
+
env.OTEL_TRACES_SAMPLER_ARG = constants_1.DEFAULT_OTEL_TRACES_SAMPLER_ARG;
|
|
35
|
+
env.OTEL_SERVICE_NAME = service;
|
|
36
|
+
if (disableLogs) {
|
|
37
|
+
env.OTEL_LOGS_EXPORTER = 'none';
|
|
38
|
+
}
|
|
39
|
+
if (disableTracing) {
|
|
40
|
+
env.OTEL_TRACES_EXPORTER = 'none';
|
|
41
|
+
}
|
|
42
|
+
if (disableMetrics) {
|
|
43
|
+
env.OTEL_METRICS_EXPORTER = 'none';
|
|
44
|
+
}
|
|
45
|
+
if (apiKey) {
|
|
46
|
+
if (env.OTEL_EXPORTER_OTLP_HEADERS) {
|
|
47
|
+
env.OTEL_EXPORTER_OTLP_HEADERS = `${env.OTEL_EXPORTER_OTLP_HEADERS},Authorization=${apiKey}`;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
env.OTEL_EXPORTER_OTLP_HEADERS = `Authorization=${apiKey}`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
let sdk;
|
|
55
|
+
let contextManager;
|
|
56
|
+
const getModuleId = (moduleName) => {
|
|
57
|
+
try {
|
|
58
|
+
const moduleId = require.resolve(moduleName);
|
|
59
|
+
return moduleId;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
api_1.diag.error('Error resolving module id', e);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const isSupported = (supportedVersions, version, includePrerelease) => {
|
|
67
|
+
if (typeof version === 'undefined') {
|
|
68
|
+
return supportedVersions.includes('*');
|
|
69
|
+
}
|
|
70
|
+
return supportedVersions.some((supportedVersion) => {
|
|
71
|
+
return semver.satisfies(version, supportedVersion, { includePrerelease });
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
const hrtimeToMs = (hrtime) => {
|
|
75
|
+
return hrtime[0] * 1e3 + hrtime[1] / 1e6;
|
|
76
|
+
};
|
|
77
|
+
const healthCheckUrl = async (ui, url, requestConfigs) => {
|
|
78
|
+
ui.text = `Checking health of ${url}...`;
|
|
79
|
+
try {
|
|
80
|
+
const res = await (0, node_fetch_1.default)(url, requestConfigs);
|
|
81
|
+
if (res.ok) {
|
|
82
|
+
ui.succeed(`Health check passed for ${url}`);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
ui.fail(`Health check failed for ${url}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
api_1.diag.error('Error checking health of url', e);
|
|
90
|
+
ui.fail(`Health check failed for ${url}`);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const initSDK = (config) => {
|
|
94
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _o, _p, _q, _r, _s, _t;
|
|
95
|
+
const defaultDisableStartupLogs = (_a = config.disableStartupLogs) !== null && _a !== void 0 ? _a : !constants_1.DEFAULT_SOPHONZ_STARTUP_LOGS;
|
|
96
|
+
const ui = (0, ora_1.default)({
|
|
97
|
+
isSilent: defaultDisableStartupLogs,
|
|
98
|
+
prefixText: UI_LOG_PREFIX,
|
|
99
|
+
spinner: cli_spinners_1.default.dots,
|
|
100
|
+
text: 'Initializing OpenTelemetry SDK...',
|
|
101
|
+
}).start();
|
|
102
|
+
const defaultApiKey = (_b = config.apiKey) !== null && _b !== void 0 ? _b : (0, constants_1.DEFAULT_SOPHONZ_API_KEY)();
|
|
103
|
+
const defaultDetectResources = (_c = config.detectResources) !== null && _c !== void 0 ? _c : true;
|
|
104
|
+
const defaultDisableLogs = (_d = config.disableLogs) !== null && _d !== void 0 ? _d : constants_1.DEFAULT_OTEL_LOGS_EXPORTER === 'none';
|
|
105
|
+
const defaultDisableMetrics = (_e = config.disableMetrics) !== null && _e !== void 0 ? _e : constants_1.DEFAULT_OTEL_METRICS_EXPORTER === 'none';
|
|
106
|
+
const defaultDisableTracing = (_f = config.disableTracing) !== null && _f !== void 0 ? _f : constants_1.DEFAULT_OTEL_TRACES_EXPORTER === 'none';
|
|
107
|
+
const defaultEnableInternalProfiling = (_g = config.enableInternalProfiling) !== null && _g !== void 0 ? _g : false;
|
|
108
|
+
const defaultServiceName = (_h = config.service) !== null && _h !== void 0 ? _h : (0, constants_1.DEFAULT_SERVICE_NAME)();
|
|
109
|
+
ui.succeed(`Service name is configured to be "${defaultServiceName}"`);
|
|
110
|
+
if (!env.OTEL_EXPORTER_OTLP_HEADERS && !defaultApiKey) {
|
|
111
|
+
ui.fail('apiKey or SOPHONZ_API_KEY or OTEL_EXPORTER_OTLP_HEADERS is not set');
|
|
112
|
+
ui.stopAndPersist({
|
|
113
|
+
text: 'OpenTelemetry SDK initialization skipped',
|
|
114
|
+
symbol: '🚫',
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
ui.text = 'Setting otel envs...';
|
|
119
|
+
setOtelEnvs({
|
|
120
|
+
apiKey: defaultApiKey,
|
|
121
|
+
disableLogs: defaultDisableLogs,
|
|
122
|
+
disableMetrics: defaultDisableMetrics,
|
|
123
|
+
disableTracing: defaultDisableTracing,
|
|
124
|
+
service: defaultServiceName,
|
|
125
|
+
});
|
|
126
|
+
ui.succeed('Set default otel envs');
|
|
127
|
+
const stopOnTerminationSignals = (_j = config.stopOnTerminationSignals) !== null && _j !== void 0 ? _j : constants_1.DEFAULT_SOPHONZ_NODE_STOP_ON_TERMINATION_SIGNALS;
|
|
128
|
+
let defaultConsoleCapture = (_k = config.consoleCapture) !== null && _k !== void 0 ? _k : constants_1.DEFAULT_SOPHONZ_NODE_CONSOLE_CAPTURE;
|
|
129
|
+
if (constants_1.DEFAULT_OTEL_LOG_LEVEL === 'debug' || constants_1.DEFAULT_OTEL_LOG_LEVEL === String(api_1.DiagLogLevel.DEBUG)) {
|
|
130
|
+
defaultConsoleCapture = false;
|
|
131
|
+
ui.warn(`OTEL_LOG_LEVEL is set to 'debug', disabling console instrumentation`);
|
|
132
|
+
}
|
|
133
|
+
ui.text = 'Initializing OpenTelemetry Logger...';
|
|
134
|
+
const _logger = new otel_logger_1.Logger({
|
|
135
|
+
detectResource: defaultDetectResources,
|
|
136
|
+
service: defaultServiceName,
|
|
137
|
+
});
|
|
138
|
+
ui.succeed('Initialized OpenTelemetry Logger');
|
|
139
|
+
Promise.all([
|
|
140
|
+
healthCheckUrl(ui, constants_1.DEFAULT_OTEL_TRACES_EXPORTER_URL, {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
headers: {
|
|
143
|
+
'Content-Type': 'application/json',
|
|
144
|
+
},
|
|
145
|
+
body: JSON.stringify({}),
|
|
146
|
+
}),
|
|
147
|
+
healthCheckUrl(ui, _logger.getExporterUrl(), {
|
|
148
|
+
method: 'POST',
|
|
149
|
+
headers: {
|
|
150
|
+
'Content-Type': 'application/json',
|
|
151
|
+
},
|
|
152
|
+
body: JSON.stringify({}),
|
|
153
|
+
}),
|
|
154
|
+
healthCheckUrl(ui, constants_1.DEFAULT_OTEL_METRICS_EXPORTER_URL, {
|
|
155
|
+
method: 'POST',
|
|
156
|
+
headers: {
|
|
157
|
+
'Content-Type': 'application/json',
|
|
158
|
+
},
|
|
159
|
+
body: JSON.stringify({}),
|
|
160
|
+
}),
|
|
161
|
+
]);
|
|
162
|
+
const defaultBetaMode = (_l = config.betaMode) !== null && _l !== void 0 ? _l : (0, constants_1.DEFAULT_SOPHONZ_NODE_BETA_MODE)();
|
|
163
|
+
const defaultAdvancedNetworkCapture = (_o = config.advancedNetworkCapture) !== null && _o !== void 0 ? _o : constants_1.DEFAULT_SOPHONZ_NODE_ADVANCED_NETWORK_CAPTURE;
|
|
164
|
+
const defaultExceptionCapture = (_p = config.experimentalExceptionCapture) !== null && _p !== void 0 ? _p : constants_1.DEFAULT_SOPHONZ_NODE_EXPERIMENTAL_EXCEPTION_CAPTURE;
|
|
165
|
+
const defaultSentryIntegrationEnabled = (_q = config.sentryIntegrationEnabled) !== null && _q !== void 0 ? _q : constants_1.DEFAULT_SOPHONZ_NODE_SENTRY_INTEGRATION_ENABLED;
|
|
166
|
+
contextManager = semver.gte(process.version, '14.8.0')
|
|
167
|
+
? new MutableAsyncLocalStorageContextManager_1.MutableAsyncLocalStorageContextManager()
|
|
168
|
+
: undefined;
|
|
169
|
+
ui.text = 'Initializing instrumentations packages...';
|
|
170
|
+
const allInstrumentations = [
|
|
171
|
+
...(0, auto_instrumentations_node_1.getNodeAutoInstrumentations)({
|
|
172
|
+
'@opentelemetry/instrumentation-http': defaultAdvancedNetworkCapture
|
|
173
|
+
? (0, http_1.getSophonzHTTPInstrumentationConfig)({
|
|
174
|
+
httpCaptureHeadersClientRequest: env.OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST,
|
|
175
|
+
httpCaptureHeadersClientResponse: env.OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE,
|
|
176
|
+
httpCaptureHeadersServerRequest: env.OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
|
|
177
|
+
httpCaptureHeadersServerResponse: env.OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
|
|
178
|
+
})
|
|
179
|
+
: { enabled: true },
|
|
180
|
+
'@opentelemetry/instrumentation-fs': {
|
|
181
|
+
enabled: false,
|
|
182
|
+
},
|
|
183
|
+
...config.instrumentations,
|
|
184
|
+
}),
|
|
185
|
+
new instrumentation_runtime_node_1.RuntimeNodeInstrumentation(),
|
|
186
|
+
...(defaultConsoleCapture
|
|
187
|
+
? [
|
|
188
|
+
new console_1.default({
|
|
189
|
+
betaMode: defaultBetaMode,
|
|
190
|
+
loggerOptions: {
|
|
191
|
+
baseUrl: _logger.getExporterUrl(),
|
|
192
|
+
service: defaultServiceName,
|
|
193
|
+
},
|
|
194
|
+
contextManager,
|
|
195
|
+
}),
|
|
196
|
+
]
|
|
197
|
+
: []),
|
|
198
|
+
...(defaultSentryIntegrationEnabled
|
|
199
|
+
? [new instrumentation_node_1.SentryNodeInstrumentation()]
|
|
200
|
+
: []),
|
|
201
|
+
...(defaultExceptionCapture
|
|
202
|
+
? [
|
|
203
|
+
new instrumentation_node_exception_1.ExceptionInstrumentation({
|
|
204
|
+
_internalForceFlush: exports.shutdown,
|
|
205
|
+
}),
|
|
206
|
+
]
|
|
207
|
+
: []),
|
|
208
|
+
...((_r = config.additionalInstrumentations) !== null && _r !== void 0 ? _r : []),
|
|
209
|
+
];
|
|
210
|
+
sdk = new sdk_node_1.NodeSDK({
|
|
211
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
212
|
+
'telemetry.distro.name': 'sophonz',
|
|
213
|
+
'telemetry.distro.version': package_json_1.version,
|
|
214
|
+
}),
|
|
215
|
+
logRecordProcessor: defaultDisableLogs ? undefined : _logger.getProcessor(),
|
|
216
|
+
metricReader: (_s = config.metricReader) !== null && _s !== void 0 ? _s : (defaultDisableMetrics ? undefined : (0, metrics_1.getSophonzMetricReader)()),
|
|
217
|
+
spanProcessors: [
|
|
218
|
+
...(defaultDisableTracing
|
|
219
|
+
? []
|
|
220
|
+
: [
|
|
221
|
+
new spanProcessor_1.default({
|
|
222
|
+
exporter: new exporter_trace_otlp_proto_1.OTLPTraceExporter({
|
|
223
|
+
timeoutMillis: constants_1.DEFAULT_OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
|
|
224
|
+
url: constants_1.DEFAULT_OTEL_TRACES_EXPORTER_URL,
|
|
225
|
+
}),
|
|
226
|
+
enableSophonzGlobalContext: defaultBetaMode,
|
|
227
|
+
contextManager,
|
|
228
|
+
}),
|
|
229
|
+
]),
|
|
230
|
+
],
|
|
231
|
+
instrumentations: config.programmaticImports ? [] : allInstrumentations,
|
|
232
|
+
contextManager: contextManager,
|
|
233
|
+
});
|
|
234
|
+
ui.succeed('Initialized instrumentations packages');
|
|
235
|
+
if (defaultEnableInternalProfiling) {
|
|
236
|
+
ui.text = 'Enabling internal profiling...';
|
|
237
|
+
for (const instrumentation of allInstrumentations) {
|
|
238
|
+
const _originalEnable = instrumentation.enable;
|
|
239
|
+
instrumentation.enable = function (...args) {
|
|
240
|
+
const start = process.hrtime();
|
|
241
|
+
const result = _originalEnable.apply(this, args);
|
|
242
|
+
const end = process.hrtime(start);
|
|
243
|
+
ui.succeed(`Enabled instrumentation ${instrumentation.constructor.name} in ${hrtimeToMs(end)} ms`);
|
|
244
|
+
return result;
|
|
245
|
+
};
|
|
246
|
+
const modules = instrumentation
|
|
247
|
+
._modules;
|
|
248
|
+
for (const module of modules) {
|
|
249
|
+
if (typeof module.patch === 'function') {
|
|
250
|
+
(0, shimmer_1.wrap)(module, 'patch', (original) => {
|
|
251
|
+
return (...args) => {
|
|
252
|
+
const start = process.hrtime();
|
|
253
|
+
const result = original.apply(this, args);
|
|
254
|
+
const end = process.hrtime(start);
|
|
255
|
+
ui.succeed(`Instrumented ${module.name}${module.moduleVersion ? ` [v${module.moduleVersion}] ` : ' '}in ${hrtimeToMs(end)} ms`);
|
|
256
|
+
return result;
|
|
257
|
+
};
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
for (const file of module.files) {
|
|
261
|
+
if (typeof file.patch === 'function') {
|
|
262
|
+
(0, shimmer_1.wrap)(file, 'patch', (original) => {
|
|
263
|
+
return (...args) => {
|
|
264
|
+
const start = process.hrtime();
|
|
265
|
+
const result = original.apply(this, args);
|
|
266
|
+
const end = process.hrtime(start);
|
|
267
|
+
ui.succeed(`Instrumented ${module.name}${module.moduleVersion ? ` [v${module.moduleVersion}] ` : ' '}file ${file.name} in ${hrtimeToMs(end)} ms`);
|
|
268
|
+
return result;
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
ui.text = 'Starting OpenTelemetry Node SDK...';
|
|
277
|
+
sdk.start();
|
|
278
|
+
ui.succeed('Started OpenTelemetry Node SDK');
|
|
279
|
+
if (config.programmaticImports) {
|
|
280
|
+
ui.text = 'Repatching instrumentation packages...';
|
|
281
|
+
for (const instrumentation of allInstrumentations) {
|
|
282
|
+
const modules = instrumentation
|
|
283
|
+
._modules;
|
|
284
|
+
if (Array.isArray(modules)) {
|
|
285
|
+
instrumentation.disable();
|
|
286
|
+
for (const module of modules) {
|
|
287
|
+
if (getModuleId(module.name)) {
|
|
288
|
+
try {
|
|
289
|
+
const _m = require(module.name);
|
|
290
|
+
module.moduleExports = _m;
|
|
291
|
+
}
|
|
292
|
+
catch (e) {
|
|
293
|
+
api_1.diag.error('Error re-requiring moduleExports for nodejs module', {
|
|
294
|
+
module: module.name,
|
|
295
|
+
version: module.moduleVersion,
|
|
296
|
+
error: e,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
const _pkg = require(path_1.default.join(module.name, 'package.json'));
|
|
301
|
+
module.moduleVersion = _pkg.version;
|
|
302
|
+
}
|
|
303
|
+
catch (e) {
|
|
304
|
+
api_1.diag.error('Error re-requiring package.json for nodejs module', {
|
|
305
|
+
module: module.name,
|
|
306
|
+
version: module.moduleVersion,
|
|
307
|
+
error: e,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
if (isSupported(module.supportedVersions, module.moduleVersion, module.includePrerelease) &&
|
|
311
|
+
typeof module.patch === 'function' &&
|
|
312
|
+
module.moduleExports) {
|
|
313
|
+
api_1.diag.debug('Applying instrumentation patch for nodejs module on instrumentation enabled', {
|
|
314
|
+
module: module.name,
|
|
315
|
+
version: module.moduleVersion,
|
|
316
|
+
});
|
|
317
|
+
try {
|
|
318
|
+
module.patch(module.moduleExports, module.moduleVersion);
|
|
319
|
+
}
|
|
320
|
+
catch (e) {
|
|
321
|
+
api_1.diag.error('Error applying instrumentation patch for nodejs module', e);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const files = (_t = module.files) !== null && _t !== void 0 ? _t : [];
|
|
325
|
+
const supportedFileInstrumentations = files.filter((f) => isSupported(f.supportedVersions, module.moduleVersion, module.includePrerelease));
|
|
326
|
+
for (const sfi of supportedFileInstrumentations) {
|
|
327
|
+
try {
|
|
328
|
+
const _m = require(sfi.name);
|
|
329
|
+
sfi.moduleExports = _m;
|
|
330
|
+
}
|
|
331
|
+
catch (e) {
|
|
332
|
+
api_1.diag.error('Error re-requiring moduleExports for nodejs module file', e);
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
api_1.diag.debug('Applying instrumentation patch for nodejs module file on require hook', {
|
|
336
|
+
module: module.name,
|
|
337
|
+
version: module.moduleVersion,
|
|
338
|
+
fileName: sfi.name,
|
|
339
|
+
});
|
|
340
|
+
try {
|
|
341
|
+
sfi.patch(sfi.moduleExports, module.moduleVersion);
|
|
342
|
+
}
|
|
343
|
+
catch (e) {
|
|
344
|
+
api_1.diag.error('Error applying instrumentation patch for nodejs module file', e);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
ui.succeed('Repatched instrumentation packages');
|
|
352
|
+
}
|
|
353
|
+
function handleTerminationSignal(signal) {
|
|
354
|
+
api_1.diag.debug(`${signal} received, shutting down...`);
|
|
355
|
+
_shutdown().finally(() => process.exit());
|
|
356
|
+
}
|
|
357
|
+
if (stopOnTerminationSignals) {
|
|
358
|
+
process.on('SIGTERM', () => {
|
|
359
|
+
handleTerminationSignal('SIGTERM');
|
|
360
|
+
});
|
|
361
|
+
process.on('SIGINT', () => {
|
|
362
|
+
handleTerminationSignal('SIGINT');
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
if (defaultAdvancedNetworkCapture) {
|
|
366
|
+
ui.succeed(`Enabled Advanced Network Capture`);
|
|
367
|
+
}
|
|
368
|
+
if (defaultBetaMode) {
|
|
369
|
+
ui.succeed(`Enabled Beta Mode`);
|
|
370
|
+
}
|
|
371
|
+
if (defaultConsoleCapture) {
|
|
372
|
+
ui.succeed(`Enabled Console Capture`);
|
|
373
|
+
}
|
|
374
|
+
if (defaultDetectResources) {
|
|
375
|
+
ui.succeed(`Enabled Resource Detection`);
|
|
376
|
+
}
|
|
377
|
+
if (defaultExceptionCapture) {
|
|
378
|
+
ui.succeed(`Enabled Exception Capture`);
|
|
379
|
+
}
|
|
380
|
+
if (constants_1.DEFAULT_OTEL_LOG_LEVEL) {
|
|
381
|
+
ui.succeed(`Enable SDK Logger with Level "${constants_1.DEFAULT_OTEL_LOG_LEVEL}"`);
|
|
382
|
+
}
|
|
383
|
+
if (config.programmaticImports) {
|
|
384
|
+
ui.succeed(`Enabled Programmatic Imports`);
|
|
385
|
+
}
|
|
386
|
+
if (env.OTEL_PROPAGATORS) {
|
|
387
|
+
ui.succeed(`Using Propagators: "${env.OTEL_PROPAGATORS}"`);
|
|
388
|
+
}
|
|
389
|
+
if (env.OTEL_RESOURCE_ATTRIBUTES) {
|
|
390
|
+
ui.succeed(`Resource Attributes: "${env.OTEL_RESOURCE_ATTRIBUTES}"`);
|
|
391
|
+
}
|
|
392
|
+
if (env.OTEL_NODE_RESOURCE_DETECTORS) {
|
|
393
|
+
ui.succeed(`Resource Detectors: "${env.OTEL_NODE_RESOURCE_DETECTORS}"`);
|
|
394
|
+
}
|
|
395
|
+
if (constants_1.DEFAULT_OTEL_TRACES_SAMPLER) {
|
|
396
|
+
ui.succeed(`Traces Sampler: "${constants_1.DEFAULT_OTEL_TRACES_SAMPLER}"`);
|
|
397
|
+
}
|
|
398
|
+
if (defaultSentryIntegrationEnabled) {
|
|
399
|
+
ui.succeed(`Enabled Sentry Integration`);
|
|
400
|
+
}
|
|
401
|
+
if (stopOnTerminationSignals) {
|
|
402
|
+
ui.succeed('Enabled stopOnTerminationSignals');
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
ui.warn('Disabled stopOnTerminationSignals (user is responsible for graceful shutdown on termination signals)');
|
|
406
|
+
}
|
|
407
|
+
if (defaultDisableLogs) {
|
|
408
|
+
ui.warn('Logs are disabled');
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
ui.succeed(`Sending logs to "${_logger.getExporterUrl()}"`);
|
|
412
|
+
}
|
|
413
|
+
if (defaultDisableMetrics) {
|
|
414
|
+
ui.warn('Metrics are disabled');
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
ui.succeed(`Sending metrics to "${constants_1.DEFAULT_OTEL_METRICS_EXPORTER_URL}"`);
|
|
418
|
+
}
|
|
419
|
+
if (defaultDisableTracing) {
|
|
420
|
+
ui.warn('Tracing is disabled');
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
ui.succeed(`Sending traces to "${constants_1.DEFAULT_OTEL_TRACES_EXPORTER_URL}"`);
|
|
424
|
+
}
|
|
425
|
+
ui.stopAndPersist({
|
|
426
|
+
text: `OpenTelemetry SDK initialized successfully`,
|
|
427
|
+
symbol: '🦄',
|
|
428
|
+
});
|
|
429
|
+
if (!defaultDisableStartupLogs) {
|
|
430
|
+
setTimeout(() => {
|
|
431
|
+
const _targetUrl = `https://sophonz.com/services?service=${encodeURIComponent(defaultServiceName)}`;
|
|
432
|
+
ui.info(`
|
|
433
|
+
|
|
434
|
+
View your app dashboard here:
|
|
435
|
+
${_targetUrl}
|
|
436
|
+
To disable these startup logs, set SOPHONZ_STARTUP_LOGS=false
|
|
437
|
+
|
|
438
|
+
`);
|
|
439
|
+
}, 1000);
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
exports.initSDK = initSDK;
|
|
443
|
+
const init = (config) => (0, exports.initSDK)({
|
|
444
|
+
consoleCapture: true,
|
|
445
|
+
experimentalExceptionCapture: true,
|
|
446
|
+
programmaticImports: true,
|
|
447
|
+
sentryIntegrationEnabled: true,
|
|
448
|
+
...config,
|
|
449
|
+
});
|
|
450
|
+
exports.init = init;
|
|
451
|
+
const _shutdown = () => {
|
|
452
|
+
var _a, _b;
|
|
453
|
+
const ui = (0, ora_1.default)({
|
|
454
|
+
spinner: cli_spinners_1.default.dots,
|
|
455
|
+
text: 'Shutting down OpenTelemetry SDK...',
|
|
456
|
+
}).start();
|
|
457
|
+
return ((_b = (_a = sdk === null || sdk === void 0 ? void 0 : sdk.shutdown()) === null || _a === void 0 ? void 0 : _a.then(() => {
|
|
458
|
+
ui.succeed('OpenTelemetry SDK shut down successfully');
|
|
459
|
+
}, (err) => {
|
|
460
|
+
ui.fail(`Error shutting down OpenTeLoader SDK: ${err}`);
|
|
461
|
+
})) !== null && _b !== void 0 ? _b : Promise.resolve());
|
|
462
|
+
};
|
|
463
|
+
const shutdown = () => {
|
|
464
|
+
api_1.diag.debug('shutdown() called');
|
|
465
|
+
return _shutdown();
|
|
466
|
+
};
|
|
467
|
+
exports.shutdown = shutdown;
|
|
468
|
+
const setTraceAttributes = (attributes) => {
|
|
469
|
+
if (contextManager &&
|
|
470
|
+
typeof contextManager.getMutableContext === 'function') {
|
|
471
|
+
const mutableContext = contextManager.getMutableContext();
|
|
472
|
+
if (mutableContext != null) {
|
|
473
|
+
if (mutableContext.traceAttributes == null) {
|
|
474
|
+
mutableContext.traceAttributes = new Map();
|
|
475
|
+
}
|
|
476
|
+
for (const [k, v] of Object.entries(attributes)) {
|
|
477
|
+
mutableContext.traceAttributes.set(k, v);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
exports.setTraceAttributes = setTraceAttributes;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Context } from '@opentelemetry/api';
|
|
2
|
+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
|
|
3
|
+
import { BatchSpanProcessor, Span } from '@opentelemetry/sdk-trace-base';
|
|
4
|
+
import type { MutableAsyncLocalStorageContextManager } from './MutableAsyncLocalStorageContextManager';
|
|
5
|
+
export default class SophonzSpanProcessor extends BatchSpanProcessor {
|
|
6
|
+
private readonly enableSophonzGlobalContext;
|
|
7
|
+
private readonly contextManager;
|
|
8
|
+
constructor({ exporter, enableSophonzGlobalContext, contextManager, }: {
|
|
9
|
+
exporter: OTLPTraceExporter;
|
|
10
|
+
enableSophonzGlobalContext: boolean;
|
|
11
|
+
contextManager?: MutableAsyncLocalStorageContextManager;
|
|
12
|
+
});
|
|
13
|
+
private _setTraceAttributes;
|
|
14
|
+
onStart(_span: Span, _parentContext: Context): void;
|
|
15
|
+
onEnd(_span: Span): void;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=spanProcessor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spanProcessor.d.ts","sourceRoot":"","sources":["../../src/spanProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AAEzE,OAAO,KAAK,EAAE,sCAAsC,EAAE,MAAM,0CAA0C,CAAC;AAEvG,MAAM,CAAC,OAAO,OAAO,oBAAqB,SAAQ,kBAAkB;IAClE,OAAO,CAAC,QAAQ,CAAC,0BAA0B,CAAU;IACrD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAEjB;gBAEF,EACV,QAAQ,EACR,0BAA0B,EAC1B,cAAc,GACf,EAAE;QACD,QAAQ,EAAE,iBAAiB,CAAC;QAC5B,0BAA0B,EAAE,OAAO,CAAC;QACpC,cAAc,CAAC,EAAE,sCAAsC,CAAC;KACzD;IAMD,OAAO,CAAC,mBAAmB;IAyB3B,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,GAAG,IAAI;IAQnD,KAAK,CAAC,KAAK,EAAE,IAAI,GAAG,IAAI;CAYzB"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
4
|
+
class SophonzSpanProcessor extends sdk_trace_base_1.BatchSpanProcessor {
|
|
5
|
+
constructor({ exporter, enableSophonzGlobalContext, contextManager, }) {
|
|
6
|
+
super(exporter);
|
|
7
|
+
this.enableSophonzGlobalContext = enableSophonzGlobalContext;
|
|
8
|
+
this.contextManager = contextManager;
|
|
9
|
+
}
|
|
10
|
+
_setTraceAttributes(span) {
|
|
11
|
+
if (this.enableSophonzGlobalContext &&
|
|
12
|
+
this.contextManager != null &&
|
|
13
|
+
typeof this.contextManager.getMutableContext === 'function') {
|
|
14
|
+
const spanAttributes = span.attributes;
|
|
15
|
+
const mutableContext = this.contextManager.getMutableContext();
|
|
16
|
+
const traceAttributes = mutableContext === null || mutableContext === void 0 ? void 0 : mutableContext.traceAttributes;
|
|
17
|
+
if (traceAttributes != null) {
|
|
18
|
+
traceAttributes.forEach((value, key) => {
|
|
19
|
+
if (spanAttributes[key] == null) {
|
|
20
|
+
span.setAttribute(key, value);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
onStart(_span, _parentContext) {
|
|
27
|
+
this._setTraceAttributes(_span);
|
|
28
|
+
super.onStart(_span, _parentContext);
|
|
29
|
+
}
|
|
30
|
+
onEnd(_span) {
|
|
31
|
+
_span._ended = false;
|
|
32
|
+
this._setTraceAttributes(_span);
|
|
33
|
+
_span._ended = true;
|
|
34
|
+
super.onEnd(_span);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.default = SophonzSpanProcessor;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracing.d.ts","sourceRoot":"","sources":["../../src/tracing.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY,GAAI,SAAI,WAQhC,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,aAAa,MAAM,GAAG,SAAS,YAe9D,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringToBoolean = exports.jsonToString = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const api_1 = require("@opentelemetry/api");
|
|
6
|
+
const json_stringify_safe_1 = tslib_1.__importDefault(require("json-stringify-safe"));
|
|
7
|
+
const jsonToString = (json) => {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.stringify(json);
|
|
10
|
+
}
|
|
11
|
+
catch (ex) {
|
|
12
|
+
api_1.diag.debug(`Failed to stringify json. e = ${ex}`);
|
|
13
|
+
return (0, json_stringify_safe_1.default)(json);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
exports.jsonToString = jsonToString;
|
|
17
|
+
const stringToBoolean = (stringValue) => {
|
|
18
|
+
var _a;
|
|
19
|
+
switch ((_a = stringValue === null || stringValue === void 0 ? void 0 : stringValue.toLowerCase()) === null || _a === void 0 ? void 0 : _a.trim()) {
|
|
20
|
+
case 'true':
|
|
21
|
+
case 'yes':
|
|
22
|
+
case '1':
|
|
23
|
+
return true;
|
|
24
|
+
case 'false':
|
|
25
|
+
case 'no':
|
|
26
|
+
case '0':
|
|
27
|
+
return false;
|
|
28
|
+
default:
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
exports.stringToBoolean = stringToBoolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,UAAU,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sophonz/node-sdk",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"homepage": "https://sophonz.com",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/sophonz-labs/sophonz-js.git"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@opentelemetry/api": "^1.9.1",
|
|
15
|
+
"@opentelemetry/api-logs": "^0.217.0",
|
|
16
|
+
"@opentelemetry/auto-instrumentations-node": "^0.75.0",
|
|
17
|
+
"@opentelemetry/core": "^2.7.1",
|
|
18
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.217.0",
|
|
19
|
+
"@opentelemetry/exporter-metrics-otlp-proto": "^0.217.0",
|
|
20
|
+
"@opentelemetry/exporter-trace-otlp-proto": "^0.217.0",
|
|
21
|
+
"@opentelemetry/instrumentation": "^0.217.0",
|
|
22
|
+
"@opentelemetry/instrumentation-http": "^0.217.0",
|
|
23
|
+
"@opentelemetry/instrumentation-runtime-node": "^0.30.0",
|
|
24
|
+
"@opentelemetry/resources": "^2.7.1",
|
|
25
|
+
"@opentelemetry/sdk-logs": "^0.217.0",
|
|
26
|
+
"@opentelemetry/sdk-metrics": "^2.7.1",
|
|
27
|
+
"@opentelemetry/sdk-node": "^0.217.0",
|
|
28
|
+
"@opentelemetry/sdk-trace-base": "~2.7.1",
|
|
29
|
+
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
30
|
+
"@sophonz/instrumentation-console": "*",
|
|
31
|
+
"@sophonz/instrumentation-node": "*",
|
|
32
|
+
"@sophonz/instrumentation-node-exception": "*",
|
|
33
|
+
"cli-spinners": "^3.4.0",
|
|
34
|
+
"json-stringify-safe": "^5.0.1",
|
|
35
|
+
"lodash.isobject": "^3.0.2",
|
|
36
|
+
"lodash.isplainobject": "^4.0.6",
|
|
37
|
+
"lodash.isstring": "^4.0.1",
|
|
38
|
+
"node-fetch": "^3.3.2",
|
|
39
|
+
"ora": "^9.4.0",
|
|
40
|
+
"pino-abstract-transport": "^3.0.0",
|
|
41
|
+
"semver": "^7.8.0",
|
|
42
|
+
"shimmer": "^1.2.1",
|
|
43
|
+
"tslib": "^2.8.1",
|
|
44
|
+
"winston": "^3.19.0",
|
|
45
|
+
"winston-transport": "^4.9.0"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"build/*"
|
|
49
|
+
],
|
|
50
|
+
"main": "build/src/index.js",
|
|
51
|
+
"bin": {
|
|
52
|
+
"opentelemetry-instrument": "build/bin/opentelemetry-instrument.js"
|
|
53
|
+
},
|
|
54
|
+
"types": "build/src/index.d.ts",
|
|
55
|
+
"exports": {
|
|
56
|
+
".": {
|
|
57
|
+
"types": "./build/src/index.d.ts",
|
|
58
|
+
"require": "./build/src/index.js",
|
|
59
|
+
"default": "./build/src/index.js"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|