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 98d29fe commit 4f2edcb
Show file tree
Hide file tree
Showing 11 changed files with 23 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
6 changes: 1 addition & 5 deletions lib/otel/segments/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ function createConsumerSegment(agent, otelSpan) {

const txAttrs = transaction.trace.attributes
txAttrs.addAttribute(DESTINATIONS.TRANS_SCOPE, 'message.queueName', destination)
// txAttrs.addAttribute(
// DESTINATIONS.TRANS_SCOPE,
// 'host',
//
// )
transaction.setPartialName(segmentName)

const segment = agent.tracer.createSegment({
id: otelSpan?.spanContext()?.spanId,
recorder,
name: transaction.getFullName(),
parent: transaction.trace.root,
Expand Down
1 change: 1 addition & 0 deletions lib/otel/segments/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = function createDbSegment(agent, otelSpan) {
const parsed = parseStatement(agent.config, otelSpan, system)
const { name, operation } = setName(parsed)
const segment = agent.tracer.createSegment({
id: otelSpan?.spanContext()?.spanId,
name,
recorder: getRecorder({ operation, parsed, system }),
parent: context.segment,
Expand Down
1 change: 1 addition & 0 deletions lib/otel/segments/http-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function createHttpExternalSegment(agent, otelSpan) {
const host = otelSpan.attributes[ATTR_HTTP_HOST] || 'Unknown'
const name = NAMES.EXTERNAL.PREFIX + host
const segment = agent.tracer.createSegment({
id: otelSpan?.spanContext()?.spanId,
name,
recorder: recordExternal(host, 'http'),
parent: context.segment,
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
1 change: 1 addition & 0 deletions lib/otel/segments/producer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function createProducerSegment(agent, otelSpan) {
const name = setName(otelSpan)

const segment = agent.tracer.createSegment({
id: otelSpan?.spanContext()?.spanId,
name,
recorder: genericRecorder,
parent: context.segment,
Expand Down
2 changes: 2 additions & 0 deletions lib/otel/segments/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function rpcSegment({ agent, otelSpan, transaction, rpcSystem }) {
transaction.nameState.setPrefix(rpcSystem)
transaction.nameState.appendPath(transaction.url)
const segment = agent.tracer.createSegment({
id: otelSpan?.spanContext()?.spanId,
name,
recorder: httpRecorder,
parent: transaction.trace.root,
Expand All @@ -71,6 +72,7 @@ function httpSegment({ agent, otelSpan, transaction, httpMethod }) {
// accept dt headers?
// synthetics.assignHeadersToTransaction(agent.config, transaction, )
return agent.tracer.createSegment({
id: otelSpan?.spanContext()?.spanId,
recorder: httpRecorder,
name: requestUrl.pathname,
parent: transaction.trace.root,
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 @@ -15,6 +15,7 @@ const {
ATTR_DB_SYSTEM,
ATTR_GRPC_STATUS_CODE,
ATTR_HTTP_ROUTE,
ATTR_HTTP_RES_STATUS_CODE,
ATTR_HTTP_STATUS_CODE,
ATTR_HTTP_STATUS_TEXT,
ATTR_MESSAGING_MESSAGE_CONVERSATION_ID,
Expand Down Expand Up @@ -86,7 +87,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 @@ -97,14 +101,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 4f2edcb

Please sign in to comment.