Skip to content

Commit

Permalink
[GA] Autofill ECS cluster info if it's missing (#1180)
Browse files Browse the repository at this point in the history
Merge as the changes have been reviewed in the previous PR.
  • Loading branch information
bjrara committed May 17, 2024
1 parent 6b65593 commit 3ddb83b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"strings"

"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
deprecatedsemconv "go.opentelemetry.io/collector/semconv/v1.18.0"
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/internal/version"
Expand Down Expand Up @@ -126,28 +127,38 @@ func (n *attributesNormalizer) normalizeTelemetryAttributes(attributes, resource
}

var (
sdkName string
sdkVersion string
sdkName string
sdkVersion string
sdkLang string
)
var (
sdkAutoName string
sdkAutoVersion string
sdkLang string
)
sdkName, sdkVersion, sdkLang = "-", "-", "-"
mode := instrumentationModeManual

// TODO read telemetry.auto.version from telemetry.distro.* from v1.22
resourceAttributes.Range(func(k string, v pcommon.Value) bool {
switch k {
case semconv.AttributeTelemetrySDKName:
sdkName = strings.ReplaceAll(v.Str(), " ", "")
sdkName = removeWhitespaces(v.Str())
case semconv.AttributeTelemetrySDKLanguage:
sdkLang = strings.ReplaceAll(v.Str(), " ", "")
sdkLang = removeWhitespaces(v.Str())
case semconv.AttributeTelemetrySDKVersion:
sdkVersion = strings.ReplaceAll(v.Str(), " ", "")
case semconv.AttributeTelemetryAutoVersion:
sdkAutoVersion = strings.ReplaceAll(v.Str(), " ", "")
sdkVersion = removeWhitespaces(v.Str())
}
switch k {
case semconv.AttributeTelemetryDistroName:
sdkAutoName = removeWhitespaces(v.Str())
case deprecatedsemconv.AttributeTelemetryAutoVersion, semconv.AttributeTelemetryDistroVersion:
sdkAutoVersion = removeWhitespaces(v.Str())
}
return true
})
if sdkAutoName != "" {
sdkName = sdkAutoName
mode = instrumentationModeAuto
}
if sdkAutoVersion != "" {
sdkVersion = sdkAutoVersion
mode = instrumentationModeAuto
Expand Down Expand Up @@ -217,3 +228,7 @@ func truncateStringByLength(val string, length int) string {
}
return val
}

func removeWhitespaces(val string) string {
return strings.ReplaceAll(val, " ", "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
deprecatedsemconv "go.opentelemetry.io/collector/semconv/v1.18.0"
conventions "go.opentelemetry.io/collector/semconv/v1.22.0"
"go.uber.org/zap"

attr "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/attributes"
Expand Down Expand Up @@ -139,7 +140,7 @@ func Test_attributesNormalizer_appendNewAttributes(t *testing.T) {

completeResourceAttributes := pcommon.NewMap()
completeResourceAttributes.PutStr(conventions.AttributeTelemetrySDKName, "opentelemetry")
completeResourceAttributes.PutStr(conventions.AttributeTelemetryAutoVersion, "0.0.1 auto")
completeResourceAttributes.PutStr(deprecatedsemconv.AttributeTelemetryAutoVersion, "0.0.1 auto")
completeResourceAttributes.PutStr(conventions.AttributeTelemetrySDKVersion, "0.0.1 test")
completeResourceAttributes.PutStr(conventions.AttributeTelemetrySDKLanguage, "go")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common"
Expand Down Expand Up @@ -114,7 +114,9 @@ func (h *resourceAttributesResolver) Process(attributes, resourceAttributes pcom
attributes.PutStr(attr.AWSLocalEnvironment, val.Str())
} else {
if h.defaultEnvPrefix == appsignalsconfig.PlatformECS {
if clusterName, ok := getECSClusterName(resourceAttributes); ok {
if clusterName, _ := getECSClusterName(resourceAttributes); clusterName != "" {
attributes.PutStr(attr.AWSLocalEnvironment, getDefaultEnvironment(h.defaultEnvPrefix, clusterName))
} else if clusterName = ecsutil.GetECSUtilSingleton().Cluster; clusterName != "" {
attributes.PutStr(attr.AWSLocalEnvironment, getDefaultEnvironment(h.defaultEnvPrefix, clusterName))
}
} else if h.defaultEnvPrefix == appsignalsconfig.PlatformEC2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

mapset "github.com/deckarep/golang-set/v2"
"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.17.0"
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/informers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
mapset "github.com/deckarep/golang-set/v2"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.17.0"
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down

0 comments on commit 3ddb83b

Please sign in to comment.