-
Notifications
You must be signed in to change notification settings - Fork 196
/
util.go
123 lines (104 loc) · 3.83 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package cloudwatch
import (
"log"
"math/rand"
"sort"
"time"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/private-amazon-cloudwatch-agent-staging/metric/distribution"
"github.com/aws/private-amazon-cloudwatch-agent-staging/metric/distribution/regular"
"github.com/aws/private-amazon-cloudwatch-agent-staging/metric/distribution/seh1"
)
const (
maxValuesPerDatum = 5000
// Constant for estimate encoded metric size
// Action=PutMetricData
pmdActionSize = 20
// &Version=2010-08-01
versionSize = 19
// &MetricData.member.100.StatisticValues.Maximum=1558.3086995967291&MetricData.member.100.StatisticValues.Minimum=1558.3086995967291&MetricData.member.100.StatisticValues.SampleCount=1000&MetricData.member.100.StatisticValues.Sum=1558.3086995967291
statisticsSize = 246
// &MetricData.member.100.Timestamp=2018-05-29T21%3A14%3A00Z
timestampSize = 57
overallConstPerRequestSize = pmdActionSize + versionSize
// &Namespace=, this is per request
namespaceOverheads = 11
// &MetricData.member.100.Dimensions.member.1.Name= &MetricData.member.100.Dimensions.member.1.Value=
dimensionOverheads = 48 + 49
// &MetricData.member.100.MetricName=
metricNameOverheads = 34
// &MetricData.member.100.StorageResolution=1
highResolutionOverheads = 42
// &MetricData.member.100.Values.member.100=1558.3086995967291 &MetricData.member.100.Counts.member.100=1000
valuesCountsOverheads = 59 + 45
// &MetricData.member.100.Value=1558.3086995967291
valueOverheads = 47
// &MetricData.member.1.Unit=Kilobytes/Second
unitOverheads = 42
)
// Set seed once.
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
// publishJitter returns a random duration between 0 and the given publishInterval.
func publishJitter(publishInterval time.Duration) time.Duration {
jitter := seededRand.Int63n(int64(publishInterval))
return time.Duration(jitter)
}
func setNewDistributionFunc(maxValuesPerDatumLimit int) {
if maxValuesPerDatumLimit >= maxValuesPerDatum {
distribution.NewDistribution = seh1.NewSEH1Distribution
} else {
distribution.NewDistribution = regular.NewRegularDistribution
}
}
func resize(dist distribution.Distribution, listMaxSize int) (distList []distribution.Distribution) {
var ok bool
// If this is SEH1 distribution, it has already considered the list max size.
if _, ok = dist.(*seh1.SEH1Distribution); ok {
distList = append(distList, dist)
return
}
var regularDist *regular.RegularDistribution
if regularDist, ok = dist.(*regular.RegularDistribution); !ok {
log.Printf("E! The distribution type %T is not supported for resizing.", dist)
return
}
values, _ := regularDist.ValuesAndCounts()
sort.Float64s(values)
newSEH1Dist := seh1.NewSEH1Distribution().(*seh1.SEH1Distribution)
for i := 0; i < len(values); i++ {
if !newSEH1Dist.CanAdd(values[i], listMaxSize) {
distList = append(distList, newSEH1Dist)
newSEH1Dist = seh1.NewSEH1Distribution().(*seh1.SEH1Distribution)
}
newSEH1Dist.AddEntry(values[i], regularDist.GetCount(values[i]))
}
if newSEH1Dist.Size() > 0 {
distList = append(distList, newSEH1Dist)
}
return
}
func payload(datum *cloudwatch.MetricDatum) (size int) {
size += timestampSize
for _, dimension := range datum.Dimensions {
size += len(*dimension.Name) + len(*dimension.Value) + dimensionOverheads
}
if datum.MetricName != nil {
// The metric name won't be nil, but it should fail in the validation instead of panic here.
size += len(*datum.MetricName) + metricNameOverheads
}
if datum.StorageResolution != nil {
size += highResolutionOverheads
}
valuesCountsLen := len(datum.Values)
if valuesCountsLen != 0 {
size += valuesCountsLen*valuesCountsOverheads + statisticsSize
} else {
size += valueOverheads
}
if datum.Unit != nil {
size += unitOverheads
}
return
}