| 1 | package Sentry::Tracing::Span; | ||
| 2 | use Mojo::Base -base, -signatures; | ||
| 3 | |||
| 4 | use HTTP::Status qw(status_message); | ||
| 5 | use Readonly; | ||
| 6 | use Sentry::Tracing::Status; | ||
| 7 | use Sentry::Tracing::Transaction; | ||
| 8 | use Sentry::Util qw(uuid4); | ||
| 9 | use Time::HiRes qw(time); | ||
| 10 | |||
| 11 | Readonly my $SPAN_ID_LENGTH => 16; | ||
| 12 | |||
| 13 | # https://develop.sentry.dev/sdk/unified-api/tracing | ||
| 14 | |||
| 15 | # Hexadecimal string representing a uuid4 value. The length is exactly 32 | ||
| 16 | # characters. Dashes are not allowed. Has to be lowercase | ||
| 17 | has span_id => sub { substr(uuid4(), 0, $SPAN_ID_LENGTH) }; | ||
| 18 | |||
| 19 | # Optional. A map or list of tags for this event. Each tag must be less than 200 | ||
| 20 | # characters. | ||
| 21 | has tags => sub { {} }; | ||
| 22 | |||
| 23 | # Required. Determines which trace the Span belongs to. The value should be 16 | ||
| 24 | # random bytes encoded as a hex string (32 characters long). | ||
| 25 | has trace_id => sub { uuid4() }; | ||
| 26 | |||
| 27 | # Recommended. Short code identifying the type of operation the span is | ||
| 28 | # measuring. | ||
| 29 | has op => undef; | ||
| 30 | |||
| 31 | # Optional. Longer description of the span's operation, which uniquely | ||
| 32 | # identifies the span but is consistent across instances of the span. | ||
| 33 | has description => undef; | ||
| 34 | |||
| 35 | # Required. A timestamp representing when the measuring started. The format is | ||
| 36 | # either a string as defined in RFC 3339 or a numeric (integer or float) value | ||
| 37 | # representing the number of seconds that have elapsed since the Unix epoch. The | ||
| 38 | # start_timestamp value must be greater or equal the timestamp value, otherwise | ||
| 39 | # the Span is discarded as invalid. | ||
| 40 | has start_timestamp => time; | ||
| 41 | |||
| 42 | # Required. A timestamp representing when the measuring finished. The format is | ||
| 43 | # either a string as defined in RFC 3339 or a numeric (integer or float) value | ||
| 44 | # representing the number of seconds that have elapsed since the Unix epoch. | ||
| 45 | has timestamp => undef; | ||
| 46 | |||
| 47 | # Optional. Describes the status of the Span/Transaction. | ||
| 48 | has status => undef; | ||
| 49 | |||
| 50 | # Optional. Arbitrary data associated with this Span. | ||
| 51 | has data => undef; | ||
| 52 | |||
| 53 | has parent_span_id => undef; | ||
| 54 | |||
| 55 | # Was this span chosen to be sent as part of the sample? | ||
| 56 | has sampled => undef; | ||
| 57 | |||
| 58 | has spans => sub { [] }; | ||
| 59 | has transaction => undef; | ||
| 60 | |||
| 61 | sub start_child ($self, $span_context = {}) { | ||
| 62 | my $child_span = Sentry::Tracing::Span->new({ | ||
| 63 | $span_context->%*, | ||
| 64 | parent_span_id => $self->span_id, | ||
| 65 | sampled => $self->sampled, | ||
| 66 | trace_id => $self->trace_id, | ||
| 67 | start_timestamp => time, | ||
| 68 | }); | ||
| 69 | |||
| 70 | push $self->spans->@*, $child_span; | ||
| 71 | |||
| 72 | $child_span->transaction($self->transaction); | ||
| 73 | |||
| 74 | return $child_span; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub get_trace_context ($self) { | ||
| 78 | return { | ||
| 79 | data => $self->data, | ||
| 80 | description => $self->description, | ||
| 81 | op => $self->op, | ||
| 82 | parent_span_id => $self->parent_span_id, | ||
| 83 | span_id => $self->span_id, | ||
| 84 | status => $self->status, | ||
| 85 | tags => $self->tags, | ||
| 86 | trace_id => $self->trace_id, | ||
| 87 | }; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub TO_JSON ($self) { | ||
| 91 | return { | ||
| 92 | data => $self->data, | ||
| 93 | description => $self->description, | ||
| 94 | op => $self->op, | ||
| 95 | parent_span_id => $self->parent_span_id, | ||
| 96 | span_id => $self->span_id, | ||
| 97 | start_timestamp => $self->start_timestamp, | ||
| 98 | status => $self->status, | ||
| 99 | tags => $self->tags, | ||
| 100 | timestamp => $self->timestamp, | ||
| 101 | trace_id => $self->trace_id, | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub set_tag ($self, $key, $value) { | ||
| 106 | $self->tags({ $self->tags->%*, $key => $value }); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub set_http_status ($self, $status) { | ||
| 110 | $self->set_tag('http.status_code' => $status); | ||
| 111 | $self->status(Sentry::Tracing::Status->from_http_code($status)); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub to_hash ($self) { | ||
| 115 | return { 'sentry-trace' => $self->to_sentry_trace }; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub finish ($self) { | ||
| 119 | $self->timestamp(time); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; |