« lib/ForgeOps/Tracker/Changes.pm
↑ Summary
blib/lib/ForgeOps/Tracker/Integrations/PSGIPerformance.pm »
| 1 | package ForgeOps::Tracker::TraceParent; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # Reads and writes the W3C Trace Context `traceparent` header (https://www.w3.org/TR/trace-context/), | ||
| 7 | # the vendor-neutral format for carrying one trace across service boundaries: | ||
| 8 | # "00-<32 hex trace-id>-<16 hex parent-id>-<2 hex flags>". Used in both directions: the | ||
| 9 | # PSGI/Dancer2 performance integrations parse an incoming one so the request continues the | ||
| 10 | # caller's trace instead of starting its own, and ForgeOps::Tracker::http_span builds an outgoing | ||
| 11 | # one so the next service along continues this request's. Ported from gems/forge_ops_tracker's | ||
| 12 | # trace_parent.rb. | ||
| 13 | # | ||
| 14 | # Deliberately strict on the way in, the same posture the spec asks receivers to take: a malformed | ||
| 15 | # value, uppercase hex, the reserved version "ff", or an all-zero trace/parent id are all treated | ||
| 16 | # as "no usable header at all" (parse returns undef and the request starts a fresh trace). A | ||
| 17 | # version this client doesn't know yet is still accepted as long as its first four fields have | ||
| 18 | # version 00's shape, which is what the spec says a version-00 parser should do with a future | ||
| 19 | # version; version 00 itself must have exactly four fields. | ||
| 20 | use constant HEADER => 'traceparent'; | ||
| 21 | |||
| 22 | # Always "01" (sampled) on the way out: whether a trace is actually sent is only decided once the | ||
| 23 | # request is over, long after this header has gone out on an outbound call, so there's no honest | ||
| 24 | # earlier answer than "this may be recorded." | ||
| 25 | my $SAMPLED_FLAGS = '01'; | ||
| 26 | |||
| 27 | my $PATTERN = qr{\A([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})(-.*)?\z}s; | ||
| 28 | |||
| 29 | # parse($value): { trace_id => ..., parent_span_id => ... } for a usable header, undef for | ||
| 30 | # anything else (absent, blank, malformed, or one of the explicitly invalid values above). | ||
| 31 | sub parse { | ||
| 32 | my ($value) = @_; | ||
| 33 | return undef if !defined $value || ref $value; | ||
| 34 | |||
| 35 | (my $trimmed = $value) =~ s/\A\s+|\s+\z//g; | ||
| 36 | my ($version, $trace_id, $parent_id, undef, $rest) = $trimmed =~ $PATTERN or return undef; | ||
| 37 | return undef if $version eq 'ff'; | ||
| 38 | return undef if $version eq '00' && defined $rest; | ||
| 39 | return undef if $trace_id eq '0' x 32 || $parent_id eq '0' x 16; | ||
| 40 | |||
| 41 | return { trace_id => $trace_id, parent_span_id => $parent_id }; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub build { | ||
| 45 | my ($trace_id, $span_id) = @_; | ||
| 46 | return "00-$trace_id-$span_id-$SAMPLED_FLAGS"; | ||
| 47 | } | ||
| 48 | |||
| 49 | # 32 lowercase hex characters, the W3C trace-id format; never all zeros. | ||
| 50 | sub generate_trace_id { _random_hex(16) } | ||
| 51 | |||
| 52 | # 16 lowercase hex characters, the W3C parent-id (span id) format; never all zeros. | ||
| 53 | sub generate_span_id { _random_hex(8) } | ||
| 54 | |||
| 55 | sub _random_hex { | ||
| 56 | my ($bytes) = @_; | ||
| 57 | my $hex; | ||
| 58 | do { | ||
| 59 | $hex = ''; | ||
| 60 | if (open my $fh, '<:raw', '/dev/urandom') { | ||
| 61 | read($fh, my $raw, $bytes); | ||
| 62 | close $fh; | ||
| 63 | $hex = unpack('H*', $raw) if defined $raw && length($raw) == $bytes; | ||
| 64 | } | ||
| 65 | # No /dev/urandom (Windows) or a short read: fall back to rand, which is fine for an id | ||
| 66 | # that only has to be unique within one project's traces, not unpredictable. | ||
| 67 | $hex = join('', map { sprintf('%02x', int(rand(256))) } 1 .. $bytes) unless length $hex; | ||
| 68 | } while ($hex !~ /[^0]/); | ||
| 69 | return $hex; | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; |