lib/ForgeOps/Tracker/Changes.pm

filter: Statementstmt 41.1% 21 / 51 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 36.3% 4 / 11 Podpod 0.0% 0 / 6 totaltotal 23.8% 25 / 105 CC 18 SCAR 31.3
CC
18
Cov
25%
build_change
56.0
_iso8601
20.0
is_denied_env_var
20.0
CRAP 22.9
« blib/lib/ForgeOps/Tracker/Integrations/Dancer2.pm ↑ Summary blib/lib/ForgeOps/Tracker/TraceParent.pm »
1 package ForgeOps::Tracker::Changes;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Payloads for "what changed": one explicit record_change() call (POST /api/v1/changes), and the
9 # startup snapshot (POST /api/v1/change_snapshots) that ForgeOps diffs against the previous boot's
10 # to record what changed between deploys. Mirrors gems/forge_ops_tracker's Change and
11 # ChangeSnapshot.
12 #
13 # The snapshot leaves out any key it can't determine reliably rather than guessing, since the
14 # server reads a missing key as "unknown", never as "everything was removed". That's why there's no
15 # dependencies key here: Perl has no single reliable record of which module versions an app runs
16 # (no lockfile every app has, and %INC only lists what happened to be loaded so far), so the
17 # snapshot carries the Perl version and, when opted in, environment variable names. Names only,
18 # never values.
19  
20 # The server rejects any other kind outright (422), so an unknown one is sent as "other" rather
21 # than dropped: the change still gets recorded, just less specifically categorized.
22 10 our @KINDS = qw(feature_flag config migration dependency infrastructure other);
23 10 my %KNOWN_KIND = map { $_ => 1 } @KINDS;
24 10 my $MAX_TITLE_LENGTH = 200;
25 10 my $MAX_ENV_VAR_NAMES = 3000;
26  
27 # Host-specific variables that differ from one box to the next (or one boot to the next) without
28 # anything about the deploy having changed, so a fleet doesn't look like it's changing on every
29 # restart. The client's own FORGE_OPS_* settings are dropped too.
30 10 my %ENV_VAR_DENYLIST = map { $_ => 1 } qw(
31 HOSTNAME HOST HOME PATH PWD OLDPWD SHLVL _ TERM USER LOGNAME SHELL LANG TMPDIR TZ PORT DYNO
32 INVOCATION_ID JOURNAL_STREAM
33 );
34 10 my @ENV_VAR_DENYLIST_PATTERNS = (
35 qr/^LC_/, qr/^SYSTEMD_/, qr/^MEMORY_PRESSURE_/, qr/^KUBERNETES_/, qr/^FORGE_OPS_/,
36 qr/_SERVICE_HOST$/, qr/_SERVICE_PORT/, qr/_PORT_.*_TCP/,
37 );
38  
39 0 ▶sub normalize_kind {
Subroutine normalize_kind: not called CC=3 SCAR=24.8
Pod undocumented
40 0 my ($kind) = @_;
41 0 ▶ return defined $kind && $KNOWN_KIND{$kind} ? $kind : 'other';
Branch: 1 branch
BranchTrueFalseTotal
defined $kind && $KNOWN_KIND{$kind} ? : 0 0 0
Condition0%0/3
defined $kind and $KNOWN_KIND{$kind}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $kind and $KNOWN_KIND{$kind}
defined $kind $KNOWN_KIND{$kind}
Truth table0%0/3
A and B
A= defined $kindB= $KNOWN_KIND{$kind}
ABresult
0X0
100
111
42 }
43  
44 # build_change($configuration, %change): the body for one record_change() call, or undef when
45 # there's nothing sendable (a blank title: the server requires one, so posting it anyway would only
46 # ever come back 422). occurred_at is an ISO 8601 string or epoch seconds, defaulting to now.
47 0 ▶sub build_change {
Subroutine build_change: not called CC=7 SCAR=40.3
Pod undocumented
48 0 my ($configuration, %change) = @_;
49  
50 0 ▶ my $title = defined $change{title} ? "$change{title}" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $change{'title'} ? : 0 0 0
51 0 $title =~ s/\A\s+|\s+\z//g;
52 0 ▶ return undef unless length $title;
Branch: 1 branch
BranchTrueFalseTotal
unless length $title 0 0 0
53  
54 my %payload = (
55 kind => normalize_kind($change{kind}),
56 title => substr($title, 0, $MAX_TITLE_LENGTH),
57 details => ref $change{details} eq 'HASH' ? $change{details} : {},
58 environment => defined $change{environment} ? "$change{environment}" : $configuration->{environment},
59 0 ▶ occurred_at => _iso8601($change{occurred_at}),
Branch: 2 branches
BranchTrueFalseTotal
ref $change{'details'} eq 'HASH' ? : 0 0 0
defined $change{'environment'} ? : 0 0 0
60 );
61 0 for my $key (qw(service actor url id)) {
62 0 ▶ $payload{$key} = "$change{$key}" if defined $change{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $change{$key} 0 0 0
63 }
64 0 return \%payload;
65 }
66  
67 0 ▶sub _iso8601 {
Subroutine _iso8601: not called CC=4 SCAR=30.0
68 0 my ($value) = @_;
69 0 ▶ return "$value" if defined $value && $value !~ /\A\d+(?:\.\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and not $value =~ /\A\d+(?:\.\d+)?\z/ 0 0 0
Condition0%0/3
defined $value and not $value =~ /\A\d+(?:\.\d+)?\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and not $value =~ /\A\d+(?:\.\d+)?\z/
defined $value not $value =~ /\A\d+(?:\.\d+)?\z/
Truth table0%0/3
A and not(B)
A= defined $valueB= not $value =~ /\A\d+(?:\.\d+)?\z/
ABresult
0X0
100
111
70  
71 0 ▶ my $time = defined $value ? $value : Time::HiRes::time();
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
72 0 return strftime('%Y-%m-%dT%H:%M:%S', gmtime(int $time)) . sprintf('.%03dZ', int(($time - int $time) * 1000));
73 }
74  
75 0 ▶sub is_denied_env_var {
Subroutine is_denied_env_var: not called CC=4 SCAR=30.0
Pod undocumented
76 0 my ($name) = @_;
77 0 ▶ return 1 if $ENV_VAR_DENYLIST{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV_VAR_DENYLIST{$name} 0 0 0
78 0 for my $pattern (@ENV_VAR_DENYLIST_PATTERNS) {
79 0 ▶ return 1 if $name =~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /$pattern/ 0 0 0
80 }
81 0 return 0;
82 }
83  
84 # env_var_names(\%env): names only, never values, sorted so the same set always serializes the same
85 # way. Defaults to this process's %ENV.
86 0 ▶sub env_var_names {
Subroutine env_var_names: not called CC=3 SCAR=24.8
Pod undocumented
87 0 my ($env) = @_;
88 0 ▶ $env ||= \%ENV;
Condition0%0/2
$env ||= \%ENV
l!l
00
MC/DC0%0/1
$env ||= \%ENV
$env
Truth table0%0/2
A
A= $env
Aresult
11
00
89 0 my @names = sort grep { !is_denied_env_var($_) } keys %$env;
90 0 ▶ splice(@names, $MAX_ENV_VAR_NAMES) if @names > $MAX_ENV_VAR_NAMES;
Branch: 1 branch
BranchTrueFalseTotal
if @names > $MAX_ENV_VAR_NAMES 0 0 0
91 0 return \@names;
92 }
93  
94 0 ▶sub runtime {
Subroutine runtime: not called CC=1 SCAR=6.9
Pod undocumented
95 0 return sprintf('perl %vd', $^V);
96 }
97  
98 0 ▶sub build_snapshot {
Subroutine build_snapshot: not called CC=2 SCAR=17.9
Pod undocumented
99 0 my ($configuration) = @_;
100 0 my %state = (runtime => runtime());
101 0 ▶ $state{env_var_names} = env_var_names() if $configuration->{track_env_var_names};
Branch: 1 branch
BranchTrueFalseTotal
if $configuration->{"track_env_var_names"} 0 0 0
102 0 return { environment => $configuration->{environment}, state => \%state };
103 }
104  
105 10 1;
« blib/lib/ForgeOps/Tracker/Integrations/Dancer2.pm ↑ Summary blib/lib/ForgeOps/Tracker/TraceParent.pm »