diff --git a/client.go b/client.go index b0911079..63d6a674 100644 --- a/client.go +++ b/client.go @@ -116,6 +116,11 @@ func convertLocked(c *Client, projName string) *LogProject { return p } +// SetUserAgent set a custom userAgent +func (c *Client) SetUserAgent(userAgent string) { + c.UserAgent = userAgent +} + // SetHTTPClient set a custom http client, all request will send to sls by this client func (c *Client) SetHTTPClient(client *http.Client) { c.HTTPClient = client diff --git a/client_interface.go b/client_interface.go index a3afb50b..59e858b2 100644 --- a/client_interface.go +++ b/client_interface.go @@ -41,6 +41,8 @@ func CreateTokenAutoUpdateClient(endpoint string, tokenUpdateFunc UpdateTokenFun // ClientInterface for all log's open api type ClientInterface interface { + // SetUserAgent set userAgent for sls client + SetUserAgent(userAgent string) // SetHTTPClient set a custom http client, all request will send to sls by this client SetHTTPClient(client *http.Client) // #################### Client Operations ##################### diff --git a/producer/producer.go b/producer/producer.go index 27cc28c6..60339337 100644 --- a/producer/producer.go +++ b/producer/producer.go @@ -44,6 +44,9 @@ func InitProducer(producerConfig *ProducerConfig) *Producer { if producerConfig.HTTPClient != nil { client.SetHTTPClient(producerConfig.HTTPClient) } + if producerConfig.UserAgent != "" { + client.SetUserAgent(producerConfig.UserAgent) + } finalProducerConfig := validateProducerConfig(producerConfig) retryQueue := initRetryQueue() errorStatusMap := func() map[int]*string { diff --git a/producer/producer_config.go b/producer/producer_config.go index acc4a366..f74651bb 100644 --- a/producer/producer_config.go +++ b/producer/producer_config.go @@ -33,6 +33,7 @@ type ProducerConfig struct { UpdateStsToken func() (accessKeyID, accessKeySecret, securityToken string, expireTime time.Time, err error) StsTokenShutDown chan struct{} HTTPClient *http.Client + UserAgent string } func GetDefaultProducerConfig() *ProducerConfig { diff --git a/token_auto_update_client.go b/token_auto_update_client.go index ad93469f..e954eb4a 100644 --- a/token_auto_update_client.go +++ b/token_auto_update_client.go @@ -137,6 +137,10 @@ func (c *TokenAutoUpdateClient) processError(err error) (retry bool) { } +func (c *TokenAutoUpdateClient) SetUserAgent(userAgent string) { + c.logClient.SetUserAgent(userAgent) +} + // SetHTTPClient set a custom http client, all request will send to sls by this client func (c *TokenAutoUpdateClient) SetHTTPClient(client *http.Client) { c.logClient.SetHTTPClient(client)