Skip to content

Commit 5048dab

Browse files
committed
agents,lib,src,test: add traceSampleRate support
Add end-to-end traceSampleRate handling across config, runtime propagation, tracing decisions, and regression tests. Why: - Enable configurable probabilistic trace sampling with predictable behavior. - Ensure consistent semantics across all config entry points. - Prevent invalid updates from corrupting current sampling behavior. - Keep transaction consistency by deciding sampling at the root span only. What changed: - Added traceSampleRate parsing and normalization in JS config paths with explicit default fallback and finite/range validation in [0, 1]. - Added native config sanitization for traceSampleRate to reject invalid values before merge, preserving previous valid configuration. - Ensured runtime sampling state is synchronized from effective current config after updates to avoid stale shared-memory sample rates. - Added gRPC reconfigure support for traceSampleRate in proto and agent mapping, including generated protobuf updates. - Updated tracing logic so root spans perform the sampling decision and child spans inherit parent traceFlags. - Extended tests for: - invalid value handling (including NaN/Infinity) - env/package bootstrap behavior - partial updates preserving existing traceSampleRate - gRPC invalid-update fallback behavior - sampling behavior at 0%, 50% (tolerance), and 100% - worker-thread sampling behavior - explicit parent/child trace consistency assertions
1 parent 369ce34 commit 5048dab

15 files changed

Lines changed: 508 additions & 43 deletions

agents/grpc/proto/reconfigure.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ message ReconfigureBody {
1818
optional uint32 tracingModulesBlacklist = 11;
1919
optional bool contCpuProfile = 12;
2020
optional bool assetsEnabled = 13;
21+
optional double traceSampleRate = 14;
2122
}
2223

2324
message ReconfigureEvent {

agents/grpc/src/grpc_agent.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,10 @@ void GrpcAgent::reconfigure(const grpcagent::CommandRequest& request) {
17581758
out["assetsEnabled"] = body.assetsenabled();
17591759
}
17601760

1761+
if (body.has_tracesamplerate()) {
1762+
out["traceSampleRate"] = body.tracesamplerate();
1763+
}
1764+
17611765
DebugJSON("Reconfigure out: \n%s\n", out);
17621766

17631767
UpdateConfig(out.dump());

agents/grpc/src/proto/reconfigure.pb.cc

Lines changed: 52 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agents/grpc/src/proto/reconfigure.pb.h

Lines changed: 49 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/internal/otel/trace.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
JSONStringify,
5+
MathRandom,
56
SafeMap,
67
Uint8Array,
78
} = primordials;
@@ -278,10 +279,14 @@ class Tracer {
278279
}
279280

280281
const internalId = newInternalSpanId();
281-
// Follow parent, root always sampled.
282-
let flags = api.TraceFlags.SAMPLED;
283-
if (parentContext)
282+
// Root spans make the sampling decision, child spans inherit parent flags.
283+
let flags;
284+
if (!parentContext) {
285+
const sampled = MathRandom() < binding.trace_sample_rate[0];
286+
flags = sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE;
287+
} else {
284288
flags = parentContext.traceFlags;
289+
}
285290
const spanContext = new SpanContext(internalId,
286291
spanId,
287292
flags,

0 commit comments

Comments
 (0)