Skip to content

Commit

Permalink
Merge pull request #253 from crimson-gao/producer-v4-sign
Browse files Browse the repository at this point in the history
Producer v4 sign
  • Loading branch information
crimson-gao authored Feb 4, 2024
2 parents 4da4cf6 + c636d3a commit b3bcdc2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// If you keep using long-lived AccessKeyID and AccessKeySecret,
// use the example code below.
//
// provider := NewStaticCredProvider(accessKeyID, accessKeySecret, securityToken)
// provider := NewStaticCredentailsProvider(accessKeyID, accessKeySecret, securityToken)
// client := CreateNormalInterfaceV2(endpoint, provider)
func CreateNormalInterface(endpoint, accessKeyID, accessKeySecret, securityToken string) ClientInterface {
return &Client{
Expand Down
4 changes: 3 additions & 1 deletion producer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ producer同时提供了简单的使用代码simple:([producer_simple_demo](https
```go langgo l
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = os.Getenv("Endpoint")
provider := sls.NewStaticCredProvider(os.Getenv("AccessKeyID"), os.Getenv("AccessKeySecret"), "")
provider := sls.NewStaticCredentailsProvider(os.Getenv("AccessKeyID"), os.Getenv("AccessKeySecret"), "")
producerConfig.CredentialsProvider = provider
producerInstance:=producer.InitProducer(producerConfig)
ch := make(chan os.Signal)
Expand Down Expand Up @@ -137,6 +137,8 @@ func(callback *Callback)Fail(result *producer.Result){
| NoRetryStatusCodeList | []int | 用户配置的不需要重试的错误码列表,当发送日志失败时返回的错误码在列表中,则不会重试。默认包含400,404两个值。 |
| UpdateStsToken | Func | 函数类型,该函数内去实现自己的获取ststoken 的逻辑,producer 会自动刷新ststoken并放入client 当中。
| StsTokenShutDown | channel| 关闭ststoken 自动刷新的通讯信道,当该信道关闭时,不再自动刷新ststoken值。当producer关闭的时候,该参数不为nil值,则会主动调用close去关闭该信道停止ststoken的自动刷新。 |
| Region | String | 日志服务的区域,当签名版本使用 AuthV4 时必选。 例如cn-hangzhou。 |
| AuthVersion | String | 使用的签名版本,可选枚举值为 AuthV1, AuthV4。AuthV4 签名示例可参考程序 [producer_test.go](producer_test.go)|

## 关于性能

Expand Down
7 changes: 6 additions & 1 deletion producer/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func InitProducer(producerConfig *ProducerConfig) *Producer {
if err != nil {
level.Warn(logger).Log("msg", "Failed to create ststoken client, use default client without ststoken.", "error", err)
}

if producerConfig.Region != "" {
client.SetRegion(producerConfig.Region)
}
if producerConfig.AuthVersion != "" {
client.SetAuthVersion(producerConfig.AuthVersion)
}
if producerConfig.HTTPClient != nil {
client.SetHTTPClient(producerConfig.HTTPClient)
}
Expand Down
2 changes: 2 additions & 0 deletions producer/producer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type ProducerConfig struct {
StsTokenShutDown chan struct{}
AccessKeyID string // Deprecated: use CredentialsProvider instead
AccessKeySecret string // Deprecated: use CredentialsProvider instead
Region string
AuthVersion sls.AuthVersionType
}

func GetDefaultProducerConfig() *ProducerConfig {
Expand Down
32 changes: 32 additions & 0 deletions producer/producer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package producer

import (
"fmt"
"os"
"testing"
"time"

sls "github.com/aliyun/aliyun-log-go-sdk"
)

func TestVsSign(t *testing.T) {
producerConfig := GetDefaultProducerConfig()
producerConfig.Endpoint = os.Getenv("LOG_TEST_ENDPOINT")
provider := sls.NewStaticCredentialsProvider(os.Getenv("LOG_TEST_ACCESS_KEY_ID"), os.Getenv("LOG_TEST_ACCESS_KEY_SECRET"), "")
producerConfig.CredentialsProvider = provider
producerConfig.Region = os.Getenv("LOG_TEST_REGION")
producerConfig.AuthVersion = sls.AuthV4
producerInstance := InitProducer(producerConfig)

producerInstance.Start() // 启动producer实例
for i := 0; i < 100; i++ {
// GenerateLog is producer's function for generating SLS format logs
log := GenerateLog(uint32(time.Now().Unix()), map[string]string{"content": "test", "content2": fmt.Sprintf("%v", i)})
err := producerInstance.SendLog(os.Getenv("LOG_TEST_PROJECT"), os.Getenv("LOG_TEST_LOGSTORE"), "127.0.0.1", "topic", log)
if err != nil {
fmt.Println(err)
}
}
producerInstance.Close(60) // 有限关闭,传递int值,参数值需为正整数,单位为秒
producerInstance.SafeClose() // 安全关闭
}

0 comments on commit b3bcdc2

Please sign in to comment.