Manage Eventstores with the SLS Go SDK

Updated at:

An Eventstore is a Simple Log Service (SLS) resource for collecting, storing, and querying event data. Each Eventstore belongs to a project, and a project can contain multiple Eventstores.

This topic provides Go examples for creating, updating, listing, getting, and deleting an Eventstore.

Prerequisites

Before you begin, make sure that you have completed the following steps:

Important

Use a RAM user's AccessKey pair instead of your Alibaba Cloud account's AccessKey pair for API calls and routine O&M. Do not hardcode AccessKey credentials in your project code. If leaked, all resources in your account are at risk.

Usage notes

The examples on this page use the public SLS endpoint for the China (Hangzhou) region: cn-hangzhou.log.aliyuncs.com. To access SLS from another Alibaba Cloud service in the same region, use the internal endpoint instead, such as cn-hangzhou-intranet.log.aliyuncs.com. For endpoint-to-region mappings, see Endpoint.

Set up the client

All examples on this page use the following client setup, which authenticates with AccessKey credentials from environment variables.

package main

import (
	"fmt"
	"os"

	sls "github.com/aliyun/aliyun-log-go-sdk"
)

var (
	// SLS endpoint. Replace with the endpoint for your region.
	endpoint = "cn-hangzhou.log.aliyuncs.com"

	// SLS project name. Replace with your project name.
	project = "ali-test-project"

	// Eventstore name. Replace with your Eventstore name.
	eventStoreName = "test-events"

	// Create a client with AccessKey credentials from environment variables.
	client = sls.CreateNormalInterface(
		endpoint,
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
		"",
	)
)

Create an Eventstore

Call CreateEventStore to create an Eventstore in a project.

func createEventStore() {
	eventStore := &sls.LogStore{
		Name:          eventStoreName,
		TTL:           30,
		ShardCount:    2,
		AutoSplit:     true,
		MaxSplitShard: 64,
	}
	if err := client.CreateEventStore(project, eventStore); err != nil {
		panic(err)
	}
	fmt.Printf("Create Eventstore %s success\n", eventStoreName)
}

Eventstore parameters

Parameter

Type

Description

Name

string

Eventstore name.

TTL

int

Data retention period in days. For example, 30 retains data for 30 days.

ShardCount

int

Number of shards. Shards control read/write throughput.

AutoSplit

bool

Whether to enable automatic shard splitting. Set to true to let SLS split shards when write throughput exceeds a single shard's capacity.

MaxSplitShard

int

Maximum number of shards after automatic splitting. Applies only when AutoSplit is true.

Update an Eventstore

Call UpdateEventStore to modify an Eventstore's configuration, such as the TTL or automatic shard splitting settings.

func updateEventStore() {
	eventStore := &sls.LogStore{
		Name:       eventStoreName,
		TTL:        60,
		ShardCount: 2,
		AutoSplit:  false,
	}
	if err := client.UpdateEventStore(project, eventStore); err != nil {
		panic(err)
	}
	fmt.Printf("Update Eventstore %s success\n", eventStoreName)
}

List Eventstores

Call ListEventStore to retrieve the names of all Eventstores in a project. The method accepts offset and size parameters for pagination.

func listEventStores() {
	eventStores, err := client.ListEventStore(project, 0, 10)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Eventstores: %v\n", eventStores)
}

Get Eventstore details

Call GetEventStore to retrieve the configuration of a specified Eventstore.

func getEventStore() {
	eventStore, err := client.GetEventStore(project, eventStoreName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Get Eventstore %s success\n", eventStore.Name)
}

Delete an Eventstore

Call DeleteEventStore to remove an Eventstore from a project.

func deleteEventStore() {
	if err := client.DeleteEventStore(project, eventStoreName); err != nil {
		panic(err)
	}
	fmt.Printf("Delete Eventstore %s success\n", eventStoreName)
}

Complete example

The following program runs all five operations in sequence: create, update, list, get, and delete.

package main

import (
	"fmt"
	"os"
	"strings"

	sls "github.com/aliyun/aliyun-log-go-sdk"
)

var (
	// SLS endpoint. Replace with the endpoint for your region.
	endpoint = "cn-hangzhou.log.aliyuncs.com"

	// SLS project name. Replace with your project name.
	project = "ali-test-project"

	// Eventstore name. Replace with your Eventstore name.
	eventStoreName = "test-events"

	// Create a client with AccessKey credentials from environment variables.
	client = sls.CreateNormalInterface(
		endpoint,
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
		"",
	)
)

func createEventStore() {
	eventStore := &sls.LogStore{
		Name:          eventStoreName,
		TTL:           30,
		ShardCount:    2,
		AutoSplit:     true,
		MaxSplitShard: 64,
	}
	if err := client.CreateEventStore(project, eventStore); err != nil {
		panic(err)
	}
	fmt.Printf("Create Eventstore %s success\n", eventStoreName)
}

func updateEventStore() {
	eventStore := &sls.LogStore{
		Name:       eventStoreName,
		TTL:        60,
		ShardCount: 2,
		AutoSplit:  false,
	}
	if err := client.UpdateEventStore(project, eventStore); err != nil {
		panic(err)
	}
	fmt.Printf("Update Eventstore %s success\n", eventStoreName)
}

func listEventStores() {
	eventStores, err := client.ListEventStore(project, 0, 10)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Eventstores: %s\n", strings.Join(eventStores, ", "))
}

func getEventStore() {
	eventStore, err := client.GetEventStore(project, eventStoreName)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Get Eventstore %s success\n", eventStore.Name)
}

func deleteEventStore() {
	if err := client.DeleteEventStore(project, eventStoreName); err != nil {
		panic(err)
	}
	fmt.Printf("Delete Eventstore %s success\n", eventStoreName)
}

func main() {
	createEventStore()
	updateEventStore()
	listEventStores()
	getEventStore()
	deleteEventStore()
}

Expected output:

Create Eventstore test-events success
Update Eventstore test-events success
Eventstores: test-events
Get Eventstore test-events success
Delete Eventstore test-events success

References