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