|
| 1 | +/* |
| 2 | + * Copyright 2026 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const Subscriber = require('../base.js') |
| 9 | +const shouldTrackError = require('./should-track-error.js') |
| 10 | +const recordExternal = require('#agentlib/metrics/recorders/http_external.js') |
| 11 | + |
| 12 | +module.exports = class GrpcClientSubscriber extends Subscriber { |
| 13 | + constructor({ agent, logger, channelName }) { |
| 14 | + super({ |
| 15 | + agent, |
| 16 | + logger, |
| 17 | + channelName, |
| 18 | + packageName: '@grpc/grpc-js', |
| 19 | + }) |
| 20 | + |
| 21 | + this.opaque = true |
| 22 | + } |
| 23 | + |
| 24 | + handler(data, ctx) { |
| 25 | + const { arguments: args, self: client } = data |
| 26 | + const { transaction } = ctx |
| 27 | + |
| 28 | + const { channel } = client |
| 29 | + const authority = channel.target?.path || channel.getDefaultAuthority |
| 30 | + // In grpc-js>=1.8.0 `.methodName` becomes `.method`. |
| 31 | + const method = client.methodName || client.method |
| 32 | + |
| 33 | + const newCtx = this.createSegment({ |
| 34 | + name: `External/${authority}${method}`, |
| 35 | + recorder: recordExternal(authority, 'gRPC'), |
| 36 | + ctx |
| 37 | + }) |
| 38 | + |
| 39 | + // Acquire the original parameters to the handler, create patched |
| 40 | + // versions of them, and update the parameters list with the patched |
| 41 | + // instances. |
| 42 | + const origMetadata = args[0] |
| 43 | + const origListener = args[1] |
| 44 | + const nrMetadata = origMetadata.clone() |
| 45 | + const nrListener = Object.assign({}, origListener) |
| 46 | + args[0] = nrMetadata |
| 47 | + args[1] = nrListener |
| 48 | + |
| 49 | + this.#addDistributedTraceHeaders(transaction, nrMetadata) |
| 50 | + this.#wrapListenerCallback(nrListener, origListener, authority, method, newCtx) |
| 51 | + |
| 52 | + return newCtx |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * When distributed tracing (DT) is enabled, pull DT headers from the |
| 57 | + * provided transaction and add them to the metadata object used by gRPC |
| 58 | + * when delivering data. |
| 59 | + * |
| 60 | + * @param {Transaction} transaction The transaction that contains the DT data. |
| 61 | + * @param {object} destination The gRPC metadata object to add headers to. |
| 62 | + * This object will be mutated. |
| 63 | + */ |
| 64 | + #addDistributedTraceHeaders(transaction, destination) { |
| 65 | + if (this.agent.config.distributed_tracing.enabled === false) { |
| 66 | + this.logger.debug('Distributed tracing disabled by instrumentation.') |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + const outboundAgentHeaders = Object.create(null) |
| 71 | + transaction.insertDistributedTraceHeaders(outboundAgentHeaders) |
| 72 | + for (const [key, value] of Object.entries(outboundAgentHeaders)) { |
| 73 | + destination.add(key, value) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * When the original gRPC "listener" object has an `onReceiveStatus` method |
| 79 | + * defined, wrap that method to capture tracing data and attach the wrapped |
| 80 | + * method to the patched `nrListener` instance. |
| 81 | + * |
| 82 | + * @param {object} nrListener New Relic clone of the original listener. |
| 83 | + * @param {object} origListener Original gRPC listener object. |
| 84 | + * @param {string} authority Value of the `:authority` header. |
| 85 | + * @param {string} method The gRPC method name to be invoked. |
| 86 | + * @param {AsyncContext} ctx The context to execute the `onReceiveStatus` |
| 87 | + * handler under. |
| 88 | + */ |
| 89 | + #wrapListenerCallback(nrListener, origListener, authority, method, ctx) { |
| 90 | + if (typeof origListener?.onReceiveStatus !== 'function') { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + const agent = this.agent |
| 95 | + const [hostname, port] = authority.split(':') |
| 96 | + |
| 97 | + nrListener.onReceiveStatus = function nrOnReceiveStatus(status) { |
| 98 | + const { segment, transaction } = ctx |
| 99 | + const { code, details } = status |
| 100 | + segment.addAttribute('grpc.statusCode', code) |
| 101 | + segment.addAttribute('grpc.statusText', details) |
| 102 | + segment.addAttribute('component', 'gRPC') |
| 103 | + |
| 104 | + if (shouldTrackError(code, agent.config) === true) { |
| 105 | + agent.errors.add(transaction, details) |
| 106 | + } |
| 107 | + |
| 108 | + const protocol = 'grpc:' |
| 109 | + segment.captureExternalAttributes({ |
| 110 | + protocol, |
| 111 | + host: authority, |
| 112 | + port, |
| 113 | + hostname, |
| 114 | + method, |
| 115 | + path: method |
| 116 | + }) |
| 117 | + |
| 118 | + const boundFn = agent.tracer.bindFunction( |
| 119 | + origListener.onReceiveStatus, |
| 120 | + ctx, |
| 121 | + true |
| 122 | + ) |
| 123 | + boundFn(status) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments