forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
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
- Loading branch information
1 parent
6adbaa2
commit 6363baa
Showing
4 changed files
with
126 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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_OPERATION | ||
} = require('@opentelemetry/semantic-conventions') | ||
|
||
function createConsumerSegment({ agent, otelSpan }) { | ||
const transaction = new Transaction(agent) | ||
transaction.type = TYPES.BG | ||
|
||
const operation = otelSpan.attributes[SEMATTRS_MESSAGING_OPERATION] | ||
const destination = otelSpan.attributes[SEMATTRS_MESSAGING_DESTINATION] ?? 'unknown' | ||
const segmentName = `OtherTransaction/consumer/${operation}/${destination}` | ||
|
||
transaction.trace.attributes.addAttribute( | ||
DESTINATIONS.TRANS_SCOPE, | ||
'message.queueName', | ||
destination | ||
) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 { 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') | ||
|
||
const expectedName = 'OtherTransaction/consumer/receive/unknown' | ||
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'], | ||
'unknown' | ||
) | ||
|
||
transaction.end() | ||
}) |