File Coverage

blib/lib/WebService/DataDog/Event.pm
Criterion Covered Total %
statement 15 104 14.4
branch 0 78 0.0
condition 0 21 0.0
subroutine 5 12 41.6
pod 5 5 100.0
total 25 220 11.3


line stmt bran cond sub pod time code
1             package WebService::DataDog::Event;
2              
3 1     1   15195 use strict;
  1         2  
  1         31  
4 1     1   3 use warnings;
  1         1  
  1         25  
5              
6 1     1   4 use base qw( WebService::DataDog );
  1         0  
  1         316  
7 1     1   4 use Carp qw( carp croak );
  1         1  
  1         44  
8 1     1   4 use Data::Dumper;
  1         1  
  1         1413  
9              
10              
11             =head1 NAME
12              
13             WebService::DataDog::Event - Interface to Event functions in DataDog's API.
14              
15             =head1 VERSION
16              
17             Version 1.0.1
18              
19             =cut
20              
21             our $VERSION = '1.0.1';
22              
23              
24             =head1 SYNOPSIS
25              
26             This module allows you interact with the Event endpoint of the DataDog API.
27              
28             Per DataDog: "The events service allows you to programatically post events to
29             the stream and fetch events from the stream."
30              
31              
32             =head1 METHODS
33              
34             =head2 search()
35              
36             Search the event stream using specified parameters.
37              
38             my $event = $datadog->build('Event');
39             my $event_list = $event->search(
40             start => $start_time,
41             end => $end_time, # Optional - default 'now'
42             priority => $priority, # Optional - low|normal
43             sources => $sources, # Optional - list of sources. Ex: Datadog, Github, Pingdom, Webmetrics
44             tags => $tag_list, # Optional - list of tags associated with the event
45             );
46            
47             Examples:
48             + Find all events in the last 48 hours.
49             my $event_list = $event->search(
50             start => time() - ( 48 * 60 * 60 ),
51             );
52            
53             + Find all events in the last 24 hours tagged with 'env:prod'.
54             my $event_list = $event->search(
55             start => time() - ( 24 * 60 * 60 ),
56             end => time(),
57             tags => [ 'env:prod' ],
58             );
59            
60             Parameters:
61              
62             =over 4
63              
64             =item * start
65              
66             The start of the date/time range to be searched. UNIX/Epoch/POSIX time.
67              
68             =item * end
69              
70             Optional. The end of the date/time range to be searched. UNIX/Epoch/POSIX time.
71             Default = now.
72              
73             =item * priority
74              
75             Optional. Event priority level. Accepted values: low, normal.
76              
77             =item * sources
78              
79             Optional. List of sources that generated events.
80              
81             =item * tags
82              
83             Optional. List of tags associated with the events.
84              
85             =back
86              
87             =cut
88              
89             sub search
90             {
91 0     0 1   my ( $self, %args ) = @_;
92 0           my $verbose = $self->verbose();
93              
94             # Perform various error checks before attempting to search events
95 0           $self->_search_error_checks( %args );
96            
97 0           my $url = $WebService::DataDog::API_ENDPOINT . 'events' . '?';
98            
99 0           $url .= 'start=' . $args{'start'};
100 0 0         $url .= '&end=' . ( defined $args{'end'} ? $args{'end'} : time() );
101            
102 0 0         if ( defined( $args{'priority'} ) )
103             {
104 0           $url .= '&priority=' . $args{'priority'};
105             }
106            
107 0 0         if ( defined( $args{'tags'} ) )
108             {
109 0           $url .= '&tags=' . ( join( ',', @{ $args{'tags'} } ) );
  0            
110             }
111            
112 0 0         if ( defined( $args{'sources'} ) )
113             {
114 0           $url .= '&sources=' . ( join( ',', @{ $args{'sources'} } ) );
  0            
115             }
116            
117 0           my $response = $self->_send_request(
118             method => 'GET',
119             url => $url,
120             data => { '' => [] }
121             );
122            
123 0 0 0       if ( !defined($response) || !defined($response->{'events'}) )
124             {
125 0           croak "Fatal error. No response or 'events' missing from response.";
126             }
127            
128 0           return $response->{'events'};
129             }
130              
131              
132             =head2 get_event()
133              
134             Deprecated. Please use retrieve() instead.
135              
136             =cut
137              
138             sub get_event
139             {
140 0     0 1   my ( $self, %args ) = @_;
141            
142 0           carp "get_event() is deprecated. Please use retrieve() instead.";
143            
144 0           return $self->retrieve( %args );
145             }
146              
147              
148             =head2 retrieve()
149              
150             Get details of specified event.
151             NOTE: Receiving a 404 response likely means the requested event id does not exist.
152              
153             my $event = $datadog->build('Event');
154             my $event_data = $event->retrieve( id => $event_id );
155             =cut
156              
157             sub retrieve
158             {
159 0     0 1   my ( $self, %args ) = @_;
160 0           my $verbose = $self->verbose();
161            
162             # Check for mandatory parameters
163 0           foreach my $arg ( qw( id ) )
164             {
165 0 0 0       croak "ERROR - Argument '$arg' is required."
166             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
167             }
168            
169             # Check that id specified is a number
170 0 0         croak "ERROR - Event id must be a number. You specified >" . $args{'id'} . "<"
171             unless $args{'id'} =~ /^\d+$/;
172            
173 0           my $url = $WebService::DataDog::API_ENDPOINT . 'events' . '/' . $args{'id'};
174 0           my $response = $self->_send_request(
175             method => 'GET',
176             url => $url,
177             data => { '' => [] }
178             );
179            
180 0 0 0       if ( !defined($response) || !defined($response->{'event'}) )
181             {
182 0           croak "Fatal error. No response or 'event' missing from response.";
183             }
184            
185 0           return $response->{'event'};
186             }
187              
188              
189             =head2 post_event()
190              
191             Deprecated. Please use create() instead.
192              
193             =cut
194              
195             sub post_event
196             {
197 0     0 1   my ( $self, %args ) = @_;
198            
199 0           carp "post_event() is deprecated. Please use create() instead.";
200            
201 0           return $self->create( %args );
202             }
203              
204              
205             =head2 create()
206              
207             Post event to DataDog event stream. This will overlay red areas on all dashboards,
208             corresponding to each event. Example uses: code pushes, server/service restarts, etc.
209              
210             Per DataDog: "This end point allows you to post events to the stream. You can
211             tag them, set priority and event aggregate them with other events."
212              
213             my $event = $datadog->build('Event');
214             $event->create(
215             title => $event_title,
216             text => $event_text, # Body/Description of the event.
217             date_happened => $timestamp, # Optional, default "now"
218             priority => $priority, # Optional. normal|low
219             related_event_id => $event_id, # Optional, id of aggregate event
220             tags => $tag_list, # Optional - tags to apply to event (easy to search by)
221             alert_type => $alert_type, # Optional. error|warning|info|success
222             aggregation_key => $agg_key, # Optional. Arbitrary string to use for aggregation.
223             source_type_name => $source_type, # Optional. nagios|hudson|jenkins|user|my apps|feed|chef|puppet|git|bitbucket|fabric|capistrano
224             );
225            
226             Examples:
227             + Submit a user event, with timestamp of `now`.
228             $event->create(
229             title => 'Test event',
230             text => 'Testing posting to event stream',
231             source_type_name => 'user',
232             );
233            
234             Parameters:
235              
236             =over 4
237              
238             =item * title
239              
240             The event title.
241              
242             =item * text
243              
244             Optional. Event body/description.
245              
246             =item * date_happened
247              
248             Optional. Default value 'now'. POSIX/Unix time.
249              
250             =item * priority
251              
252             Optional. Allowed values: normal, low.
253              
254             =item * related_event_id
255              
256             Optional. The id of the aggregate event.
257              
258             =item * tags
259              
260             Optional. List of tags associated with the event.
261              
262             =item * alert_type
263              
264             Optional. "error", "warning", "info" or "success"
265              
266             =item * aggregation_key
267              
268             Optional. An arbitrary string to use for aggregation.
269              
270             =item * source_type_name
271              
272             Optional. The type of event being posted. Allowed values: nagios, hudson,
273             jenkins, user, my apps, feed, chef, puppet, git, bitbucket, fabric, capistrano
274              
275             =back
276              
277             =cut
278              
279             sub create
280             {
281 0     0 1   my ( $self, %args ) = @_;
282 0           my $verbose = $self->verbose();
283            
284             # Perform various error checks before attempting to send metrics
285 0           $self->_create_error_checks( %args );
286            
287 0           my $data = {
288             title => $args{'title'},
289             text => $args{'text'},
290             };
291            
292 0 0         if ( defined( $args{'date_happened'} ) )
293             {
294 0           $data->{'date_happened'} = $args{'date_happened'};
295             }
296            
297 0 0         if ( defined $args{'priority'} )
298             {
299 0           $data->{'priority'} = $args{'priority'};
300             }
301            
302 0 0         if ( defined( $args{'related_event_id'} ) )
303             {
304 0           $data->{'related_event_id'} = $args{'related_event_id'};
305             }
306            
307 0 0         if ( defined( $args{'tags'} ) )
308             {
309 0           $data->{'tags'} = $args{'tags'};
310             }
311            
312 0 0         if ( defined( $args{'alert_type'} ) )
313             {
314 0           $data->{'alert_type'} = $args{'alert_type'};
315             }
316            
317 0 0         if ( defined( $args{'source_type_name'} ) )
318             {
319 0           $data->{'source_type_name'} = $args{'source_type_name'};
320             }
321            
322 0           my $url = $WebService::DataDog::API_ENDPOINT . 'events';
323            
324 0           my $response = $self->_send_request(
325             method => 'POST',
326             url => $url,
327             data => $data,
328             );
329            
330 0 0         croak "ERROR - did not receive 'status: ok'. Response:", Dumper($response)
331             unless $response->{'status'} eq "ok";
332            
333 0           return;
334             }
335              
336              
337             =head1 INTERNAL FUNCTIONS
338              
339             =head2 _search_error_checks()
340              
341             Error checking for search()
342              
343             =cut
344              
345             sub _search_error_checks
346             {
347 0     0     my ( $self, %args ) = @_;
348 0           my $verbose = $self->verbose();
349            
350             # Check for mandatory parameters
351 0           foreach my $arg ( qw( start ) )
352             {
353 0 0 0       croak "Argument '$arg' is required for search()"
354             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
355             }
356            
357             # Check that 'start' is valid
358 0 0         croak "ERROR - invalid 'start' value >" . $args{'start'} . "<. Must be POSIX/Unixtime"
359             unless ( $args{'start'} =~ /^\d{10,}$/ ); #min 10 digits, allowing for older data back to 1/1/2000
360            
361             # Check that 'end' is valid
362 0 0         if ( defined $args{'end'} )
363             {
364 0 0         croak "ERROR - invalid 'end' value >" . $args{'end'} . "<. Must be POSIX/Unixtime"
365             unless ( $args{'end'} =~ /^\d{10,}$/ ); #min 10 digits, allowing for older data back to 1/1/2000
366             }
367            
368             # Check that 'priority' is valid
369 0 0         if ( defined $args{'priority'} )
370             {
371 0 0 0       croak "ERROR - invalid 'priority' value >" . $args{'priority'} . "<. Allowed values: low, normal."
372             unless ( lc( $args{'priority'} ) eq "low" || lc( $args{'priority'} ) eq "normal" );
373             }
374            
375             # Check that 'tags' is valid
376 0 0         if ( defined( $args{'tags'} ) )
377             {
378 0 0         if ( !Data::Validate::Type::is_arrayref( $args{'tags'} ) )
379             {
380 0           croak "ERROR - invalid 'tags' value. Must be an arrayref.";
381             }
382             }
383            
384             # Check that 'sources' is valid
385 0 0         if ( defined( $args{'sources'} ) )
386             {
387 0 0         if ( !Data::Validate::Type::is_arrayref( $args{'sources'} ) )
388             {
389 0           croak "ERROR - invalid 'sources' value. Must be an arrayref.";
390             }
391             }
392            
393 0           return;
394             }
395              
396              
397             =head2 _create_error_checks()
398              
399             Error checking for create()
400              
401             =cut
402              
403             sub _create_error_checks
404             {
405 0     0     my ( $self, %args ) = @_;
406 0           my $verbose = $self->verbose();
407            
408             # Check for mandatory parameters
409 0           foreach my $arg ( qw( title text ) )
410             {
411 0 0 0       croak "Argument '$arg' is required for create()"
412             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
413             }
414            
415             # Check that title is <= 100 characters. Per Carlo @ DDog. Undocumented?
416 0 0         croak( "ERROR - invalid 'title' >" . $args{'title'} . "<. Title must be 100 characters or less." )
417             if ( length( $args{'title'} ) > 100 );
418            
419             # Check that 'date_happened' is valid
420 0 0         if ( defined( $args{'date_happened'} ) )
421             {
422 0 0         croak "ERROR - invalid 'date_happened' >" . $args{'date_happened'} . "<. Must be POSIX/Unixtime"
423             unless ( $args{'date_happened'} =~ /^\d{10,}$/ ); #min 10 digits, allowing for older data back to 1/1/2000
424             }
425            
426             # Check that 'priority' is valid
427 0 0         if ( defined $args{'priority'} )
428             {
429 0 0 0       croak "ERROR - invalid 'priority' >" . $args{'priority'} . "<. Allowed values: low, normal."
430             unless ( lc( $args{'priority'} ) eq "low" || lc( $args{'priority'} ) eq "normal" );
431             }
432            
433             # Check that 'related_event_id' is valid
434 0 0         if ( defined( $args{'related_event_id'} ) )
435             {
436 0 0         croak "ERROR - invalid 'related_event_id' >" . $args{'related_event_id'} . "<"
437             unless $args{'related_event_id'} =~ /^\d+$/;
438             }
439            
440             # Check that 'tags' is valid
441 0 0         if ( defined( $args{'tags'} ) )
442             {
443 0 0         if ( !Data::Validate::Type::is_arrayref( $args{'tags'} ) )
444             {
445 0           croak "ERROR - invalid 'tags' value. Must be an arrayref.";
446             }
447             }
448            
449             # Check that 'alert_type' is valid
450 0 0         if ( defined( $args{'alert_type'} ) )
451             {
452 0 0         croak "ERROR - invalid 'alert_type' >" . $args{'alert_type'} . "<. Allowed values: error, warning, info, success"
453             unless $args{'alert_type'} =~ /^error|warning|info|success$/;
454             }
455            
456             # Check that 'source_type_name' is valid
457 0 0         if ( defined( $args{'source_type_name'} ) )
458             {
459 0 0         croak "ERROR - invalid 'source_type_name' >" . $args{'source_type_name'} . "<. Allowed values: nagios|hudson|jenkins|user|my apps|feed|chef|puppet|git|bitbucket|fabric|capistrano"
460             unless $args{'source_type_name'} =~ /^nagios|hudson|jenkins|user|my apps|feed|chef|puppet|git|bitbucket|fabric|capistrano$/; ## no critic qw( RegularExpressions::RequireExtendedFormatting RegularExpressions::ProhibitComplexRegexes )
461             }
462            
463 0           return;
464             }
465              
466              
467             1;