File Coverage

blib/lib/WebService/DataDog/Graph.pm
Criterion Covered Total %
statement 18 36 50.0
branch 0 10 0.0
condition 0 9 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 25 63 39.6


line stmt bran cond sub pod time code
1             package WebService::DataDog::Graph;
2              
3 1     1   47315 use strict;
  1         2  
  1         25  
4 1     1   3 use warnings;
  1         1  
  1         20  
5              
6 1     1   3 use base qw( WebService::DataDog );
  1         1  
  1         298  
7 1     1   7 use Carp qw( carp croak );
  1         2  
  1         48  
8 1     1   4 use Data::Dumper;
  1         2  
  1         32  
9 1     1   3 use Try::Tiny;
  1         1  
  1         227  
10              
11              
12             =head1 NAME
13              
14             WebService::DataDog::Graph - Interface to Graph functions in DataDog's API.
15              
16             =head1 VERSION
17              
18             Version 1.0.3
19              
20             =cut
21              
22             our $VERSION = '1.0.3';
23              
24              
25             =head1 SYNOPSIS
26              
27             This module allows you interact with the graph endpoint of the DataDog API.
28              
29             Per DataDog: "You can take graph snapshots using the API"
30              
31              
32             =head1 METHODS
33              
34             =head2 snapshot()
35              
36             Take a graph snapshot.
37            
38             my $graph = $datadog->build('Graph');
39             my $snapshot_url = $graph->snapshot(
40             metric_query => $metric_query,
41             start => $start_timestamp,
42             end => $end_timestamp,
43             event_query => $event_query, # optional -- default=None
44             );
45            
46             Example:
47             my $snapshot_url = $graph->snapshot(
48             metric_query => "system.load.1{*}",
49             start => 1388632282
50             end => 1388718682
51             );
52            
53             Parameters:
54              
55             =over 4
56              
57             =item * metric_query
58              
59             Metric query to capture in the graph.
60              
61             =item * start
62              
63             The POSIX timestamp of the start of the query.
64              
65             =item * end
66              
67             The POSIX timestamp of the end of the query.
68              
69             =item * event_query
70              
71             A query that will add event bands to the graph.
72              
73             =back
74              
75             =cut
76              
77             sub snapshot
78             {
79 0     0 1   my ( $self, %args ) = @_;
80 0           my $verbose = $self->verbose();
81            
82             # Check for mandatory parameters
83 0           foreach my $arg ( qw( metric_query start end ) )
84             {
85 0 0 0       croak "ERROR - Argument '$arg' is required for snapshot()."
86             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
87             }
88            
89             # Check for valid parameters
90 0           foreach my $arg ( qw( start end ) )
91             {
92 0 0         croak "ERROR - Argument '$arg' must be an integer, required for snapshot()."
93             if $args{$arg} !~ /^\d+$/;
94             }
95            
96 0           my $url = $WebService::DataDog::API_ENDPOINT . 'graph/snapshot?';
97 0           $url .= 'metric_query=' . $args{'metric_query'} . '&';
98 0           $url .= 'start=' . $args{'start'} . '&';
99 0           $url .= 'end=' . $args{'end'};
100            
101 0 0 0       if ( defined( $args{'event_query'} ) && $args{'event_query'} ne '' )
102             {
103 0           $url .= '&event_query=' . $args{'event_query'};
104             }
105            
106 0           my $response = $self->_send_request(
107             method => 'GET',
108             url => $url,
109             data => { '' => [] }
110             );
111            
112 0 0         if ( !defined($response) )
113             {
114 0           croak "Fatal error. No response.";
115             }
116            
117 0 0 0       if ( !defined($response->{'snapshot_url'}) || $response->{'snapshot_url'} eq '' )
118             {
119 0           croak "Fatal error. Missing or invalid snapshot_url.";
120             }
121            
122 0           return $response->{'snapshot_url'};
123             }
124              
125              
126             1;