diff --git a/exporter/tencentcloudlogserviceexporter/config.go b/exporter/tencentcloudlogserviceexporter/config.go index 6dd2d8f1a8d7..6697ce2ee1fd 100644 --- a/exporter/tencentcloudlogserviceexporter/config.go +++ b/exporter/tencentcloudlogserviceexporter/config.go @@ -15,6 +15,8 @@ package tencentcloudlogserviceexporter import ( + "errors" + "go.opentelemetry.io/collector/config" ) @@ -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 +} diff --git a/exporter/tencentcloudlogserviceexporter/config_test.go b/exporter/tencentcloudlogserviceexporter/config_test.go index 4da9d11bea1b..0f4e756ef608 100644 --- a/exporter/tencentcloudlogserviceexporter/config_test.go +++ b/exporter/tencentcloudlogserviceexporter/config_test.go @@ -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)] @@ -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) diff --git a/exporter/tencentcloudlogserviceexporter/logs_exporter.go b/exporter/tencentcloudlogserviceexporter/logs_exporter.go index 8ede1f0b8e3d..de411d20964e 100644 --- a/exporter/tencentcloudlogserviceexporter/logs_exporter.go +++ b/exporter/tencentcloudlogserviceexporter/logs_exporter.go @@ -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, diff --git a/exporter/tencentcloudlogserviceexporter/logs_exporter_test.go b/exporter/tencentcloudlogserviceexporter/logs_exporter_test.go index b23e44324ca1..1faf33078e99 100644 --- a/exporter/tencentcloudlogserviceexporter/logs_exporter_test.go +++ b/exporter/tencentcloudlogserviceexporter/logs_exporter_test.go @@ -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) } diff --git a/exporter/tencentcloudlogserviceexporter/uploader.go b/exporter/tencentcloudlogserviceexporter/uploader.go index 7d8c9533299f..d472b0484654 100644 --- a/exporter/tencentcloudlogserviceexporter/uploader.go +++ b/exporter/tencentcloudlogserviceexporter/uploader.go @@ -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" @@ -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()), @@ -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