| 1 | package ForgeOps::Tracker::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use JSON::PP qw(encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use ForgeOps::Tracker::DeliveryStatus; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Delivers one payload over HTTP. Every failure mode: DNS, connection, timeout, a non-2xx | ||||||||||||||||||||||||||||
| 10 | # response: is caught here and turned into a false return rather than a thrown exception, since | ||||||||||||||||||||||||||||
| 11 | # a broken or unreachable tracker must never be able to break the host app. Uses HTTP::Tiny and | ||||||||||||||||||||||||||||
| 12 | # JSON::PP, both core since Perl 5.14: same reasoning as every other SDK in this repo (see e.g. | ||||||||||||||||||||||||||||
| 13 | # sdks/node/src/client.js): this has to work in any host app without adding a dependency of its | ||||||||||||||||||||||||||||
| 14 | # own for something as simple as one POST request. deliver and deliver_performance_samples both | ||||||||||||||||||||||||||||
| 15 | # post through the same private _post helper, which differs only in which URI it posts to and | ||||||||||||||||||||||||||||
| 16 | # what payload shape it sends. | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | # Every deliver* method answers 1 only when the payload was delivered. Whether a 0 is worth another | ||||||||||||||||||||||||||||
| 19 | # attempt is last_status: see ForgeOps::Tracker::DeliveryStatus. | ||||||||||||||||||||||||||||
| 20 | 19 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 19 | my ($class, $configuration) = @_; | |||||||||||||||||||||||||||
| 22 | return bless { | ||||||||||||||||||||||||||||
| 23 | configuration => $configuration, | ||||||||||||||||||||||||||||
| 24 | 19 | http => HTTP::Tiny->new(timeout => $configuration->{timeout}), | |||||||||||||||||||||||||||
| 25 | last_status => ForgeOps::Tracker::DeliveryStatus::FAILED, | ||||||||||||||||||||||||||||
| 26 | }, $class; | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # The DeliveryStatus of the most recent deliver* call on this client, in this thread (ithreads give | ||||||||||||||||||||||||||||
| 30 | # each thread its own copy of the client, so one thread's attempt never overwrites another's). | ||||||||||||||||||||||||||||
| 31 | 12 | ▶ | sub last_status { | ||||||||||||||||||||||||||
|
Subroutine
last_status: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 33 | 12 | return $self->{last_status}; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 16 | ▶ | sub deliver { | ||||||||||||||||||||||||||
|
Subroutine
deliver: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 16 | my ($self, $payload) = @_; | |||||||||||||||||||||||||||
| 38 | 16 | return $self->_post($self->{configuration}->ingestion_uri, $payload, 'delivery'); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | sub deliver_performance_samples { | ||||||||||||||||||||||||||
|
Subroutine
deliver_performance_samples: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 2 | my ($self, $samples) = @_; | |||||||||||||||||||||||||||
| 43 | return $self->_post( | ||||||||||||||||||||||||||||
| 44 | $self->{configuration}->performance_samples_uri, | ||||||||||||||||||||||||||||
| 45 | 2 | { samples => $samples }, | |||||||||||||||||||||||||||
| 46 | 'performance samples delivery', | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Delivers a batch of individual capture_metric entries as { metrics => [...] }, and infrastructure | ||||||||||||||||||||||||||||
| 51 | # readings the same way. | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub deliver_metrics { | ||||||||||||||||||||||||||
|
Subroutine
deliver_metrics: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 1 | my ($self, $entries) = @_; | |||||||||||||||||||||||||||
| 54 | 1 | return $self->_post($self->{configuration}->custom_metrics_uri, { metrics => $entries }, 'metrics delivery'); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub deliver_infrastructure_metrics { | ||||||||||||||||||||||||||
|
Subroutine
deliver_infrastructure_metrics: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my ($self, $entries) = @_; | |||||||||||||||||||||||||||
| 59 | 0 | return $self->_post($self->{configuration}->infrastructure_metrics_uri, { metrics => $entries }, 'infrastructure metrics delivery'); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Delivers one finished trace, { trace_id => ..., spans => [...] }, to the spans endpoint. | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub deliver_spans { | ||||||||||||||||||||||||||
|
Subroutine
deliver_spans: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 0 | my ($self, $trace) = @_; | |||||||||||||||||||||||||||
| 65 | 0 | return $self->_post($self->{configuration}->spans_uri, $trace, 'span delivery'); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Delivers one record_change() call. A plan without change tracking answers 403, which is just a | ||||||||||||||||||||||||||||
| 69 | # false return here like any other non-2xx (with a last_status of REJECTED). | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub deliver_change { | ||||||||||||||||||||||||||
|
Subroutine
deliver_change: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ($self, $change) = @_; | |||||||||||||||||||||||||||
| 72 | 0 | return $self->_post($self->{configuration}->changes_uri, $change, 'change delivery'); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Delivers the startup snapshot, { environment => ..., state => {...} }. | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub deliver_change_snapshot { | ||||||||||||||||||||||||||
|
Subroutine
deliver_change_snapshot: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my ($self, $snapshot) = @_; | |||||||||||||||||||||||||||
| 78 | 0 | return $self->_post($self->{configuration}->change_snapshots_uri, $snapshot, 'change snapshot delivery'); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 19 | ▶ | sub _post { | ||||||||||||||||||||||||||
|
Subroutine
_post: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 19 | my ($self, $uri, $payload, $description) = @_; | |||||||||||||||||||||||||||
| 83 | 19 | my $status = $self->_attempt($uri, $payload, $description); | |||||||||||||||||||||||||||
| 84 | 19 | $self->{last_status} = $status; | |||||||||||||||||||||||||||
| 85 | 19 | ▶ | return $status eq ForgeOps::Tracker::DeliveryStatus::DELIVERED ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # One POST, answered as a DeliveryStatus. | ||||||||||||||||||||||||||||
| 89 | 19 | ▶ | sub _attempt { | ||||||||||||||||||||||||||
|
Subroutine
_attempt: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||
| 90 | 19 | my ($self, $uri, $payload, $description) = @_; | |||||||||||||||||||||||||||
| 91 | 19 | my $config = $self->{configuration}; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 19 | my $api_key = $config->api_key; | |||||||||||||||||||||||||||
| 94 | 19 | ▶ | return ForgeOps::Tracker::DeliveryStatus::FAILED unless $uri && defined $api_key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$uri and defined $api_key
MC/DC50%1/2
$uri and defined $api_key
$uri
defined $api_key
Truth table66%2/3
A and B
A= $uriB= defined $api_key
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 16 | my $response = eval { | |||||||||||||||||||||||||||
| 97 | $self->{http}->post( | ||||||||||||||||||||||||||||
| 98 | 16 | $uri, | |||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | headers => { | ||||||||||||||||||||||||||||
| 101 | 'Authorization' => "Bearer $api_key", | ||||||||||||||||||||||||||||
| 102 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||
| 104 | content => encode_json($payload), | ||||||||||||||||||||||||||||
| 105 | }, | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 16 | ▶ | if (!$response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $config->log("[forge-ops-tracker] $description failed: $@") if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | return ForgeOps::Tracker::DeliveryStatus::FAILED; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 16 | my $status = ForgeOps::Tracker::DeliveryStatus::for_code($response->{status}); | |||||||||||||||||||||||||||
| 115 | 16 | ▶ | if ($status eq ForgeOps::Tracker::DeliveryStatus::REJECTED) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 116 | 7 | $config->log( | |||||||||||||||||||||||||||
| 117 | "[forge-ops-tracker] $description rejected: $response->{status} $response->{reason}, the server will not accept a retry" | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | } elsif ($status eq ForgeOps::Tracker::DeliveryStatus::FAILED) { | ||||||||||||||||||||||||||||
| 120 | 6 | $config->log( | |||||||||||||||||||||||||||
| 121 | "[forge-ops-tracker] $description failed: $response->{status} $response->{reason}" | ||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 16 | return $status; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 11 | 1; | |||||||||||||||||||||||||||