Skip to content

Instantly share code, notes, and snippets.

@Lms24
Last active June 12, 2024 11:04
Show Gist options
  • Save Lms24/9a631295aef58cf22fb8f5307953335c to your computer and use it in GitHub Desktop.
Save Lms24/9a631295aef58cf22fb8f5307953335c to your computer and use it in GitHub Desktop.
show the difference of storing a sampling decision on the client for subsequent trace propagations vs. not doing that
// Assumption for this script:
// All backend requests happen without an active client-side span. Meaning, depending on the values stored
// PropagationContext, server side transactions are either sampled with a server-side sample rate or
// force-sampled by continuing the trace from the client.
const serverSampleRate = 0.05;
const clientSampleRate = 0.2;
const serverTxnsPerPage = 10;
const repetitions = 10_000;
let longTraceSampledTxns = 0;
let shortTracesSampledTxns = 0;
for (let i = 0; i < repetitions; i++) {
const clientSideSampled = sampleFrontendSpan();
if (clientSideSampled) {
longTraceSampledTxns++;
for (let j = 0; j < serverTxnsPerPage; j++) {
const backendSampled = sampleBackendSpan(true);
if (backendSampled) {
longTraceSampledTxns++;
}
}
}
}
for (let i = 0; i < repetitions; i++) {
const clientSideSampled = sampleFrontendSpan();
if (clientSideSampled) {
shortTracesSampledTxns++;
}
for (let j = 0; j < serverTxnsPerPage; j++) {
const backendSampled = sampleBackendSpan(false);
if (backendSampled) {
shortTracesSampledTxns++;
}
}
}
function sampleFrontendSpan() {
return Math.random() < clientSampleRate;
}
function sampleBackendSpan(force) {
return force || Math.random() < serverSampleRate;
}
console.log('Long traces - txns sampled:', longTraceSampledTxns);
console.log('Short traces - txns sampled:', shortTracesSampledTxns);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment