@hvedinich/utils 0.0.76 → 0.0.79

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/index.js CHANGED
@@ -3,6 +3,7 @@ const Logger = require('./src/log');
3
3
  const utils = require('./src/utils');
4
4
  const error = require('./src/error');
5
5
  const server = require('./src/server');
6
+ const { initOpenTelemetry } = require('./src/observability/otel');
6
7
 
7
8
  module.exports = {
8
9
  constants,
@@ -10,4 +11,7 @@ module.exports = {
10
11
  utils,
11
12
  error,
12
13
  server,
14
+ observability: {
15
+ initOpenTelemetry,
16
+ },
13
17
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hvedinich/utils",
3
- "version": "0.0.76",
3
+ "version": "0.0.79",
4
4
  "description": "utils module",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,10 +17,16 @@
17
17
  },
18
18
  "homepage": "https://github.com/hvedinich/utils#readme",
19
19
  "dependencies": {
20
+ "@opentelemetry/api": "^1.9.0",
21
+ "@opentelemetry/auto-instrumentations-node": "^0.52.1",
22
+ "@opentelemetry/exporter-trace-otlp-grpc": "^0.52.1",
23
+ "@opentelemetry/resources": "^1.28.0",
24
+ "@opentelemetry/sdk-node": "^0.52.1",
25
+ "@opentelemetry/semantic-conventions": "^1.28.0",
20
26
  "apollo-server-express": "2.17.0",
21
27
  "axios": "^0.27.2",
22
- "express": "4.17.1",
23
28
  "cors": "2.8.5",
29
+ "express": "4.17.1",
24
30
  "graphql-playground-middleware-express": "1.7.20",
25
31
  "jsonwebtoken": "8.5.1",
26
32
  "pino": "^9.5.0"
@@ -39,5 +45,8 @@
39
45
  },
40
46
  "publishConfig": {
41
47
  "access": "public"
48
+ },
49
+ "overrides": {
50
+ "graphql": "15.8.0"
42
51
  }
43
52
  }
package/src/log/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const pino = require('pino');
2
+ const otelApi = require('@opentelemetry/api');
2
3
 
3
4
  const ctxParser = ctx => {
4
5
  if (!ctx) return { uid: 'unknown' };
@@ -16,17 +17,39 @@ const errorParser = error => {
16
17
  };
17
18
  };
18
19
 
20
+ const getTraceContext = () => {
21
+ try {
22
+ const activeSpan = otelApi.trace.getSpan(otelApi.context.active());
23
+ if (!activeSpan) return {};
24
+
25
+ const spanContext = activeSpan.spanContext();
26
+ if (!spanContext || !spanContext.traceId) return {};
27
+
28
+ return {
29
+ trace_id: spanContext.traceId,
30
+ span_id: spanContext.spanId,
31
+ };
32
+ } catch (error) {
33
+ console.error('[OTEL] Error extracting trace context:', error.message);
34
+ return {};
35
+ }
36
+ };
37
+
19
38
  const PinoLevelToSeverityLookup = {
20
39
  debug: 'DEBUG',
21
40
  info: 'INFO',
22
41
  error: 'ERROR',
23
42
  };
43
+
24
44
  module.exports = class Logger {
25
45
  constructor(name, environment) {
26
46
  this.logger = pino({
27
47
  name,
28
48
  mixin() {
29
- return { environment };
49
+ return {
50
+ environment,
51
+ ...getTraceContext(),
52
+ };
30
53
  },
31
54
  level: 'debug',
32
55
  timestamp: pino.stdTimeFunctions.isoTime,
@@ -0,0 +1,55 @@
1
+ const { NodeSDK } = require('@opentelemetry/sdk-node');
2
+ const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
3
+ const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
4
+ const { Resource } = require('@opentelemetry/resources');
5
+ const { ATTR_SERVICE_NAME } = require('@opentelemetry/semantic-conventions');
6
+
7
+ function initOpenTelemetry(options = {}) {
8
+ const { serviceName, otlpEndpoint, isEnabled, instrumentations = {} } = options;
9
+
10
+ if (!isEnabled || !serviceName || !otlpEndpoint) {
11
+ console.log(`[OTEL] OpenTelemetry disabled for ${serviceName}`);
12
+ return null;
13
+ }
14
+
15
+ console.log(`[OTEL] Initializing OpenTelemetry for ${serviceName}`);
16
+ console.log(`[OTEL] Exporting to ${otlpEndpoint}`);
17
+
18
+ const traceExporter = new OTLPTraceExporter({
19
+ url: otlpEndpoint,
20
+ });
21
+
22
+ const defaultInstrumentations = {
23
+ '@opentelemetry/instrumentation-http': { enabled: true },
24
+ '@opentelemetry/instrumentation-express': { enabled: true },
25
+ '@opentelemetry/instrumentation-graphql': { enabled: true },
26
+ '@opentelemetry/instrumentation-mongodb': { enabled: true },
27
+ '@opentelemetry/instrumentation-mongoose': { enabled: true },
28
+ '@opentelemetry/instrumentation-ioredis': { enabled: true },
29
+ ...instrumentations,
30
+ };
31
+
32
+ const resource = new Resource({
33
+ [ATTR_SERVICE_NAME]: serviceName,
34
+ });
35
+
36
+ const sdk = new NodeSDK({
37
+ resource,
38
+ traceExporter,
39
+ instrumentations: [getNodeAutoInstrumentations(defaultInstrumentations)],
40
+ });
41
+
42
+ try {
43
+ sdk.start();
44
+ console.log(`[OTEL] SDK started successfully for ${serviceName}`);
45
+ } catch (error) {
46
+ console.error('[OTEL] Error starting SDK', error);
47
+ return null;
48
+ }
49
+
50
+ return sdk;
51
+ }
52
+
53
+ module.exports = {
54
+ initOpenTelemetry,
55
+ };