Skip to content

Commit

Permalink
Enhance the unit conversion in emf exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrara committed Oct 22, 2024
1 parent 30c18b8 commit d0d473e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
32 changes: 26 additions & 6 deletions exporter/awsemfexporter/grouped_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package awsemfexporter // import "github.com/open-telemetry/opentelemetry-collec

import (
"encoding/json"
"errors"
"reflect"
"strings"

"go.opentelemetry.io/collector/pdata/pmetric"
Expand Down Expand Up @@ -77,9 +79,13 @@ func addToGroupedMetric(
}
}

unit, val, err := translateUnit(pmd, descriptor, dp.value)
if err != nil {
config.logger.Warn("failed to translate unit", zap.Error(err))
}
metric := &metricInfo{
value: dp.value,
unit: translateUnit(pmd, descriptor),
value: val,
unit: unit,
}

if dp.timestampMs > 0 {
Expand Down Expand Up @@ -186,14 +192,28 @@ func mapGetHelper(labels map[string]string, key string) string {
return ""
}

func translateUnit(metric pmetric.Metric, descriptor map[string]MetricDescriptor) string {
func translateUnit(metric pmetric.Metric, descriptor map[string]MetricDescriptor, value any) (string, any, error) {
unit := metric.Unit()
if descriptor, exists := descriptor[metric.Name()]; exists {
if unit == "" || descriptor.Overwrite {
return descriptor.Unit
return descriptor.Unit, value, nil
}
}
switch unit {
case "1":
unit = "Count"
case "ns":
// CloudWatch doesn't support Nanoseconds
switch v := value.(type) {
case int:
return "Microseconds", v / 1000, nil
case int64:
return "Microseconds", v / 1000, nil
case float64:
return "Microseconds", v / 1000, nil
default:
return unit, value, errors.New("Failed to convert ns to us with type " + reflect.TypeOf(value).String())
}
case "ms":
unit = "Milliseconds"
case "s":
Expand All @@ -202,8 +222,8 @@ func translateUnit(metric pmetric.Metric, descriptor map[string]MetricDescriptor
unit = "Microseconds"
case "By":
unit = "Bytes"
case "Bi":
case "bit":
unit = "Bits"
}
return unit
return unit, value, nil
}
36 changes: 26 additions & 10 deletions exporter/awsemfexporter/grouped_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,25 +487,41 @@ func TestTranslateUnit(t *testing.T) {
}

translateUnitCases := map[string]string{
"Count": "Count",
"ms": "Milliseconds",
"s": "Seconds",
"us": "Microseconds",
"By": "Bytes",
"Bi": "Bits",
"raw": "raw",
"1": "Count",
"ms": "Milliseconds",
"ns": "Microseconds",
"s": "Seconds",
"us": "Microseconds",
"By": "Bytes",
"bit": "Bits",
}
for input, output := range translateUnitCases {
t.Run(input, func(_ *testing.T) {
metric.SetUnit(input)

v := translateUnit(metric, translator.metricDescriptor)
assert.Equal(t, output, v)
convertedUnit, data, err := translateUnit(metric, translator.metricDescriptor, 1000)
if err != nil {
assert.Error(t, err)
} else {
assert.Equal(t, output, convertedUnit)
if input == "ns" {
assert.Equal(t, 1, data)
}
}
})
}

metric.SetName("forceOverwrite")
v := translateUnit(metric, translator.metricDescriptor)
assert.Equal(t, "Count", v)
convertedUnit, _, _ := translateUnit(metric, translator.metricDescriptor, 1)
assert.Equal(t, "Count", convertedUnit)

metric.SetName("failedConversion")
metric.SetUnit("ns")
convertedUnit, data, err := translateUnit(metric, translator.metricDescriptor, "any")
assert.NotNil(t, err)
assert.Equal(t, "ns", convertedUnit)
assert.Equal(t, "any", data)
}

func generateTestMetricMetadata(namespace string, timestamp int64, logGroup, logStreamName, instrumentationScopeName string, metricType pmetric.MetricType) cWMetricMetadata {
Expand Down

0 comments on commit d0d473e

Please sign in to comment.