Skip to content

Commit

Permalink
support logstore metering mode (#230)
Browse files Browse the repository at this point in the history
* feat: supp logstore metering mode

* test: add ft
  • Loading branch information
crimson-gao authored Sep 11, 2023
1 parent 487e9c7 commit 5bfa9d2
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 3 deletions.
6 changes: 6 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ type ClientInterface interface {
UpdateLogStoreV2(project string, logstore *LogStore) error
// CheckLogstoreExist check logstore exist or not
CheckLogstoreExist(project string, logstore string) (bool, error)
// GetLogStoreMeteringMode get the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest
GetLogStoreMeteringMode(project string, logstore string) (*GetMeteringModeResponse, error)
// GetLogStoreMeteringMode update the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest
//
// Warning: this method may affect your billings, for more details ref: https://www.aliyun.com/price/detail/sls
UpdateLogStoreMeteringMode(project string, logstore string, meteringMode string) error

// #################### MetricStore Operations #####################
// CreateMetricStore creates a new metric store in SLS.
Expand Down
20 changes: 17 additions & 3 deletions client_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ func (c *Client) ListLogStore(project string) ([]string, error) {
}

// ListLogStoreV2 list logstores with params :
// offset: start offset
// size: max return size
// telemetryType : telemetry type filter
//
// offset: start offset
// size: max return size
// telemetryType : telemetry type filter
func (c *Client) ListLogStoreV2(project string, offset, size int, telemetryType string) ([]string, error) {
proj := convert(c, project)
return proj.ListLogStoreV2(offset, size, telemetryType)
Expand Down Expand Up @@ -69,6 +70,19 @@ func (c *Client) UpdateLogStoreV2(project string, logstore *LogStore) (err error
return proj.UpdateLogStoreV2(logstore)
}

// GetLogStoreMeteringMode get the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest
func (c *Client) GetLogStoreMeteringMode(project string, logstore string) (*GetMeteringModeResponse, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetMeteringMode()
}

// GetLogStoreMeteringMode update the metering mode of logstore, eg. ChargeByFunction / ChargeByDataIngest
// Warning: this method may affect your billings, for more details ref: https://www.aliyun.com/price/detail/sls
func (c *Client) UpdateLogStoreMeteringMode(project string, logstore string, meteringMode string) error {
ls := convertLogstore(c, project, logstore)
return ls.UpdateMeteringMode(meteringMode)
}

// ListMachineGroup returns machine group name list and the total number of machine groups.
// The offset starts from 0 and the size is the max number of machine groups could be returned.
func (c *Client) ListMachineGroup(project string, offset, size int) (m []string, total int, err error) {
Expand Down
19 changes: 19 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,22 @@ func (s *ClientTestSuite) TestClientCommonHeader() {
s.Require().NoError(err)
fmt.Println(len(stores))
}

func (s *ClientTestSuite) TestMeteringMode() {

res, err := s.client.GetLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName)
s.Require().NoError(err)
s.Require().Equal(CHARGE_BY_FUNCTION, res.MeteringMode)
// change to data ingest
err = s.client.UpdateLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName, CHARGE_BY_DATA_INGEST)
s.Require().NoError(err)
res, err = s.client.GetLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName)
s.Require().NoError(err)
s.Require().Equal(CHARGE_BY_DATA_INGEST, res.MeteringMode)
// change back
err = s.client.UpdateLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName, CHARGE_BY_FUNCTION)
s.Require().NoError(err)
res, err = s.client.GetLogStoreMeteringMode(s.env.ProjectName, s.env.LogstoreName)
s.Require().NoError(err)
s.Require().Equal(CHARGE_BY_FUNCTION, res.MeteringMode)
}
1 change: 1 addition & 0 deletions error_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ const CONFIG_NOT_EXIST = "ConfigNotExist"
const CONFIG_ALREADY_EXIST = "ConfigAlreadyExist"
const BAD_REQUEST = "BadRequest"
const INVALID_PARAMETER = "InvalidParameter"
const NOT_SUPPORTED = "NotSupported"
50 changes: 50 additions & 0 deletions log_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,3 +1076,53 @@ func (s *LogStore) CheckIndexExist() (bool, error) {

return true, nil
}

func (s *LogStore) GetMeteringMode() (*GetMeteringModeResponse, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/logstores/%s/meteringmode", s.Name)
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, NewBadResponseError("", r.Header, r.StatusCode)
}
res := GetMeteringModeResponse{}
err = json.Unmarshal(data, &res)
if err != nil {
return nil, NewBadResponseError(string(data), r.Header, r.StatusCode)

}
return &res, nil

}

func (s *LogStore) UpdateMeteringMode(meteringMode string) error {

body := map[string]string{
"meteringMode": meteringMode,
}
uri := fmt.Sprintf("/logstores/%s/meteringmode", s.Name)
requestBody, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("cant marshal body:%w", err)
}

h := map[string]string{
"Content-Type": "application/json",
"x-log-bodyrawsize": strconv.Itoa(len(requestBody)),
}

r, err := request(s.project, "PUT", uri, h, requestBody)

if err != nil {
r.Body.Close()
return err
}
return nil
}
9 changes: 9 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,12 @@ func CreateDefaultIndex() *Index {
},
}
}

type GetMeteringModeResponse struct {
MeteringMode string `json:"meteringMode"`
}

const (
CHARGE_BY_FUNCTION = "ChargeByFunction"
CHARGE_BY_DATA_INGEST = "ChargeByDataIngest"
)
20 changes: 20 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,26 @@ func (c *TokenAutoUpdateClient) ListMachineGroup(project string, offset, size in
return
}

func (c *TokenAutoUpdateClient) GetLogStoreMeteringMode(project string, logstore string) (res *GetMeteringModeResponse, err error) {
for i := 0; i < c.maxTryTimes; i++ {
res, err = c.logClient.GetLogStoreMeteringMode(project, logstore)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) UpdateLogStoreMeteringMode(project string, logstore string, meteringMode string) (err error) {
for i := 0; i < c.maxTryTimes; i++ {
err = c.logClient.UpdateLogStoreMeteringMode(project, logstore, meteringMode)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) ListMachines(project, machineGroupName string) (ms []*Machine, total int, err error) {
for i := 0; i < c.maxTryTimes; i++ {
ms, total, err = c.logClient.ListMachines(project, machineGroupName)
Expand Down

0 comments on commit 5bfa9d2

Please sign in to comment.