-
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 segment synthesizer and provided ability to convert http …
…client otel spans to external http trace segments
- Loading branch information
Showing
7 changed files
with
452 additions
and
66 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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' | ||
const { RulesEngine } = require('./rules') | ||
const defaultLogger = require('../logger').child({ component: 'segment-synthesizer' }) | ||
const NAMES = require('../metrics/names') | ||
const { SEMATTRS_HTTP_HOST } = require('@opentelemetry/semantic-conventions') | ||
|
||
class SegmentSynthesizer { | ||
constructor(agent, { logger = defaultLogger } = {}) { | ||
this.agent = agent | ||
this.logger = logger | ||
this.engine = new RulesEngine() | ||
} | ||
|
||
synthesize(otelSpan) { | ||
const rule = this.engine.test(otelSpan) | ||
if (!rule?.type) { | ||
this.logger.debug( | ||
'Cannot match a rule to span name: %s, kind %s', | ||
otelSpan?.name, | ||
otelSpan?.kind | ||
) | ||
return | ||
} | ||
|
||
if (rule?.type === 'external') { | ||
return this.createExternalSegment(otelSpan) | ||
} | ||
this.logger.debug('Found type: %s, no synthesize rule currently built', rule.type) | ||
} | ||
|
||
// TODO: should we move these to somewhere else and use in the places | ||
// where external segments are created in our agent | ||
createExternalSegment(otelSpan) { | ||
const context = this.agent.tracer.getContext() | ||
const host = otelSpan.attributes[SEMATTRS_HTTP_HOST] || 'Unknown' | ||
const name = NAMES.EXTERNAL.PREFIX + host | ||
return this.agent.tracer.createSegment({ | ||
name, | ||
parent: context.segment, | ||
transaction: context.transaction | ||
}) | ||
} | ||
} | ||
|
||
module.exports = SegmentSynthesizer |
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,102 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const test = require('node:test') | ||
const assert = require('node:assert') | ||
|
||
const helper = require('../../../lib/agent_helper') | ||
const { ROOT_CONTEXT, SpanKind, TraceFlags } = require('@opentelemetry/api') | ||
const { BasicTracerProvider, Span } = require('@opentelemetry/sdk-trace-base') | ||
const SegmentSynthesizer = require('../../../../lib/otel/segment-synthesis') | ||
const { | ||
SEMATTRS_DB_SYSTEM, | ||
SEMATTRS_HTTP_HOST, | ||
SEMATTRS_HTTP_METHOD | ||
} = require('@opentelemetry/semantic-conventions') | ||
const createMockLogger = require('../../mocks/logger') | ||
|
||
test.beforeEach((ctx) => { | ||
const loggerMock = createMockLogger() | ||
const agent = helper.loadMockedAgent() | ||
const synthesizer = new SegmentSynthesizer(agent, { logger: loggerMock }) | ||
const tracer = new BasicTracerProvider().getTracer('default') | ||
const parentId = '5c1c63257de34c67' | ||
ctx.nr = { | ||
agent, | ||
loggerMock, | ||
parentId, | ||
synthesizer, | ||
tracer | ||
} | ||
}) | ||
|
||
test.afterEach((ctx) => { | ||
helper.unloadAgent(ctx.nr.agent) | ||
}) | ||
|
||
test('should create http external segment from otel http client span', (t, end) => { | ||
const { agent, synthesizer, parentId, tracer } = t.nr | ||
helper.runInTransaction(agent, (tx) => { | ||
const spanContext = { | ||
traceId: tx.trace.id, | ||
spanId: tx.trace.root.id, | ||
traceFlags: TraceFlags.SAMPLED | ||
} | ||
const span = new Span(tracer, ROOT_CONTEXT, 'test-span', spanContext, SpanKind.CLIENT, parentId) | ||
span.setAttribute(SEMATTRS_HTTP_METHOD, 'GET') | ||
span.setAttribute(SEMATTRS_HTTP_HOST, 'newrelic.com') | ||
const segment = synthesizer.synthesize(span) | ||
assert.equal(segment.name, 'External/newrelic.com') | ||
assert.equal(segment.parentId, tx.trace.root.id) | ||
tx.end() | ||
end() | ||
}) | ||
}) | ||
|
||
test('should log warning if a rule does have a synthesis for the given type', (t, end) => { | ||
const { agent, synthesizer, loggerMock, parentId, tracer } = t.nr | ||
|
||
helper.runInTransaction(agent, (tx) => { | ||
const spanContext = { | ||
traceId: tx.trace.id, | ||
spanId: tx.trace.root.id, | ||
traceFlags: TraceFlags.SAMPLED | ||
} | ||
const span = new Span(tracer, ROOT_CONTEXT, 'test-span', spanContext, SpanKind.CLIENT, parentId) | ||
span.setAttribute(SEMATTRS_DB_SYSTEM, 'postgres') | ||
const segment = synthesizer.synthesize(span) | ||
assert.ok(!segment) | ||
assert.deepEqual(loggerMock.debug.args[0], [ | ||
'Found type: %s, no synthesize rule currently built', | ||
'db' | ||
]) | ||
tx.end() | ||
end() | ||
}) | ||
}) | ||
|
||
test('should log warning span does not match a rule', (t, end) => { | ||
const { agent, synthesizer, loggerMock, parentId, tracer } = t.nr | ||
|
||
helper.runInTransaction(agent, (tx) => { | ||
const spanContext = { | ||
traceId: tx.trace.id, | ||
spanId: tx.trace.root.id, | ||
traceFlags: TraceFlags.SAMPLED | ||
} | ||
|
||
const span = new Span(tracer, ROOT_CONTEXT, 'test-span', spanContext, 'bogus', parentId) | ||
const segment = synthesizer.synthesize(span) | ||
assert.ok(!segment) | ||
assert.deepEqual(loggerMock.debug.args[0], [ | ||
'Cannot match a rule to span name: %s, kind %s', | ||
'test-span', | ||
'bogus' | ||
]) | ||
tx.end() | ||
end() | ||
}) | ||
}) |
Oops, something went wrong.