Skip to content

Commit

Permalink
wip: fixed attr issues with using real otel libs
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed Feb 18, 2025
1 parent b85111c commit edd2579
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/otel/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions lib/otel/segments/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions lib/otel/segments/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 9 additions & 5 deletions lib/otel/span-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}

Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion lib/shim/message-shim/subscribe-consume.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/transaction/trace/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -38,15 +39,15 @@ 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
this.attributes = new Attributes(ATTRIBUTE_SCOPE)
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()

Expand Down
3 changes: 2 additions & 1 deletion lib/transaction/tracer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down Expand Up @@ -109,6 +109,7 @@ function createSegment({ name, recorder, parent, transaction }) {
transaction.incrementCounters()

const segment = new TraceSegment({
id,
config: this.agent.config,
name,
collect,
Expand Down

0 comments on commit edd2579

Please sign in to comment.