diff --git a/agent/stats/service_connect_linux.go b/agent/stats/service_connect_linux.go index e6cdea13944..e78fa35453e 100644 --- a/agent/stats/service_connect_linux.go +++ b/agent/stats/service_connect_linux.go @@ -117,7 +117,17 @@ func convertToTACSStats(mf map[string]*prometheus.MetricFamily, taskId string) ( } metricCounts = append(metricCounts, &metricCount) } - metricCounts = convertHistogramMetricCounts(metricCounts) + + metricValues, metricCounts = convertHistogramMetricCounts(metricValues, metricCounts) + + // If all values are 0 in metricCount, then no need to send the metrics to TACS + if len(metricCounts) == 0 { + logger.Debug("There were no non-zero metricCount received for TargetResponseTime metric. Skipping this metric.", logger.Fields{ + field.TaskID: taskId, + }) + continue + } + default: logger.Warn("Service connect stats received invalid Metric type", logger.Fields{ field.TaskID: taskId, @@ -212,13 +222,20 @@ func (sc *ServiceConnectStats) resetStats() { } // CloudWatch accepts the histogram buckets in a disjoint manner while the prometheus emits these values in a cumulative way. -// This method performs that conversion -func convertHistogramMetricCounts(metricCounts []*int64) []*int64 { - prevCount := *metricCounts[0] - for i := 1; i < len(metricCounts); i++ { +// This method performs that conversion. We discard any metricCount that is 0 and also its corresponding metricValue. +func convertHistogramMetricCounts(metricValues []*float64, metricCounts []*int64) ([]*float64, []*int64) { + var mV []*float64 + var mC []*int64 + prevCount := int64(0) + for i := 0; i < len(metricCounts); i++ { prevCount, *metricCounts[i] = *metricCounts[i], *metricCounts[i]-prevCount + if metricCounts[i] != nil && *metricCounts[i] != 0 { + mV = append(mV, metricValues[i]) + mC = append(mC, metricCounts[i]) + } } - return metricCounts + + return mV, mC } // This method sorts the dimensions according to the keyName. diff --git a/agent/stats/service_connect_linux_test.go b/agent/stats/service_connect_linux_test.go index 5de58362794..1a78a00e741 100644 --- a/agent/stats/service_connect_linux_test.go +++ b/agent/stats/service_connect_linux_test.go @@ -94,7 +94,7 @@ func TestRetrieveServiceConnectMetrics(t *testing.T) { { stats: `# TYPE MetricFamily3 histogram MetricFamily3{DimensionX="value1", DimensionY="value2", Direction="egress", le="0.5"} 1 - MetricFamily3{DimensionX="value1", DimensionY="value2", Direction="egress", le="1"} 2 + MetricFamily3{DimensionX="value1", DimensionY="value2", Direction="egress", le="1"} 1 MetricFamily3{DimensionX="value1", DimensionY="value2", Direction="egress", le="5"} 3 `, expectedStats: []*ecstcs.GeneralMetricsWrapper{ @@ -110,14 +110,22 @@ func TestRetrieveServiceConnectMetrics(t *testing.T) { }}, GeneralMetrics: []*ecstcs.GeneralMetric{ { - MetricCounts: []*int64{aws.Int64(1), aws.Int64(1), aws.Int64(1)}, + MetricCounts: []*int64{aws.Int64(1), aws.Int64(2)}, MetricName: aws.String("MetricFamily3"), - MetricValues: []*float64{aws.Float64(0.5), aws.Float64(1), aws.Float64(5)}, + MetricValues: []*float64{aws.Float64(0.5), aws.Float64(5)}, }, }, }, }, }, + { + stats: `# TYPE MetricFamily3 histogram + MetricFamily3{DimensionX="value1", DimensionY="value2", Direction="egress", le="0.5"} 0 + MetricFamily3{DimensionX="value1", DimensionY="value2", Direction="egress", le="1"} 0 + MetricFamily3{DimensionX="value1", DimensionY="value2", Direction="egress", le="5"} 0 + `, + expectedStats: []*ecstcs.GeneralMetricsWrapper{}, + }, } for _, test := range tests {