@contello/opentelemetry 2.0.0 → 2.1.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/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,34 +80,37 @@ 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
- const promise = result;
92
- return (async () => {
93
- try {
94
- const value = await promise;
95
- finish(false);
96
- return value;
97
- } catch (error) {
98
- fail(error);
99
- throw error;
100
- }
101
- })();
89
+ return fail(error);
102
90
  }
103
- finish(true);
104
- return result;
91
+ const cached = !(result instanceof Promise);
92
+ return (0, _entwico_dash.maybeThen)(result, (value) => {
93
+ finish(cached);
94
+ return value;
95
+ }, fail);
105
96
  });
106
97
  };
107
98
  return { wrap };
108
99
  }
109
100
  /**
101
+ * Renames the active span and/or merges attributes into it. No-op when telemetry is
102
+ * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
103
+ * enrich an outer span (e.g. the platform HTTP server span) with context only the
104
+ * contello layer knows.
105
+ */
106
+ function updateActiveSpan(update) {
107
+ if (!otel) return;
108
+ const span = otel.trace.getActiveSpan();
109
+ if (!span?.isRecording()) return;
110
+ if (update.attributes) span.setAttributes(update.attributes);
111
+ if (update.name) span.updateName(update.name);
112
+ }
113
+ /**
110
114
  * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
111
115
  * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
112
116
  */
@@ -133,3 +137,4 @@ function injectTraceContext(message) {
133
137
  exports.createOperationTelemetry = createOperationTelemetry;
134
138
  exports.graphqlOperationAttributes = graphqlOperationAttributes;
135
139
  exports.injectTraceContext = injectTraceContext;
140
+ exports.updateActiveSpan = updateActiveSpan;
package/dist/index.d.cts CHANGED
@@ -9,6 +9,16 @@ type OperationTelemetry = {
9
9
  wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;
10
10
  };
11
11
  declare function createOperationTelemetry(scope: string): OperationTelemetry;
12
+ /**
13
+ * Renames the active span and/or merges attributes into it. No-op when telemetry is
14
+ * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
15
+ * enrich an outer span (e.g. the platform HTTP server span) with context only the
16
+ * contello layer knows.
17
+ */
18
+ declare function updateActiveSpan(update: {
19
+ name?: string | undefined;
20
+ attributes?: OperationAttributes | undefined;
21
+ }): void;
12
22
  /**
13
23
  * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
14
24
  * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
@@ -20,5 +30,5 @@ declare function graphqlOperationAttributes(document: string, variables?: Record
20
30
  */
21
31
  declare function injectTraceContext(message: any): any;
22
32
  //#endregion
23
- export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext };
33
+ export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
24
34
  //# 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;;;;;iBAkF3D,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;;iBAsBN,kBAAA,CAAmB,OAAY"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";KAMY,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;;;;;;;iBA0E3D,gBAAA,CAAiB,MAAA;EAC/B,IAAA;EACA,UAAA,GAAa,mBAAmB;AAAA;;;;AAzFuD;iBAkHzE,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;AAxGqD;iBA8H3D,kBAAA,CAAmB,OAAY"}
package/dist/index.d.ts CHANGED
@@ -9,6 +9,16 @@ type OperationTelemetry = {
9
9
  wrap: <T>(name: string, fn: () => T, attributes?: OperationAttributes | undefined) => T;
10
10
  };
11
11
  declare function createOperationTelemetry(scope: string): OperationTelemetry;
12
+ /**
13
+ * Renames the active span and/or merges attributes into it. No-op when telemetry is
14
+ * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
15
+ * enrich an outer span (e.g. the platform HTTP server span) with context only the
16
+ * contello layer knows.
17
+ */
18
+ declare function updateActiveSpan(update: {
19
+ name?: string | undefined;
20
+ attributes?: OperationAttributes | undefined;
21
+ }): void;
12
22
  /**
13
23
  * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
14
24
  * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
@@ -20,5 +30,5 @@ declare function graphqlOperationAttributes(document: string, variables?: Record
20
30
  */
21
31
  declare function injectTraceContext(message: any): any;
22
32
  //#endregion
23
- export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext };
33
+ export { OperationAttributes, OperationTelemetry, createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
24
34
  //# 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;;;;;iBAkF3D,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;;iBAsBN,kBAAA,CAAmB,OAAY"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";KAMY,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;;;;;;;iBA0E3D,gBAAA,CAAiB,MAAA;EAC/B,IAAA;EACA,UAAA,GAAa,mBAAmB;AAAA;;;;AAzFuD;iBAkHzE,0BAAA,CACd,QAAA,UACA,SAAA,GAAY,MAAA,gCACX,mBAAmB;;;;AAxGqD;iBA8H3D,kBAAA,CAAmB,OAAY"}
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,34 +79,37 @@ 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
- const promise = result;
91
- return (async () => {
92
- try {
93
- const value = await promise;
94
- finish(false);
95
- return value;
96
- } catch (error) {
97
- fail(error);
98
- throw error;
99
- }
100
- })();
88
+ return fail(error);
101
89
  }
102
- finish(true);
103
- return result;
90
+ const cached = !(result instanceof Promise);
91
+ return maybeThen(result, (value) => {
92
+ finish(cached);
93
+ return value;
94
+ }, fail);
104
95
  });
105
96
  };
106
97
  return { wrap };
107
98
  }
108
99
  /**
100
+ * Renames the active span and/or merges attributes into it. No-op when telemetry is
101
+ * disabled, `@opentelemetry/api` is absent, or no recording span is active. Used to
102
+ * enrich an outer span (e.g. the platform HTTP server span) with context only the
103
+ * contello layer knows.
104
+ */
105
+ function updateActiveSpan(update) {
106
+ if (!otel) return;
107
+ const span = otel.trace.getActiveSpan();
108
+ if (!span?.isRecording()) return;
109
+ if (update.attributes) span.setAttributes(update.attributes);
110
+ if (update.name) span.updateName(update.name);
111
+ }
112
+ /**
109
113
  * Attributes for GraphQL operation spans, gated by `OTEL_CONTELLO_CAPTURE_QUERY` and
110
114
  * `OTEL_CONTELLO_CAPTURE_VARIABLES` (both off by default).
111
115
  */
@@ -129,6 +133,6 @@ function injectTraceContext(message) {
129
133
  return message;
130
134
  }
131
135
  //#endregion
132
- export { createOperationTelemetry, graphqlOperationAttributes, injectTraceContext };
136
+ export { createOperationTelemetry, graphqlOperationAttributes, injectTraceContext, updateActiveSpan };
133
137
 
134
138
  //# sourceMappingURL=index.js.map
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 const promise = result;\n\n return (async () => {\n try {\n const value = await promise;\n\n finish(false);\n\n return value;\n } catch (error) {\n fail(error);\n\n throw error;\n }\n })() as T;\n }\n\n finish(true);\n\n return result;\n });\n };\n\n return { wrap };\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,oBAA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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 (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;;;AChBA,MAAM,OAAO,QAAQ,UAAU,MAAM,KAAA;AAarC,SAAS,yBAAy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,GAAG;GACd,SAAS,OAAO;IACd,OAAO,KAAK,KAAK;GACnB;GAEA,MAAM,SAAS,EAAE,kBAAkB;GAEnC,OAAO,UACL,SACC,UAAU;IACT,OAAO,MAAM;IAEb,OAAO;GACT,GACA,IACF;EACF,CAAC;CACH;CAEA,OAAO,EAAE,KAAK;AAChB;;;;;;;AAQA,SAAgB,iBAAiB,QAGxB;CACP,IAAI,CAAC,MACH;CAGF,MAAM,OAAO,KAAK,MAAM,cAAc;CAEtC,IAAI,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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/opentelemetry",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
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.2.0"
15
+ },
13
16
  "peerDependencies": {
14
17
  "@opentelemetry/api": ">=1.9.0"
15
18
  },