Skip to content

Commit f039ee4

Browse files
authored
Merge pull request #210 from wangkaibo34/feature/eventstore_sdk
Feature/eventstore sdk
2 parents 546f0ed + f478a34 commit f039ee4

File tree

6 files changed

+330
-4
lines changed

6 files changed

+330
-4
lines changed

client_alert.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ type PolicyConfiguration struct {
138138
ActionPolicyId string `json:"actionPolicyId"`
139139
}
140140

141+
type SinkEventStoreConfiguration struct {
142+
Enabled bool `json:"enabled"`
143+
Endpoint string `json:"endpoint"`
144+
Project string `json:"project"`
145+
EventStore string `json:"eventStore"`
146+
RoleArn string `json:"roleArn"`
147+
}
148+
149+
type SinkCmsConfiguration struct {
150+
Enabled bool `json:"enabled"`
151+
}
152+
153+
type SinkAlerthubConfiguration struct {
154+
Enabled bool `json:"enabled"`
155+
}
156+
141157
type Alert struct {
142158
Name string `json:"name"`
143159
DisplayName string `json:"displayName"`
@@ -226,8 +242,11 @@ type AlertConfiguration struct {
226242
JoinConfigurations []*JoinConfiguration `json:"joinConfigurations"`
227243
GroupConfiguration GroupConfiguration `json:"groupConfiguration"`
228244

229-
PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"`
230-
AutoAnnotation bool `json:"autoAnnotation"`
245+
PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"`
246+
AutoAnnotation bool `json:"autoAnnotation"`
247+
SinkEventStore *SinkEventStoreConfiguration `json:"sinkEventStore"`
248+
SinkCms *SinkCmsConfiguration `json:"sinkCms"`
249+
SinkAlerthub *SinkAlerthubConfiguration `json:"sinkAlerthub"`
231250

232251
Tags []string `json:"tags,omitempty"`
233252
}
@@ -563,4 +582,4 @@ func (c *Client) PublishAlertEvent(project string, alertResult []byte) error {
563582
}
564583
r.Body.Close()
565584
return nil
566-
}
585+
}

client_alert_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package sls
22

33
import (
44
"fmt"
5-
"github.com/stretchr/testify/suite"
65
"os"
76
"testing"
87
"time"
8+
9+
"github.com/stretchr/testify/suite"
910
)
1011

1112
func TestAlert(t *testing.T) {
@@ -207,6 +208,19 @@ func (s *AlertTestSuite) createAlert2() error {
207208
RepeatInterval: "5m",
208209
},
209210
AutoAnnotation: true,
211+
SinkEventStore: &SinkEventStoreConfiguration{
212+
Enabled: true,
213+
RoleArn: "acs:ram::${uid}:role/aliyunlogetlrole",
214+
Project: s.projectName,
215+
Endpoint: s.endpoint,
216+
EventStore: "alert-eventstore",
217+
},
218+
SinkAlerthub: &SinkAlerthubConfiguration{
219+
Enabled: true,
220+
},
221+
SinkCms: &SinkCmsConfiguration{
222+
Enabled: true,
223+
},
210224
},
211225
}
212226
return s.client.CreateAlert(s.projectName, alert)

client_event_store.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package sls
2+
3+
const (
4+
EventStoreTelemetryType = "Event"
5+
EventStoreIndex = `{
6+
"max_text_len": 16384,
7+
"ttl": 7,
8+
"log_reduce": false,
9+
"line": {
10+
"caseSensitive": false,
11+
"chn": true,
12+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
13+
},
14+
"keys": {
15+
"specversion": {
16+
"type": "text",
17+
"doc_value": true,
18+
"alias": "",
19+
"caseSensitive": false,
20+
"chn": false,
21+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
22+
},
23+
"id": {
24+
"type": "text",
25+
"doc_value": true,
26+
"alias": "",
27+
"caseSensitive": false,
28+
"chn": false,
29+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
30+
},
31+
"source": {
32+
"type": "text",
33+
"doc_value": true,
34+
"alias": "",
35+
"caseSensitive": false,
36+
"chn": false,
37+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", "\n", "\t", "\r"]
38+
},
39+
"type": {
40+
"type": "text",
41+
"doc_value": true,
42+
"alias": "",
43+
"caseSensitive": false,
44+
"chn": false,
45+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
46+
},
47+
"subject": {
48+
"type": "text",
49+
"doc_value": true,
50+
"alias": "",
51+
"caseSensitive": false,
52+
"chn": false,
53+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
54+
},
55+
"datacontenttype": {
56+
"type": "text",
57+
"doc_value": true,
58+
"alias": "",
59+
"caseSensitive": false,
60+
"chn": false,
61+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
62+
},
63+
"dataschema": {
64+
"type": "text",
65+
"doc_value": true,
66+
"alias": "",
67+
"caseSensitive": false,
68+
"chn": false,
69+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
70+
},
71+
"data": {
72+
"type": "json",
73+
"doc_value": true,
74+
"alias": "",
75+
"caseSensitive": false,
76+
"chn": false,
77+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"],
78+
"index_all": true,
79+
"max_depth": -1,
80+
"json_keys": {}
81+
},
82+
"time": {
83+
"type": "text",
84+
"doc_value": true,
85+
"alias": "",
86+
"caseSensitive": false,
87+
"chn": false,
88+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
89+
},
90+
"title": {
91+
"type": "text",
92+
"doc_value": true,
93+
"alias": "",
94+
"caseSensitive": false,
95+
"chn": false,
96+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
97+
},
98+
"message": {
99+
"type": "text",
100+
"doc_value": true,
101+
"alias": "",
102+
"caseSensitive": false,
103+
"chn": false,
104+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
105+
},
106+
"status": {
107+
"type": "text",
108+
"doc_value": true,
109+
"alias": "",
110+
"caseSensitive": false,
111+
"chn": false,
112+
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
113+
}
114+
}
115+
}`
116+
)
117+
118+
func (c *Client) CreateEventStore(project string, eventStore *LogStore) error {
119+
eventStore.TelemetryType = EventStoreTelemetryType
120+
err := c.CreateLogStoreV2(project, eventStore)
121+
if err != nil {
122+
return err
123+
}
124+
return c.CreateIndexString(project, eventStore.Name, EventStoreIndex)
125+
}
126+
127+
func (c *Client) UpdateEventStore(project string, eventStore *LogStore) error {
128+
eventStore.TelemetryType = EventStoreTelemetryType
129+
return c.UpdateLogStoreV2(project, eventStore)
130+
}
131+
132+
func (c *Client) DeleteEventStore(project, name string) error {
133+
return c.DeleteLogStore(project, name)
134+
}
135+
136+
func (c *Client) GetEventStore(project, name string) (*LogStore, error) {
137+
return c.GetLogStore(project, name)
138+
}
139+
140+
func (c *Client) ListEventStore(project string, offset, size int) ([]string, error) {
141+
return c.ListLogStoreV2(project, offset, size, EventStoreTelemetryType)
142+
}

client_event_store_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package sls
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
"time"
8+
9+
"github.com/stretchr/testify/suite"
10+
)
11+
12+
func TestEventStore(t *testing.T) {
13+
suite.Run(t, new(EventStoreTestSuite))
14+
}
15+
16+
type EventStoreTestSuite struct {
17+
suite.Suite
18+
endpoint string
19+
projectName string
20+
eventStoreName string
21+
accessKeyID string
22+
accessKeySecret string
23+
ttl int
24+
shardCnt int
25+
client *Client
26+
}
27+
28+
func (m *EventStoreTestSuite) SetupSuite() {
29+
m.endpoint = os.Getenv("LOG_TEST_ENDPOINT")
30+
m.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID")
31+
m.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET")
32+
suffix := time.Now().Unix()
33+
m.projectName = fmt.Sprintf("test-event-store-%d", suffix)
34+
m.eventStoreName = "test"
35+
m.ttl = 30
36+
m.shardCnt = 2
37+
m.client = &Client{
38+
Endpoint: m.endpoint,
39+
AccessKeyID: m.accessKeyID,
40+
AccessKeySecret: m.accessKeySecret,
41+
}
42+
_, err := m.client.CreateProject(m.projectName, "test event store")
43+
m.Require().Nil(err)
44+
time.Sleep(time.Minute)
45+
}
46+
47+
func (m *EventStoreTestSuite) TearDownSuite() {
48+
err := m.client.DeleteProject(m.projectName)
49+
m.Require().Nil(err)
50+
}
51+
52+
func (m *EventStoreTestSuite) TestClient_CreateAndDeleteEventStore() {
53+
eventStore := &LogStore{
54+
Name: m.eventStoreName,
55+
TTL: m.ttl,
56+
ShardCount: m.shardCnt,
57+
}
58+
ce := m.client.CreateEventStore(m.projectName, eventStore)
59+
m.Require().Nil(ce)
60+
de := m.client.DeleteEventStore(m.projectName, m.eventStoreName)
61+
m.Require().Nil(de)
62+
}
63+
64+
func (m *EventStoreTestSuite) TestClient_UpdateAndGetEventStore() {
65+
eventStore := &LogStore{
66+
Name: m.eventStoreName,
67+
TTL: m.ttl,
68+
ShardCount: m.shardCnt,
69+
}
70+
ce := m.client.CreateEventStore(m.projectName, eventStore)
71+
m.Require().Nil(ce)
72+
eventStore, ge := m.client.GetEventStore(m.projectName, m.eventStoreName)
73+
m.Require().Nil(ge)
74+
m.Require().Equal(m.eventStoreName, eventStore.Name)
75+
m.Require().Equal(m.ttl, eventStore.TTL)
76+
m.Require().Equal(m.shardCnt, eventStore.ShardCount)
77+
m.Require().Equal(EventStoreTelemetryType, eventStore.TelemetryType)
78+
79+
eventStore.TTL = 15
80+
ue := m.client.UpdateEventStore(m.projectName, eventStore)
81+
m.Require().Nil(ue)
82+
eventStore1, ge1 := m.client.GetEventStore(m.projectName, m.eventStoreName)
83+
m.Require().Nil(ge1)
84+
m.Require().Equal(m.eventStoreName, eventStore1.Name)
85+
m.Require().Equal(15, eventStore1.TTL)
86+
m.Require().Equal(EventStoreTelemetryType, eventStore1.TelemetryType)
87+
de := m.client.DeleteEventStore(m.projectName, m.eventStoreName)
88+
m.Require().Nil(de)
89+
}

client_interface.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ type ClientInterface interface {
108108
// GetMetricStore return a metric store.
109109
GetMetricStore(project, name string) (*LogStore, error)
110110

111+
// #################### EventStore Operations #####################
112+
// CreateEventStore creates a new event store in SLS.
113+
CreateEventStore(project string, eventStore *LogStore) error
114+
// UpdateEventStore updates a event store.
115+
UpdateEventStore(project string, eventStore *LogStore) error
116+
// DeleteEventStore deletes a event store.
117+
DeleteEventStore(project, name string) error
118+
// GetEventStore return a event store.
119+
GetEventStore(project, name string) (*LogStore, error)
120+
// ListEventStore returns all eventStore names of project p.
121+
ListEventStore(project string, offset, size int) ([]string, error)
122+
111123
// #################### Logtail Operations #####################
112124
// ListMachineGroup returns machine group name list and the total number of machine groups.
113125
// The offset starts from 0 and the size is the max number of machine groups could be returned.

token_auto_update_client.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,3 +1776,53 @@ func (c *TokenAutoUpdateClient) PublishAlertEvent(project string, alertResult []
17761776
}
17771777
return err
17781778
}
1779+
1780+
func (c *TokenAutoUpdateClient) CreateEventStore(project string, eventStore *LogStore) (err error) {
1781+
for i := 0; i < c.maxTryTimes; i++ {
1782+
err = c.logClient.CreateEventStore(project, eventStore)
1783+
if !c.processError(err) {
1784+
return
1785+
}
1786+
}
1787+
return
1788+
}
1789+
1790+
func (c *TokenAutoUpdateClient) UpdateEventStore(project string, eventStore *LogStore) (err error) {
1791+
for i := 0; i < c.maxTryTimes; i++ {
1792+
err = c.logClient.UpdateEventStore(project, eventStore)
1793+
if !c.processError(err) {
1794+
return
1795+
}
1796+
}
1797+
return
1798+
}
1799+
1800+
func (c *TokenAutoUpdateClient) DeleteEventStore(project, name string) (err error) {
1801+
for i := 0; i < c.maxTryTimes; i++ {
1802+
err = c.logClient.DeleteEventStore(project, name)
1803+
if !c.processError(err) {
1804+
return
1805+
}
1806+
}
1807+
return
1808+
}
1809+
1810+
func (c *TokenAutoUpdateClient) GetEventStore(project, name string) (eventStore *LogStore, err error) {
1811+
for i := 0; i < c.maxTryTimes; i++ {
1812+
eventStore, err = c.logClient.GetEventStore(project, name)
1813+
if !c.processError(err) {
1814+
return
1815+
}
1816+
}
1817+
return
1818+
}
1819+
1820+
func (c *TokenAutoUpdateClient) ListEventStore(project string, offset, size int) (eventStores []string, err error) {
1821+
for i := 0; i < c.maxTryTimes; i++ {
1822+
eventStores, err = c.logClient.ListEventStore(project, offset, size)
1823+
if !c.processError(err) {
1824+
return
1825+
}
1826+
}
1827+
return
1828+
}

0 commit comments

Comments
 (0)