Skip to content

Commit

Permalink
[tencentcloudexporter]move the logic of configuration check to the Va…
Browse files Browse the repository at this point in the history
…lidate function (#6211)
  • Loading branch information
wgliang authored Nov 9, 2021
1 parent ae3a556 commit 111e068
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
12 changes: 12 additions & 0 deletions exporter/tencentcloudlogserviceexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package tencentcloudlogserviceexporter

import (
"errors"

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

Expand All @@ -34,3 +36,13 @@ type Config struct {
// TencentCloud access key secret
SecretKey string `mapstructure:"secret_key"`
}

var _ config.Exporter = (*Config)(nil)

// Validate checks if the exporter configuration is valid
func (cfg *Config) Validate() error {
if cfg == nil || cfg.Region == "" || cfg.LogSet == "" || cfg.Topic == "" {
return errors.New("missing tencentcloudlogservice params: Region, LogSet, Topic")
}
return nil
}
6 changes: 3 additions & 3 deletions exporter/tencentcloudlogserviceexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLoadConfig(t *testing.T) {
factories.Exporters[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.Error(t, err)
require.NotNil(t, cfg)

e0 := cfg.Exporters[config.NewComponentID(typeStr)]
Expand All @@ -59,8 +59,8 @@ func TestLoadConfig(t *testing.T) {
params := componenttest.NewNopExporterCreateSettings()

le, err := factory.CreateLogsExporter(context.Background(), params, e0)
require.Error(t, err)
require.Nil(t, le)
require.NoError(t, err)
require.NotNil(t, le)

le, err = factory.CreateLogsExporter(context.Background(), params, e1)
require.NoError(t, err)
Expand Down
6 changes: 1 addition & 5 deletions exporter/tencentcloudlogserviceexporter/logs_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ import (

// newLogsExporter return a new LogService logs exporter.
func newLogsExporter(set component.ExporterCreateSettings, cfg config.Exporter) (component.LogsExporter, error) {

l := &logServiceLogsSender{
logger: set.Logger,
}

var err error
if l.client, err = newLogServiceClient(cfg.(*Config), set.Logger); err != nil {
return nil, err
}
l.client = newLogServiceClient(cfg.(*Config), set.Logger)

return exporterhelper.NewLogsExporter(
cfg,
Expand Down
4 changes: 2 additions & 2 deletions exporter/tencentcloudlogserviceexporter/logs_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ func TestNewLogsExporter(t *testing.T) {

func TestNewFailsWithEmptyLogsExporterName(t *testing.T) {
got, err := newLogsExporter(componenttest.NewNopExporterCreateSettings(), &Config{})
assert.Error(t, err)
require.Nil(t, got)
assert.NoError(t, err)
require.NotNil(t, got)
}
20 changes: 3 additions & 17 deletions exporter/tencentcloudlogserviceexporter/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package tencentcloudlogserviceexporter

import (
"errors"
"os"

"github.com/pierrec/lz4"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
tchttp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http"
Expand All @@ -43,19 +40,8 @@ type logServiceClientImpl struct {
}

// newLogServiceClient Create Log Service client
func newLogServiceClient(config *Config, logger *zap.Logger) (logServiceClient, error) {
if config == nil || config.Region == "" || config.LogSet == "" || config.Topic == "" {
return nil, errors.New("missing logservice params: Region, LogSet, Topic")
}

var credential *common.Credential
if config.SecretID == "" || config.SecretKey == "" {
credential = common.NewCredential(
os.Getenv("TENCENTCLOUD_SECRET_ID"),
os.Getenv("TENCENTCLOUD_SECRET_KEY"))
} else {
credential = common.NewCredential(config.SecretID, config.SecretKey)
}
func newLogServiceClient(config *Config, logger *zap.Logger) logServiceClient {
credential := common.NewCredential(config.SecretID, config.SecretKey)

c := &logServiceClientImpl{
clientInstance: common.NewCommonClient(credential, config.Region, profile.NewClientProfile()),
Expand All @@ -65,7 +51,7 @@ func newLogServiceClient(config *Config, logger *zap.Logger) (logServiceClient,
}

logger.Info("Create LogService client success", zap.String("logset", config.LogSet), zap.String("topic", config.Topic))
return c, nil
return c
}

// sendLogs send message to LogService
Expand Down

0 comments on commit 111e068

Please sign in to comment.