Skip to content

Commit

Permalink
Merge pull request #165 from chaolee50/master
Browse files Browse the repository at this point in the history
add interface CreateMetricStore
  • Loading branch information
shabicheng authored Jun 20, 2022
2 parents bb82d75 + 81651ad commit 4385247
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ type ClientInterface interface {
// CheckLogstoreExist check logstore exist or not
CheckLogstoreExist(project string, logstore string) (bool, error)

// #################### MetricStore Operations #####################
// CreateMetricStore creates a new metric store in SLS.
CreateMetricStore(project string, metricStore *LogStore) error
// UpdateMetricStore updates a metric store.
UpdateMetricStore(project string, metricStore *LogStore) error
// DeleteMetricStore deletes a metric store.
DeleteMetricStore(project, name string) error
// GetMetricStore return a metric store.
GetMetricStore(project, name string) (*LogStore, error)

// #################### Logtail Operations #####################
// 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.
Expand Down
55 changes: 55 additions & 0 deletions client_metric_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sls

import "time"

// CreateMetricStore .
func (c *Client) CreateMetricStore(project string, metricStore *LogStore) error {
metricStore.TelemetryType = "Metrics"
err := c.CreateLogStoreV2(project, metricStore)
if err != nil {
return err
}
time.Sleep(time.Second * 3)
subStore := &SubStore{}
subStore.Name = "prom"
subStore.SortedKeyCount = 2
subStore.TimeIndex = 2
subStore.TTL = metricStore.TTL
subStore.Keys = append(subStore.Keys, SubStoreKey{
Name: "__name__",
Type: "text",
}, SubStoreKey{
Name: "__labels__",
Type: "text",
}, SubStoreKey{
Name: "__time_nano__",
Type: "long",
}, SubStoreKey{
Name: "__value__",
Type: "double",
})
if !subStore.IsValid() {
panic("metric store invalid")
}
return c.CreateSubStore(project, metricStore.Name, subStore)
}

// UpdateMetricStore .
func (c *Client) UpdateMetricStore(project string, metricStore *LogStore) error {
metricStore.TelemetryType = "Metrics"
err := c.UpdateLogStoreV2(project, metricStore)
if err != nil {
return err
}
return c.UpdateSubStoreTTL(project, metricStore.Name, metricStore.TTL)
}

// DeleteMetricStore .
func (c *Client) DeleteMetricStore(project, name string) error {
return c.DeleteLogStore(project, name)
}

// GetMetricStore .
func (c *Client) GetMetricStore(project, name string) (*LogStore, error) {
return c.GetLogStore(project, name)
}
88 changes: 88 additions & 0 deletions client_metric_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package sls

import (
"fmt"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"
)

func TestMetricStore(t *testing.T) {
suite.Run(t, new(MetricStoreTestSuite))
}

type MetricStoreTestSuite struct {
suite.Suite
endpoint string
projectName string
metricStoreName string
accessKeyID string
accessKeySecret string
ttl int
shardCnt int
client *Client
}

func (m *MetricStoreTestSuite) SetupSuite() {
m.endpoint = os.Getenv("LOG_TEST_ENDPOINT")
m.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID")
m.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET")
suffix := time.Now().Unix()
m.projectName = fmt.Sprintf("test-metric-store-%d", suffix)
m.metricStoreName = "test"
m.ttl = 30
m.shardCnt = 2
m.client = &Client{
Endpoint: m.endpoint,
AccessKeyID: m.accessKeyID,
AccessKeySecret: m.accessKeySecret,
}
_, err := m.client.CreateProject(m.projectName, "test metric store")
m.Require().Nil(err)
time.Sleep(time.Minute)
}

func (m *MetricStoreTestSuite) TearDownSuite() {
err := m.client.DeleteProject(m.projectName)
m.Require().Nil(err)
}

func (m *MetricStoreTestSuite) TestClient_CreateAndDeleteMetricStore() {
metricStore := &LogStore{
Name: m.metricStoreName,
TTL: m.ttl,
ShardCount: m.shardCnt,
}
ce := m.client.CreateMetricStore(m.projectName, metricStore)
m.Require().Nil(ce)
de := m.client.DeleteMetricStore(m.projectName, m.metricStoreName)
m.Require().Nil(de)
}

func (m *MetricStoreTestSuite) TestClient_UpdateAndGetMetricStore() {
metricStore1 := &LogStore{
Name: m.metricStoreName,
TTL: m.ttl,
ShardCount: m.shardCnt,
}
ce := m.client.CreateMetricStore(m.projectName, metricStore1)
m.Require().Nil(ce)
metricStore, ge := m.client.GetMetricStore(m.projectName, m.metricStoreName)
m.Require().Nil(ge)
m.Require().Equal(m.metricStoreName, metricStore.Name)
m.Require().Equal(m.ttl, metricStore.TTL)
m.Require().Equal(m.shardCnt, metricStore.ShardCount)
m.Require().Equal("Metrics", metricStore.TelemetryType)

metricStore1.TTL = 15
ue := m.client.UpdateMetricStore(m.projectName, metricStore1)
m.Require().Nil(ue)
metricStore2, ge2 := m.client.GetMetricStore(m.projectName, m.metricStoreName)
m.Require().Nil(ge2)
m.Require().Equal(m.metricStoreName, metricStore2.Name)
m.Require().Equal(15, metricStore2.TTL)
m.Require().Equal("Metrics", metricStore.TelemetryType)
de := m.client.DeleteMetricStore(m.projectName, m.metricStoreName)
m.Require().Nil(de)
}
36 changes: 36 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,3 +1549,39 @@ func (c *TokenAutoUpdateClient) DeleteExport(project string, name string) (err e
}
return
}
func (c *TokenAutoUpdateClient) CreateMetricStore(project string, metricStore *LogStore) (err error) {
for i := 0; i < c.maxTryTimes; i++ {
err = c.logClient.CreateMetricStore(project, metricStore)
if !c.processError(err) {
return
}
}
return
}
func (c *TokenAutoUpdateClient) UpdateMetricStore(project string, metricStore *LogStore) (err error) {
for i := 0; i < c.maxTryTimes; i++ {
err = c.logClient.UpdateMetricStore(project, metricStore)
if !c.processError(err) {
return
}
}
return
}
func (c *TokenAutoUpdateClient) DeleteMetricStore(project, name string) (err error) {
for i := 0; i < c.maxTryTimes; i++ {
err = c.logClient.DeleteMetricStore(project, name)
if !c.processError(err) {
return
}
}
return
}
func (c *TokenAutoUpdateClient) GetMetricStore(project, name string) (metricStore *LogStore, err error) {
for i := 0; i < c.maxTryTimes; i++ {
metricStore, err = c.logClient.GetMetricStore(project, name)
if !c.processError(err) {
return
}
}
return
}

0 comments on commit 4385247

Please sign in to comment.