Skip to content

Commit

Permalink
Merge pull request #219 from crimson-gao/header
Browse files Browse the repository at this point in the history
supp user-defined headers
  • Loading branch information
qiansheng91 authored Aug 11, 2023
2 parents e672da6 + ef9f5cf commit 0ac9c47
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ type Client struct {
AuthVersion AuthVersionType // v1 or v4 signature,default is v1

accessKeyLock sync.RWMutex
// User defined common headers, do no use x-log- as key prefix.
// When conflict with sdk pre-defined headers, the value will
// be ignored
CommonHeaders map[string]string
}

func convert(c *Client, projName string) *LogProject {
Expand All @@ -121,6 +125,7 @@ func convertLocked(c *Client, projName string) *LogProject {
p.UserAgent = c.UserAgent
p.AuthVersion = c.AuthVersion
p.Region = c.Region
p.CommonHeaders = c.CommonHeaders
if c.HTTPClient != nil {
p.httpClient = c.HTTPClient
}
Expand Down
6 changes: 6 additions & 0 deletions client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func (c *Client) request(project, method, uri string, headers map[string]string,
headers[HTTPHeaderUserAgent] = DefaultLogUserAgent
}

for k, v := range c.CommonHeaders {
if _, ok := headers[k]; !ok {
headers[k] = v
}
}

c.accessKeyLock.RLock()
stsToken := c.SecurityToken
accessKeyID := c.AccessKeyID
Expand Down
90 changes: 90 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package sls

import (
"fmt"
"testing"
"time"

"github.com/Netflix/go-env"
"github.com/stretchr/testify/suite"
)

func TestClientTestSuite(t *testing.T) {
suite.Run(t, new(ClientTestSuite))
}

type TestEnvInfo struct {
Endpoint string `env:"LOG_TEST_ENDPOINT"`
ProjectName string `env:"LOG_TEST_PROJECT"`
LogstoreName string `env:"LOG_TEST_LOGSTORE"`
AccessKeyID string `env:"LOG_TEST_ACCESS_KEY_ID"`
AccessKeySecret string `env:"LOG_TEST_ACCESS_KEY_SECRET"`
}

type ClientTestSuite struct {
suite.Suite
env TestEnvInfo
client *Client
}

func (s *ClientTestSuite) SetupSuite() {
_, err := env.UnmarshalFromEnviron(&s.env)
s.Require().NoError(err)
s.client = &Client{
Endpoint: s.env.Endpoint,
AccessKeyID: s.env.AccessKeyID,
AccessKeySecret: s.env.AccessKeySecret,
UserAgent: DefaultLogUserAgent,
}
}

func (s *ClientTestSuite) TearDownSuite() {

}

func (s *ClientTestSuite) TestClientCommonHeader() {
// test nil common headers
exists, err := s.client.CheckProjectExist(s.env.ProjectName)
s.Require().NoError(err)
s.True(exists)

// test common headers
s.client.CommonHeaders = map[string]string{
"Test-Header": "header",
"131": "ad",
"kk": "gg",
}
logstore, err := s.client.GetLogStore(s.env.ProjectName, s.env.LogstoreName)
s.Require().NoError(err)
s.Equal(logstore.Name, s.env.LogstoreName)

// test conflict common headers
s.client.CommonHeaders = map[string]string{
"HTTPHeaderHost": "wrong host",
"131": "ad",
"kk": "gg",
}
source := "127.0.0.1"
key, value := "a", "b"
n := uint32(time.Now().Unix())
lg := &LogGroup{
Source: &source,
Logs: []*Log{
&Log{
Time: &n,
Contents: []*LogContent{
&LogContent{
Key: &key,
Value: &value,
},
},
},
},
}
err = s.client.PostLogStoreLogs(s.env.ProjectName, s.env.LogstoreName, lg, nil)
s.Require().NoError(err)
// direct request with client
stores, err := s.client.ListSubStore(s.env.ProjectName, s.env.LogstoreName)
s.Require().NoError(err)
fmt.Println(len(stores))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/aliyun/aliyun-log-go-sdk
go 1.13

require (
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d // indirect
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58
github.com/frankban/quicktest v1.10.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d h1:wvStE9wLpws31NiWUx+38wny1msZ/tm+eL5xmm4Y7So=
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d/go.mod h1:9XMFaCeRyW7fC9XJOWQ+NdAv8VLG7ys7l3x4ozEGLUQ=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
Expand Down
5 changes: 5 additions & 0 deletions log_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ type LogProject struct {
baseURL string
retryTimeout time.Duration
httpClient *http.Client

// User defined common headers, do no use x-log- as key prefix.
// When conflict with sdk pre-defined headers, the value will
// be ignored
CommonHeaders map[string]string
}

// NewLogProject creates a new SLS project.
Expand Down
6 changes: 6 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ func realRequest(ctx context.Context, project *LogProject, method, uri string, h
headers[HTTPHeaderUserAgent] = DefaultLogUserAgent
}

for k, v := range project.CommonHeaders {
if _, ok := headers[k]; !ok {
headers[k] = v
}
}

// Access with token
if project.SecurityToken != "" {
headers[HTTPHeaderAcsSecurityToken] = project.SecurityToken
Expand Down

0 comments on commit 0ac9c47

Please sign in to comment.