github.com/hhsnopek/up@v0.1.1/internal/metrics/metrics.go (about)

     1  // Package metrics provides higher level CloudWatch metrics operations.
     2  package metrics
     3  
     4  import (
     5  	"time"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/session"
     9  	"github.com/aws/aws-sdk-go/service/cloudwatch"
    10  	"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
    11  )
    12  
    13  // DefaultClient is the default client used for cloudwatch.
    14  var DefaultClient = cloudwatch.New(session.New(aws.NewConfig()))
    15  
    16  // Metrics helper.
    17  type Metrics struct {
    18  	client cloudwatchiface.CloudWatchAPI
    19  	in     cloudwatch.GetMetricStatisticsInput
    20  }
    21  
    22  // New metrics.
    23  func New() *Metrics {
    24  	return &Metrics{
    25  		client: DefaultClient,
    26  	}
    27  }
    28  
    29  // Client sets the client.
    30  func (m *Metrics) Client(c cloudwatchiface.CloudWatchAPI) *Metrics {
    31  	m.client = c
    32  	return m
    33  }
    34  
    35  // Namespace sets the namespace.
    36  func (m *Metrics) Namespace(name string) *Metrics {
    37  	m.in.Namespace = &name
    38  	return m
    39  }
    40  
    41  // Metric sets the metric name.
    42  func (m *Metrics) Metric(name string) *Metrics {
    43  	m.in.MetricName = &name
    44  	return m
    45  }
    46  
    47  // Stats sets the stats.
    48  func (m *Metrics) Stats(names []string) *Metrics {
    49  	m.in.Statistics = aws.StringSlice(names)
    50  	return m
    51  }
    52  
    53  // Stat adds the stat.
    54  func (m *Metrics) Stat(name string) *Metrics {
    55  	m.in.Statistics = append(m.in.Statistics, &name)
    56  	return m
    57  }
    58  
    59  // Dimension adds a dimension.
    60  func (m *Metrics) Dimension(name, value string) *Metrics {
    61  	m.in.Dimensions = append(m.in.Dimensions, &cloudwatch.Dimension{
    62  		Name:  &name,
    63  		Value: &value,
    64  	})
    65  
    66  	return m
    67  }
    68  
    69  // Period sets the period.
    70  func (m *Metrics) Period(minutes int64) *Metrics {
    71  	m.in.Period = &minutes
    72  	return m
    73  }
    74  
    75  // TimeRange sets the start and time times.
    76  func (m *Metrics) TimeRange(start, end time.Time) *Metrics {
    77  	m.in.StartTime = &start
    78  	m.in.EndTime = &end
    79  	return m
    80  }
    81  
    82  // Input returns the API input.
    83  func (m *Metrics) Input() *cloudwatch.GetMetricStatisticsInput {
    84  	return &m.in
    85  }
    86  
    87  // Get metrics.
    88  func Get(m *Metrics) (*cloudwatch.GetMetricStatisticsOutput, error) {
    89  	return m.client.GetMetricStatistics(m.Input())
    90  }