@vizydrop/tracer 2.6.5 → 3.0.0

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/package.json CHANGED
@@ -1,13 +1,21 @@
1
1
  {
2
2
  "name": "@vizydrop/tracer",
3
3
  "license": "MIT",
4
- "version": "2.6.5",
4
+ "version": "3.0.0",
5
5
  "scripts": {
6
6
  "test": "jest",
7
7
  "lint": "eslint --quiet ."
8
8
  },
9
9
  "dependencies": {
10
- "elastic-apm-node": "4.9.0",
10
+ "@opentelemetry/api": "^1.9.0",
11
+ "@opentelemetry/auto-instrumentations-node": "^0.55.3",
12
+ "@opentelemetry/exporter-metrics-otlp-proto": "^0.57.1",
13
+ "@opentelemetry/exporter-trace-otlp-proto": "^0.57.1",
14
+ "@opentelemetry/resources": "^1.30.1",
15
+ "@opentelemetry/sdk-metrics": "^1.30.1",
16
+ "@opentelemetry/sdk-node": "^0.57.1",
17
+ "@opentelemetry/sdk-trace-node": "^1.30.1",
18
+ "@opentelemetry/semantic-conventions": "^1.28.0",
11
19
  "lodash": "4.17.21"
12
20
  },
13
21
  "devDependencies": {
@@ -1,14 +1,21 @@
1
- const captureCorrelationIdMiddleware = (agent, getCorrelationId) => {
1
+ const { trace } = require(`@opentelemetry/api`);
2
+
3
+ const captureCorrelationIdMiddleware = (getCorrelationId) => {
2
4
  if (!getCorrelationId && typeof getCorrelationId !== `function`) {
3
5
  throw new Error(`getCorrelationId expect to be a function`);
4
6
  }
5
7
 
6
8
  return (req, res, next) => {
7
- if (agent) {
8
- agent.setLabel(`correlationId`, getCorrelationId(req));
9
+ const currentSpan = trace.getActiveSpan();
10
+
11
+ if (currentSpan) {
12
+ currentSpan.setAttribute(`correlationId`, getCorrelationId(req));
9
13
  }
14
+
10
15
  next();
11
16
  };
12
17
  };
13
18
 
14
- module.exports = {captureCorrelationIdMiddleware};
19
+ module.exports = {
20
+ captureCorrelationIdMiddleware
21
+ };
@@ -1,12 +1,17 @@
1
- const captureCorrelationIdMiddleware = (agent, getCorrelationId) => {
1
+ const { trace } = require(`@opentelemetry/api`);
2
+
3
+ const captureCorrelationIdMiddleware = (getCorrelationId) => {
2
4
  if (!getCorrelationId && typeof getCorrelationId !== `function`) {
3
5
  throw new Error(`getCorrelationId expect to be a function`);
4
6
  }
5
7
 
6
8
  return async (ctx, next) => {
7
- if (agent) {
8
- agent.setLabel(`correlationId`, getCorrelationId(ctx.request));
9
+ const currentSpan = trace.getActiveSpan();
10
+
11
+ if (currentSpan) {
12
+ currentSpan.setAttribute(`correlationId`, getCorrelationId(ctx.request));
9
13
  }
14
+
10
15
  await next();
11
16
  };
12
17
  };
@@ -1,11 +1,5 @@
1
1
  const _ = require(`lodash/fp`);
2
2
 
3
- function removeNameDoubleSlash(payload) {
4
- // eslint-disable-next-line no-param-reassign
5
- payload.name = payload.name.replace(/\/\//g, `/`);
6
- return payload;
7
- }
8
-
9
3
  function replaceSensitiveData(text) {
10
4
  if (!text) {
11
5
  return text;
@@ -32,34 +26,9 @@ function replaceSensitiveData(text) {
32
26
  }, text);
33
27
  }
34
28
 
35
- function stripSensitiveData(payload) {
36
- const urlData = _.get(`context.request.url`, payload);
37
- if (urlData) {
38
- Object.entries(urlData).forEach(([key, value]) => {
39
- urlData[key] = replaceSensitiveData(value);
40
- });
41
- }
42
-
43
- return payload;
44
- }
45
-
46
- function skipOptionsRequest(payload) {
47
- const method = _.get(`context.request.method`, payload);
48
- return method === `OPTIONS` ? null : payload;
49
- }
50
-
51
- function requiresRequestType(fn) {
52
- return (payload) => {
53
- if (payload && payload.type === `request`) {
54
- return fn(payload);
55
- }
56
-
57
- return payload;
58
- };
59
- }
29
+ const buildIgnoredUrlMatcher = ignoreUrls => url => ignoreUrls.some(ignored => url.match(ignored));
60
30
 
61
31
  module.exports = {
62
- removeNameDoubleSlash: requiresRequestType(removeNameDoubleSlash),
63
- stripSensitiveData: requiresRequestType(stripSensitiveData),
64
- skipOptionsRequest: requiresRequestType(skipOptionsRequest),
32
+ replaceSensitiveData,
33
+ buildIgnoredUrlMatcher
65
34
  };
package/src/tracer.js CHANGED
@@ -1,63 +1,82 @@
1
- const APM = require(`elastic-apm-node`);
2
- const requestFilters = require(`./requestFilters`);
1
+ const { trace } = require(`@opentelemetry/api`);
2
+ const { NodeSDK } = require(`@opentelemetry/sdk-node`);
3
+ const { ConsoleSpanExporter } = require(`@opentelemetry/sdk-trace-node`);
4
+ const { Resource } = require(`@opentelemetry/resources`);
5
+ const { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } = require(`@opentelemetry/semantic-conventions`);
6
+ const { getNodeAutoInstrumentations } = require(`@opentelemetry/auto-instrumentations-node`);
7
+ const { OTLPTraceExporter } = require(`@opentelemetry/exporter-trace-otlp-proto`);
8
+ const { OTLPMetricExporter } = require(`@opentelemetry/exporter-metrics-otlp-proto`);
9
+ const { PeriodicExportingMetricReader, ConsoleMetricExporter } = require(`@opentelemetry/sdk-metrics`);
10
+ const { replaceSensitiveData, buildIgnoredUrlMatcher } = require(`./requestFilters`);
11
+
12
+ const addDefaultOptions = overrides => ({
13
+ metricsIntervalMillis: 1000,
14
+ ignoreUrls: [
15
+ `/api/v1/status`,
16
+ /\/assets\/.*/,
17
+ /\/favicon.*/,
18
+ `/status`,
19
+ ],
20
+ ...overrides,
21
+ });
22
+
23
+ const createOpenTelemetryAgent = ({
24
+ serviceName,
25
+ serviceVersion,
26
+ metricsIntervalMillis,
27
+ ignoreUrls,
28
+ flushToConsole,
29
+ traceExporterOptions,
30
+ metricExporterOptions
31
+ }) => {
32
+ const matchIgnoredUrl = buildIgnoredUrlMatcher(ignoreUrls);
33
+ const traceExporter = flushToConsole ?
34
+ new ConsoleSpanExporter() :
35
+ new OTLPTraceExporter(traceExporterOptions);
36
+ const metricReader = flushToConsole ?
37
+ new PeriodicExportingMetricReader({
38
+ exporter: new ConsoleMetricExporter(),
39
+ metricsIntervalMillis
40
+ }) :
41
+ new PeriodicExportingMetricReader({ exporter: new OTLPMetricExporter(metricExporterOptions) });
42
+
43
+ const agent = new NodeSDK({
44
+ resource: new Resource({
45
+ [ATTR_SERVICE_NAME]: serviceName,
46
+ [ATTR_SERVICE_VERSION]: serviceVersion
47
+ }),
48
+ traceExporter,
49
+ metricReader,
50
+ instrumentations: [
51
+ getNodeAutoInstrumentations({
52
+ '@opentelemetry/instrumentation-http': {
53
+ ignoreIncomingRequestHook: ({ url }) => matchIgnoredUrl(url),
54
+ ignoreOutgoingRequestHook: ({ path }) => matchIgnoredUrl(path),
55
+ requestHook: (span, request) => {
56
+ if (span) {
57
+ span.updateName(replaceSensitiveData(request.url));
58
+ }
59
+ }
60
+ }
61
+ })
62
+ ]
63
+ });
64
+
65
+ agent.start();
3
66
 
4
- function createConfig(overrides) {
5
67
  return {
6
- // metrics
7
- metricsInterval: 0,
8
- breakdownMetrics: false,
9
-
10
- centralConfig: false,
11
-
12
- // request processing
13
- usePathAsTransactionName: false,
14
- captureBody: `off`,
15
- captureHeaders: false,
16
- ignoreUrls: [
17
- `/api/v1/status`,
18
- /\/assets\/.*/,
19
- /\/favicon.*/,
20
- `/status`,
21
- ],
22
-
23
- // stack traces, erros,
24
- errorOnAbortedRequests: false,
25
- captureErrorLogStackTraces: false,
26
- captureSpanStackTraces: false,
27
- stackTraceLimit: 0,
28
- captureExceptions: false,
29
- logUncaughtExceptions: false,
30
-
31
- active: true,
32
-
33
- // overrides
34
- ...overrides,
68
+ shutdown: () => agent.shutdown()
35
69
  };
36
- }
70
+ };
71
+
72
+ function createTracer({ openTelemetryOptions }) {
73
+ const config = addDefaultOptions(openTelemetryOptions);
74
+ const agent = createOpenTelemetryAgent(config);
37
75
 
38
- function createTracer(opts) {
39
- const conf = createConfig(opts);
40
-
41
- const agent = APM.start(conf);
42
-
43
- agent.addFilter(requestFilters.skipOptionsRequest);
44
- agent.addFilter(requestFilters.removeNameDoubleSlash);
45
- agent.addFilter(requestFilters.stripSensitiveData);
46
-
47
- if (
48
- conf.active &&
49
- conf.logger &&
50
- conf.logger.additionalFields &&
51
- typeof conf.logger.additionalFields.add === `function`
52
- ) {
53
- conf.logger.additionalFields.add({
54
- "trace.id": () => agent.currentTraceIds[`trace.id`],
55
- "transaction.id": () => agent.currentTraceIds[`transaction.id`],
56
- "span.id": () => agent.currentTraceIds[`span.id`],
57
- });
58
- }
59
-
60
- return agent;
76
+ return {
77
+ trace,
78
+ agent
79
+ };
61
80
  }
62
81
 
63
82
  module.exports = {createTracer};
package/version.txt CHANGED
@@ -1 +1 @@
1
- 2.6.5
1
+ 3.0.0