@envelop/opentelemetry 2.0.0 → 3.0.0-alpha-dc1e98d.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.
Files changed (3) hide show
  1. package/index.js +51 -2
  2. package/index.mjs +52 -3
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- require('@envelop/core');
5
+ const core = require('@envelop/core');
6
6
  const opentelemetry = require('@opentelemetry/api');
7
7
  const tracing = require('@opentelemetry/tracing');
8
8
  const graphql = require('graphql');
@@ -28,6 +28,35 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = opentelemetry.Spa
28
28
  }
29
29
  const tracer = tracingProvider.getTracer(serviceName);
30
30
  return {
31
+ onResolverCalled: options.resolvers
32
+ ? ({ info, context, args }) => {
33
+ if (context && typeof context === 'object' && context[tracingSpanSymbol]) {
34
+ tracer.getActiveSpanProcessor();
35
+ const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), context[tracingSpanSymbol]);
36
+ const { fieldName, returnType, parentType } = info;
37
+ const resolverSpan = tracer.startSpan(`${parentType.name}.${fieldName}`, {
38
+ attributes: {
39
+ [exports.AttributeName.RESOLVER_FIELD_NAME]: fieldName,
40
+ [exports.AttributeName.RESOLVER_TYPE_NAME]: parentType.toString(),
41
+ [exports.AttributeName.RESOLVER_RESULT_TYPE]: returnType.toString(),
42
+ [exports.AttributeName.RESOLVER_ARGS]: JSON.stringify(args || {}),
43
+ },
44
+ }, ctx);
45
+ return ({ result }) => {
46
+ if (result instanceof Error) {
47
+ resolverSpan.recordException({
48
+ name: exports.AttributeName.RESOLVER_EXCEPTION,
49
+ message: JSON.stringify(result),
50
+ });
51
+ }
52
+ else {
53
+ resolverSpan.end();
54
+ }
55
+ };
56
+ }
57
+ return () => { };
58
+ }
59
+ : undefined,
31
60
  onExecute({ args, extendContext }) {
32
61
  var _a, _b;
33
62
  const executionSpan = tracer.startSpan(`${args.operationName || 'Anonymous Operation'}`, {
@@ -39,12 +68,32 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = opentelemetry.Spa
39
68
  ...(options.variables ? { [exports.AttributeName.EXECUTION_VARIABLES]: JSON.stringify((_b = args.variableValues) !== null && _b !== void 0 ? _b : {}) } : {}),
40
69
  },
41
70
  });
71
+ const resultCbs = {
72
+ onExecuteDone({ result }) {
73
+ if (core.isAsyncIterable(result)) {
74
+ executionSpan.end();
75
+ // eslint-disable-next-line no-console
76
+ console.warn(`Plugin "newrelic" encountered a AsyncIterator which is not supported yet, so tracing data is not available for the operation.`);
77
+ return;
78
+ }
79
+ if (result.data && options.result) {
80
+ executionSpan.setAttribute(exports.AttributeName.EXECUTION_RESULT, JSON.stringify(result));
81
+ }
82
+ if (result.errors && result.errors.length > 0) {
83
+ executionSpan.recordException({
84
+ name: exports.AttributeName.EXECUTION_ERROR,
85
+ message: JSON.stringify(result.errors),
86
+ });
87
+ }
88
+ executionSpan.end();
89
+ },
90
+ };
42
91
  if (options.resolvers) {
43
92
  extendContext({
44
93
  [tracingSpanSymbol]: executionSpan,
45
94
  });
46
95
  }
47
- return {};
96
+ return resultCbs;
48
97
  },
49
98
  };
50
99
  };
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import '@envelop/core';
2
- import { SpanKind } from '@opentelemetry/api';
1
+ import { isAsyncIterable } from '@envelop/core';
2
+ import { SpanKind, trace, context } from '@opentelemetry/api';
3
3
  import { BasicTracerProvider, SimpleSpanProcessor, ConsoleSpanExporter } from '@opentelemetry/tracing';
4
4
  import { print } from 'graphql';
5
5
 
@@ -25,6 +25,35 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.SERVER,
25
25
  }
26
26
  const tracer = tracingProvider.getTracer(serviceName);
27
27
  return {
28
+ onResolverCalled: options.resolvers
29
+ ? ({ info, context: context$1, args }) => {
30
+ if (context$1 && typeof context$1 === 'object' && context$1[tracingSpanSymbol]) {
31
+ tracer.getActiveSpanProcessor();
32
+ const ctx = trace.setSpan(context.active(), context$1[tracingSpanSymbol]);
33
+ const { fieldName, returnType, parentType } = info;
34
+ const resolverSpan = tracer.startSpan(`${parentType.name}.${fieldName}`, {
35
+ attributes: {
36
+ [AttributeName.RESOLVER_FIELD_NAME]: fieldName,
37
+ [AttributeName.RESOLVER_TYPE_NAME]: parentType.toString(),
38
+ [AttributeName.RESOLVER_RESULT_TYPE]: returnType.toString(),
39
+ [AttributeName.RESOLVER_ARGS]: JSON.stringify(args || {}),
40
+ },
41
+ }, ctx);
42
+ return ({ result }) => {
43
+ if (result instanceof Error) {
44
+ resolverSpan.recordException({
45
+ name: AttributeName.RESOLVER_EXCEPTION,
46
+ message: JSON.stringify(result),
47
+ });
48
+ }
49
+ else {
50
+ resolverSpan.end();
51
+ }
52
+ };
53
+ }
54
+ return () => { };
55
+ }
56
+ : undefined,
28
57
  onExecute({ args, extendContext }) {
29
58
  var _a, _b;
30
59
  const executionSpan = tracer.startSpan(`${args.operationName || 'Anonymous Operation'}`, {
@@ -36,12 +65,32 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.SERVER,
36
65
  ...(options.variables ? { [AttributeName.EXECUTION_VARIABLES]: JSON.stringify((_b = args.variableValues) !== null && _b !== void 0 ? _b : {}) } : {}),
37
66
  },
38
67
  });
68
+ const resultCbs = {
69
+ onExecuteDone({ result }) {
70
+ if (isAsyncIterable(result)) {
71
+ executionSpan.end();
72
+ // eslint-disable-next-line no-console
73
+ console.warn(`Plugin "newrelic" encountered a AsyncIterator which is not supported yet, so tracing data is not available for the operation.`);
74
+ return;
75
+ }
76
+ if (result.data && options.result) {
77
+ executionSpan.setAttribute(AttributeName.EXECUTION_RESULT, JSON.stringify(result));
78
+ }
79
+ if (result.errors && result.errors.length > 0) {
80
+ executionSpan.recordException({
81
+ name: AttributeName.EXECUTION_ERROR,
82
+ message: JSON.stringify(result.errors),
83
+ });
84
+ }
85
+ executionSpan.end();
86
+ },
87
+ };
39
88
  if (options.resolvers) {
40
89
  extendContext({
41
90
  [tracingSpanSymbol]: executionSpan,
42
91
  });
43
92
  }
44
- return {};
93
+ return resultCbs;
45
94
  },
46
95
  };
47
96
  };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@envelop/opentelemetry",
3
- "version": "2.0.0",
3
+ "version": "3.0.0-alpha-dc1e98d.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "^1.7.0",
6
+ "@envelop/core": "2.0.0-alpha-dc1e98d.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {