From edd257914e9509286c6d4f0b7d5d8c2cb779df19 Mon Sep 17 00:00:00 2001 From: Bob Evans Date: Thu, 13 Feb 2025 16:17:15 -0500 Subject: [PATCH] wip: fixed attr issues with using real otel libs --- lib/otel/constants.js | 3 ++- lib/otel/segments/consumer.js | 5 ----- lib/otel/segments/internal.js | 1 + lib/otel/span-processor.js | 14 +++++++++----- lib/shim/message-shim/subscribe-consume.js | 1 - lib/transaction/trace/segment.js | 5 +++-- lib/transaction/tracer/index.js | 3 ++- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/otel/constants.js b/lib/otel/constants.js index 79dcc88ef8..ab9c3d10ea 100644 --- a/lib/otel/constants.js +++ b/lib/otel/constants.js @@ -111,7 +111,8 @@ module.exports = { * * @example 200 */ - ATTR_HTTP_STATUS_CODE: 'http.response.status_code', + ATTR_HTTP_RES_STATUS_CODE: 'http.response.status_code', + ATTR_HTTP_STATUS_CODE: 'http.status_code', /** * The http response status text diff --git a/lib/otel/segments/consumer.js b/lib/otel/segments/consumer.js index 23eb685c16..50f078264d 100644 --- a/lib/otel/segments/consumer.js +++ b/lib/otel/segments/consumer.js @@ -32,11 +32,6 @@ function createConsumerSegment(agent, otelSpan) { const txAttrs = transaction.trace.attributes txAttrs.addAttribute(DESTINATIONS.TRANS_SCOPE, 'message.queueName', destination) - // txAttrs.addAttribute( - // DESTINATIONS.TRANS_SCOPE, - // 'host', - // - // ) transaction.name = segmentName const segment = agent.tracer.createSegment({ diff --git a/lib/otel/segments/internal.js b/lib/otel/segments/internal.js index e558db4f70..b005bb8cfe 100644 --- a/lib/otel/segments/internal.js +++ b/lib/otel/segments/internal.js @@ -10,6 +10,7 @@ module.exports = function createInternalSegment(agent, otelSpan) { const context = agent.tracer.getContext() const name = otelSpan.name const segment = agent.tracer.createSegment({ + id: otelSpan?.spanContext()?.spanId, name, parent: context.segment, recorder: customRecorder, diff --git a/lib/otel/span-processor.js b/lib/otel/span-processor.js index 73cc9a5fbc..e1dda44ed2 100644 --- a/lib/otel/span-processor.js +++ b/lib/otel/span-processor.js @@ -17,6 +17,7 @@ const { ATTR_HTTP_ROUTE, ATTR_HTTP_STATUS_CODE, ATTR_HTTP_STATUS_TEXT, + ATTR_HTTP_RES_STATUS_CODE, ATTR_NET_PEER_NAME, ATTR_NET_PEER_PORT, ATTR_NET_HOST_NAME, @@ -82,7 +83,10 @@ module.exports = class NrSpanProcessor { // End the corresponding transaction for the entry point server span. // We do then when the span ends to ensure all data has been processed - // for the correspondig server span. + // for the corresponding server span. + if (transaction.statusCode) { + transaction.finalizeNameFromUri(transaction.parsedUrl, transaction.statusCode) + } transaction.end() } @@ -93,14 +97,14 @@ module.exports = class NrSpanProcessor { if (key === ATTR_HTTP_ROUTE) { // TODO: can we get the route params? transaction.nameState.appendPath(sanitized) - } else if (key === ATTR_HTTP_STATUS_CODE) { - transaction.finalizeNameFromUri(transaction.parsedUrl, sanitized) - transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, 'http.statusCode', sanitized) + } else if (key === ATTR_HTTP_STATUS_CODE || key === ATTR_HTTP_RES_STATUS_CODE) { key = 'http.statusCode' + transaction.statusCode = sanitized + transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, key, sanitized) // Not using const as it is not in semantic-conventions } else if (key === ATTR_HTTP_STATUS_TEXT) { - transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, 'http.statusText', sanitized) key = 'http.statusText' + transaction.trace.attributes.addAttribute(DESTINATIONS.TRANS_COMMON, key, sanitized) } else if (key === ATTR_SERVER_PORT || key === ATTR_NET_HOST_PORT) { key = 'port' } else if (key === ATTR_SERVER_ADDRESS || key === ATTR_NET_HOST_NAME) { diff --git a/lib/shim/message-shim/subscribe-consume.js b/lib/shim/message-shim/subscribe-consume.js index de35aef70f..a86bf94428 100644 --- a/lib/shim/message-shim/subscribe-consume.js +++ b/lib/shim/message-shim/subscribe-consume.js @@ -238,7 +238,6 @@ function createConsumerWrapper({ shim, spec, consumer }) { * finalizes transaction name and ends transaction */ function endTransaction() { - tx.finalizeName(null) // Use existing partial name. tx.end() } } diff --git a/lib/transaction/trace/segment.js b/lib/transaction/trace/segment.js index de9d4a8552..813ca18fd1 100644 --- a/lib/transaction/trace/segment.js +++ b/lib/transaction/trace/segment.js @@ -30,6 +30,7 @@ const ATTRIBUTE_SCOPE = 'segment' * for now), and has one or more children (that are also part of the same * transaction trace), as well as an associated timer. * @param {object} params to function + * @param {number} params.id id if passed in used as segment id. This is only used in otel bridge to ensure span id is same as segment * @param {object} params.config agent config * @param {string} params.name Human-readable name for this segment (e.g. 'http', 'net', 'express', * 'mysql', etc). @@ -38,7 +39,7 @@ const ATTRIBUTE_SCOPE = 'segment' * @param {TraceSegment} params.root root segment * @param {boolean} params.isRoot flag to indicate it is the root segment */ -function TraceSegment({ config, name, collect, parentId, root, isRoot = false }) { +function TraceSegment({ id, config, name, collect, parentId, root, isRoot = false }) { this.isRoot = isRoot this.root = root this.name = name @@ -46,7 +47,7 @@ function TraceSegment({ config, name, collect, parentId, root, isRoot = false }) this.spansEnabled = config?.distributed_tracing?.enabled && config?.span_events?.enabled // Generate a unique id for use in span events. - this.id = hashes.makeId() + this.id = id || hashes.makeId() this.parentId = parentId this.timer = new Timer() diff --git a/lib/transaction/tracer/index.js b/lib/transaction/tracer/index.js index ae14d45ff4..c844164916 100644 --- a/lib/transaction/tracer/index.js +++ b/lib/transaction/tracer/index.js @@ -81,7 +81,7 @@ function getSpanContext() { const currentSegment = this.getSegment() return currentSegment && currentSegment.getSpanContext() } -function createSegment({ name, recorder, parent, transaction }) { +function createSegment({ id, name, recorder, parent, transaction }) { if (!parent || !transaction?.isActive()) { logger.trace( { @@ -109,6 +109,7 @@ function createSegment({ name, recorder, parent, transaction }) { transaction.incrementCounters() const segment = new TraceSegment({ + id, config: this.agent.config, name, collect,