Skip to content

Commit

Permalink
Rename customconfiguration to rules. Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lisguo committed Oct 28, 2023
1 parent 325ff61 commit 5d8f7f8
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 81 deletions.
6 changes: 3 additions & 3 deletions plugins/processors/awsappsignals/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package awsappsignals

import (
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/customconfiguration"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/rules"
)

type Config struct {
Resolvers []string `mapstructure:"resolvers"`
Rules []customconfiguration.Rule `mapstructure:"rules"`
Resolvers []string `mapstructure:"resolvers"`
Rules []rules.Rule `mapstructure:"rules"`
}

func (cfg *Config) Validate() error {
Expand Down
12 changes: 6 additions & 6 deletions plugins/processors/awsappsignals/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"

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

func TestLoadConfig(t *testing.T) {
Expand All @@ -27,9 +27,9 @@ func TestLoadConfig(t *testing.T) {
id: component.NewIDWithName("awsappsignals", ""),
expected: &Config{
Resolvers: []string{"eks"},
Rules: []customconfiguration.Rule{
Rules: []rules.Rule{
{
Selectors: []customconfiguration.Selector{
Selectors: []rules.Selector{
{
Dimension: "Operation",
Match: "* /api/visits/*",
Expand All @@ -43,7 +43,7 @@ func TestLoadConfig(t *testing.T) {
RuleName: "keep01",
},
{
Selectors: []customconfiguration.Selector{
Selectors: []rules.Selector{
{
Dimension: "RemoteService",
Match: "UnknownRemoteService",
Expand All @@ -56,7 +56,7 @@ func TestLoadConfig(t *testing.T) {
Action: "drop",
},
{
Selectors: []customconfiguration.Selector{
Selectors: []rules.Selector{
{
Dimension: "Operation",
Match: "* /api/visits/*",
Expand All @@ -66,7 +66,7 @@ func TestLoadConfig(t *testing.T) {
Match: "*",
},
},
Replacements: []customconfiguration.Replacement{
Replacements: []rules.Replacement{
{
TargetDimension: "RemoteOperation",
Value: "ListPetsByCustomer",
Expand Down
52 changes: 4 additions & 48 deletions plugins/processors/awsappsignals/internal/resolver/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const (
// To mitigate this issue, we've introduced a 2-minute deletion delay. This ensures that any
// metric data that arrives within those 2 minutes, containing the old IP, will still get mapped correctly to a service.
deletionDelay = 2 * time.Minute

jitterKubernetesAPISeconds = 10
)

var DefaultHostedInAttributeMap = map[string]string{
Expand Down Expand Up @@ -518,8 +520,8 @@ func getEksResolver(logger *zap.Logger) subResolver {
logger.Fatal("Failed to create eks client", zap.Error(err))
}

// add a time jitter of 10 seconds
jitterSleep(10)
// jitter calls to the kubernetes api
jitterSleep(jitterKubernetesAPISeconds)

sharedInformerFactory := informers.NewSharedInformerFactory(clientset, 0)
podInformer := sharedInformerFactory.Core().V1().Pods().Informer()
Expand Down Expand Up @@ -553,8 +555,6 @@ func getEksResolver(logger *zap.Logger) subResolver {
workloadPodCount: podWatcher.workloadPodCount,
safeStopCh: safeStopCh,
}

go instance.debugPrint()
})

return instance
Expand Down Expand Up @@ -587,50 +587,6 @@ func (e *eksResolver) GetWorkloadAndNamespaceByIP(ip string) (string, string, er
return "", "", errors.New("no EKS workload found for ip: " + ip)
}

func printSyncMap(name string, m *sync.Map, logger *zap.Logger) {
logger.Debug("", zap.String("MapName", name))
m.Range(func(key, value interface{}) bool {
logger.Debug("", zap.Any("key", key), zap.Any("value", value))
return true
})
logger.Debug("DEBUG ====================")
}

func (e *eksResolver) debugPrint() {
// call some logic every 5 minutes for ever
for {
select {
case <-time.After(5 * time.Minute):
e.debug()
case <-e.safeStopCh.ch:
return
}
}
}

func (e *eksResolver) debug() {
e.logger.Debug("start debug print")
// print ipToServiceAndNamespace
printSyncMap("ipToServiceAndNamespace", e.ipToServiceAndNamespace, e.logger)

// print serviceAndNamespaceToSelectors
printSyncMap("serviceAndNamespaceToSelectors", e.serviceAndNamespaceToSelectors, e.logger)

// print ipToPod
printSyncMap("ipToPod", e.ipToPod, e.logger)

// print podToWorkloadAndNamespace
printSyncMap("podToWorkloadAndNamespace", e.podToWorkloadAndNamespace, e.logger)

// print workloadAndNamespaceToLabels
printSyncMap("workloadAndNamespaceToLabels", e.workloadAndNamespaceToLabels, e.logger)

// print serviceToWorkload
e.logger.Debug("workload pod count", zap.Any("workloadPodCount", e.workloadPodCount))
printSyncMap("serviceToWorkload", e.serviceToWorkload, e.logger)
e.logger.Debug("end debug print")
}

func (e *eksResolver) Process(attributes, resourceAttributes pcommon.Map) error {
if value, ok := attributes.Get(attr.AWSRemoteService); ok {
valueStr := value.AsString()
Expand Down
10 changes: 5 additions & 5 deletions plugins/processors/awsappsignals/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/customconfiguration"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/internal/normalizer"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/internal/resolver"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/rules"
)

const (
Expand All @@ -38,7 +38,7 @@ type stopper interface {
type awsappsignalsprocessor struct {
logger *zap.Logger
config *Config
replaceActions *customconfiguration.ReplaceActions
replaceActions *rules.ReplaceActions
allowlistMutators []allowListMutator
metricMutators []attributesMutator
traceMutators []attributesMutator
Expand All @@ -53,13 +53,13 @@ func (ap *awsappsignalsprocessor) Start(_ context.Context, _ component.Host) err
attributesNormalizer := normalizer.NewAttributesNormalizer(ap.logger)
ap.metricMutators = []attributesMutator{attributesResolver, attributesNormalizer}

ap.replaceActions = customconfiguration.NewReplacer(ap.config.Rules)
ap.replaceActions = rules.NewReplacer(ap.config.Rules)
ap.traceMutators = []attributesMutator{attributesResolver, attributesNormalizer, ap.replaceActions}

keeper := customconfiguration.NewKeeper(ap.config.Rules)
keeper := rules.NewKeeper(ap.config.Rules)
ap.allowlistMutators = []allowListMutator{keeper}

dropper := customconfiguration.NewDropper(ap.config.Rules)
dropper := rules.NewDropper(ap.config.Rules)
ap.allowlistMutators = []allowListMutator{dropper}

return nil
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 customconfiguration
package rules

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package customconfiguration
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package rules

import "go.opentelemetry.io/collector/pdata/pcommon"

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 customconfiguration
package rules

import "go.opentelemetry.io/collector/pdata/pcommon"

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 customconfiguration
package rules

import (
"testing"
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 customconfiguration
package rules

import "go.opentelemetry.io/collector/pdata/pcommon"

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 customconfiguration
package rules

import (
"testing"
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 customconfiguration
package rules

import (
"go.opentelemetry.io/collector/pdata/pcommon"
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 customconfiguration
package rules

import (
"testing"
Expand Down
22 changes: 11 additions & 11 deletions translator/translate/otel/processor/awsappsignals/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.opentelemetry.io/collector/processor"

"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/customconfiguration"
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsappsignals/rules"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
)

Expand Down Expand Up @@ -66,11 +66,11 @@ func (t *translator) Translate(conf *confmap.Conf) (component.Config, error) {
}

func (t *translator) translateCustomRules(conf *confmap.Conf, configKey string, cfg *awsappsignals.Config) (component.Config, error) {
var rules []customconfiguration.Rule
var rules []rules.Rule
rulesConfigKey := common.ConfigKey(configKey, common.AppSignalsRules)
if conf.IsSet(rulesConfigKey) {
for _, rule := range conf.Get(rulesConfigKey).([]interface{}) {
ruleConfig := customconfiguration.Rule{}
ruleConfig := rules.Rule{}
ruleMap := rule.(map[string]interface{})
selectors := ruleMap["selectors"].([]interface{})
action := ruleMap["action"].(string)
Expand All @@ -81,11 +81,11 @@ func (t *translator) translateCustomRules(conf *confmap.Conf, configKey string,
}

var err error
ruleConfig.Action, err = customconfiguration.GetAllowListAction(action)
ruleConfig.Action, err = rules.GetAllowListAction(action)
if err != nil {
return nil, err
}
if ruleConfig.Action == customconfiguration.AllowListActionReplace {
if ruleConfig.Action == rules.AllowListActionReplace {
replacements, ok := ruleMap["replacements"]
if !ok {
return nil, errors.New("replace action set, but no replacements defined for service rule")
Expand All @@ -101,10 +101,10 @@ func (t *translator) translateCustomRules(conf *confmap.Conf, configKey string,
return cfg, nil
}

func getServiceSelectors(selectorsList []interface{}) []customconfiguration.Selector {
var selectors []customconfiguration.Selector
func getServiceSelectors(selectorsList []interface{}) []rules.Selector {
var selectors []rules.Selector
for _, selector := range selectorsList {
selectorConfig := customconfiguration.Selector{}
selectorConfig := rules.Selector{}
selectorsMap := selector.(map[string]interface{})

selectorConfig.Dimension = selectorsMap["dimension"].(string)
Expand All @@ -114,10 +114,10 @@ func getServiceSelectors(selectorsList []interface{}) []customconfiguration.Sele
return selectors
}

func getServiceReplacements(replacementsList interface{}) []customconfiguration.Replacement {
var replacements []customconfiguration.Replacement
func getServiceReplacements(replacementsList interface{}) []rules.Replacement {
var replacements []rules.Replacement
for _, replacement := range replacementsList.([]interface{}) {
replacementConfig := customconfiguration.Replacement{}
replacementConfig := rules.Replacement{}
replacementMap := replacement.(map[string]interface{})

replacementConfig.TargetDimension = replacementMap["target_dimension"].(string)
Expand Down

0 comments on commit 5d8f7f8

Please sign in to comment.