Skip to content

Commit

Permalink
[cloudwatch] Add 'PutMetricData' (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
evalphobia authored Mar 13, 2020
1 parent a8a27a0 commit 29b6c45
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 9 deletions.
14 changes: 14 additions & 0 deletions cloudwatch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func (svc *CloudWatch) DoGetMetricStatistics(in *SDK.GetMetricStatisticsInput) (
return out, nil
}

// PutMetricData executes PutMetricData operation.
func (svc *CloudWatch) PutMetricData(in PutMetricDataInput) error {
return svc.DoPutMetricData(in.ToInput())
}

// DoPutMetricData executes PutMetricData operation.
func (svc *CloudWatch) DoPutMetricData(in *SDK.PutMetricDataInput) error {
_, err := svc.client.PutMetricData(in)
if err != nil {
svc.Errorf("error on `PutMetricData` operation; error=%s;", err.Error())
}
return err
}

// Infof logging information.
func (svc *CloudWatch) Infof(format string, v ...interface{}) {
svc.logger.Infof(serviceName, format, v...)
Expand Down
112 changes: 103 additions & 9 deletions cloudwatch/request_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (o MetricStatisticsInput) ToInput() *SDK.GetMetricStatisticsInput {
if o.Unit != "" {
in.Unit = &o.Unit
}
in.Statistics = sliceStringToPointer(o.Statistics)
in.ExtendedStatistics = sliceStringToPointer(o.ExtendedStatistics)
in.Statistics = pointers.SliceString(o.Statistics)
in.ExtendedStatistics = pointers.SliceString(o.ExtendedStatistics)

in.Dimensions = make([]*SDK.Dimension, 0, len(o.DimensionsMap)+len(o.Dimensions))
for key, val := range o.DimensionsMap {
Expand All @@ -68,14 +68,108 @@ type Dimension struct {
Value string
}

func sliceStringToPointer(list []string) []*string {
if len(list) == 0 {
return nil
type PutMetricDataInput struct {
MetricData []MetricDatum
Namespace string
}

func (o *PutMetricDataInput) AddMetric(d MetricDatum) {
o.MetricData = append(o.MetricData, d)
}

func (o PutMetricDataInput) ToInput() *SDK.PutMetricDataInput {
in := &SDK.PutMetricDataInput{}
if o.Namespace != "" {
in.Namespace = &o.Namespace
}

result := make([]*string, len(list))
for i, v := range list {
result[i] = pointers.String(v)
in.MetricData = make([]*SDK.MetricDatum, 0, len(o.MetricData))
for _, v := range o.MetricData {
in.MetricData = append(in.MetricData, v.ToSDKValue())
}
return in
}

type MetricDatum struct {
MetricName string
Unit string
StorageResolution int64
Value float64
HasValue bool // use as true when value == 0
Values []float64
Counts []float64
Timestamp time.Time

StatisticValues StatisticSet
Dimensions []Dimension
}

func (d MetricDatum) ToSDKValue() *SDK.MetricDatum {
in := &SDK.MetricDatum{}
if d.MetricName != "" {
in.MetricName = &d.MetricName
}
if d.Unit != "" {
in.Unit = &d.Unit
}
return result
if d.StorageResolution != 0 {
in.StorageResolution = &d.StorageResolution
}
if d.HasValue || d.Value != 0 {
in.Value = &d.Value
}
in.Values = pointers.SliceFloat64(d.Values)
in.Counts = pointers.SliceFloat64(d.Counts)
if !d.Timestamp.IsZero() {
in.Timestamp = &d.Timestamp
}

in.StatisticValues = d.StatisticValues.ToSDKValue()
for _, v := range d.Dimensions {
in.Dimensions = append(in.Dimensions, &SDK.Dimension{
Name: pointers.String(v.Name),
Value: pointers.String(v.Value),
})
}
return in
}

type StatisticSet struct {
Maximum float64
Minimum float64
SampleCount float64
Sum float64

// use as true when value == 0
HasMaximum bool
HasMinimum bool
HasSampleCount bool
HasSum bool
}

func (d StatisticSet) ToSDKValue() *SDK.StatisticSet {
hasValue := false

in := &SDK.StatisticSet{}
if d.HasMaximum || d.Maximum != 0 {
in.Maximum = &d.Maximum
hasValue = true
}
if d.HasMinimum || d.Minimum != 0 {
in.Minimum = &d.Minimum
hasValue = true
}
if d.HasSampleCount || d.SampleCount != 0 {
in.SampleCount = &d.SampleCount
hasValue = true
}
if d.HasSum || d.Sum != 0 {
in.Sum = &d.Sum
hasValue = true
}

if !hasValue {
return nil
}
return in
}
31 changes: 31 additions & 0 deletions private/pointers/pointers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,38 @@ func Long64(v int64) *int64 {
return &v
}

// Float64 returns the pointer of float64.
func Float64(v float64) *float64 {
return &v
}

// Bool returns the pointer of bool.
func Bool(b bool) *bool {
return &b
}

// SliceString returns the slice of string pointer.
func SliceString(list []string) []*string {
if len(list) == 0 {
return nil
}

result := make([]*string, len(list))
for i, v := range list {
result[i] = String(v)
}
return result
}

// SliceFloat64 returns the slice of float64 pointer.
func SliceFloat64(list []float64) []*float64 {
if len(list) == 0 {
return nil
}

result := make([]*float64, len(list))
for i, v := range list {
result[i] = Float64(v)
}
return result
}

0 comments on commit 29b6c45

Please sign in to comment.