Skip to content

Commit

Permalink
NH-76572 Applied community feedback back to our current code (#42)
Browse files Browse the repository at this point in the history
- Applied configgrpc for better configuration support
- Applied ToClientConn for better gRPC client creation
- Removed the usage of depreciated type & layer from apm-proto decoding
- Removed the usage of depreciated function call Subjects in SystemCertPool
- Sync-ed with the current upstream PR (More mock unit tests for gRPC responses)
  • Loading branch information
jerrytfleung authored Aug 22, 2024
1 parent a45d5e7 commit 9f862de
Show file tree
Hide file tree
Showing 16 changed files with 755 additions and 440 deletions.
23 changes: 13 additions & 10 deletions collector/extension/solarwindsapmsettingsextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configgrpc"
)

type Config struct {
Endpoint string `mapstructure:"endpoint"`
Key string `mapstructure:"key"`
Interval time.Duration `mapstructure:"interval"`
ClientConfig configgrpc.ClientConfig `mapstructure:",squash"`
Key string `mapstructure:"key"`
Interval time.Duration `mapstructure:"interval"`
}

const (
Expand All @@ -24,17 +25,19 @@ const (

func createDefaultConfig() component.Config {
return &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Interval: DefaultInterval,
}
}

func (cfg *Config) Validate() error {
// Endpoint
matched, _ := regexp.MatchString(`apm.collector.[a-z]{2,3}-[0-9]{2}.[a-z\-]*.solarwinds.com:443`, cfg.Endpoint)
matched, _ := regexp.MatchString(`apm.collector.[a-z]{2,3}-[0-9]{2}.[a-z\-]*.solarwinds.com:443`, cfg.ClientConfig.Endpoint)
if !matched {
// Replaced by the default
cfg.Endpoint = DefaultEndpoint
cfg.ClientConfig.Endpoint = DefaultEndpoint
}
// Key
keyArr := strings.Split(cfg.Key, ":")
Expand All @@ -58,10 +61,10 @@ func (cfg *Config) Validate() error {
}

func resolveServiceNameBestEffort() string {
if name, ok := os.LookupEnv("OTEL_SERVICE_NAME"); ok && len(name) > 0 {
return name
} else if name, ok := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); ok && len(name) > 0 {
return name
if otelServiceName, otelServiceNameDefined := os.LookupEnv("OTEL_SERVICE_NAME"); otelServiceNameDefined && len(otelServiceName) > 0 {
return otelServiceName
} else if awsLambdaFunctionName, awsLambdaFunctionNameDefined := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); awsLambdaFunctionNameDefined && len(awsLambdaFunctionName) > 0 {
return awsLambdaFunctionName
}
return ""
}
95 changes: 71 additions & 24 deletions collector/extension/solarwindsapmsettingsextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/confmap/confmaptest"

"github.com/open-telemetry/opentelemetry-lambda/collector/extension/solarwindsapmsettingsextension/internal/metadata"
Expand All @@ -28,183 +29,229 @@ func TestLoadConfig(t *testing.T) {
{
id: component.NewIDWithName(metadata.Type, "1"),
expected: &Config{
Endpoint: "apm.collector.na-01.cloud.solarwinds.com:443",
ClientConfig: configgrpc.ClientConfig{
Endpoint: "apm.collector.na-01.cloud.solarwinds.com:443",
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "2"),
expected: &Config{
Endpoint: "apm.collector.na-02.cloud.solarwinds.com:443",
ClientConfig: configgrpc.ClientConfig{
Endpoint: "apm.collector.na-02.cloud.solarwinds.com:443",
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "3"),
expected: &Config{
Endpoint: "apm.collector.eu-01.cloud.solarwinds.com:443",
ClientConfig: configgrpc.ClientConfig{
Endpoint: "apm.collector.eu-01.cloud.solarwinds.com:443",
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "4"),
expected: &Config{
Endpoint: "apm.collector.apj-01.cloud.solarwinds.com:443",
ClientConfig: configgrpc.ClientConfig{
Endpoint: "apm.collector.apj-01.cloud.solarwinds.com:443",
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "5"),
expected: &Config{
Endpoint: "apm.collector.na-01.st-ssp.solarwinds.com:443",
ClientConfig: configgrpc.ClientConfig{
Endpoint: "apm.collector.na-01.st-ssp.solarwinds.com:443",
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "6"),
expected: &Config{
Endpoint: "apm.collector.na-01.dev-ssp.solarwinds.com:443",
ClientConfig: configgrpc.ClientConfig{
Endpoint: "apm.collector.na-01.dev-ssp.solarwinds.com:443",
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "7"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "8"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "9"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "10"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "11"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "12"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "13"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "14"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "15"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "something:name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "16"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "17"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: ":",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "18"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "::",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "19"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: ":name",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "20"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "token:",
Interval: time.Duration(10) * time.Second,
},
},
{
id: component.NewIDWithName(metadata.Type, "21"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "token:name",
Interval: MinimumInterval,
},
},
{
id: component.NewIDWithName(metadata.Type, "22"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "token:name",
Interval: MaximumInterval,
},
},
{
id: component.NewIDWithName(metadata.Type, "23"),
expected: &Config{
Endpoint: DefaultEndpoint,
ClientConfig: configgrpc.ClientConfig{
Endpoint: DefaultEndpoint,
},
Key: "token:name",
Interval: MinimumInterval,
},
Expand All @@ -218,7 +265,7 @@ func TestLoadConfig(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))
require.NoError(t, sub.Unmarshal(cfg))
assert.NoError(t, component.ValidateConfig(cfg))
assert.Equal(t, tt.expected, cfg)
})
Expand Down
Loading

0 comments on commit 9f862de

Please sign in to comment.