diff --git a/cloudwatch/client.go b/cloudwatch/client.go index a8b15b6..f446d29 100644 --- a/cloudwatch/client.go +++ b/cloudwatch/client.go @@ -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...) diff --git a/cloudwatch/request_type.go b/cloudwatch/request_type.go index 2e07fff..d0d4a5c 100644 --- a/cloudwatch/request_type.go +++ b/cloudwatch/request_type.go @@ -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 { @@ -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 } diff --git a/private/pointers/pointers.go b/private/pointers/pointers.go index e4ead3d..ef065a7 100644 --- a/private/pointers/pointers.go +++ b/private/pointers/pointers.go @@ -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 +}