Skip to content

Commit

Permalink
Merge pull request #210 from wangkaibo34/feature/eventstore_sdk
Browse files Browse the repository at this point in the history
Feature/eventstore sdk
  • Loading branch information
shabicheng authored Jun 6, 2023
2 parents 546f0ed + f478a34 commit f039ee4
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 4 deletions.
25 changes: 22 additions & 3 deletions client_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ type PolicyConfiguration struct {
ActionPolicyId string `json:"actionPolicyId"`
}

type SinkEventStoreConfiguration struct {
Enabled bool `json:"enabled"`
Endpoint string `json:"endpoint"`
Project string `json:"project"`
EventStore string `json:"eventStore"`
RoleArn string `json:"roleArn"`
}

type SinkCmsConfiguration struct {
Enabled bool `json:"enabled"`
}

type SinkAlerthubConfiguration struct {
Enabled bool `json:"enabled"`
}

type Alert struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Expand Down Expand Up @@ -226,8 +242,11 @@ type AlertConfiguration struct {
JoinConfigurations []*JoinConfiguration `json:"joinConfigurations"`
GroupConfiguration GroupConfiguration `json:"groupConfiguration"`

PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"`
AutoAnnotation bool `json:"autoAnnotation"`
PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"`
AutoAnnotation bool `json:"autoAnnotation"`
SinkEventStore *SinkEventStoreConfiguration `json:"sinkEventStore"`
SinkCms *SinkCmsConfiguration `json:"sinkCms"`
SinkAlerthub *SinkAlerthubConfiguration `json:"sinkAlerthub"`

Tags []string `json:"tags,omitempty"`
}
Expand Down Expand Up @@ -563,4 +582,4 @@ func (c *Client) PublishAlertEvent(project string, alertResult []byte) error {
}
r.Body.Close()
return nil
}
}
16 changes: 15 additions & 1 deletion client_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package sls

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

"github.com/stretchr/testify/suite"
)

func TestAlert(t *testing.T) {
Expand Down Expand Up @@ -207,6 +208,19 @@ func (s *AlertTestSuite) createAlert2() error {
RepeatInterval: "5m",
},
AutoAnnotation: true,
SinkEventStore: &SinkEventStoreConfiguration{
Enabled: true,
RoleArn: "acs:ram::${uid}:role/aliyunlogetlrole",
Project: s.projectName,
Endpoint: s.endpoint,
EventStore: "alert-eventstore",
},
SinkAlerthub: &SinkAlerthubConfiguration{
Enabled: true,
},
SinkCms: &SinkCmsConfiguration{
Enabled: true,
},
},
}
return s.client.CreateAlert(s.projectName, alert)
Expand Down
142 changes: 142 additions & 0 deletions client_event_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package sls

const (
EventStoreTelemetryType = "Event"
EventStoreIndex = `{
"max_text_len": 16384,
"ttl": 7,
"log_reduce": false,
"line": {
"caseSensitive": false,
"chn": true,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"keys": {
"specversion": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"id": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"source": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", "\n", "\t", "\r"]
},
"type": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"subject": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"datacontenttype": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"dataschema": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"data": {
"type": "json",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"],
"index_all": true,
"max_depth": -1,
"json_keys": {}
},
"time": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"title": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"message": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
},
"status": {
"type": "text",
"doc_value": true,
"alias": "",
"caseSensitive": false,
"chn": false,
"token": [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"]
}
}
}`
)

func (c *Client) CreateEventStore(project string, eventStore *LogStore) error {
eventStore.TelemetryType = EventStoreTelemetryType
err := c.CreateLogStoreV2(project, eventStore)
if err != nil {
return err
}
return c.CreateIndexString(project, eventStore.Name, EventStoreIndex)
}

func (c *Client) UpdateEventStore(project string, eventStore *LogStore) error {
eventStore.TelemetryType = EventStoreTelemetryType
return c.UpdateLogStoreV2(project, eventStore)
}

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

func (c *Client) GetEventStore(project, name string) (*LogStore, error) {
return c.GetLogStore(project, name)
}

func (c *Client) ListEventStore(project string, offset, size int) ([]string, error) {
return c.ListLogStoreV2(project, offset, size, EventStoreTelemetryType)
}
89 changes: 89 additions & 0 deletions client_event_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package sls

import (
"fmt"
"os"
"testing"
"time"

"github.com/stretchr/testify/suite"
)

func TestEventStore(t *testing.T) {
suite.Run(t, new(EventStoreTestSuite))
}

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

func (m *EventStoreTestSuite) 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-event-store-%d", suffix)
m.eventStoreName = "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 event store")
m.Require().Nil(err)
time.Sleep(time.Minute)
}

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

func (m *EventStoreTestSuite) TestClient_CreateAndDeleteEventStore() {
eventStore := &LogStore{
Name: m.eventStoreName,
TTL: m.ttl,
ShardCount: m.shardCnt,
}
ce := m.client.CreateEventStore(m.projectName, eventStore)
m.Require().Nil(ce)
de := m.client.DeleteEventStore(m.projectName, m.eventStoreName)
m.Require().Nil(de)
}

func (m *EventStoreTestSuite) TestClient_UpdateAndGetEventStore() {
eventStore := &LogStore{
Name: m.eventStoreName,
TTL: m.ttl,
ShardCount: m.shardCnt,
}
ce := m.client.CreateEventStore(m.projectName, eventStore)
m.Require().Nil(ce)
eventStore, ge := m.client.GetEventStore(m.projectName, m.eventStoreName)
m.Require().Nil(ge)
m.Require().Equal(m.eventStoreName, eventStore.Name)
m.Require().Equal(m.ttl, eventStore.TTL)
m.Require().Equal(m.shardCnt, eventStore.ShardCount)
m.Require().Equal(EventStoreTelemetryType, eventStore.TelemetryType)

eventStore.TTL = 15
ue := m.client.UpdateEventStore(m.projectName, eventStore)
m.Require().Nil(ue)
eventStore1, ge1 := m.client.GetEventStore(m.projectName, m.eventStoreName)
m.Require().Nil(ge1)
m.Require().Equal(m.eventStoreName, eventStore1.Name)
m.Require().Equal(15, eventStore1.TTL)
m.Require().Equal(EventStoreTelemetryType, eventStore1.TelemetryType)
de := m.client.DeleteEventStore(m.projectName, m.eventStoreName)
m.Require().Nil(de)
}
12 changes: 12 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ type ClientInterface interface {
// GetMetricStore return a metric store.
GetMetricStore(project, name string) (*LogStore, error)

// #################### EventStore Operations #####################
// CreateEventStore creates a new event store in SLS.
CreateEventStore(project string, eventStore *LogStore) error
// UpdateEventStore updates a event store.
UpdateEventStore(project string, eventStore *LogStore) error
// DeleteEventStore deletes a event store.
DeleteEventStore(project, name string) error
// GetEventStore return a event store.
GetEventStore(project, name string) (*LogStore, error)
// ListEventStore returns all eventStore names of project p.
ListEventStore(project string, offset, size int) ([]string, 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
50 changes: 50 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1776,3 +1776,53 @@ func (c *TokenAutoUpdateClient) PublishAlertEvent(project string, alertResult []
}
return err
}

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

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

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

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

func (c *TokenAutoUpdateClient) ListEventStore(project string, offset, size int) (eventStores []string, err error) {
for i := 0; i < c.maxTryTimes; i++ {
eventStores, err = c.logClient.ListEventStore(project, offset, size)
if !c.processError(err) {
return
}
}
return
}

0 comments on commit f039ee4

Please sign in to comment.