1 package cloudwatch2
2
3 import (
4 "context"
5 "strings"
6 "testing"
7
8 "github.com/aws/aws-sdk-go-v2/service/cloudwatch"
9 "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
10 )
11
12 func TestStats(t *testing.T) {
13 testCases := []struct {
14 name string
15 vals []float64
16 xMin float64
17 xMax float64
18 xSum float64
19 xCt float64
20 }{
21 {
22 "empty",
23 []float64{},
24 0.0,
25 0.0,
26 0.0,
27 0.0,
28 },
29 {
30 "single",
31 []float64{3.1416},
32 3.1416,
33 3.1416,
34 3.1416,
35 1.0,
36 },
37 {
38 "double",
39 []float64{1.0, 9.0},
40 1.0,
41 9.0,
42 10.0,
43 2.0,
44 },
45 {
46 "multiple",
47 []float64{5.0, 1.0, 9.0, 5.0},
48 1.0,
49 9.0,
50 20.0,
51 4.0,
52 },
53 }
54
55 for _, tc := range testCases {
56 t.Run(tc.name, func(t *testing.T) {
57 s := stats(tc.vals)
58 if tc.xMin != *s.Minimum {
59 t.Errorf("expected [%f]: %f\n", tc.xMin, *s.Minimum)
60 }
61 if tc.xMax != *s.Maximum {
62 t.Errorf("expected [%f]: %f\n", tc.xMax, *s.Maximum)
63 }
64 if tc.xSum != *s.Sum {
65 t.Errorf("expected [%f]: %f\n", tc.xSum, *s.Sum)
66 }
67 if tc.xCt != *s.SampleCount {
68 t.Errorf("expected [%f]: %f\n", tc.xCt, *s.SampleCount)
69 }
70 })
71 }
72 }
73
74 type mockCloudWatch struct {
75 CloudWatchAPI
76 latestName string
77 latestData []types.MetricDatum
78 }
79
80 func (mcw *mockCloudWatch) PutMetricData(ctx context.Context, params *cloudwatch.PutMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error) {
81 mcw.latestName = *params.Namespace
82 mcw.latestData = params.MetricData
83
84 return nil, nil
85 }
86
87 func TestSend(t *testing.T) {
88 ns := "example-namespace"
89 svc := &mockCloudWatch{}
90 cw := New(ns, svc)
91
92 c := cw.NewCounter("c").With("charlie", "cat")
93 h := cw.NewHistogram("h").With("hotel", "horse")
94 g := cw.NewGauge("g").With("golf", "giraffe")
95
96 c.Add(4.0)
97 c.Add(5.0)
98 c.Add(6.0)
99 h.Observe(3.0)
100 h.Observe(5.0)
101 h.Observe(7.0)
102 g.Set(2.0)
103 g.Set(5.0)
104 g.Set(8.0)
105
106 err := cw.Send()
107 if err != nil {
108 t.Fatalf("unexpected: %v\n", err)
109 }
110
111 if ns != svc.latestName {
112 t.Errorf("expected namespace %q; not %q\n", ns, svc.latestName)
113 }
114
115 if len(svc.latestData) != 3 {
116 t.Errorf("expected 3 datums: %v\n", svc.latestData)
117 }
118 for _, datum := range svc.latestData {
119 initial := *datum.MetricName
120 if len(datum.Dimensions) != 1 {
121 t.Errorf("expected 1 dimension: %v\n", datum)
122 }
123 if !strings.HasPrefix(*datum.Dimensions[0].Name, initial) {
124 t.Errorf("expected %q in Name of %v\n", initial, datum.Dimensions)
125 }
126 if !strings.HasPrefix(*datum.Dimensions[0].Value, initial) {
127 t.Errorf("expected %q in Value of %v\n", initial, datum.Dimensions)
128 }
129 if datum.StatisticValues == nil {
130 t.Errorf("expected StatisticValues in %v\n", datum)
131 }
132 if *datum.StatisticValues.Sum != 15.0 {
133 t.Errorf("expected 15.0 for Sum in %v\n", datum)
134 }
135 if *datum.StatisticValues.SampleCount != 3.0 {
136 t.Errorf("expected 3.0 for SampleCount in %v\n", datum)
137 }
138 }
139 }
140
View as plain text