Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
musa-asad committed Feb 6, 2025
1 parent add9a98 commit ec9494b
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var attributeEntityToFieldMap = map[string]string{

// GetEntityField returns entity field for provided attribute
func GetEntityField(attribute string, value ...string) string {
if attribute == AttributeEntityK8sClusterName {
if attribute == AttributeEntityK8sClusterName && len(value) == 1 {
switch value[0] {
case AttributeEntityEKSPlatform:
return EksCluster
Expand Down
110 changes: 110 additions & 0 deletions exporter/awsemfexporter/internal/entity/entityattributes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package entity

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetEntityField(t *testing.T) {
tests := []struct {
name string
attribute string
values []string
want string
}{
{
name: "AttributeEntityType from map",
attribute: AttributeEntityType,
values: nil,
want: EntityType,
},
{
name: "AttributeEntityServiceName from map",
attribute: AttributeEntityServiceName,
values: nil,
want: Name,
},
{
name: "AttributeEntityDeploymentEnvironment from map",
attribute: AttributeEntityDeploymentEnvironment,
values: nil,
want: Environment,
},
{
name: "AttributeEntityK8sNamespaceName from map",
attribute: AttributeEntityK8sNamespaceName,
values: nil,
want: K8sNamespace,
},
{
name: "AttributeEntityK8sWorkloadName from map",
attribute: AttributeEntityK8sWorkloadName,
values: nil,
want: K8sWorkload,
},
{
name: "AttributeEntityK8sNodeName from map",
attribute: AttributeEntityK8sNodeName,
values: nil,
want: K8sNode,
},
{
name: "AttributeEntityPlatformType from map",
attribute: AttributeEntityPlatformType,
values: nil,
want: PlatformType,
},
{
name: "AttributeEntityInstanceID from map",
attribute: AttributeEntityInstanceID,
values: nil,
want: InstanceID,
},
{
name: "AttributeEntityServiceNameSource from map",
attribute: AttributeEntityServiceNameSource,
values: nil,
want: AWSServiceNameSource,
},
{
name: "K8sClusterName with EKSPlatform",
attribute: AttributeEntityK8sClusterName,
values: []string{AttributeEntityEKSPlatform},
want: EksCluster,
},
{
name: "K8sClusterName with K8sPlatform",
attribute: AttributeEntityK8sClusterName,
values: []string{AttributeEntityK8sPlatform},
want: K8sCluster,
},
{
name: "K8sClusterName with unknown platform",
attribute: AttributeEntityK8sClusterName,
values: []string{"unknown"},
want: "",
},
{
name: "Unknown attribute",
attribute: "unknown",
values: nil,
want: "",
},
{
name: "K8sClusterName with no values provided",
attribute: AttributeEntityK8sClusterName,
values: nil,
want: "",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := GetEntityField(tc.attribute, tc.values...)
assert.Equalf(t, tc.want, got,
"GetEntityField(%q, %v) = %q; want %q",
tc.attribute, tc.values, got, tc.want)
})
}
}
4 changes: 3 additions & 1 deletion exporter/awsemfexporter/metric_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ func translateGroupedMetricToCWMetric(groupedMetric *groupedMetric, config *Conf
}

if k == entity.AttributeEntityK8sClusterName {
fields[entity.GetEntityField(k, labels[entity.AttributeEntityPlatformType])] = v
if entityField := entity.GetEntityField(k, labels[entity.AttributeEntityPlatformType]); entityField != "" {
fields[entityField] = v
}
continue
}

Expand Down
146 changes: 134 additions & 12 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package awsemfexporter
import (
"errors"
"fmt"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter/internal/entity"
"math"
"reflect"
"sort"
Expand Down Expand Up @@ -1552,22 +1553,22 @@ func TestGroupedMetricToCWMeasurementsWithFilters(t *testing.T) {
MetricNameSelectors: []string{"metric(1|3)"},
},
}, []cWMeasurement{
{
Namespace: namespace,
Dimensions: [][]string{{}},
Metrics: []map[string]string{
{
"Name": "metric1",
"Unit": "Count",
},
{
"Name": "metric3",
"Unit": "Seconds",
},
{
Namespace: namespace,
Dimensions: [][]string{{}},
Metrics: []map[string]string{
{
"Name": "metric1",
"Unit": "Count",
},
{
"Name": "metric3",
"Unit": "Seconds",
},
},
},
},
},
{
"label matchers",
[]*MetricDeclaration{
Expand Down Expand Up @@ -2589,6 +2590,127 @@ func TestTranslateOtToGroupedMetricForInitialDeltaValue(t *testing.T) {
}
}

func TestEntityAttributesToFields(t *testing.T) {
timestamp := int64(1596151098037)
namespace := "TestNamespace"

labels := map[string]string{
"normal_label": "normal_value",
entity.AttributeEntityPlatformType: entity.AttributeEntityEKSPlatform,
entity.AttributeEntityK8sClusterName: "myEksCluster",
entity.AttributeEntityK8sNamespaceName: "myNamespace",
entity.AttributeEntityInstanceID: "i-0123456789",
"com.amazonaws.cloudwatch.entity.internal.unknown_field": "should_not_appear",
}

metrics := map[string]*metricInfo{
"metric1": {value: 1, unit: "Count"},
}

groupedMetric := &groupedMetric{
labels: labels,
metrics: metrics,
metadata: cWMetricMetadata{
groupedMetricMetadata: groupedMetricMetadata{
namespace: namespace,
timestampMs: timestamp,
},
},
}

config := &Config{
DimensionRollupOption: "",
logger: zap.NewNop(),
}

cw := translateGroupedMetricToCWMetric(groupedMetric, config)

require.Len(t, cw.measurements, 1)
dims := cw.measurements[0].Dimensions

assert.Equal(t, [][]string{{"normal_label"}}, dims)

assert.Equal(t, timestamp, cw.timestampMs)
assert.Equal(t, namespace, cw.measurements[0].Namespace)

expectedFields := map[string]any{
"normal_label": "normal_value",
"metric1": 1,
"EKS.Cluster": "myEksCluster",
"K8s.Namespace": "myNamespace",
"PlatformType": entity.AttributeEntityEKSPlatform,
"EC2.InstanceId": "i-0123456789",
}
assert.Equal(t, expectedFields, cw.fields)

expectedMeasurement := []cWMeasurement{{
Namespace: namespace,
Dimensions: [][]string{{"normal_label"}},
Metrics: []map[string]string{{
"Name": "metric1",
"Unit": "Count",
}},
}}
assertCWMeasurementSliceEqual(t, expectedMeasurement, cw.measurements)
}

func TestEntityK8sClusterWithMissingPlatformType(t *testing.T) {
labels := map[string]string{
entity.AttributeEntityK8sClusterName: "myEksCluster",
}

groupedMetric := &groupedMetric{
labels: labels,
metrics: map[string]*metricInfo{
"metric1": {value: 1, unit: "Count"},
},
metadata: cWMetricMetadata{
groupedMetricMetadata: groupedMetricMetadata{
namespace: "NoPlatformType",
timestampMs: 10000,
},
},
}
config := &Config{
logger: zap.NewNop(),
}
cw := translateGroupedMetricToCWMetric(groupedMetric, config)

require.Len(t, cw.measurements, 1)
assert.Empty(t, cw.measurements[0].Dimensions[0], "should have no dimension since no normal labels")

assert.Empty(t, cw.fields["K8s.Cluster"])
assert.Empty(t, cw.fields["EKS.Cluster"])

expectedFields := map[string]any{
"metric1": 1,
}
assert.Equal(t, expectedFields, cw.fields)
}

func TestUnknownEntityAttributeIsDropped(t *testing.T) {
labels := map[string]string{
entity.AWSEntityPrefix + "unknown_field": "some_value",
}

groupedMetric := &groupedMetric{
labels: labels,
metrics: map[string]*metricInfo{
"metric1": {value: 1, unit: "Count"},
},
metadata: cWMetricMetadata{
groupedMetricMetadata: groupedMetricMetadata{
namespace: "UnknownEntityAttribute",
timestampMs: 12345,
},
},
}
cw := translateGroupedMetricToCWMetric(groupedMetric, &Config{})

assert.NotContains(t, cw.fields, "unknown_field")
assert.Equal(t, 1, cw.fields["metric1"])
}

func generateTestMetrics(tm testMetric) pmetric.Metrics {
md := pmetric.NewMetrics()
now := time.Now()
Expand Down

0 comments on commit ec9494b

Please sign in to comment.