@graphql-mesh/plugin-newrelic 0.95.1 → 0.95.2-alpha-20230814094115-529edb92f

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/cjs/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const newrelic_1 = tslib_1.__importDefault(require("newrelic"));
5
+ const attribute_filter_js_1 = tslib_1.__importDefault(require("newrelic/lib/config/attribute-filter.js"));
5
6
  const names_js_1 = tslib_1.__importDefault(require("newrelic/lib/metrics/names.js"));
6
7
  const http_external_js_1 = tslib_1.__importDefault(require("newrelic/lib/metrics/recorders/http_external.js"));
7
8
  const cat_js_1 = tslib_1.__importDefault(require("newrelic/lib/util/cat.js"));
@@ -9,7 +10,11 @@ const newrelic_2 = require("@envelop/newrelic");
9
10
  const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
10
11
  const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
11
12
  const utils_1 = require("@graphql-mesh/utils");
13
+ const DESTS = attribute_filter_js_1.default.DESTINATIONS;
12
14
  const EnvelopAttributeName = 'Envelop_NewRelic_Plugin';
15
+ function isPromise(value) {
16
+ return value?.then != null;
17
+ }
13
18
  function useMeshNewrelic(options, { instrumentationApi = newrelic_1.default?.shim, agentApi = newrelic_1.default } = {}) {
14
19
  if (!instrumentationApi?.agent) {
15
20
  options.logger.debug('Agent unavailable. Please check your New Relic Agent configuration and ensure New Relic is enabled.');
@@ -20,6 +25,20 @@ function useMeshNewrelic(options, { instrumentationApi = newrelic_1.default?.shi
20
25
  .incrementCallCount();
21
26
  const logger = instrumentationApi.logger.child({ component: EnvelopAttributeName });
22
27
  const segmentByRequestContext = new WeakMap();
28
+ const headersToTrack = {
29
+ 'user-agent': 'userAgent',
30
+ host: 'host',
31
+ 'content-type': 'contentType',
32
+ 'content-length': 'contentLength',
33
+ accept: 'accept',
34
+ };
35
+ function sendResAttributes(res, currentSegment = instrumentationApi.getSegment(), currentTransaction = instrumentationApi.agent.tracer.getTransaction()) {
36
+ currentSegment.addAttribute('http.statusCode', res.status);
37
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'http.statusCode', res.status);
38
+ currentTransaction.statusCode = res.status;
39
+ currentSegment.addAttribute('http.statusText', res.statusText);
40
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'http.statusText', res.statusText);
41
+ }
23
42
  return {
24
43
  onPluginInit({ addPlugin }) {
25
44
  addPlugin((0, newrelic_2.useNewRelic)({
@@ -33,10 +52,37 @@ function useMeshNewrelic(options, { instrumentationApi = newrelic_1.default?.shi
33
52
  : undefined,
34
53
  }));
35
54
  },
36
- onRequest({ url, requestHandler, setRequestHandler }) {
55
+ onRequest({ request, url, requestHandler, setRequestHandler }) {
37
56
  const currentTransaction = instrumentationApi.agent.tracer.getTransaction();
38
57
  if (!currentTransaction) {
39
- setRequestHandler((...args) => agentApi.startWebTransaction(url.pathname, () => requestHandler(...args)));
58
+ setRequestHandler((...args) => agentApi.startWebTransaction(url.pathname, () => {
59
+ const currentSegment = instrumentationApi.getSegment();
60
+ segmentByRequestContext.set(request, currentSegment);
61
+ const currentTransaction = instrumentationApi.agent.tracer.getTransaction();
62
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'request.uri', url.pathname);
63
+ currentSegment.addAttribute('request.uri', url.pathname);
64
+ currentTransaction.parsedUrl = url;
65
+ currentTransaction.verb = request.method;
66
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'request.method', request.method);
67
+ currentTransaction.nameState.setVerb(request.method);
68
+ currentSegment.addAttribute('request.method', request.method);
69
+ for (const headerName in headersToTrack) {
70
+ const headerValue = request.headers.get(headerName);
71
+ if (headerValue) {
72
+ const key = `request.headers.${headersToTrack[headerName]}`;
73
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, key, headerValue);
74
+ currentSegment.addAttribute(key, headerValue);
75
+ }
76
+ }
77
+ const res$ = requestHandler(...args);
78
+ if (res$) {
79
+ if (isPromise(res$)) {
80
+ return res$.then(sendResAttributes).then(() => res$);
81
+ }
82
+ sendResAttributes(res$);
83
+ }
84
+ return res$;
85
+ }));
40
86
  }
41
87
  },
42
88
  onExecute({ args: { contextValue } }) {
package/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import newRelic from 'newrelic';
2
+ import attributeFilter from 'newrelic/lib/config/attribute-filter.js';
2
3
  import NAMES from 'newrelic/lib/metrics/names.js';
3
4
  import recordExternal from 'newrelic/lib/metrics/recorders/http_external.js';
4
5
  import cat from 'newrelic/lib/util/cat.js';
@@ -6,7 +7,11 @@ import { useNewRelic } from '@envelop/newrelic';
6
7
  import { process } from '@graphql-mesh/cross-helpers';
7
8
  import { stringInterpolator } from '@graphql-mesh/string-interpolation';
8
9
  import { getHeadersObj } from '@graphql-mesh/utils';
10
+ const DESTS = attributeFilter.DESTINATIONS;
9
11
  const EnvelopAttributeName = 'Envelop_NewRelic_Plugin';
12
+ function isPromise(value) {
13
+ return value?.then != null;
14
+ }
10
15
  export default function useMeshNewrelic(options, { instrumentationApi = newRelic?.shim, agentApi = newRelic } = {}) {
11
16
  if (!instrumentationApi?.agent) {
12
17
  options.logger.debug('Agent unavailable. Please check your New Relic Agent configuration and ensure New Relic is enabled.');
@@ -17,6 +22,20 @@ export default function useMeshNewrelic(options, { instrumentationApi = newRelic
17
22
  .incrementCallCount();
18
23
  const logger = instrumentationApi.logger.child({ component: EnvelopAttributeName });
19
24
  const segmentByRequestContext = new WeakMap();
25
+ const headersToTrack = {
26
+ 'user-agent': 'userAgent',
27
+ host: 'host',
28
+ 'content-type': 'contentType',
29
+ 'content-length': 'contentLength',
30
+ accept: 'accept',
31
+ };
32
+ function sendResAttributes(res, currentSegment = instrumentationApi.getSegment(), currentTransaction = instrumentationApi.agent.tracer.getTransaction()) {
33
+ currentSegment.addAttribute('http.statusCode', res.status);
34
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'http.statusCode', res.status);
35
+ currentTransaction.statusCode = res.status;
36
+ currentSegment.addAttribute('http.statusText', res.statusText);
37
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'http.statusText', res.statusText);
38
+ }
20
39
  return {
21
40
  onPluginInit({ addPlugin }) {
22
41
  addPlugin(useNewRelic({
@@ -30,10 +49,37 @@ export default function useMeshNewrelic(options, { instrumentationApi = newRelic
30
49
  : undefined,
31
50
  }));
32
51
  },
33
- onRequest({ url, requestHandler, setRequestHandler }) {
52
+ onRequest({ request, url, requestHandler, setRequestHandler }) {
34
53
  const currentTransaction = instrumentationApi.agent.tracer.getTransaction();
35
54
  if (!currentTransaction) {
36
- setRequestHandler((...args) => agentApi.startWebTransaction(url.pathname, () => requestHandler(...args)));
55
+ setRequestHandler((...args) => agentApi.startWebTransaction(url.pathname, () => {
56
+ const currentSegment = instrumentationApi.getSegment();
57
+ segmentByRequestContext.set(request, currentSegment);
58
+ const currentTransaction = instrumentationApi.agent.tracer.getTransaction();
59
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'request.uri', url.pathname);
60
+ currentSegment.addAttribute('request.uri', url.pathname);
61
+ currentTransaction.parsedUrl = url;
62
+ currentTransaction.verb = request.method;
63
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, 'request.method', request.method);
64
+ currentTransaction.nameState.setVerb(request.method);
65
+ currentSegment.addAttribute('request.method', request.method);
66
+ for (const headerName in headersToTrack) {
67
+ const headerValue = request.headers.get(headerName);
68
+ if (headerValue) {
69
+ const key = `request.headers.${headersToTrack[headerName]}`;
70
+ currentTransaction.trace.attributes.addAttribute(DESTS.TRANS_COMMON, key, headerValue);
71
+ currentSegment.addAttribute(key, headerValue);
72
+ }
73
+ }
74
+ const res$ = requestHandler(...args);
75
+ if (res$) {
76
+ if (isPromise(res$)) {
77
+ return res$.then(sendResAttributes).then(() => res$);
78
+ }
79
+ sendResAttributes(res$);
80
+ }
81
+ return res$;
82
+ }));
37
83
  }
38
84
  },
39
85
  onExecute({ args: { contextValue } }) {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@graphql-mesh/plugin-newrelic",
3
- "version": "0.95.1",
3
+ "version": "0.95.2-alpha-20230814094115-529edb92f",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@graphql-mesh/cross-helpers": "^0.4.0",
7
- "@graphql-mesh/types": "^0.95.1",
8
- "@graphql-mesh/utils": "^0.95.1",
7
+ "@graphql-mesh/types": "0.95.2-alpha-20230814094115-529edb92f",
8
+ "@graphql-mesh/utils": "0.95.2-alpha-20230814094115-529edb92f",
9
9
  "graphql": "*",
10
10
  "graphql-yoga": "^4.0.3",
11
11
  "newrelic": "^7 || ^8.0.0 || ^10.0.0",