Logging/DetailStats.pm

filter: Statementstmt 100% 76 / 76 Branchbran 87.5% 7 / 8 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 1 totaltotal 89.5% 103 / 115 CC 13 SCAR 15.5
CC
13
Cov
94%
as_str
10.3
_scrub_gaql
3.0
_scrub_content
2.0
CRAP 4.7
« Logging/SummaryStats.pm ↑ Summary Logging/GoogleAdsLogger.pm »
1 # Copyright 2019, Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Ads::GoogleAds::Logging::DetailStats;
16  
17 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # The following needs to be on one line because CPAN uses a particularly hacky
22 # eval() to determine module versions.
23 11 ▶use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 11 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶use Encode qw( encode_utf8 decode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # A list of fields in HTTP headers, content and GAQL that need to be scrubbed
30 # before logging for privacy reasons.
31 11 ▶use constant REDACTED_STRING => "REDACTED";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶use constant SCRUBBED_HEADERS => qw(developer-token Authorization);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 # Below fields will be scrubbed in the HTTP request and response content.
34 # CustomerUserAccess.emailAddress
35 # CustomerUserAccess.inviterUserEmailAddress
36 # CustomerUserAccessInvitation.emailAddress
37 # ChangeEvent.userEmail
38 # PlacesLocationFeedData.emailAddress
39 # CreateCustomerClientRequest.emailAddress
40 11 ▶use constant SCRUBBED_CONTENT_FIELDS =>
Subroutine BEGIN: called CC=1 SCAR=0.0
41 11 qw(emailAddress inviterUserEmailAddress userEmail requestUserEmail phoneNumber consumerName text email);
42 # Below fields will be scrubbed in the GAQL statement of SearchGoogleAdsRequest
43 # and SearchGoogleAdsStreamRequest.
44 11 ▶use constant SCRUBBED_GAQL_FIELDS => qw(customer_user_access\.email_address
Subroutine BEGIN: called CC=1 SCAR=0.0
45 customer_user_access\.inviter_user_email_address
46 customer_user_access_invitation\.email_address
47 change_event\.user_email feed\.places_location_feed_data\.email_address
48 local_services_lead\.contact_details\.phone_number
49 local_services_lead\.contact_details\.email
50 local_services_lead\.contact_details\.consumer_name
51 local_services_lead_conversation\.message_details\.text
52 multi_party_auth_review\.request_user_email
53 multi_party_auth_review\.customer_user_access_invitation_review\.new_customer_user_access_invitation\.email_address
54 11 );
55  
56 11 my %host_of : ATTR(:name<host> :default<>);
57 11 my %method_of : ATTR(:name<method> :default<>);
58 11 my %request_headers_of : ATTR(:name<request_headers> :default<>);
59 11 my %request_content_of : ATTR(:name<request_content> :default<>);
60 11 my %response_headers_of : ATTR(:name<response_headers> :default<>);
61 11 my %response_content_of : ATTR(:name<response_content> :default<>);
62 11 my %fault_of : ATTR(:name<fault> :default<>);
63  
64 2 ▶sub as_str : STRINGIFY {
Subroutine as_str: called CC=10 SCAR=23.3
Pod undocumented
65 2 my $self = shift;
66 2 ▶ my $host = $self->get_host() || "";
Condition50%1/2
$self->get_host || ''
l!l
20
MC/DC0%0/1
$self->get_host || ''
$self->get_host
Truth table50%1/2
A
A= $self->get_host
Aresult
11
00
67 2 ▶ my $method = $self->get_method() || "";
Condition50%1/2
$self->get_method || ''
l!l
20
MC/DC0%0/1
$self->get_method || ''
$self->get_method
Truth table50%1/2
A
A= $self->get_method
Aresult
11
00
68 2 ▶ my $request_headers = $self->get_request_headers() || {};
Condition50%1/2
$self->get_request_headers || {}
l!l
20
MC/DC0%0/1
$self->get_request_headers || {}
$self->get_request_headers
Truth table50%1/2
A
A= $self->get_request_headers
Aresult
11
00
69 2 ▶ my $request_content = $self->get_request_content() || "";
Condition50%1/2
$self->get_request_content || ''
l!l
20
MC/DC0%0/1
$self->get_request_content || ''
$self->get_request_content
Truth table50%1/2
A
A= $self->get_request_content
Aresult
11
00
70 2 ▶ my $response_headers = $self->get_response_headers() || {};
Condition50%1/2
$self->get_response_headers || {}
l!l
20
MC/DC0%0/1
$self->get_response_headers || {}
$self->get_response_headers
Truth table50%1/2
A
A= $self->get_response_headers
Aresult
11
00
71 2 my $response_content = $self->get_response_content();
72 2 my $fault = $self->get_fault();
73  
74 # Scrub the sensitive HTTP headers.
75 2 foreach my $header (SCRUBBED_HEADERS) {
76 4 $request_headers->header($header => REDACTED_STRING);
77 }
78  
79 # Delete the unuseful "::std_case" header from request headers and response headers.
80 2 delete $request_headers->{"::std_case"};
81 2 delete $response_headers->{"::std_case"};
82  
83 # Scrub the sensitive fields in the HTTP request and response.
84 2 $request_content = _scrub_content($request_content);
85 2 $request_content = _scrub_gaql($request_content);
86 2 ▶ $response_content = _scrub_content($response_content) if $response_content;
Branch: 1 branch
BranchTrueFalseTotal
if $response_content 1 1 2
87  
88 2 my $json_coder = JSON::XS->new->utf8->pretty;
89 2 my $detail_message = sprintf(
90 "Request\n" .
91 "-------\n" .
92 "MethodName: %s\n" .
93 "Host: %s\n" .
94 "Headers: %s\n" .
95 "Request: %s\n" .
96 "\nResponse\n" .
97 "-------\n" .
98 "Headers: %s\n",
99 $method, $host, $json_coder->encode({%$request_headers}),
100 $request_content, $json_coder->encode({%$response_headers}));
101  
102 2 ▶ $detail_message .= "Response: ${response_content}\n" if $response_content;
Branch: 1 branch
BranchTrueFalseTotal
if $response_content 1 1 2
103 2 ▶ $detail_message .= "Fault: ${fault}\n" if $fault;
Branch: 1 branch
BranchTrueFalseTotal
if $fault 1 1 2
104  
105 2 return $detail_message;
106 11 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 # Scrubs the sensitive fields in HTTP content.
109 3 ▶sub _scrub_content {
Subroutine _scrub_content: called CC=2 SCAR=6.9
110 3 my $content = shift;
111 3 foreach my $field (SCRUBBED_CONTENT_FIELDS) {
112 24 $content =~ s/("$field"\s?:\s?)".+?"/$1"${\REDACTED_STRING}"/g;
113 }
114  
115 3 return $content;
116 }
117  
118 # Scrubs the sensitive fields in GAQL statement.
119 2 ▶sub _scrub_gaql {
Subroutine _scrub_gaql: called CC=3 SCAR=11.0
120 2 my $content = shift;
121  
122 2 ▶ return $content if $content !~ /"query"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /"query"/ 0 2 2
123 2 foreach my $field (SCRUBBED_GAQL_FIELDS) {
124 22 $content =~
125 8 s/(SELECT.+WHERE.+$field.+?['"]).+?(['"])/$1${\REDACTED_STRING}$2/i;
126 }
127  
128 2 return $content;
129 }
130  
131 11 return 1;
132  
133 =pod
134  
135 =head1 NAME
136  
137 Google::Ads::GoogleAds::Logging::DetailStats
138  
139 =head1 DESCRIPTION
140  
141 Class that wraps the detailed HTTP request and response like host, method,
142 headers, payload.
143  
144 =head1 ATTRIBUTES
145  
146 =head2 host
147  
148 The Google Ads API server endpoint.
149  
150 =head2 method
151  
152 The name of the service method that was called.
153  
154 =head2 request_headers
155  
156 The REST HTTP request headers.
157  
158 =head2 request_content
159  
160 The REST HTTP request payload.
161  
162 =head2 response_headers
163  
164 The REST HTTP response headers.
165  
166 =head2 response_content
167  
168 The REST HTTP response payload.
169  
170 =head2 fault
171  
172 The stack trace of up to 16K characters if a fault occurs.
173  
174 =head1 LICENSE AND COPYRIGHT
175  
176 Copyright 2019 Google LLC
177  
178 Licensed under the Apache License, Version 2.0 (the "License");
179 you may not use this file except in compliance with the License.
180 You may obtain a copy of the License at
181  
182 http://www.apache.org/licenses/LICENSE-2.0
183  
184 Unless required by applicable law or agreed to in writing, software
185 distributed under the License is distributed on an "AS IS" BASIS,
186 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
187 See the License for the specific language governing permissions and
188 limitations under the License.
189  
190 =head1 REPOSITORY INFORMATION
191  
192 $Rev: $
193 $LastChangedBy: $
194 $Id: $
195  
196 =cut
« Logging/SummaryStats.pm ↑ Summary Logging/GoogleAdsLogger.pm »