-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added otel consumer span processing (#2854)
- Loading branch information
1 parent
554b4bf
commit cba5bb5
Showing
6 changed files
with
153 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
module.exports = createConsumerSegment | ||
|
||
// Notes: | ||
// + https://github.com/open-telemetry/semantic-conventions/blob/v1.24.0/docs/messaging/messaging-spans.md | ||
// + We probably want to inspect `messaging.system` so that we can generate | ||
// attributes according to our own internal specs. | ||
|
||
const Transaction = require('../../transaction/') | ||
const { DESTINATIONS, TYPES } = Transaction | ||
|
||
const { | ||
SEMATTRS_MESSAGING_SYSTEM, | ||
SEMATTRS_MESSAGING_DESTINATION, | ||
SEMATTRS_MESSAGING_DESTINATION_KIND | ||
} = require('@opentelemetry/semantic-conventions') | ||
|
||
function createConsumerSegment(agent, otelSpan) { | ||
const transaction = new Transaction(agent) | ||
transaction.type = TYPES.BG | ||
|
||
const system = otelSpan.attributes[SEMATTRS_MESSAGING_SYSTEM] ?? 'unknown' | ||
const destination = otelSpan.attributes[SEMATTRS_MESSAGING_DESTINATION] ?? 'unknown' | ||
const destKind = otelSpan.attributes[SEMATTRS_MESSAGING_DESTINATION_KIND] ?? 'unknown' | ||
const segmentName = `OtherTransaction/Message/${system}/${destKind}/Named/${destination}` | ||
|
||
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({ | ||
name: segmentName, | ||
parent: transaction.trace.root, | ||
transaction | ||
}) | ||
transaction.baseSegment = segment | ||
|
||
return { segment, transaction } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
// Tests to verify that we can map OTEL "consumer" spans to NR segments. | ||
|
||
const test = require('node:test') | ||
const assert = require('node:assert') | ||
|
||
const { BasicTracerProvider } = require('@opentelemetry/sdk-trace-base') | ||
const { SpanKind } = require('@opentelemetry/api') | ||
const { | ||
SEMATTRS_MESSAGING_SYSTEM, | ||
SEMATTRS_MESSAGING_DESTINATION, | ||
SEMATTRS_MESSAGING_DESTINATION_KIND | ||
} = require('@opentelemetry/semantic-conventions') | ||
|
||
const { DESTINATIONS } = require('../../../../lib/transaction') | ||
const helper = require('../../../lib/agent_helper') | ||
const createSpan = require('./fixtures/span') | ||
const SegmentSynthesizer = require('../../../../lib/otel/segment-synthesis') | ||
|
||
test.beforeEach((ctx) => { | ||
const logs = [] | ||
const logger = { | ||
debug(...args) { | ||
logs.push(args) | ||
} | ||
} | ||
const agent = helper.loadMockedAgent() | ||
const synth = new SegmentSynthesizer(agent, { logger }) | ||
const tracer = new BasicTracerProvider().getTracer('default') | ||
|
||
ctx.nr = { | ||
agent, | ||
logger, | ||
logs, | ||
synth, | ||
tracer | ||
} | ||
}) | ||
|
||
test.afterEach((ctx) => { | ||
helper.unloadAgent(ctx.nr.agent) | ||
}) | ||
|
||
test('should create consumer segment from otel span', (t) => { | ||
const { synth, tracer } = t.nr | ||
const span = createSpan({ tracer, kind: SpanKind.CONSUMER }) | ||
span.setAttribute('messaging.operation', 'receive') | ||
span.setAttribute(SEMATTRS_MESSAGING_SYSTEM, 'msgqueuer') | ||
span.setAttribute(SEMATTRS_MESSAGING_DESTINATION, 'dest1') | ||
span.setAttribute(SEMATTRS_MESSAGING_DESTINATION_KIND, 'topic1') | ||
|
||
const expectedName = 'OtherTransaction/Message/msgqueuer/topic1/Named/dest1' | ||
const { segment, transaction } = synth.synthesize(span) | ||
assert.equal(segment.name, expectedName) | ||
assert.equal(segment.parentId, segment.root.id) | ||
assert.equal(transaction.name, expectedName) | ||
assert.equal(transaction.type, 'bg') | ||
assert.equal(transaction.baseSegment, segment) | ||
assert.equal( | ||
transaction.trace.attributes.get(DESTINATIONS.TRANS_SCOPE)['message.queueName'], | ||
'dest1' | ||
) | ||
|
||
transaction.end() | ||
}) |