Chunk/Store/S3.pm

filter: Statementstmt 55.7% 44 / 79 Branchbran 9.3% 3 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 47.2% 59 / 125 CC 18 SCAR 40.2
CC
18
Cov
42%
store
132.0
_build_bucket
30.0
BEGIN
2.3
CRAP 55.9
↑ Summary Chunk.pm »
1 package Log::Log4perl::Appender::Chunk::Store::S3;
2 3 $Log::Log4perl::Appender::Chunk::Store::S3::VERSION = '0.013';
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 extends qw/Log::Log4perl::Appender::Chunk::Store/;
5  
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Net::Amazon::S3;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Net::Amazon::S3::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 my $LOGGER = Log::Log4perl->get_logger();
16  
17 3 ▶sub BEGIN{
Subroutine BEGIN: called CC=2 SCAR=8.1
18 3 eval "require Net::Amazon::S3::Client;";
19 3 ▶ if( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
20 0 die "\n\nFor ".__PACKAGE__.": Cannot load Net::Amazon::S3::Client\n\n -> Please install that if you want to use S3 Log Chunk storage.\n\n";
21 }
22 }
23  
24 3 has 's3_client' => ( is => 'ro', isa => 'Net::Amazon::S3::Client', lazy_build => 1 );
25 3 has 'bucket' => ( is => 'ro' , isa => 'Net::Amazon::S3::Client::Bucket', lazy_build => 1);
26  
27  
28 3 has 'host' => ( is => 'ro', isa => 'Maybe[Str]');
29 3 has 'location_constraint' => ( is => 'ro', isa => 'Maybe[Str]');
30 3 has 'bucket_name' => ( is => 'ro' , isa => 'Str' , required => 1);
31 3 has 'aws_access_key_id' => ( is => 'ro' , isa => 'Str', required => 1 );
32 3 has 'aws_secret_access_key' => ( is => 'ro' , isa => 'Str' , required => 1);
33 3 has 'retry' => ( is => 'ro' , isa => 'Bool' , required => 1 , default => 1);
34  
35 3 has 'log_auth_links' => ( is => 'ro' , isa => 'Bool' , required => 1, default => 0);
36  
37 3 has 'with_forking' => ( is => 'ro', isa => 'Bool', default => 1 );
38  
39  
40 # Single object properties.
41  
42 # Short access list name
43 3 has 'acl_short' => ( is => 'ro' , isa => 'Maybe[Str]', default => undef );
44  
45 # Expires in this amount of days.
46 3 has 'expires_in_days' => ( is => 'ro' , isa => 'Maybe[Int]' , default => undef );
47  
48 3 has 'vivify_bucket' => ( is => 'ro' , isa => 'Bool' , required => 1 , default => 0 );
49  
50 4 ▶sub _build_s3_client{
Subroutine _build_s3_client: called CC=2 SCAR=7.2
51 4 my ($self) = @_;
52 4 ▶ return Net::Amazon::S3::Client->new( s3 =>
Branch: 1 branch
BranchTrueFalseTotal
$self->host ? : 4 0 4
53 Net::Amazon::S3->new(
54 aws_access_key_id => $self->aws_access_key_id(),
55 aws_secret_access_key => $self->aws_secret_access_key(),
56 retry => $self->retry(),
57 ( $self->host() ? ( host => $self->host() ) : () )
58 ));
59 }
60  
61 =head1 NAME
62  
63 Log::Log4perl::Appender::Chunk::Store::S3 - Store chunks in an S3 bucket
64  
65 =head1 SYNOPSIS
66  
67 Example:
68  
69 # Built-in store class S3
70 log4perl.appender.Chunk.store_class=S3
71 # S3 Mandatory options
72 log4perl.appender.Chunk.store_args.bucket_name=MyLogChunks
73 log4perl.appender.Chunk.store_args.aws_access_key_id=YourAWSAccessKey
74 log4perl.appender.Chunk.store_args.aws_secret_access_key=YourAWS
75  
76  
77 See L<Log::Log4perl::Appender::Chunk>'s synopsis for a more complete example.
78  
79 =head1 OPTIONS
80  
81 =over
82  
83 =item bucket_name
84  
85 Mandatory. Name of the Amazon S3 bucket to store the log chunks.
86  
87 =item location_constraint
88  
89 Optional. If your current Net::Amazon::S3 supports it, you can use that to set the location constraint
90 of the vivified bucket. See L<Net::Amazon::S3#add_bucket> for more info on supported constraints.
91  
92 Note that if you specify a location constraint, you will have to follow Amazon's recommendation about
93 naming your bucket. See L<http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html> for restriction
94 about bucket names. In Particular, if you are outside the US, you will not be able to have upper case characters
95 in your bucket name.
96  
97 =item host
98  
99 Optional. If the bucket you are using is not in the USA, and depending on the version of L<Net::Amazon::S3> you
100 have, you might want to set that to a different host, according to
101 L<http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region>
102  
103 See <Net::Amazon::S3#new>
104  
105 =item aws_access_key_id
106  
107 Mandatory. Your S3 access key ID. See L<Net::Amazon::S3>
108  
109 =item asw_secret_acccess_key
110  
111 Mandatory. Your S3 Secret access key. See L<Net::Amazon::S3>
112  
113 =item retry
114  
115 Optional. See L<Net::Amazon::S3>
116  
117 Defaults to true (1).
118  
119 =item with_forking
120  
121 Optional. Causes this to (double) fork before actually storing stuff to S3, so your processes
122 are not slowed down by slow transfers to S3. On highly loaded machines, this
123 could actually be slower than not forking at all. So set this to false (0) on higly
124 loaded machines.
125  
126 Defaults to true (1).
127  
128 =item acl_short
129  
130 Optional. Shortcut to commonly used ACL rules. Valid values are:
131 private public-read public-read-write authenticated-read.
132  
133 See L<https://metacpan.org/source/PFIG/Net-Amazon-S3-0.59/lib/Net/Amazon/S3/Client/Object.pm>
134  
135 Defaults to undef, meaning your Amazon Bucket's default will be applied. That's probably
136 the most desirable behaviour.
137  
138 =item expires_in_days
139  
140 Optional. Amount of days in the future stored chunks should expire. No value means never.
141  
142 Defaults to undef.
143  
144 =item vivify_bucket
145  
146 Optional. If true, this writer will attempt to vivify a non existing bucket name if possible.
147  
148 Defaults to false.
149  
150 =item log_auth_links
151  
152 Optional. If true, this writer will log (at DEBUG level) the authenticated links to the stored chunks
153 in other log appenders.
154  
155 Use with care as this could lead to confidential information leakage.
156  
157 Defaults to false.
158  
159 =back
160  
161 =head1 METHODS
162  
163 =head2 clone
164  
165 Returns a fresh copy of myself based on the same settings. Mainly used internaly.
166  
167 Usage:
168  
169 my $clone = $this->clone();
170  
171 =cut
172  
173 2 ▶sub clone{
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
174 2 my ($self) = @_;
175 2 return __PACKAGE__->new({bucket_name => $self->bucket_name(),
176 aws_access_key_id => $self->aws_access_key_id(),
177 aws_secret_access_key => $self->aws_secret_access_key(),
178 retry => $self->retry(),
179 acl_short => $self->acl_short(),
180 expires_in_days => $self->expires_in_days(),
181 vivify_bucket => $self->vivify_bucket(),
182 log_auth_links => $self->log_auth_links(),
183 host => $self->host(),
184 location_constraint => $self->location_constraint(),
185 with_forking => $self->with_forking()
186 });
187 }
188  
189 0 ▶sub _build_bucket{
Subroutine _build_bucket: not called CC=5 SCAR=34.0
190 0 my ($self) = @_;
191  
192 0 my $s3_client = $self->s3_client();
193 0 my $bucket_name = $self->bucket_name();
194  
195 # Try to hit an existing bucket from the list
196 0 my @buckets = $s3_client->buckets();
197 0 foreach my $bucket ( @buckets ){
198 0 ▶ if( $bucket->name() eq $bucket_name ){
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket->name eq $bucket_name) 0 0 0
199 # Hit!
200 0 return $bucket;
201 }
202 }
203  
204 0 ▶ unless( $self->vivify_bucket() ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->vivify_bucket) 0 0 0
205 0 confess("Could not find bucket ".$bucket_name." in this account [access_key_id='".$self->aws_access_key_id()."'] and no vivify_bucket option");
206 }
207 0 ▶ return $self->s3_client()->create_bucket( name => $bucket_name,
Branch: 1 branch
BranchTrueFalseTotal
$self->location_constraint ? : 0 0 0
208 ( $self->location_constraint() ? ( location_constraint => $self->location_constraint() ) : () )
209 );
210 }
211  
212 4 ▶sub _expiry_ymd{
Subroutine _expiry_ymd: called CC=2 SCAR=7.6
213 4 my ($self) = @_;
214 4 ▶ unless( $self->expires_in_days() ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->expires_in_days) 0 4 4
215 0 return undef;
216 }
217 4 return DateTime->now()->add( days => $self->expires_in_days() )->ymd();
218 }
219  
220 =head2 store
221  
222 See superclass L<Log::Log4perl::Appender::Chunk::Store>
223  
224 =cut
225  
226 0 ▶sub store{
Subroutine store: not called CC=11 SCAR=48.8
Pod documented
227 0 my ($self, $chunk_id, $big_message) = @_;
228  
229 0 ▶ if( $self->with_forking() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_forking) 0 0 0
230  
231 0 ▶ defined(my $child = fork()) or confess("Cannot fork: $!");
Branch: 1 branch
BranchTrueFalseTotal
defined(my $child = fork) or confess("Cannot fork: $!") 0 0 0
232 0 ▶ if( $child ){
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 0 0 0
233 ## We are the main parent. We wait for the child.
234 0 waitpid($child, 0);
235 0 return 1;
236 }
237  
238 # We are the child
239 # Double fork to avoid zombies.
240 0 ▶ defined( my $grand_child = fork() ) or confess("Cannot double fork: $!");
Branch: 1 branch
BranchTrueFalseTotal
defined(my $grand_child = fork) or confess("Cannot double fork: $!") 0 0 0
241 0 ▶ if( $grand_child ){
Branch: 1 branch
BranchTrueFalseTotal
if ($grand_child) 0 0 0
242 # We are the child but we dont wait for
243 # our grand child. It will be picked up by init
244 0 exit(0);
245 }
246 # Grand child. We can do stuff.
247 0 $self = $self->clone();
248 }
249  
250 0 my $expires_ymd = $self->_expiry_ymd();
251 0 ▶ my $s3object = $self->bucket()->object( key => $chunk_id,
Branch: 2 branches
BranchTrueFalseTotal
$self->acl_short ? : 0 0 0
$expires_ymd ? : 0 0 0
252 content_type => 'text/plain; charset=utf-8',
253 $self->acl_short() ? ( acl_short => $self->acl_short() ) : (),
254 $expires_ymd ? ( expires => $expires_ymd ) : (),
255 );
256 0 eval{
257 0 $s3object->put(Encode::encode_utf8($big_message));
258 };
259 0 ▶ if( my $err = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
260 0 $LOGGER->error("Log chunk storing error: $err");
261 0 warn "Log chunk storing error: $err";
262 0 exit(1);
263 }
264 0 ▶ if( $self->log_auth_links() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->log_auth_links) 0 0 0
265 0 $LOGGER->info("Stored log chunk in ".$s3object->query_string_authentication_uri());
266 }
267  
268 0 ▶ if( $self->with_forking() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_forking) { } 0 0 0
269 0 exit(0);
270 }else{
271 0 return 1;
272 }
273 }
274  
275 3 __PACKAGE__->meta->make_immutable();
↑ Summary Chunk.pm »