|
| 1 | +/* |
| 2 | + * Copyright 2025 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const SpanLink = require('#agentlib/spans/span-link.js') |
| 9 | +const SpanEvent = require('#agentlib/spans/span-event.js') |
| 10 | +const sinon = require('sinon') |
| 11 | +const assert = require('node:assert') |
| 12 | + |
| 13 | +const TIMESTAMP = 1765285200000 // 2025-12-09T09:00:00.000-04:00 |
| 14 | + |
| 15 | +function setupPartialTransaction(tx, partialType) { |
| 16 | + tx.priority = 42 |
| 17 | + tx.sampled = true |
| 18 | + tx.partialType = partialType |
| 19 | + tx.createPartialTrace() |
| 20 | +} |
| 21 | + |
| 22 | +function createSpanLink({ segment, spanId, traceId, linkSpanId, linkTraceId, testAttr }) { |
| 23 | + const link = new SpanLink({ |
| 24 | + link: { |
| 25 | + attributes: { test: testAttr }, |
| 26 | + context: { spanId: linkSpanId, traceId: linkTraceId } |
| 27 | + }, |
| 28 | + spanContext: { |
| 29 | + spanId, |
| 30 | + traceId |
| 31 | + }, |
| 32 | + timestamp: TIMESTAMP |
| 33 | + }) |
| 34 | + segment.spanLinks.push(link) |
| 35 | + return link |
| 36 | +} |
| 37 | + |
| 38 | +function addSegment({ spanEventAggregator, tx, segment, parentId = '1', isEntry = false }) { |
| 39 | + spanEventAggregator.addSegment({ segment, transaction: tx, parentId, isEntry }) |
| 40 | +} |
| 41 | + |
| 42 | +function stubEntityRelationship(hasEntity) { |
| 43 | + return sinon.stub(SpanEvent.prototype, 'hasEntityRelationshipAttrs').get(() => hasEntity) |
| 44 | +} |
| 45 | + |
| 46 | +function createSegment(agent, id, name, parent, tx) { |
| 47 | + return agent.tracer.createSegment({ |
| 48 | + id, |
| 49 | + name, |
| 50 | + parent, |
| 51 | + transaction: tx |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +function setupPartialTrace({ agent, partialType, tx, addRootSpanLink = false, add99SpanLinks = false, addExitSpan = false, numOfChildSegments = 2 }) { |
| 56 | + setupPartialTransaction(tx, partialType) |
| 57 | + |
| 58 | + // setup root segment and span links on root if needed |
| 59 | + const rootSegment = agent.tracer.getSegment() |
| 60 | + |
| 61 | + if (add99SpanLinks) { |
| 62 | + for (let i = 0; i < 99; i += 1) { |
| 63 | + rootSegment.addSpanLink({ fake: 'link' }) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (addRootSpanLink) { |
| 68 | + createSpanLink({ segment: rootSegment, spanId: rootSegment.id, traceId: 'trace1', linkSpanId: rootSegment.id, linkTraceId: 'trace1', testAttr: 'rootTest' }) |
| 69 | + } |
| 70 | + |
| 71 | + // setup child 1 segment with span link |
| 72 | + const child1Name = addExitSpan ? 'MessageBroker/api.example.com/users' : 'child1-segment' |
| 73 | + const child1Segment = createSegment(agent, 'child1', child1Name, rootSegment, tx) |
| 74 | + createSpanLink({ segment: child1Segment, spanId: 'span1', traceId: 'trace1', linkSpanId: 'parent1', linkTraceId: 'trace1', testAttr: 'test1' }) |
| 75 | + |
| 76 | + if (numOfChildSegments === 1) { |
| 77 | + return { rootSegment, child1Segment } |
| 78 | + } |
| 79 | + |
| 80 | + // setup child 2 segment with span links if needed |
| 81 | + const child2Segment = createSegment(agent, 'child2', 'child2-segment', child1Segment, tx) |
| 82 | + createSpanLink({ segment: child2Segment, spanId: 'span2', traceId: 'trace2', linkSpanId: 'parent2', linkTraceId: 'trace1', testAttr: 'test2' }) |
| 83 | + |
| 84 | + return { rootSegment, child1Segment, child2Segment } |
| 85 | +} |
| 86 | + |
| 87 | +function setupPartialTraceForCompactCompression(agent, tx) { |
| 88 | + setupPartialTransaction(tx, 'compact') |
| 89 | + |
| 90 | + const reparentSpanLinkSpy = sinon.spy(tx.partialTrace, 'reparentSpanLinks') |
| 91 | + const rootSegment = agent.tracer.getSegment() |
| 92 | + |
| 93 | + // first exit span to a message broker |
| 94 | + const child1Segment = createSegment(agent, 'child1', 'MessageBroker/api.example.com/users', rootSegment, tx) |
| 95 | + |
| 96 | + // entry span to another service |
| 97 | + const child2Segment = createSegment(agent, 'child2', 'child2-segment', child1Segment, tx) |
| 98 | + |
| 99 | + // second exit span to the same message broker |
| 100 | + const child3Segment = createSegment(agent, 'child3', 'MessageBroker/api.example.com/users', rootSegment, tx) |
| 101 | + |
| 102 | + // entry span to another service |
| 103 | + const child4Segment = createSegment(agent, 'child4', 'child4-segment', child3Segment, tx) |
| 104 | + |
| 105 | + createSpanLink({ segment: child1Segment, spanId: 'span1', traceId: 'trace1', linkSpanId: 'parent1', linkTraceId: 'trace1', testAttr: 'test1' }) |
| 106 | + createSpanLink({ segment: child3Segment, spanId: 'span1', traceId: 'trace1', linkSpanId: 'parent1', linkTraceId: 'trace1', testAttr: 'test2' }) |
| 107 | + |
| 108 | + return { rootSegment, child1Segment, child2Segment, child3Segment, child4Segment, reparentSpanLinkSpy } |
| 109 | +} |
| 110 | + |
| 111 | +function addSegmentsForCompactCompression({ spanEventAggregator, tx, segments }) { |
| 112 | + addSegment({ spanEventAggregator, tx, segment: segments.rootSegment, isEntry: true }) |
| 113 | + tx.baseSegment = segments.rootSegment |
| 114 | + |
| 115 | + // simulate that the segment has entity relationship attrs to keep the span |
| 116 | + const hasEntityStub = stubEntityRelationship(true) |
| 117 | + |
| 118 | + addSegment({ spanEventAggregator, tx, segment: segments.child1Segment, parentId: segments.rootSegment.id }) |
| 119 | + addSegment({ spanEventAggregator, tx, segment: segments.child2Segment, parentId: segments.child1Segment.id, isEntry: true }) |
| 120 | + addSegment({ spanEventAggregator, tx, segment: segments.child3Segment, parentId: segments.rootSegment.id }) |
| 121 | + addSegment({ spanEventAggregator, tx, segment: segments.child4Segment, parentId: segments.child3Segment.id, isEntry: true }) |
| 122 | + hasEntityStub.restore() |
| 123 | +} |
| 124 | + |
| 125 | +function assertSpanLinkAttributes(spanLinks) { |
| 126 | + for (let i = 0; i < spanLinks.length; i += 1) { |
| 127 | + const link = spanLinks[i] |
| 128 | + assert.equal(Object.keys(link.userAttributes).length, 0) |
| 129 | + assert.equal(Object.keys(link.agentAttributes).length, 0) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +module.exports = { |
| 134 | + setupPartialTrace, |
| 135 | + stubEntityRelationship, |
| 136 | + assertSpanLinkAttributes, |
| 137 | + addSegment, |
| 138 | + createSegment, |
| 139 | + createSpanLink, |
| 140 | + setupPartialTraceForCompactCompression, |
| 141 | + addSegmentsForCompactCompression |
| 142 | +} |
0 commit comments