Skip to content

Commit b45554d

Browse files
dyladanyurishkuro
andauthored
Add the random trace id flag (#474) (#486)
* Add the random trace id flag * Wording * Wording Co-authored-by: Yuri Shkuro <[email protected]> * Add SHOULD wording to trace id randomness * Specify that implementers SHOULD set random flag when appropriate * Random flag means at least 7 bytes * Flag wording * Only 2 flags are specified * Remove redundant wording * At least 7 bytes * Review comments * Remove RECOMMENDED wording Co-authored-by: Yuri Shkuro <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]>
1 parent bfcc82c commit b45554d

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Diff for: spec/20-http_request_header_format.md

+27-8
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ trace-flags = 2HEXDIGLC ; 8 bit flags. Currently, only one bit is used. S
100100

101101
#### trace-id
102102

103-
This is the ID of the whole trace forest and is used to uniquely identify a <a href="#dfn-distributed-traces">distributed trace</a> through a system. It is represented as a 16-byte array, for example, `4bf92f3577b34da6a3ce929d0e0e4736`. All bytes as zero (`00000000000000000000000000000000`) is considered an invalid value.
103+
This is the ID of the whole trace forest and is used to uniquely identify a <a href="#dfn-distributed-traces">distributed trace</a> through a system.
104+
It is represented as a 16-byte array, for example, `4bf92f3577b34da6a3ce929d0e0e4736`.
105+
All bytes as zero (`00000000000000000000000000000000`) is considered an invalid value.
104106

105-
If the `trace-id` value is invalid (for example if it contains non-allowed characters or all zeros), vendors MUST ignore the `traceparent`.
107+
The value of `trace-id` SHOULD be globally unique.
108+
One recommended method to ensure global uniqueness, as well as to address some privacy and security considerations, to a satisfactory degree of certainty is to randomly (or pseudo-randomly) generate the `trace-id`.
109+
Implementers SHOULD use a `trace-id` generation method which randomly (or pseudo-randomly) generates at least the right-most 7 bytes of the ID.
110+
If the right-most 7 bytes are randomly (or pseudo-randomly) generated, the corresponding [random trace id flag](#random-trace-id-flag) SHOULD be set.
111+
For more details, see [considerations for trace-id field generation](#considerations-for-trace-id-field-generation).
106112

107-
See [considerations for trace-id field
108-
generation](#considerations-for-trace-id-field-generation) for recommendations
109-
on how to operate with `trace-id`.
113+
If the `trace-id` value is invalid (for example if it contains non-allowed characters or all zeros), vendors MUST ignore the `traceparent`.
110114

111115
#### parent-id
112116

@@ -116,6 +120,8 @@ Vendors MUST ignore the `traceparent` when the `parent-id` is invalid (for examp
116120

117121
#### trace-flags
118122

123+
The current version of this specification (`00`) supports only two flags: `sampled` and `random-trace-id`.
124+
119125
An <a data-cite='!BIT-FIELD#firstHeading'>8-bit field</a> that controls tracing flags such as sampling, trace level, etc. These flags are recommendations given by the caller rather than strict rules to follow for three reasons:
120126

121127
1. An untrusted caller may be able to abuse a tracing system by setting these flags maliciously.
@@ -126,20 +132,22 @@ You can find more in the section [Security considerations](#security-considerati
126132

127133
Like other fields, `trace-flags` is hex-encoded. For example, all `8` flags set would be `ff` and no flags set would be `00`.
128134

129-
As this is a bit field, you cannot interpret flags by decoding the hex value and looking at the resulting number. For example, a flag `00000001` could be encoded as `01` in hex, or `09` in hex if present with the flag `00001000`. A common mistake in bit fields is forgetting to mask when interpreting flags.
135+
As this is a bit field, the flags cannot be interpreted by a simple equality comparison.
136+
For example, both `01` (`00000001`) and `03` (`00000011`) represent that the trace has been sampled because the sampled flag (`00000001`) is set, and `03` and `02` (`00000010`) both represent that at least the right-most 7 bytes of the `trace-id` are randomly (or pseudo-randomly) generated because the random bit (`00000010`) is set.
137+
A common mistake when interpreting bit-fields is using a comparison of the whole number rather than interpreting a single bit.
130138

131139
Here is an example of properly handling trace flags:
132140

133141
``` java
134142
static final byte FLAG_SAMPLED = 1; // 00000001
143+
static final byte FLAG_RANDOM = 2; // 00000010
135144
...
136145
boolean sampled = (traceFlags & FLAG_SAMPLED) == FLAG_SAMPLED;
146+
boolean random = (traceFlags & FLAG_RANDOM) == FLAG_RANDOM;
137147
```
138148

139149
##### Sampled flag
140150

141-
The current version of this specification (`00`) only supports a single flag called `sampled`.
142-
143151
When set, the least significant bit (right-most), denotes that the caller may have recorded trace data. When unset, the caller did not record trace data out-of-band.
144152

145153
There are a number of recording scenarios that may break distributed tracing:
@@ -176,6 +184,17 @@ There are two additional options that vendors MAY follow:
176184
- A component that makes a deferred or delayed recording decision may communicate the priority of a recording by setting `sampled` flag to `1` for a subset of requests.
177185
- A component may also fall back to probability sampling and set the `sampled` flag to `1` for the subset of requests.
178186

187+
##### Random Trace ID Flag
188+
189+
The second least significant bit of the trace-flags field denotes the random-trace-id flag.
190+
If that flag is set, at least the right-most 7 bytes of the trace ID MUST be random (or pseudo-random).
191+
If the flag is not set, the trace ID MAY still be randomly (or pseudo-randomly) generated.
192+
When unset, the trace ID MAY be generated in any way that satisfies the requirements of the [trace ID format](#trace-id).
193+
194+
When at least the right-most 7 bytes of the `trace-id` are randomly (or pseudo-randomly) generated, the random trace ID flag SHOULD be set to `1`.
195+
This allows downstream consumers to implement features such as trace sampling or database sharding based on these bytes.
196+
For additional information, see [considerations for trace-id field generation](#considerations-for-trace-id-field-generation).
197+
179198
##### Other Flags
180199

181200
The behavior of other flags, such as (`00000100`) is not defined and is reserved for future use. Vendors MUST set those to zero.

Diff for: spec/60-trace-id-format.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ concerns](#privacy-considerations) of exposing unwanted information. Randomness
2222
also allows tracing vendors to base sampling decisions on `trace-id` field value
2323
and avoid propagating an additional sampling context.
2424

25+
If the `random-trace-id` flag is set, at least the right-most 7 bytes of the
26+
`trace-id` MUST be randomly (or pseudo-randomly) generated.
27+
2528
As shown in the next section, if part of the `trace-id` is nonrandom,
2629
it is important for the random part of the `trace-id` to be as far right in the
2730
`trace-id` as possible for better inter-operability with some existing systems.
@@ -50,7 +53,9 @@ systems with these existing systems, the following practices are recommended:
5053
compliant 16 bytes `trace-id` from a shorter identifier, it SHOULD left pad
5154
the original identifier with zeroes. For example, the identifier
5255
`53ce929d0e0e4736`, SHOULD be converted to `trace-id` value
53-
`000000000000000053ce929d0e0e4736`.
56+
`000000000000000053ce929d0e0e4736`. If the resultant `trace-id` value does
57+
not satisfy the constraints of the `random-trace-id` flag, the flag MUST
58+
be set to `0`.
5459
2. When a system receives an inbound message and needs to convert the 16 bytes
5560
`trace-id` to a shorter identifier, the rightmost part of `trace-id` SHOULD
5661
be used as this identifier. For instance, if the value of `trace-id` was

0 commit comments

Comments
 (0)