| 1 | package ForgeOps::Tracker::SpanBuffer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use POSIX qw(strftime); | ||
| 6 | use ForgeOps::Tracker::SqlStatement; | ||
| 7 | use ForgeOps::Tracker::TraceParent; | ||
| 8 | |||
| 9 | # One trace's worth of spans (a request's own call tree), sharing a single trace id. Held in a | ||
| 10 | # plain package variable on ForgeOps::Tracker for the same one-process-per-worker reasoning its | ||
| 11 | # $current_user and @current_breadcrumbs document. Nesting comes from a stack of open span ids: a | ||
| 12 | # span opened while another is open becomes its child, and anything else parents under the root. | ||
| 13 | # | ||
| 14 | # The trace id and remote parent span id come from the request's context (see | ||
| 15 | # ForgeOps::Tracker::start_trace), so a trace this sends and an error event from the same request | ||
| 16 | # always agree on which trace they belong to. When the request continued another service's trace, | ||
| 17 | # the root span's parent_span_id is that service's span, which ForgeOps treats as a remote parent. | ||
| 18 | use constant MAX_SPANS => 500; | ||
| 19 | |||
| 20 | # The kinds the ingestion API accepts; anything else would fail validation for the whole trace, | ||
| 21 | # so an unknown kind is sent as "other" instead. | ||
| 22 | my %KINDS = map { $_ => 1 } qw(controller service database redis http job other); | ||
| 23 | |||
| 24 | # new($configuration, trace_id => ..., remote_parent_span_id => ...): both optional; a fresh W3C | ||
| 25 | # trace id is generated when none is given. | ||
| 26 | sub new { | ||
| 27 | my ($class, $configuration, %options) = @_; | ||
| 28 | return bless { | ||
| 29 | configuration => $configuration, | ||
| 30 | trace_id => defined $options{trace_id} ? $options{trace_id} : ForgeOps::Tracker::TraceParent::generate_trace_id(), | ||
| 31 | remote_parent_span_id => $options{remote_parent_span_id}, | ||
| 32 | root_span_id => ForgeOps::Tracker::TraceParent::generate_span_id(), | ||
| 33 | spans => [], | ||
| 34 | open => [], | ||
| 35 | }, $class; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub trace_id { $_[0]{trace_id} } | ||
| 39 | |||
| 40 | # Opens a span and returns its id; pair with finish(). $id is one generated beforehand, as | ||
| 41 | # ForgeOps::Tracker::http_span does so its outgoing traceparent header can name the span. | ||
| 42 | sub open_span { | ||
| 43 | my ($self, $id) = @_; | ||
| 44 | $id = ForgeOps::Tracker::TraceParent::generate_span_id() unless defined $id; | ||
| 45 | push @{ $self->{open} }, $id; | ||
| 46 | return $id; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub finish { | ||
| 50 | my ($self, $id, $name, $kind, $started_at, $duration_ms, $data) = @_; | ||
| 51 | my $parent = $self->_current_parent_without($id); | ||
| 52 | $self->{open} = [ grep { $_ ne $id } @{ $self->{open} } ]; | ||
| 53 | $self->_record($id, $parent, $name, $kind, $started_at, $duration_ms, $data); | ||
| 54 | } | ||
| 55 | |||
| 56 | # Records an already-finished span as a child of whatever is currently open. | ||
| 57 | sub record_leaf { | ||
| 58 | my ($self, $name, $kind, $started_at, $duration_ms, $data) = @_; | ||
| 59 | $self->_record(ForgeOps::Tracker::TraceParent::generate_span_id(), $self->_current_parent, $name, $kind, $started_at, $duration_ms, $data); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _current_parent { | ||
| 63 | my ($self) = @_; | ||
| 64 | return @{ $self->{open} } ? $self->{open}[-1] : $self->{root_span_id}; | ||
| 65 | } | ||
| 66 | |||
| 67 | # The parent of span $id: whatever is open just beneath it on the stack, or the root. | ||
| 68 | sub _current_parent_without { | ||
| 69 | my ($self, $id) = @_; | ||
| 70 | my @open = @{ $self->{open} }; | ||
| 71 | for my $i (reverse 0 .. $#open) { | ||
| 72 | next unless $open[$i] eq $id; | ||
| 73 | return $i > 0 ? $open[ $i - 1 ] : $self->{root_span_id}; | ||
| 74 | } | ||
| 75 | return $self->{root_span_id}; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _record { | ||
| 79 | my ($self, $id, $parent, $name, $kind, $started_at, $duration_ms, $data) = @_; | ||
| 80 | return if @{ $self->{spans} } >= MAX_SPANS - 1; # leave room for the root | ||
| 81 | push @{ $self->{spans} }, $self->_build($id, $parent, $name, $kind, $started_at, $duration_ms, $data); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _build { | ||
| 85 | my ($self, $id, $parent, $name, $kind, $started_at, $duration_ms, $data) = @_; | ||
| 86 | my $config = $self->{configuration}; | ||
| 87 | return { | ||
| 88 | span_id => $id, | ||
| 89 | parent_span_id => $parent, | ||
| 90 | name => $name, | ||
| 91 | kind => (defined $kind && $KINDS{$kind}) ? $kind : 'other', | ||
| 92 | started_at => _format_time($started_at), | ||
| 93 | duration_ms => 0 + sprintf('%.2f', $duration_ms), | ||
| 94 | environment => $config->{environment}, | ||
| 95 | release => $config->{release}, | ||
| 96 | data => (defined $kind && $kind eq 'database') ? mask_database_data($data) : ($data || {}), | ||
| 97 | }; | ||
| 98 | } | ||
| 99 | |||
| 100 | # mask_database_data($data): a copy of a database span's data with "db.statement" masked by | ||
| 101 | # ForgeOps::Tracker::SqlStatement (with its "db.system", so a MySQL or MariaDB query's "double | ||
| 102 | # quoted" strings are masked too), so the SQL as written never reaches the payload however the hash | ||
| 103 | # was built. A statement that isn't a plain string, or is blank, is dropped. The caller's hash is | ||
| 104 | # never modified. | ||
| 105 | sub mask_database_data { | ||
| 106 | my ($data) = @_; | ||
| 107 | return {} unless ref $data eq 'HASH'; | ||
| 108 | return $data unless exists $data->{'db.statement'}; | ||
| 109 | |||
| 110 | my %masked = %$data; | ||
| 111 | my $statement = delete $masked{'db.statement'}; | ||
| 112 | $statement = ForgeOps::Tracker::SqlStatement::mask($statement, system => $masked{'db.system'}) | ||
| 113 | if defined $statement && !ref $statement; | ||
| 114 | $masked{'db.statement'} = $statement if defined $statement && !ref $statement; | ||
| 115 | return \%masked; | ||
| 116 | } | ||
| 117 | |||
| 118 | # The wire payload once the trace is over: the root span plus everything recorded beneath it, or | ||
| 119 | # undef when the root was faster than trace_capture_threshold (seconds) and the request didn't | ||
| 120 | # error. An errored request's trace is always sent, however fast it was, since the waterfall of | ||
| 121 | # what led up to an error is exactly what an issue page wants to show next to it. | ||
| 122 | sub finish_trace { | ||
| 123 | my ($self, $root_name, $started_at, $duration_ms, $errored) = @_; | ||
| 124 | return undef if !$errored && $duration_ms < $self->{configuration}{trace_capture_threshold} * 1000; | ||
| 125 | |||
| 126 | return { | ||
| 127 | trace_id => $self->{trace_id}, | ||
| 128 | spans => [ | ||
| 129 | $self->_build($self->{root_span_id}, $self->{remote_parent_span_id}, $root_name, 'controller', $started_at, $duration_ms, {}), | ||
| 130 | @{ $self->{spans} }, | ||
| 131 | ], | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | |||
| 135 | # ISO 8601 with milliseconds, UTC, from an epoch-seconds float. | ||
| 136 | sub _format_time { | ||
| 137 | my ($epoch_seconds) = @_; | ||
| 138 | my $whole = int($epoch_seconds); | ||
| 139 | return strftime('%Y-%m-%dT%H:%M:%S', gmtime($whole)) . sprintf('.%03dZ', int(($epoch_seconds - $whole) * 1000)); | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; |