Skip to content

Commit 14bb326

Browse files
committed
add CreateMetricStore
1 parent 286864f commit 14bb326

4 files changed

+69
-23
lines changed

client_interface.go

+10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ type ClientInterface interface {
9494
// CheckLogstoreExist check logstore exist or not
9595
CheckLogstoreExist(project string, logstore string) (bool, error)
9696

97+
// #################### MetricStore Operations #####################
98+
// CreateMetricStore creates a new metric store in SLS.
99+
CreateMetricStore(project string, metricStore LogStore) error
100+
// UpdateMetricStore updates a metric store.
101+
UpdateMetricStore(project string, metricStore LogStore) error
102+
// DeleteMetricStore deletes a metric store.
103+
DeleteMetricStore(project, name string) error
104+
// GetMetricStore return a metric store.
105+
GetMetricStore(project, name string) (*LogStore, error)
106+
97107
// #################### Logtail Operations #####################
98108
// ListMachineGroup returns machine group name list and the total number of machine groups.
99109
// The offset starts from 0 and the size is the max number of machine groups could be returned.

client_metric_store.go

+9-20
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@ package sls
33
import "time"
44

55
// CreateMetricStore .
6-
func (c *Client) CreateMetricStore(project, name string, ttl, shard int) error {
7-
logStore := &LogStore{
8-
Name: name,
9-
TTL: ttl,
10-
ShardCount: shard,
11-
TelemetryType: "Metrics",
12-
AutoSplit: true,
13-
MaxSplitShard: 64,
14-
}
15-
err := c.CreateLogStoreV2(project, logStore)
6+
func (c *Client) CreateMetricStore(project string, metricStore LogStore) error {
7+
metricStore.TelemetryType = "Metrics"
8+
err := c.CreateLogStoreV2(project, &metricStore)
169
if err != nil {
1710
return err
1811
}
@@ -21,7 +14,7 @@ func (c *Client) CreateMetricStore(project, name string, ttl, shard int) error {
2114
subStore.Name = "prom"
2215
subStore.SortedKeyCount = 2
2316
subStore.TimeIndex = 2
24-
subStore.TTL = ttl
17+
subStore.TTL = metricStore.TTL
2518
subStore.Keys = append(subStore.Keys, SubStoreKey{
2619
Name: "__name__",
2720
Type: "text",
@@ -38,21 +31,17 @@ func (c *Client) CreateMetricStore(project, name string, ttl, shard int) error {
3831
if !subStore.IsValid() {
3932
panic("metric store invalid")
4033
}
41-
return c.CreateSubStore(project, name, subStore)
34+
return c.CreateSubStore(project, metricStore.Name, subStore)
4235
}
4336

4437
// UpdateMetricStore .
45-
func (c *Client) UpdateMetricStore(project, name string, ttl int) error {
46-
metricStore := &LogStore{
47-
Name: name,
48-
TelemetryType: "Metrics",
49-
TTL: ttl,
50-
}
51-
err := c.UpdateLogStoreV2(project, metricStore)
38+
func (c *Client) UpdateMetricStore(project string, metricStore LogStore) error {
39+
metricStore.TelemetryType = "Metrics"
40+
err := c.UpdateLogStoreV2(project, &metricStore)
5241
if err != nil {
5342
return err
5443
}
55-
return c.UpdateSubStoreTTL(project, name, ttl)
44+
return c.UpdateSubStoreTTL(project, metricStore.Name, metricStore.TTL)
5645
}
5746

5847
// DeleteMetricStore .

client_metric_store_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,24 @@ func (m *MetricStoreTestSuite) TearDownSuite() {
4949
}
5050

5151
func (m *MetricStoreTestSuite) TestClient_CreateAndDeleteMetricStore() {
52-
ce := m.client.CreateMetricStore(m.projectName, m.metricStoreName, m.ttl, m.shardCnt)
52+
metricStore := LogStore{
53+
Name: m.metricStoreName,
54+
TTL: m.ttl,
55+
ShardCount: m.shardCnt,
56+
}
57+
ce := m.client.CreateMetricStore(m.projectName, metricStore)
5358
m.Require().Nil(ce)
5459
de := m.client.DeleteMetricStore(m.projectName, m.metricStoreName)
5560
m.Require().Nil(de)
5661
}
5762

5863
func (m *MetricStoreTestSuite) TestClient_UpdateAndGetMetricStore() {
59-
ce := m.client.CreateMetricStore(m.projectName, m.metricStoreName, m.ttl, m.shardCnt)
64+
metricStore1 := LogStore{
65+
Name: m.metricStoreName,
66+
TTL: m.ttl,
67+
ShardCount: m.shardCnt,
68+
}
69+
ce := m.client.CreateMetricStore(m.projectName, metricStore1)
6070
m.Require().Nil(ce)
6171
metricStore, ge := m.client.GetMetricStore(m.projectName, m.metricStoreName)
6272
m.Require().Nil(ge)
@@ -65,7 +75,8 @@ func (m *MetricStoreTestSuite) TestClient_UpdateAndGetMetricStore() {
6575
m.Require().Equal(m.shardCnt, metricStore.ShardCount)
6676
m.Require().Equal("Metrics", metricStore.TelemetryType)
6777

68-
ue := m.client.UpdateMetricStore(m.projectName, m.metricStoreName, 15)
78+
metricStore1.TTL = 15
79+
ue := m.client.UpdateMetricStore(m.projectName, metricStore1)
6980
m.Require().Nil(ue)
7081
metricStore2, ge2 := m.client.GetMetricStore(m.projectName, m.metricStoreName)
7182
m.Require().Nil(ge2)

token_auto_update_client.go

+36
Original file line numberDiff line numberDiff line change
@@ -1549,3 +1549,39 @@ func (c *TokenAutoUpdateClient) DeleteExport(project string, name string) (err e
15491549
}
15501550
return
15511551
}
1552+
func (c *TokenAutoUpdateClient) CreateMetricStore(project string, metricStore LogStore) (err error) {
1553+
for i := 0; i < c.maxTryTimes; i++ {
1554+
err = c.logClient.CreateMetricStore(project, metricStore)
1555+
if !c.processError(err) {
1556+
return
1557+
}
1558+
}
1559+
return
1560+
}
1561+
func (c *TokenAutoUpdateClient) UpdateMetricStore(project string, metricStore LogStore) (err error) {
1562+
for i := 0; i < c.maxTryTimes; i++ {
1563+
err = c.logClient.UpdateMetricStore(project, metricStore)
1564+
if !c.processError(err) {
1565+
return
1566+
}
1567+
}
1568+
return
1569+
}
1570+
func (c *TokenAutoUpdateClient) DeleteMetricStore(project, name string) (err error) {
1571+
for i := 0; i < c.maxTryTimes; i++ {
1572+
err = c.logClient.DeleteMetricStore(project, name)
1573+
if !c.processError(err) {
1574+
return
1575+
}
1576+
}
1577+
return
1578+
}
1579+
func (c *TokenAutoUpdateClient) GetMetricStore(project, name string) (metricStore *LogStore, err error) {
1580+
for i := 0; i < c.maxTryTimes; i++ {
1581+
metricStore, err = c.logClient.GetMetricStore(project, name)
1582+
if !c.processError(err) {
1583+
return
1584+
}
1585+
}
1586+
return
1587+
}

0 commit comments

Comments
 (0)