Skip to content

Commit

Permalink
Upload payload function
Browse files Browse the repository at this point in the history
  • Loading branch information
varunch77 committed Jan 13, 2025
1 parent c1d9494 commit 6ca1cc0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ func (c *CloudWatch) pushMetricDatum() {
entity, datums := c.BuildMetricDatum(metric)
numberOfPartitions := len(datums)
entityStr := entityToString(entity)
entityPresent := false
if entityStr != "" {
c.metricDatumBatch.Size += calculateEntitySize(entity)
entityPresent = true
}
for i := 0; i < numberOfPartitions; i++ {
c.metricDatumBatch.Partition[entityStr] = append(c.metricDatumBatch.Partition[entityStr], datums[i])
c.metricDatumBatch.Size += payload(datums[i])
c.metricDatumBatch.Size += payload(datums[i], entityPresent)
c.metricDatumBatch.Count++
if c.metricDatumBatch.isFull() {
// if batch is full
Expand Down
2 changes: 1 addition & 1 deletion plugins/outputs/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ func (c *CloudWatch) calculateTotalPayloadSize(metrics pmetric.Metrics, entity *
for _, agg := range aggregations {
_, datums := c.BuildMetricDatum(agg)
for _, datum := range datums {
totalSize += payload(datum)
totalSize += payload(datum, false)
}
}
if entity != nil {
Expand Down
15 changes: 14 additions & 1 deletion plugins/outputs/cloudwatch/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func resize(dist distribution.Distribution, listMaxSize int) (distList []distrib
return
}

func payload(datum *cloudwatch.MetricDatum) int {
func payload(datum *cloudwatch.MetricDatum, entityPresent bool) int {
size := timestampSize

for _, dimension := range datum.Dimensions {
Expand All @@ -117,17 +117,30 @@ func payload(datum *cloudwatch.MetricDatum) int {
size += highResolutionOverheads
}

entityValueMultipler := 0
valuesCountsLen := len(datum.Values)
if valuesCountsLen != 0 {
size += valuesCountsLen*valuesCountsOverheads + statisticsSize
entityValueMultipler += 2*valuesCountsLen + 4
} else {
size += valueOverheads
entityValueMultipler += 1

Check failure on line 127 in plugins/outputs/cloudwatch/util.go

View workflow job for this annotation

GitHub Actions / Check lint

increment-decrement: should replace entityValueMultipler += 1 with entityValueMultipler++ (revive)
}

if datum.Unit != nil {
size += unitOverheads
}

if entityPresent {
entityPrefixMultiplier := 1 + // timestamp
2*len(datum.Dimensions) + // dimensions
1 + // metric name
1 + // storage resolution
entityValueMultipler + // values
1 // unit
size += entityPrefixMultiplier * entityMetricDataPrefixOverhead
}

return size
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/outputs/cloudwatch/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestPayload_ValuesAndCounts(t *testing.T) {
datum.SetStorageResolution(1)
datum.SetTimestamp(time.Now())
datum.SetUnit("None")
assert.Equal(t, 867, payload(datum))
assert.Equal(t, 867, payload(datum, false))
}

func TestPayload_Value(t *testing.T) {
Expand All @@ -131,15 +131,15 @@ func TestPayload_Value(t *testing.T) {
datum.SetStorageResolution(1)
datum.SetTimestamp(time.Now())
datum.SetUnit("None")
assert.Equal(t, 356, payload(datum))
assert.Equal(t, 356, payload(datum, false))
}

func TestPayload_Min(t *testing.T) {
datum := new(cloudwatch.MetricDatum)
datum.SetValue(1.23456789)
datum.SetMetricName("MetricName")
datum.SetTimestamp(time.Now())
assert.Equal(t, 148, payload(datum))
assert.Equal(t, 148, payload(datum, false))
}

func TestCalculateEntitySize(t *testing.T) {
Expand Down

0 comments on commit 6ca1cc0

Please sign in to comment.