Skip to content

Commit c98bd79

Browse files
committed
refactor out Transaction.applySamplingStrategy
1 parent c8ef470 commit c98bd79

File tree

6 files changed

+29
-36
lines changed

6 files changed

+29
-36
lines changed

lib/samplers/adaptive-sampler.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,16 @@ class AdaptiveSampler extends Sampler {
106106
return false
107107
}
108108

109-
applySamplingDecision(transaction) {
109+
applySamplingDecision({ transaction, tracestate = null }) {
110110
if (!transaction) return
111+
if (tracestate) {
112+
// Explicitly set sampled and priority from tracestate intrinsics if available
113+
transaction.sampled = tracestate?.intrinsics ? tracestate.isSampled : null
114+
transaction.priority = tracestate?.intrinsics ? tracestate.priority : null
115+
return
116+
}
117+
118+
// If a tracestate is not defined, then do our normal priority calculation.
111119
// eslint-disable-next-line sonarjs/pseudo-random
112120
const initPriority = Math.random()
113121
transaction.sampled = this.shouldSample(initPriority)

lib/samplers/always-off-sampler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
const Sampler = require('./sampler')
77

88
class AlwaysOffSampler extends Sampler {
9-
applySamplingDecision(transaction) {
9+
applySamplingDecision({ transaction }) {
1010
if (!transaction) return
1111
transaction.priority = 0
1212
transaction.sampled = false

lib/samplers/always-on-sampler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
const Sampler = require('./sampler')
77

88
class AlwaysOnSampler extends Sampler {
9-
applySamplingDecision(transaction) {
9+
applySamplingDecision({ transaction }) {
1010
if (!transaction) return
1111
transaction.priority = 2.0
1212
transaction.sampled = true

lib/samplers/ratio-based-sampler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TraceIdRatioBasedSampler extends Sampler {
2525
this._upperBound = Math.floor(this._ratio * 0xffffffff)
2626
}
2727

28-
applySamplingDecision(transaction) {
28+
applySamplingDecision({ transaction }) {
2929
if (!transaction) return
3030
// eslint-disable-next-line sonarjs/pseudo-random
3131
const initPriority = Math.random()

lib/samplers/sampler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class Sampler {
1111
/**
1212
* Sets `priority` and `sampled` on the transaction
1313
* in respect to this sampler's decision.
14-
* @param {Transaction} transaction the transaction to update
14+
* @param {object} params to make the sampling decision with
15+
* @param {Transaction} params.transaction the transaction to update
1516
*/
16-
applySamplingDecision(transaction) {
17+
applySamplingDecision({ transaction }) {
1718
throw new Error('must implement applySamplingDecision for %s', transaction)
1819
}
1920
}

lib/transaction/index.js

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,35 +1029,13 @@ function acceptTraceContextPayload(traceparentHeader, tracestateHeader, transpor
10291029
}
10301030

10311031
// Decide sampling from w3c data
1032+
let sampler = null
10321033
if (traceparent.isSampled === true) {
1033-
applySamplingStrategy({ transaction: this, tracestate, sampler: this.agent.remoteParentSampledSampler })
1034+
sampler = this.agent.remoteParentSampledSampler
1035+
sampler.applySamplingDecision({ transaction: this, tracestate })
10341036
} else if (traceparent.isSampled === false) {
1035-
applySamplingStrategy({ transaction: this, tracestate, sampler: this.agent.remoteParentNotSampledSampler })
1036-
}
1037-
}
1038-
1039-
/**
1040-
* Updates the transaction's `sampled` and `priority` properties
1041-
* based on the given sampling configuration.
1042-
*
1043-
* @param {*} params Function parameters.
1044-
* @param {Transaction} params.transaction The transaction to update.
1045-
* @param {Tracestate} [params.tracestate] A tracestate object with embedded New Relic intrinsics.
1046-
* @param {object} params.sampler The sampler to use, e.g. AdaptiveSampler or TraceIdRatioBasedSampler.
1047-
*/
1048-
function applySamplingStrategy({ transaction, tracestate = null, sampler }) {
1049-
if (sampler?.toString() !== 'AdaptiveSampler') {
1050-
sampler.applySamplingDecision(transaction)
1051-
} else {
1052-
// This case is if we're coming from the deprecated newrelic header logic
1053-
// and we don't want to override the sampled and priority values
1054-
if (!tracestate) {
1055-
return
1056-
}
1057-
1058-
// Explicitly set sampled and priority from tracestate intrinsics if available
1059-
transaction.sampled = tracestate?.intrinsics ? tracestate.isSampled : null
1060-
transaction.priority = tracestate?.intrinsics ? tracestate.priority : null
1037+
sampler = this.agent.remoteParentNotSampledSampler
1038+
sampler.applySamplingDecision({ transaction: this, tracestate })
10611039
}
10621040
}
10631041

@@ -1164,10 +1142,16 @@ const _dtDefineAttrsFromTraceData = function _dtDefineAttrsFromTraceData(data, t
11641142
// Even though New Relic headers are deprecated,
11651143
// we still have to apply our sampling decision on top
11661144
// of the priority and sampled values we receive.
1145+
// However, this only applies if the sampler is NOT
1146+
// the default sampler (adaptive). In that case,
1147+
// we leave it alone.
1148+
let sampler
11671149
if (data?.sa) {
1168-
applySamplingStrategy({ transaction: this, sampler: this.agent.remoteParentSampledSampler })
1150+
sampler = this.agent.remoteParentSampledSampler
1151+
if (sampler.toString() !== 'AdaptiveSampler') sampler.applySamplingDecision({ transaction: this })
11691152
} else {
1170-
applySamplingStrategy({ transaction: this, sampler: this.agent.remoteParentNotSampledSampler })
1153+
sampler = this.agent.remoteParentNotSampledSampler
1154+
if (sampler.toString() !== 'AdaptiveSampler') sampler.applySamplingDecision({ transaction: this })
11711155
}
11721156
}
11731157

@@ -1384,7 +1368,7 @@ Transaction.prototype.isSampled = function isSampled() {
13841368
Transaction.prototype._calculatePriority = function _calculatePriority() {
13851369
if (this.priority === null) {
13861370
const sampler = this.agent.transactionSampler
1387-
sampler.applySamplingDecision(this)
1371+
sampler.applySamplingDecision({ transaction: this })
13881372
}
13891373
}
13901374

0 commit comments

Comments
 (0)