@contello/opentelemetry 2.1.0 → 2.1.2

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/dist/index.cjs CHANGED
@@ -1,4 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _entwico_dash = require("@entwico/dash");
2
3
  let node_module = require("node:module");
3
4
  //#region src/api.ts
4
5
  function loadApi() {
@@ -79,20 +80,19 @@ function createOperationTelemetry(scope) {
79
80
  message: error instanceof Error ? error.message : String(error)
80
81
  });
81
82
  span.end();
83
+ throw error;
82
84
  };
83
85
  let result;
84
86
  try {
85
87
  result = fn();
86
88
  } catch (error) {
87
- fail(error);
88
- throw error;
89
- }
90
- if (result instanceof Promise) {
91
- result.then(() => finish(false), (error) => fail(error));
92
- return result;
89
+ return fail(error);
93
90
  }
94
- finish(true);
95
- return result;
91
+ const cached = !(result instanceof Promise);
92
+ return (0, _entwico_dash.maybeThen)(result, (value) => {
93
+ finish(cached);
94
+ return value;
95
+ }, fail);
96
96
  });
97
97
  };
98
98
  return { wrap };
@@ -118,7 +118,7 @@ function graphqlOperationAttributes(document, variables) {
118
118
  if (!otel || !otelEnv.captureQuery && !otelEnv.captureVariables) return;
119
119
  const attrs = {};
120
120
  if (otelEnv.captureQuery) attrs["graphql.document"] = document;
121
- if (otelEnv.captureVariables && variables !== void 0) attrs["graphql.variables"] = JSON.stringify(variables);
121
+ if (variables !== void 0 && otelEnv.captureVariables) attrs["graphql.variables"] = JSON.stringify(variables);
122
122
  return attrs;
123
123
  }
124
124
  /**
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  //#region src/index.d.ts
2
- type OperationAttributes = Record<string, string | number | boolean>;
3
- type OperationTelemetry = {
2
+ export type OperationAttributes = Record<string, string | number | boolean>;
3
+ export type OperationTelemetry = {
4
4
  /**
5
5
  * Runs `fn` inside an active span named `name` (convention: `category:operation`) and records
6
6
  * duration/error metrics. Sync results are attributed `cached: true`, promises `cached: false`.
@@ -8,14 +8,14 @@ type OperationTelemetry = {
8
8
  */
9
9
  wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;
10
10
  };
11
- declare function createOperationTelemetry(scope: string): OperationTelemetry;
11
+ export declare function createOperationTelemetry(scope: string): OperationTelemetry;
12
12
  /**
13
13
  * Renames the active span and/or merges attributes into it. No-op when telemetry is
14
14
  * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
15
15
  * enrich an outer span (e.g. the platform HTTP server span) with context only the
16
16
  * contello layer knows.
17
17
  */
18
- declare function updateActiveSpan(update: {
18
+ export declare function updateActiveSpan(update: {
19
19
  name?: string | undefined;
20
20
  attributes?: OperationAttributes | undefined;
21
21
  }): void;
@@ -23,12 +23,11 @@ declare function updateActiveSpan(update: {
23
23
  * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
24
24
  * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
25
25
  */
26
- declare function graphqlOperationAttributes(document: string, variables?: Record<string, unknown> | undefined): OperationAttributes | undefined;
26
+ export declare function graphqlOperationAttributes(document: string, variables?: Record<string, unknown> | undefined): OperationAttributes | undefined;
27
27
  /**
28
28
  * Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL
29
29
  * WebSocket message. Returns the message unchanged when telemetry is disabled.
30
30
  */
31
- declare function injectTraceContext(message: any): any;
31
+ export declare function injectTraceContext(message: any): any;
32
32
  //#endregion
33
- export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
34
33
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";KAKY,mBAAA,GAAsB,MAAM;AAAA,KAE5B,kBAAA;EAFmB;;;AAAS;AAExC;EAME,IAAA,MAAU,IAAA,UAAc,EAAA,QAAU,CAAA,EAAG,UAAA,GAAa,mBAAA,iBAAoC,CAAA;AAAA;AAAA,iBAaxE,wBAAA,CAAyB,KAAA,WAAgB,kBAAkB;;;;;;;iBA8E3D,gBAAA,CAAiB,MAAA;EAC/B,IAAA;EACA,UAAA,GAAa,mBAAmB;AAAA;;;;AA7FuD;iBAsHzE,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;AA5GqD;iBAkI3D,kBAAA,CAAmB,OAAY"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";YAMY,sBAAsB;YAEtB;;;;;;EAMV,OAAO,GAAG,cAAc,UAAU,GAAG,aAAa,oCAAoC;;wBAaxE,yBAAyB,gBAAgB;;;;;;;wBA0EzC,iBAAiB;EAC/B;EACA,aAAa;;;;;;wBAyBC,2BACd,kBACA,YAAY,sCACX;;;;;wBAsBa,mBAAmB"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  //#region src/index.d.ts
2
- type OperationAttributes = Record<string, string | number | boolean>;
3
- type OperationTelemetry = {
2
+ export type OperationAttributes = Record<string, string | number | boolean>;
3
+ export type OperationTelemetry = {
4
4
  /**
5
5
  * Runs `fn` inside an active span named `name` (convention: `category:operation`) and records
6
6
  * duration/error metrics. Sync results are attributed `cached: true`, promises `cached: false`.
@@ -8,14 +8,14 @@ type OperationTelemetry = {
8
8
  */
9
9
  wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;
10
10
  };
11
- declare function createOperationTelemetry(scope: string): OperationTelemetry;
11
+ export declare function createOperationTelemetry(scope: string): OperationTelemetry;
12
12
  /**
13
13
  * Renames the active span and/or merges attributes into it. No-op when telemetry is
14
14
  * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
15
15
  * enrich an outer span (e.g. the platform HTTP server span) with context only the
16
16
  * contello layer knows.
17
17
  */
18
- declare function updateActiveSpan(update: {
18
+ export declare function updateActiveSpan(update: {
19
19
  name?: string | undefined;
20
20
  attributes?: OperationAttributes | undefined;
21
21
  }): void;
@@ -23,12 +23,11 @@ declare function updateActiveSpan(update: {
23
23
  * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
24
24
  * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
25
25
  */
26
- declare function graphqlOperationAttributes(document: string, variables?: Record<string, unknown> | undefined): OperationAttributes | undefined;
26
+ export declare function graphqlOperationAttributes(document: string, variables?: Record<string, unknown> | undefined): OperationAttributes | undefined;
27
27
  /**
28
28
  * Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL
29
29
  * WebSocket message. Returns the message unchanged when telemetry is disabled.
30
30
  */
31
- declare function injectTraceContext(message: any): any;
31
+ export declare function injectTraceContext(message: any): any;
32
32
  //#endregion
33
- export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
34
33
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";KAKY,mBAAA,GAAsB,MAAM;AAAA,KAE5B,kBAAA;EAFmB;;;AAAS;AAExC;EAME,IAAA,MAAU,IAAA,UAAc,EAAA,QAAU,CAAA,EAAG,UAAA,GAAa,mBAAA,iBAAoC,CAAA;AAAA;AAAA,iBAaxE,wBAAA,CAAyB,KAAA,WAAgB,kBAAkB;;;;;;;iBA8E3D,gBAAA,CAAiB,MAAA;EAC/B,IAAA;EACA,UAAA,GAAa,mBAAmB;AAAA;;;;AA7FuD;iBAsHzE,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;AA5GqD;iBAkI3D,kBAAA,CAAmB,OAAY"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";YAMY,sBAAsB;YAEtB;;;;;;EAMV,OAAO,GAAG,cAAc,UAAU,GAAG,aAAa,oCAAoC;;wBAaxE,yBAAyB,gBAAgB;;;;;;;wBA0EzC,iBAAiB;EAC/B;EACA,aAAa;;;;;;wBAyBC,2BACd,kBACA,YAAY,sCACX;;;;;wBAsBa,mBAAmB"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createRequire } from "node:module";
2
+ import { maybeThen } from "@entwico/dash";
2
3
  //#region src/api.ts
3
4
  function loadApi() {
4
5
  try {
@@ -78,20 +79,19 @@ function createOperationTelemetry(scope) {
78
79
  message: error instanceof Error ? error.message : String(error)
79
80
  });
80
81
  span.end();
82
+ throw error;
81
83
  };
82
84
  let result;
83
85
  try {
84
86
  result = fn();
85
87
  } catch (error) {
86
- fail(error);
87
- throw error;
88
- }
89
- if (result instanceof Promise) {
90
- result.then(() => finish(false), (error) => fail(error));
91
- return result;
88
+ return fail(error);
92
89
  }
93
- finish(true);
94
- return result;
90
+ const cached = !(result instanceof Promise);
91
+ return maybeThen(result, (value) => {
92
+ finish(cached);
93
+ return value;
94
+ }, fail);
95
95
  });
96
96
  };
97
97
  return { wrap };
@@ -117,7 +117,7 @@ function graphqlOperationAttributes(document, variables) {
117
117
  if (!otel || !otelEnv.captureQuery && !otelEnv.captureVariables) return;
118
118
  const attrs = {};
119
119
  if (otelEnv.captureQuery) attrs["graphql.document"] = document;
120
- if (otelEnv.captureVariables && variables !== void 0) attrs["graphql.variables"] = JSON.stringify(variables);
120
+ if (variables !== void 0 && otelEnv.captureVariables) attrs["graphql.variables"] = JSON.stringify(variables);
121
121
  return attrs;
122
122
  }
123
123
  /**
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/api.ts","../src/env.ts","../src/index.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport type * as otelApi from '@opentelemetry/api';\n\nexport type OtelApi = typeof otelApi;\n\nfunction loadApi(): OtelApi | undefined {\n try {\n return createRequire(import.meta.url)('@opentelemetry/api') as OtelApi;\n } catch {\n // @opentelemetry/api is not installed — telemetry stays disabled\n return undefined;\n }\n}\n\nexport const api = loadApi();\n","function envFlag(name: string, defaultValue: boolean): boolean {\n const raw = process.env[name];\n\n if (raw === undefined) {\n return defaultValue;\n }\n\n const value = raw.trim().toLowerCase();\n\n if (value === '') {\n return defaultValue;\n }\n\n return !['false', '0', 'no', 'off'].includes(value);\n}\n\nexport const otelEnv = {\n enabled: envFlag('OTEL_CONTELLO_ENABLED', true),\n captureQuery: envFlag('OTEL_CONTELLO_CAPTURE_QUERY', false),\n captureVariables: envFlag('OTEL_CONTELLO_CAPTURE_VARIABLES', false),\n} as const;\n","import { api } from './api';\nimport { otelEnv } from './env';\n\nconst otel = otelEnv.enabled ? api : undefined;\n\nexport type OperationAttributes = Record<string, string | number | boolean>;\n\nexport type OperationTelemetry = {\n /**\n * Runs `fn` inside an active span named `name` (convention: `category:operation`) and records\n * duration/error metrics. Sync results are attributed `cached: true`, promises `cached: false`.\n * Zero-overhead pass-through when `@opentelemetry/api` is absent or `OTEL_CONTELLO_ENABLED=false`.\n */\n wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;\n};\n\nfunction parseOperationAttributes(name: string): OperationAttributes {\n const colonIndex = name.indexOf(':');\n\n if (colonIndex === -1) {\n return { category: 'unknown', operation: name };\n }\n\n return { category: name.slice(0, colonIndex), operation: name.slice(colonIndex + 1) };\n}\n\nexport function createOperationTelemetry(scope: string): OperationTelemetry {\n if (!otel) {\n return { wrap: (_name, fn) => fn() };\n }\n\n const { SpanStatusCode } = otel;\n const tracer = otel.trace.getTracer(scope);\n const meter = otel.metrics.getMeter(scope);\n\n const duration = meter.createHistogram('contello.operation.duration', {\n description: 'duration of contello operations',\n unit: 'ms',\n });\n\n const errors = meter.createCounter('contello.operation.error', {\n description: 'number of failed contello operations',\n });\n\n const wrap = <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined): T => {\n const attrs = { ...parseOperationAttributes(name), ...attributes };\n\n return tracer.startActiveSpan(name, { attributes: attrs }, (span) => {\n const start = performance.now();\n\n const finish = (cached: boolean) => {\n span.setAttribute('cached', cached);\n duration.record(performance.now() - start, { ...attrs, cached });\n span.end();\n };\n\n const fail = (error: unknown) => {\n duration.record(performance.now() - start, { ...attrs, cached: false });\n errors.add(1, attrs);\n span.recordException(error as Error);\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error instanceof Error ? error.message : String(error),\n });\n span.end();\n };\n\n let result: T;\n\n try {\n result = fn();\n } catch (error) {\n fail(error);\n\n throw error;\n }\n\n if (result instanceof Promise) {\n // passive observer: the caller keeps the original promise (no extra hop);\n // registered before returning, so finish/fail run ahead of the caller's continuation\n // eslint-disable-next-line unicorn/prefer-await\n result.then(\n () => finish(false),\n (error: unknown) => fail(error),\n );\n\n return result;\n }\n\n finish(true);\n\n return result;\n });\n };\n\n return { wrap };\n}\n\n/**\n * Renames the active span and/or merges attributes into it. No-op when telemetry is\n * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to\n * enrich an outer span (e.g. the platform HTTP server span) with context only the\n * contello layer knows.\n */\nexport function updateActiveSpan(update: {\n name?: string | undefined;\n attributes?: OperationAttributes | undefined;\n}): void {\n if (!otel) {\n return;\n }\n\n const span = otel.trace.getActiveSpan();\n\n if (!span?.isRecording()) {\n return;\n }\n\n if (update.attributes) {\n span.setAttributes(update.attributes);\n }\n\n if (update.name) {\n span.updateName(update.name);\n }\n}\n\n/**\n * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and\n * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).\n */\nexport function graphqlOperationAttributes(\n document: string,\n variables?: Record<string, unknown> | undefined,\n): OperationAttributes | undefined {\n if (!otel || (!otelEnv.captureQuery && !otelEnv.captureVariables)) {\n return undefined;\n }\n\n const attrs: OperationAttributes = {};\n\n if (otelEnv.captureQuery) {\n attrs['graphql.document'] = document;\n }\n\n if (otelEnv.captureVariables && variables !== undefined) {\n attrs['graphql.variables'] = JSON.stringify(variables);\n }\n\n return attrs;\n}\n\n/**\n * Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL\n * WebSocket message. Returns the message unchanged when telemetry is disabled.\n */\nexport function injectTraceContext(message: any): any {\n if (!otel) {\n return message;\n }\n\n const carrier: Record<string, string> = {};\n\n otel.propagation.inject(otel.context.active(), carrier);\n\n if (carrier['traceparent']) {\n message.traceparent = carrier['traceparent'];\n }\n\n if (carrier['tracestate']) {\n message.tracestate = carrier['tracestate'];\n }\n\n return message;\n}\n"],"mappings":";;AAKA,SAAS,UAA+B;CACtC,IAAI;EACF,OAAO,cAAc,OAAO,KAAK,GAAG,CAAC,CAAC,oBAAoB;CAC5D,QAAQ;EAEN;CACF;AACF;AAEA,MAAa,MAAM,QAAQ;;;ACd3B,SAAS,QAAQ,MAAc,cAAgC;CAC7D,MAAM,MAAM,QAAQ,IAAI;CAExB,IAAI,QAAQ,KAAA,GACV,OAAO;CAGT,MAAM,QAAQ,IAAI,KAAK,CAAC,CAAC,YAAY;CAErC,IAAI,UAAU,IACZ,OAAO;CAGT,OAAO,CAAC;EAAC;EAAS;EAAK;EAAM;CAAK,CAAC,CAAC,SAAS,KAAK;AACpD;AAEA,MAAa,UAAU;CACrB,SAAS,QAAQ,yBAAyB,IAAI;CAC9C,cAAc,QAAQ,+BAA+B,KAAK;CAC1D,kBAAkB,QAAQ,mCAAmC,KAAK;AACpE;;;ACjBA,MAAM,OAAO,QAAQ,UAAU,MAAM,KAAA;AAarC,SAAS,yBAAyB,MAAmC;CACnE,MAAM,aAAa,KAAK,QAAQ,GAAG;CAEnC,IAAI,eAAe,IACjB,OAAO;EAAE,UAAU;EAAW,WAAW;CAAK;CAGhD,OAAO;EAAE,UAAU,KAAK,MAAM,GAAG,UAAU;EAAG,WAAW,KAAK,MAAM,aAAa,CAAC;CAAE;AACtF;AAEA,SAAgB,yBAAyB,OAAmC;CAC1E,IAAI,CAAC,MACH,OAAO,EAAE,OAAO,OAAO,OAAO,GAAG,EAAE;CAGrC,MAAM,EAAE,mBAAmB;CAC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AI,CAAC,MAAM,YAAY,GACrB;CAGF,IAAI,OAAO,YACT,KAAK,cAAc,OAAO,UAAU;CAGtC,IAAI,OAAO,MACT,KAAK,WAAW,OAAO,IAAI;AAE/B;;;;;AAMA,SAAgB,2BACd,UACA,WACiC;CACjC,IAAI,CAAC,QAAS,CAAC,QAAQ,gBAAgB,CAAC,QAAQ,kBAC9C;CAGF,MAAM,QAA6B,CAAC;CAEpC,IAAI,QAAQ,cACV,MAAM,sBAAsB;CAG9B,IAAI,QAAQ,oBAAoB,cAAc,KAAA,GAC5C,MAAM,uBAAuB,KAAK,UAAU,SAAS;CAGvD,OAAO;AACT;;;;;AAMA,SAAgB,mBAAmB,SAAmB;CACpD,IAAI,CAAC,MACH,OAAO;CAGT,MAAM,UAAkC,CAAC;CAEzC,KAAK,YAAY,OAAO,KAAK,QAAQ,OAAO,GAAG,OAAO;CAEtD,IAAI,QAAQ,gBACV,QAAQ,cAAc,QAAQ;CAGhC,IAAI,QAAQ,eACV,QAAQ,aAAa,QAAQ;CAG/B,OAAO;AACT"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/api.ts","../src/env.ts","../src/index.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport type * as otelApi from '@opentelemetry/api';\n\nexport type OtelApi = typeof otelApi;\n\nfunction loadApi(): OtelApi | undefined {\n try {\n return createRequire(import.meta.url)('@opentelemetry/api') as OtelApi;\n } catch {\n // @opentelemetry/api is not installed — telemetry stays disabled\n return undefined;\n }\n}\n\nexport const api = loadApi();\n","function envFlag(name: string, defaultValue: boolean): boolean {\n const raw = process.env[name];\n\n if (raw === undefined) {\n return defaultValue;\n }\n\n const value = raw.trim().toLowerCase();\n\n if (value === '') {\n return defaultValue;\n }\n\n return !['false', '0', 'no', 'off'].includes(value);\n}\n\nexport const otelEnv = {\n enabled: envFlag('OTEL_CONTELLO_ENABLED', true),\n captureQuery: envFlag('OTEL_CONTELLO_CAPTURE_QUERY', false),\n captureVariables: envFlag('OTEL_CONTELLO_CAPTURE_VARIABLES', false),\n} as const;\n","import { maybeThen } from '@entwico/dash';\nimport { api } from './api';\nimport { otelEnv } from './env';\n\nconst otel = otelEnv.enabled ? api : undefined;\n\nexport type OperationAttributes = Record<string, string | number | boolean>;\n\nexport type OperationTelemetry = {\n /**\n * Runs `fn` inside an active span named `name` (convention: `category:operation`) and records\n * duration/error metrics. Sync results are attributed `cached: true`, promises `cached: false`.\n * Zero-overhead pass-through when `@opentelemetry/api` is absent or `OTEL_CONTELLO_ENABLED=false`.\n */\n wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;\n};\n\nfunction parseOperationAttributes(name: string): OperationAttributes {\n const colonIndex = name.indexOf(':');\n\n if (colonIndex === -1) {\n return { category: 'unknown', operation: name };\n }\n\n return { category: name.slice(0, colonIndex), operation: name.slice(colonIndex + 1) };\n}\n\nexport function createOperationTelemetry(scope: string): OperationTelemetry {\n if (!otel) {\n return { wrap: (_name, fn) => fn() };\n }\n\n const { SpanStatusCode } = otel;\n const tracer = otel.trace.getTracer(scope);\n const meter = otel.metrics.getMeter(scope);\n\n const duration = meter.createHistogram('contello.operation.duration', {\n description: 'duration of contello operations',\n unit: 'ms',\n });\n\n const errors = meter.createCounter('contello.operation.error', {\n description: 'number of failed contello operations',\n });\n\n const wrap = <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined): T => {\n const attrs = { ...parseOperationAttributes(name), ...attributes };\n\n return tracer.startActiveSpan(name, { attributes: attrs }, (span) => {\n const start = performance.now();\n\n const finish = (cached: boolean) => {\n span.setAttribute('cached', cached);\n duration.record(performance.now() - start, { ...attrs, cached });\n span.end();\n };\n\n const fail = (error: unknown): never => {\n duration.record(performance.now() - start, { ...attrs, cached: false });\n errors.add(1, attrs);\n span.recordException(error as Error);\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error instanceof Error ? error.message : String(error),\n });\n span.end();\n\n throw error;\n };\n\n let result: T;\n\n try {\n result = fn();\n } catch (error) {\n return fail(error);\n }\n\n const cached = !(result instanceof Promise);\n\n return maybeThen(\n result,\n (value) => {\n finish(cached);\n\n return value;\n },\n fail,\n ) as T;\n });\n };\n\n return { wrap };\n}\n\n/**\n * Renames the active span and/or merges attributes into it. No-op when telemetry is\n * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to\n * enrich an outer span (e.g. the platform HTTP server span) with context only the\n * contello layer knows.\n */\nexport function updateActiveSpan(update: {\n name?: string | undefined;\n attributes?: OperationAttributes | undefined;\n}): void {\n if (!otel) {\n return;\n }\n\n const span = otel.trace.getActiveSpan();\n\n if (!span?.isRecording()) {\n return;\n }\n\n if (update.attributes) {\n span.setAttributes(update.attributes);\n }\n\n if (update.name) {\n span.updateName(update.name);\n }\n}\n\n/**\n * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and\n * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).\n */\nexport function graphqlOperationAttributes(\n document: string,\n variables?: Record<string, unknown> | undefined,\n): OperationAttributes | undefined {\n if (!otel || (!otelEnv.captureQuery && !otelEnv.captureVariables)) {\n return undefined;\n }\n\n const attrs: OperationAttributes = {};\n\n if (otelEnv.captureQuery) {\n attrs['graphql.document'] = document;\n }\n\n if (variables !== undefined && otelEnv.captureVariables) {\n attrs['graphql.variables'] = JSON.stringify(variables);\n }\n\n return attrs;\n}\n\n/**\n * Injects W3C trace context (`traceparent` / `tracestate`) into an outgoing GraphQL\n * WebSocket message. Returns the message unchanged when telemetry is disabled.\n */\nexport function injectTraceContext(message: any): any {\n if (!otel) {\n return message;\n }\n\n const carrier: Record<string, string> = {};\n\n otel.propagation.inject(otel.context.active(), carrier);\n\n if (carrier['traceparent']) {\n message.traceparent = carrier['traceparent'];\n }\n\n if (carrier['tracestate']) {\n message.tracestate = carrier['tracestate'];\n }\n\n return message;\n}\n"],"mappings":";;;AAKA,SAAS,UAA+B;CACtC,IAAI;EACF,OAAO,cAAc,YAAY,GAAG,CAAC,CAAC,oBAAoB;CA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mB,SAAmB;CACpD,IAAI,CAAC,MACH,OAAO;CAGT,MAAM,UAAkC,CAAC;CAEzC,KAAK,YAAY,OAAO,KAAK,QAAQ,OAAO,GAAG,OAAO;CAEtD,IAAI,QAAQ,gBACV,QAAQ,cAAc,QAAQ;CAGhC,IAAI,QAAQ,eACV,QAAQ,aAAa,QAAQ;CAG/B,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/opentelemetry",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Shared OpenTelemetry telemetry core for @contello packages",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -10,6 +10,9 @@
10
10
  "url": "https://github.com/entwico/contello-js",
11
11
  "directory": "packages/opentelemetry"
12
12
  },
13
+ "dependencies": {
14
+ "@entwico/dash": "^1.3.0"
15
+ },
13
16
  "peerDependencies": {
14
17
  "@opentelemetry/api": ">=1.9.0"
15
18
  },
@@ -24,6 +27,7 @@
24
27
  "files": [
25
28
  "dist"
26
29
  ],
30
+ "sideEffects": false,
27
31
  "types": "./dist/index.d.ts",
28
32
  "exports": {
29
33
  ".": {