Skip to content

Commit

Permalink
1. update metric namespace 2. code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrara committed May 17, 2024
1 parent 51d7b92 commit dc0e2a9
Show file tree
Hide file tree
Showing 54 changed files with 142 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,36 @@ A replacements section defines a matching against the dimensions of incoming met
## AWS AppSignals Processor Configuration Example

```yaml
awsappsignals:
awsapplicationsignals:
resolvers: ["eks"]
rules:
- selectors:
- dimension: Operation
match: "POST *"
match: "POST *"
- dimension: RemoteService
match: "*"
action: keep
rule_name: "keep01"
match: "*"
action: keep
rule_name: "keep01"
- selectors:
- dimension: Operation
match: "GET *"
match: "GET *"
- dimension: RemoteService
match: "*"
action: keep
rule_name: "keep02"
match: "*"
action: keep
rule_name: "keep02"
- selectors:
- dimension: Operation
match: "POST *"
action: drop
rule_name: "drop01"
match: "POST *"
action: drop
rule_name: "drop01"
- selectors:
- dimension: Operation
match: "*"
replacements:
- target_dimension: RemoteOperation
value: "This is a test string"
action: replace
rule_name: "replace01"
match: "*"
replacements:
- target_dimension: RemoteOperation
value: "This is a test string"
action: replace
rule_name: "replace01"
```
## Amazon CloudWatch Agent Configuration Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"time"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/rules"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/rules"
)

type Config struct {
Expand Down Expand Up @@ -62,7 +62,9 @@ func (cfg *Config) Validate() error {
if resolver.Name == "" {
return errors.New("name must not be empty for k8s resolver")
}
case PlatformEC2, PlatformGeneric, PlatformECS, PlatformLambda:
case PlatformEC2, PlatformGeneric:
case PlatformECS:
return errors.New("ecs resolver is not supported")
default:
return errors.New("unknown resolver")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const (
PlatformEC2 = "ec2"
// PlatformECS Amazon ECS
PlatformECS = "ecs"
// PlatformLambda Amazon Lambda
PlatformLambda = "lambda"
)

type Resolver struct {
Expand Down Expand Up @@ -44,13 +42,6 @@ func NewEC2Resolver(name string) Resolver {
}
}

func NewECSResolver(name string) Resolver {
return Resolver{
Name: name,
Platform: PlatformECS,
}
}

func NewGenericResolver(name string) Resolver {
return Resolver{
Name: name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package awsappsignals
package awsapplicationsignals

import (
"context"
Expand All @@ -12,7 +12,7 @@ import (
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processorhelper"

appsignalsconfig "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/config"
appsignalsconfig "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/config"
)

const (
Expand Down Expand Up @@ -89,12 +89,12 @@ func createMetricsProcessor(
func createProcessor(
params processor.CreateSettings,
cfg component.Config,
) (*awsappsignalsprocessor, error) {
) (*awsapplicationsignalsprocessor, error) {
pCfg, ok := cfg.(*appsignalsconfig.Config)
if !ok {
return nil, errors.New("could not initialize awsapplicationsignalsprocessor")
}
ap := &awsappsignalsprocessor{logger: params.Logger, config: pCfg}
ap := &awsapplicationsignalsprocessor{logger: params.Logger, config: pCfg}

return ap, nil
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package awsappsignals
package awsapplicationsignals

import (
"path/filepath"
Expand All @@ -12,8 +12,8 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/config"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/rules"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/config"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/rules"
)

var expectedRules = []rules.Rule{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package attributes
const (
// aws attributes
AWSLocalService = "aws.local.service"
AWSLocalEnvironment = "aws.local.environment"
AWSLocalOperation = "aws.local.operation"
AWSRemoteService = "aws.remote.service"
AWSRemoteOperation = "aws.remote.operation"
AWSRemoteEnvironment = "aws.remote.environment"
AWSRemoteTarget = "aws.remote.target"
AWSRemoteResourceIdentifier = "aws.remote.resource.identifier"
AWSRemoteResourceType = "aws.remote.resource.type"
Expand All @@ -18,7 +20,4 @@ const (
ResourceDetectionHostId = "host.id"
ResourceDetectionHostName = "host.name"
ResourceDetectionASG = "ec2.tag.aws:autoscaling:groupName"

AWSLocalEnvironment = "aws.local.environment"
AWSRemoteEnvironment = "aws.remote.environment"
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/common"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/config"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/config"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/common"
awsappsignalsconfig "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/config"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common"
awsapplicationsignalsconfig "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/config"
)

var emptyResourceAttributes = pcommon.NewMap()
var logger, _ = zap.NewDevelopment()

func TestAdmitAndRollup(t *testing.T) {
config := &awsappsignalsconfig.LimiterConfig{
config := &awsapplicationsignalsconfig.LimiterConfig{
Threshold: 2,
Disabled: false,
LogDroppedMetrics: false,
RotationInterval: awsappsignalsconfig.DefaultRotationInterval,
RotationInterval: awsapplicationsignalsconfig.DefaultRotationInterval,
}
config.Validate()

Expand Down Expand Up @@ -59,11 +59,11 @@ func TestAdmitAndRollup(t *testing.T) {
}

func TestAdmitByTopK(t *testing.T) {
config := awsappsignalsconfig.LimiterConfig{
config := awsapplicationsignalsconfig.LimiterConfig{
Threshold: 100,
Disabled: false,
LogDroppedMetrics: false,
RotationInterval: awsappsignalsconfig.DefaultRotationInterval,
RotationInterval: awsapplicationsignalsconfig.DefaultRotationInterval,
}
config.Validate()

Expand All @@ -89,11 +89,11 @@ func TestAdmitByTopK(t *testing.T) {
}

func TestAdmitLowCardinalityAttributes(t *testing.T) {
config := awsappsignalsconfig.LimiterConfig{
config := awsapplicationsignalsconfig.LimiterConfig{
Threshold: 10,
Disabled: false,
LogDroppedMetrics: false,
RotationInterval: awsappsignalsconfig.DefaultRotationInterval,
RotationInterval: awsapplicationsignalsconfig.DefaultRotationInterval,
}
config.Validate()

Expand All @@ -109,11 +109,11 @@ func TestAdmitLowCardinalityAttributes(t *testing.T) {
}

func TestAdmitReservedMetrics(t *testing.T) {
config := awsappsignalsconfig.LimiterConfig{
config := awsapplicationsignalsconfig.LimiterConfig{
Threshold: 10,
Disabled: false,
LogDroppedMetrics: false,
RotationInterval: awsappsignalsconfig.DefaultRotationInterval,
RotationInterval: awsapplicationsignalsconfig.DefaultRotationInterval,
}
config.Validate()

Expand All @@ -140,7 +140,7 @@ func TestAdmitReservedMetrics(t *testing.T) {
func TestClearStaleService(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())

config := awsappsignalsconfig.LimiterConfig{
config := awsapplicationsignalsconfig.LimiterConfig{
Threshold: 10,
Disabled: false,
LogDroppedMetrics: false,
Expand All @@ -166,7 +166,7 @@ func TestClearStaleService(t *testing.T) {
}

func TestInheritanceAfterRotation(t *testing.T) {
config := awsappsignalsconfig.LimiterConfig{
config := awsapplicationsignalsconfig.LimiterConfig{
Threshold: 10,
Disabled: false,
LogDroppedMetrics: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/common"
attr "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/internal/attributes"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common"
attr "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/attributes"
)

type attributesNormalizer struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ import (
semconv1 "go.opentelemetry.io/collector/semconv/v1.17.0"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/common"
appsignalsconfig "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/config"
attr "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/internal/attributes"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/common"
appsignalsconfig "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/config"
attr "github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsapplicationsignals/internal/attributes"
"github.com/aws/amazon-cloudwatch-agent/translator/util/ecsutil"
)

const (
AttributeEnvironmentDefault = "default"
AttributePlatformGeneric = "Generic"
AttributePlatformEC2 = "AWS::EC2"
AttributePlatformEKS = "AWS::EKS"
AttributePlatformLambda = "AWS::Lambda"
AttributePlatformK8S = "K8s"

AttributePlatformGeneric = "Generic"
AttributePlatformEC2 = "AWS::EC2"
AttributePlatformEKS = "AWS::EKS"
AttributePlatformK8S = "K8s"
)

var GenericInheritedAttributes = map[string]string{
Expand Down Expand Up @@ -53,18 +54,19 @@ type attributesResolver struct {

// create a new attributes resolver
func NewAttributesResolver(resolvers []appsignalsconfig.Resolver, logger *zap.Logger) *attributesResolver {
//TODO: Logic for native k8s needs to be implemented
subResolvers := []subResolver{}
for _, resolver := range resolvers {
switch resolver.Platform {
case appsignalsconfig.PlatformEKS, appsignalsconfig.PlatformK8s:
subResolvers = append(subResolvers, getKubernetesResolver(resolver.Platform, resolver.Name, logger), newKubernetesResourceAttributesResolver(resolver.Platform, resolver.Name))
case appsignalsconfig.PlatformEC2:
subResolvers = append(subResolvers, newResourceAttributesResolver(resolver.Platform, AttributePlatformEC2, DefaultInheritedAttributes))
case appsignalsconfig.PlatformECS:
subResolvers = append(subResolvers, newResourceAttributesResolver(resolver.Platform, AttributePlatformGeneric, DefaultInheritedAttributes))
default:
subResolvers = append(subResolvers, newResourceAttributesResolver(resolver.Platform, AttributePlatformGeneric, GenericInheritedAttributes))
if ecsutil.GetECSUtilSingleton().IsECS() {
subResolvers = append(subResolvers, newResourceAttributesResolver(appsignalsconfig.PlatformECS, AttributePlatformGeneric, DefaultInheritedAttributes))
} else {
subResolvers = append(subResolvers, newResourceAttributesResolver(resolver.Platform, AttributePlatformGeneric, GenericInheritedAttributes))
}
}
}
return &attributesResolver{
Expand Down Expand Up @@ -93,16 +95,16 @@ func (r *attributesResolver) Stop(ctx context.Context) error {
}

type resourceAttributesResolver struct {
platformCode string
platformType string
attributeMap map[string]string
defaultEnvPrefix string
platformType string
attributeMap map[string]string
}

func newResourceAttributesResolver(platformCode, platformType string, attributeMap map[string]string) *resourceAttributesResolver {
func newResourceAttributesResolver(defaultEnvPrefix, platformType string, attributeMap map[string]string) *resourceAttributesResolver {
return &resourceAttributesResolver{
platformCode: platformCode,
platformType: platformType,
attributeMap: attributeMap,
defaultEnvPrefix: defaultEnvPrefix,
platformType: platformType,
attributeMap: attributeMap,
}
}
func (h *resourceAttributesResolver) Process(attributes, resourceAttributes pcommon.Map) error {
Expand All @@ -112,19 +114,19 @@ func (h *resourceAttributesResolver) Process(attributes, resourceAttributes pcom
}
}
if _, ok := attributes.Get(common.MetricAttributeEnvironment); !ok {
if h.platformCode == appsignalsconfig.PlatformECS {
if h.defaultEnvPrefix == appsignalsconfig.PlatformECS {
if clusterName, ok := getECSClusterName(resourceAttributes); ok {
attributes.PutStr(common.MetricAttributeEnvironment, GetDefaultEnvironment(h.platformCode, clusterName))
attributes.PutStr(common.MetricAttributeEnvironment, GetDefaultEnvironment(h.defaultEnvPrefix, clusterName))
}
}
if h.platformCode == appsignalsconfig.PlatformEC2 {
if h.defaultEnvPrefix == appsignalsconfig.PlatformEC2 {
if asgAttr, ok := resourceAttributes.Get(attr.ResourceDetectionASG); ok {
attributes.PutStr(common.MetricAttributeEnvironment, GetDefaultEnvironment(h.platformCode, asgAttr.Str()))
attributes.PutStr(common.MetricAttributeEnvironment, GetDefaultEnvironment(h.defaultEnvPrefix, asgAttr.Str()))
}
}
}
if _, ok := attributes.Get(common.MetricAttributeEnvironment); !ok {
attributes.PutStr(common.MetricAttributeEnvironment, GetDefaultEnvironment(h.platformCode, AttributeEnvironmentDefault))
attributes.PutStr(common.MetricAttributeEnvironment, GetDefaultEnvironment(h.defaultEnvPrefix, AttributeEnvironmentDefault))
}
attributes.PutStr(common.AttributePlatformType, h.platformType)

Expand Down
Loading

0 comments on commit dc0e2a9

Please sign in to comment.