lib/ForgeOps/Tracker/Configuration.pm

filter: Statementstmt 61.2% 57 / 93 Branchbran 42.8% 24 / 56 Conditioncond 45.4% 10 / 22 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 75.0% 15 / 20 Podpod 0.0% 0 / 12 totaltotal 50.4% 109 / 216 CC 38 SCAR 38.0
CC
38
Cov
53%
should_propagate_trace
182.0
_parsed_dsn
6.1
infrastructure_metrics_uri
6.0
CRAP 44.6
« lib/ForgeOps/Tracker/StrictPrivacy.pm ↑ Summary blib/lib/ForgeOps/Tracker/PerformanceFlusher.pm »
1 package ForgeOps::Tracker::Configuration;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use ForgeOps::Tracker::StrictPrivacy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Holds a single ForgeOps DSN plus everything else the client needs to build and deliver events.
9 # Mirrors gems/forge_ops_tracker/lib/forge_ops_tracker/configuration.rb: a single DSN string
10 # carries both the ingestion URL and the project's api_key:
11 # "https://<api_key>@host/api/v1/events". Hand-parsed with a regex rather than pulling in the URI
12 # module: this SDK otherwise has zero non-core dependencies (HTTP::Tiny, threads, and
13 # Thread::Queue are all core as of the Perl versions this targets), and a DSN's shape is simple
14 # and fixed enough that a regex covers it completely without needing a general-purpose URI parser.
15 65 ▶sub new {
Subroutine new: called CC=4 SCAR=16.9
Pod undocumented
16 65 my ($class) = @_;
17 return bless {
18 dsn => $ENV{FORGE_OPS_DSN},
19 environment => $ENV{FORGE_OPS_ENVIRONMENT} || 'production',
20 release => $ENV{FORGE_OPS_RELEASE},
21 server_name => _safe_hostname(),
22 app_root => getcwd(),
23 enabled_environments => { production => 1, staging => 1 },
24 queue_size => 1000,
25 timeout => 2, # seconds
26 scrub_pii => 1,
27 # Whether EventBuilder reads a few lines of source off disk around each in_app frame's
28 # culprit line (see EventBuilder::_attach_source_context). Defaults to true so a snippet
29 # shows up with no extra setup, but this flag by itself isn't the real protection against
30 # sending proprietary source code somewhere it shouldn't go: ForgeOps' own per-project
31 # setting is the durable, server-enforced off switch, since it applies regardless of
32 # what this flag happens to be set to on any given deployment, and can't quietly drift
33 # back on the way a local config value could. Set this to false too if this host app
34 # should never even attempt that disk read in the first place.
35 capture_source_context => 1,
36 # When an error's text carries the SQL behind a failed database call (DBI's
37 # `[for Statement "..."]`, which needs ShowErrorStatement on the handle, or SQLite's
38 # `while compiling:`), send the names of the stored procedure, table and view it touched,
39 # so an issue says where to start looking. Names are identifiers, never values, which is
40 # why this defaults on. capture_sql_statement is the separate, opt-in step of also
41 # sending the statement itself, with every string and number replaced by "?"; off by
42 # default because even a masked statement describes your schema, and ForgeOps' own
43 # per-project setting is what durably governs whether the server stores it. See
44 # ForgeOps::Tracker::SqlStatement.
45 capture_sql_objects => 1,
46 capture_sql_statement => 0,
47 logger => undef, # coderef, or undef to log nowhere
48 # Whether add_breadcrumb records anything at all: on by default, matching every other
49 # client in this repo.
50 track_breadcrumbs => 1,
51 # How many of the most recent breadcrumbs are kept, oldest dropped first; 30, matching
52 # every other client's own default (all traced back to gems/forge_ops_tracker's).
53 max_breadcrumbs => 30,
54 # Whether the PSGI/Dancer2 performance integrations time every request, bucketed by
55 # transaction name, and periodically report the aggregates for a dashboard widget on a
56 # project's Performance page. On by default, the same "on unless you turn it off" posture
57 # error reporting itself already has.
58 track_performance => 1,
59 # Interval between aggregate performance reports, in seconds; requests are timed
60 # in-process and flushed as one small report on this interval, not one network call per
61 # request.
62 performance_flush_interval => 60,
63 # Seconds between flushes of the buffered capture_metric / capture_infrastructure_metric
64 # entries (see MetricBuffer). No track_metrics flag the way track_performance has one: these
65 # are explicit calls the host app's own code makes, not automatic instrumentation, so there is
66 # nothing to turn off that simply not calling them doesn't already do.
67 metric_flush_interval => 60,
68 infrastructure_metric_flush_interval => 60,
69 # Whether the PSGI/Dancer2 performance integrations start a trace per request and report
70 # it (when slow) to /spans. span() and record_span() only record inside a trace, so this
71 # gates the whole feature. On by default.
72 track_tracing => 1,
73 # A trace is only sent when its root span took at least this many seconds, or the request
74 # errored.
75 trace_capture_threshold => 1,
76 # Whether ForgeOps::Tracker::http_span hands its code a W3C `traceparent` header
77 # (https://www.w3.org/TR/trace-context/) for the outgoing request, so the service being
78 # called can continue this request's trace instead of starting its own. On by default, and
79 # independent of track_tracing: the header also carries the trace id that links an error
80 # here to an error there, which is useful with or without spans.
81 propagate_traces => 1,
82 # Which hosts get that header: undef (the default) means every host. Otherwise an arrayref
83 # where each entry is either a host string, matching that host and its subdomains on a dot
84 # boundary, ignoring case and a leading dot ("example.com" matches "api.example.com" but
85 # not "badexample.com"), or a qr// regular expression matched against the host. Useful for
86 # third-party APIs that reject unknown headers, or that shouldn't learn this app's trace
87 # ids at all.
88 trace_propagation_targets => undef,
89 # Sends one startup snapshot per process after init() (the Perl version; see
90 # ForgeOps::Tracker::Changes), which ForgeOps diffs against the previous boot's to record
91 # what changed between deploys. On by default, like every other automatic behavior here.
92 detect_changes => 1,
93 # Whether that snapshot also lists the names of this process's environment variables, so an
94 # added or removed variable shows up as a change. Off by default: names only, never values,
95 # but even names can say more about an app than some teams want to share. Host-specific
96 # names (HOSTNAME, PATH, PORT, and so on; see ForgeOps::Tracker::Changes) are always left
97 # out.
98 track_env_var_names => 0,
99 # Off by default. On, nothing this client sends carries free text that could hold personal
100 # or health data: no error message (a message_fingerprint is sent instead, so errors still
101 # group), no breadcrumbs, no endpoint, no SQL statement, no source context, no user email or
102 # username, the user's id only as a keyed hash (privacy_user_key), and outbound HTTP spans
103 # named by method and host only. See ForgeOps::Tracker::StrictPrivacy for every rule.
104 # FORGE_OPS_STRICT_PRIVACY=true turns it on from the environment; init() or setting it here
105 # wins over that.
106 strict_privacy => _env_flag($ENV{FORGE_OPS_STRICT_PRIVACY}),
107 # Context and tag keys strict privacy still sends (only plain values under exactly these
108 # top-level keys), an arrayref such as ['error_code', 'feature'] or one comma-separated
109 # string. Empty by default, so nothing from context or tags goes out.
110 # FORGE_OPS_PRIVACY_ALLOWED_FIELDS=error_code,feature sets it from the environment.
111 privacy_allowed_fields => [ ForgeOps::Tracker::StrictPrivacy::allowed_field_list($ENV{FORGE_OPS_PRIVACY_ALLOWED_FIELDS}) ],
112 # Strict privacy sends the user's id only as a keyed hash
113 # (ForgeOps::Tracker::StrictPrivacy::hashed_user_id) with this secret, which stays in your
114 # app; unset, no user is sent at all. Keep it the same across deploys and servers, or the
115 # same customer counts as two. FORGE_OPS_PRIVACY_USER_KEY sets it from the environment; an
116 # empty value counts as unset.
117 65 ▶ privacy_user_key => (defined $ENV{FORGE_OPS_PRIVACY_USER_KEY} && length $ENV{FORGE_OPS_PRIVACY_USER_KEY} ? $ENV{FORGE_OPS_PRIVACY_USER_KEY} : undef),
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'FORGE_OPS_PRIVACY_USER_KEY'} && length $ENV{'FORGE_OPS_PRIVACY_USER_KEY'} ? : 0 65 65
Condition40%2/5
$ENV{'FORGE_OPS_ENVIRONMENT'} || 'production'
l!l
065
defined $ENV{"FORGE_OPS_PRIVACY_USER_KEY"} and length $ENV{"FORGE_OPS_PRIVACY_USER_KEY"}
!ll&&!rl&&r
6500
MC/DC0%0/1
$ENV{'FORGE_OPS_ENVIRONMENT'} || 'production'
$ENV{'FORGE_OPS_ENVIRONMENT'}
MC/DC0%0/2
defined $ENV{"FORGE_OPS_PRIVACY_USER_KEY"} and length $ENV{"FORGE_OPS_PRIVACY_USER_KEY"}
defined $ENV{"FORGE_OPS_PRIVACY_USER_KEY"} length $ENV{"FORGE_OPS_PRIVACY_USER_KEY"}
Truth table50%1/2
A
A= $ENV{'FORGE_OPS_ENVIRONMENT'}
Aresult
11
00
Truth table33%1/3
A and B
A= defined $ENV{"FORGE_OPS_PRIVACY_USER_KEY"}B= length $ENV{"FORGE_OPS_PRIVACY_USER_KEY"}
ABresult
0X0
100
111
118 }, $class;
119 }
120  
121 # https://<api_key>@host[:port]/path: captures scheme, an optional userinfo (the api_key,
122 # percent-decoded), and everything from the host onward.
123 15 my $DSN_RE = qr{^(https?)://(?:([^:@/]*)@)?([^/]+)(/.*)?$};
124  
125 56 ▶sub _parsed_dsn {
Subroutine _parsed_dsn: called CC=6 SCAR=18.0
126 56 my ($self) = @_;
127 56 ▶ return undef unless $self->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dsn"} 9 47 56
128 47 ▶ return undef unless $self->{dsn} =~ $DSN_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dsn"} =~ /$DSN_RE/ 2 45 47
129  
130 45 ▶ my ($scheme, $userinfo, $host, $path) = ($1, $2, $3, $4 // '');
Condition50%1/2
$4 // ""
l!l
450
MC/DC0%0/1
$4 // ""
$4
Truth table50%1/2
A
A= $4
Aresult
11
00
131 45 ▶ my $api_key = defined($userinfo) && length($userinfo) ? _uri_unescape($userinfo) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $userinfo && length $userinfo ? : 44 1 45
Condition66%2/3
defined $userinfo and length $userinfo
!ll&&!rl&&r
1044
MC/DC50%1/2
defined $userinfo and length $userinfo
defined $userinfo length $userinfo
Truth table66%2/3
A and B
A= defined $userinfoB= length $userinfo
ABresult
0X0
100
111
132 return {
133 45 scheme => $scheme,
134 api_key => $api_key,
135 ingestion_uri => "$scheme://$host$path",
136 };
137 }
138  
139 32 ▶sub api_key {
Subroutine api_key: called CC=4 SCAR=13.9
Pod undocumented
140 32 my ($self) = @_;
141 32 my $parsed = $self->_parsed_dsn;
142 32 ▶ return undef unless $parsed && defined $parsed->{api_key} && length $parsed->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and defined $parsed->{"api_key"} and length $parsed->{"api_key"} 6 26 32
Condition83%5/6
$parsed and defined $parsed->{"api_key"} and length $parsed->{"api_key"}
!ll&&!rl&&r
6026
$parsed and defined $parsed->{"api_key"}
!ll&&!rl&&r
5126
MC/DC66%2/3
$parsed and defined $parsed->{"api_key"} and length $parsed->{"api_key"}
$parsed defined $parsed->{"api_key"} length $parsed->{"api_key"}
Truth table75%3/4
A and B and C
A= $parsedB= defined $parsed->{"api_key"}C= length $parsed->{"api_key"}
ABCresult
0XX0
10X0
1100
1111
143 26 return $parsed->{api_key};
144 }
145  
146 # The ingestion URL with credentials stripped out (they travel as the Authorization header
147 # instead, never embedded in the request URI).
148 24 ▶sub ingestion_uri {
Subroutine ingestion_uri: called CC=2 SCAR=6.9
Pod undocumented
149 24 my ($self) = @_;
150 24 my $parsed = $self->_parsed_dsn;
151 24 ▶ return undef unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 6 18 24
152 18 return $parsed->{ingestion_uri};
153 }
154  
155 # Same derivation as ingestion_uri, with the trailing "/events" swapped for
156 # "/performance_samples": one DSN, two more endpoints alongside deliver's own, matching the Ruby
157 # gem's own Configuration#performance_samples_uri.
158 4 ▶sub performance_samples_uri {
Subroutine performance_samples_uri: called CC=2 SCAR=6.9
Pod undocumented
159 4 my ($self) = @_;
160 4 my $uri = $self->ingestion_uri;
161 4 ▶ return undef unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 2 2 4
162  
163 2 (my $swapped = $uri) =~ s{/events\z}{/performance_samples};
164 2 return $swapped;
165 }
166  
167 # Same derivation again, swapping the trailing "/events" for "/custom_metrics" and
168 # "/infrastructure_metrics".
169 1 ▶sub custom_metrics_uri {
Subroutine custom_metrics_uri: called CC=2 SCAR=7.0
Pod undocumented
170 1 my ($self) = @_;
171 1 my $uri = $self->ingestion_uri;
172 1 ▶ return undef unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 1 1
173  
174 1 (my $swapped = $uri) =~ s{/events\z}{/custom_metrics};
175 1 return $swapped;
176 }
177  
178 0 ▶sub infrastructure_metrics_uri {
Subroutine infrastructure_metrics_uri: not called CC=2 SCAR=17.9
Pod undocumented
179 0 my ($self) = @_;
180 0 my $uri = $self->ingestion_uri;
181 0 ▶ return undef unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 0 0
182  
183 0 (my $swapped = $uri) =~ s{/events\z}{/infrastructure_metrics};
184 0 return $swapped;
185 }
186  
187 # Same derivation again, swapping the trailing "/events" for "/spans".
188 0 ▶sub changes_uri {
Subroutine changes_uri: not called CC=2 SCAR=17.9
Pod undocumented
189 0 my ($self) = @_;
190 0 my $uri = $self->ingestion_uri;
191 0 ▶ return undef unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 0 0
192  
193 0 (my $swapped = $uri) =~ s{/events\z}{/changes};
194 0 return $swapped;
195 }
196  
197 0 ▶sub change_snapshots_uri {
Subroutine change_snapshots_uri: not called CC=2 SCAR=17.9
Pod undocumented
198 0 my ($self) = @_;
199 0 my $uri = $self->ingestion_uri;
200 0 ▶ return undef unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 0 0
201  
202 0 (my $swapped = $uri) =~ s{/events\z}{/change_snapshots};
203 0 return $swapped;
204 }
205  
206 0 ▶sub spans_uri {
Subroutine spans_uri: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my ($self) = @_;
208 0 my $uri = $self->ingestion_uri;
209 0 ▶ return undef unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 0 0
210  
211 0 (my $swapped = $uri) =~ s{/events\z}{/spans};
212 0 return $swapped;
213 }
214  
215 # Whether an outgoing request to $host should carry a traceparent header; see propagate_traces and
216 # trace_propagation_targets above. Case-insensitive, since hostnames are.
217 0 ▶sub should_propagate_trace {
Subroutine should_propagate_trace: not called CC=13 SCAR=52.0
Pod undocumented
218 0 my ($self, $host) = @_;
219 0 ▶ return 0 unless $self->{propagate_traces};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"propagate_traces"} 0 0 0
220  
221 0 my $targets = $self->{trace_propagation_targets};
222 0 ▶ return 1 unless defined $targets;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $targets 0 0 0
223 0 ▶ return 0 unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 0 0
Condition0%0/3
defined $host and length $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table0%0/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
224  
225 0 $host = lc $host;
226 0 ▶ for my $target (ref $targets eq 'ARRAY' ? @$targets : ($targets)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $targets eq 'ARRAY' ? : 0 0 0
227 0 ▶ next unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 0 0
228 0 ▶ if (ref $target eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target eq "Regexp") 0 0 0
229 0 ▶ return 1 if $host =~ $target;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /$target/ 0 0 0
230 0 next;
231 }
232 0 (my $name = lc $target) =~ s/\A\.//;
233 0 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
234 0 ▶ return 1 if $host eq $name || substr($host, -length(".$name")) eq ".$name";
Branch: 1 branch
BranchTrueFalseTotal
if $host eq $name or substr($host, -length(".$name")) eq ".$name" 0 0 0
Condition0%0/3
$host eq $name or substr($host, -length(".$name")) eq ".$name"
l!l&&r!l&&!r
000
MC/DC0%0/2
$host eq $name or substr($host, -length(".$name")) eq ".$name"
$host eq $name substr($host, -length(".$name")) eq ".$name"
Truth table0%0/3
A or B
A= $host eq $nameB= substr($host, -length(".$name")) eq ".$name"
ABresult
1X1
011
000
235 }
236 0 return 0;
237 }
238  
239 10 ▶sub is_enabled {
Subroutine is_enabled: called CC=4 SCAR=13.9
Pod undocumented
240 10 my ($self) = @_;
241 10 ▶ return 0 unless $self->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dsn"} 1 9 10
242 9 ▶ return 0 unless defined $self->api_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->api_key 1 8 9
243 8 ▶ return 0 unless $self->{enabled_environments}{ $self->{environment} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"enabled_environments"}{$self->{"environment"}} 2 6 8
244 6 return 1;
245 }
246  
247 16 ▶sub log {
Subroutine log: called CC=2 SCAR=6.9
Pod undocumented
248 16 my ($self, $message) = @_;
249 16 ▶ $self->{logger}->($message) if $self->{logger};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logger"} 1 15 16
250 }
251  
252 # "true", "1", "yes" or "on" (any case) is on; anything else, or unset, is off.
253 65 ▶sub _env_flag {
Subroutine _env_flag: called CC=3 SCAR=15.4
254 65 my ($value) = @_;
255 65 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 65 0 65
256 0 ▶ return $value =~ /\A\s*(?:true|1|yes|on)\s*\z/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A\s*(?:true|1|yes|on)\s*\z/i ? : 0 0 0
257 }
258  
259 65 ▶sub _safe_hostname {
Subroutine _safe_hostname: called CC=2 SCAR=7.0
260 65 require Sys::Hostname;
261 65 my $name = eval { Sys::Hostname::hostname() };
262 65 ▶ return $@ ? undef : $name;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 65 65
263 }
264  
265 44 ▶sub _uri_unescape {
Subroutine _uri_unescape: called CC=1 SCAR=0.0
266 44 my ($string) = @_;
267 44 $string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
268 44 return $string;
269 }
270  
271 15 1;
« lib/ForgeOps/Tracker/StrictPrivacy.pm ↑ Summary blib/lib/ForgeOps/Tracker/PerformanceFlusher.pm »