tencentcloud_cls_sdk_go

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2022 License: Apache-2.0 Imports: 26 Imported by: 0

README

Tencent CLS Log SDK

"tencent cloud cls log sdk" 是专门为cls量身打造日志上传SDK。 一切只为满足您的需求~

USAGE

go get github.com/tencentcloud/tencentcloud-cls-sdk-go

为什么要使用CLS Log SDK

  • 异步发送:发送日志立即返回,无须等待,支持传入callback function。
  • 优雅关闭:通过调用close方法,producer会将所有其缓存的数据进行发送,防止日志丢失。
  • 感知每一条日志的成功状态: 用户可以自定义CallBack方法的实现,监控每一条日志的状态
  • 使用简单: 通过简单配置,就可以实现复杂的日志上传聚合、失败重试等逻辑
  • 失败重试: 429、500 等服务端错误,都会进行重试
  • 高性能: 得益于go语言的高并发能力

CLS Host

https://cloud.tencent.com/document/product/614/18940

Demo

func main() {
    producerConfig := GetDefaultAsyncProducerClientConfig()
	producerConfig.Endpoint = "ap-guangzhou.cls.tencentcs.com"
	producerConfig.AccessKeyID = ""
	producerConfig.AccessKeySecret = ""
	topicId := ""
	producerInstance, err := NewAsyncProducerClient(producerConfig)
	if err != nil {
		t.Error(err)
	}

	producerInstance.Start()
	var m sync.WaitGroup
	callBack := &Callback{}
	for i := 0; i < 10; i++ {
		m.Add(1)
		go func() {
			defer m.Done()
			for i := 0; i < 1000; i++ {
				log := NewCLSLog(time.Now().Unix(), map[string]string{"content": "hello world| I'm from Beijing", "content2": fmt.Sprintf("%v", i)})
				err = producerInstance.SendLog(topicId, log, callBack)
				if err != nil {
					t.Error(err)
				}
			}
		}()
	}
	m.Wait()
	producerInstance.Close(60000)
}

type Callback struct {
}

func (callback *Callback) Success(result *Result) {
	attemptList := result.GetReservedAttempts()
	for _, attempt := range attemptList {
		fmt.Printf("%+v \n", attempt)
	}
}

func (callback *Callback) Fail(result *Result) {
	fmt.Println(result.IsSuccessful())
	fmt.Println(result.GetErrorCode())
	fmt.Println(result.GetErrorMessage())
	fmt.Println(result.GetReservedAttempts())
	fmt.Println(result.GetRequestId())
	fmt.Println(result.GetTimeStampMs())
}

配置参数详解

参数 类型 描述
TotalSizeLnBytes Int64 实例能缓存的日志大小上限,默认为 100MB。
MaxSendWorkerCount Int64 client能并发的最多"goroutine"的数量,默认为50
MaxBlockSec Int 如果client可用空间不足,调用者在 send 方法上的最大阻塞时间,默认为 60 秒。
如果超过这个时间后所需空间仍无法得到满足,send 方法会抛出TimeoutException。如果将该值设为0,当所需空间无法得到满足时,send 方法会立即抛出 TimeoutException。如果您希望 send 方法一直阻塞直到所需空间得到满足,可将该值设为负数。
MaxBatchSize Int64 当一个Batch中缓存的日志大小大于等于 batchSizeThresholdInBytes 时,该 batch 将被发送,默认为 512 KB,最大可设置成 5MB。
MaxBatchCount Int 当一个Batch中缓存的日志条数大于等于 batchCountThreshold 时,该 batch 将被发送,默认为 4096,最大可设置成 40960。
LingerMs Int64 Batch从创建到可发送的逗留时间,默认为 2 秒,最小可设置成 100 毫秒。
Retries Int 如果某个Batch首次发送失败,能够对其重试的次数,默认为 10 次。
如果 retries 小于等于 0,该 ProducerBatch 首次发送失败后将直接进入失败队列。
MaxReservedAttempts Int 每个Batch每次被尝试发送都对应着一个Attemp,此参数用来控制返回给用户的 attempt 个数,默认只保留最近的 11 次 attempt 信息。
该参数越大能让您追溯更多的信息,但同时也会消耗更多的内存。
BaseRetryBackoffMs Int64 首次重试的退避时间,默认为 100 毫秒。 client采样指数退避算法,第 N 次重试的计划等待时间为 baseRetryBackoffMs * 2^(N-1)。
MaxRetryBackoffMs Int64 重试的最大退避时间,默认为 50 秒。

generate cls log

protoc --gofast_out=. cls.proto

feature

Documentation

Index

Constants

View Source
const BAD_REQUEST = "BadRequest"
View Source
const CompressionLevelDefault = -1000
View Source
const INTERNAL_SERVER_ERROR = "InternalError"
View Source
const MISSING_HOST = "MissingHost"
View Source
const MISS_ACCESS_KEY_ID = "MissAccessKeyId"
View Source
const MISS_ACCESS_SECRET = "MissAccessKeyId"
View Source
const MISS_ACCESS_TOKEN = "MissAccessKeyId"
View Source
const UNKNOWN_ERROR = "UnknownError"
View Source
const WRITE_QUOTA_EXCEED = "WriteQuotaExceed"

Variables

View Source
var (
	ErrInvalidLengthCls        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowCls          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupCls = fmt.Errorf("proto: unexpected end of group")
)

Functions

func GetLocalIP

func GetLocalIP() (string, error)

GetLocalIP get local IP with string format

func GetLogListSize

func GetLogListSize(logList []*Log) (int, error)

func GetLogSizeCalculate

func GetLogSizeCalculate(log *Log) (int, error)

func GetTimeMs

func GetTimeMs(t int64) int64

func NewCLSClient

func NewCLSClient(options *Options) (*CLSClient, *CLSError)

func ZSTDCompress

func ZSTDCompress(params ZstdEncoderParams, dst, src []byte) ([]byte, error)

func ZSTDDecompress

func ZSTDDecompress(params ZstdDecoderParams, dst, src []byte) ([]byte, error)

Types

type Accumulator

type Accumulator struct {
	// contains filtered or unexported fields
}

func NewAccumulator

func NewAccumulator(config *AsyncProducerClientConfig, worker *Worker, threadPool *SendThreadPool, producer *AsyncProducerClient) *Accumulator

NewAccumulator ...

type AsyncProducerClient

type AsyncProducerClient struct {
	Client *CLSClient
	// contains filtered or unexported fields
}

AsyncProducerClient async producer client

func NewAsyncProducerClient

func NewAsyncProducerClient(asyncProducerClientConfig *AsyncProducerClientConfig) (*AsyncProducerClient, error)

NewAsyncProducerClient 初始化Async Producer Client

func (*AsyncProducerClient) Close

func (producer *AsyncProducerClient) Close(timeoutMs int64) error

func (*AsyncProducerClient) SendLog

func (producer *AsyncProducerClient) SendLog(topicId string, log *Log, callback CallBack) error

func (*AsyncProducerClient) SendLogList

func (producer *AsyncProducerClient) SendLogList(topicId string, logList []*Log, callback CallBack) (err error)

func (*AsyncProducerClient) Start

func (producer *AsyncProducerClient) Start()

type AsyncProducerClientConfig

type AsyncProducerClientConfig struct {
	TotalSizeLnBytes    int64
	MaxSendWorkerCount  int64
	MaxBlockSec         int
	MaxBatchSize        int64
	MaxBatchCount       int
	LingerMs            int64
	Retries             int
	MaxReservedAttempts int
	BaseRetryBackoffMs  int64
	MaxRetryBackoffMs   int64
	Endpoint            string
	AccessKeyID         string
	AccessKeySecret     string
	AccessToken         string
	Source              string
	Timeout             int
	IdleConn            int
	CompressType        string
}

AsyncProducerClientConfig Producer Config

func GetDefaultAsyncProducerClientConfig

func GetDefaultAsyncProducerClientConfig() *AsyncProducerClientConfig

GetDefaultAsyncProducerClientConfig 获取默认的Producer Config

type Attempt

type Attempt struct {
	Success      bool
	RequestId    string
	ErrorCode    string
	ErrorMessage string
	TimeStampMs  int64
}

func NewAttempt

func NewAttempt(success bool, requestId, errorCode, errorMessage string, timeStampMs int64) *Attempt

NewAttempt init attempt

type CLSClient

type CLSClient struct {
	// contains filtered or unexported fields
}

func (*CLSClient) ResetSecretToken

func (client *CLSClient) ResetSecretToken(secretID string, secretKEY string, secretToken string) *CLSError

func (*CLSClient) Send

func (client *CLSClient) Send(topicId string, group *LogGroup) *CLSError

Send cls实际发送接口

type CLSError

type CLSError struct {
	HTTPCode  int32  `json:"httpCode"`
	Code      string `json:"errorCode"`
	Message   string `json:"errorMessage"`
	RequestID string `json:"requestID"`
}

Error defines sls error

func NewError

func NewError(httpCode int32, requestID, errorCode string, err error) *CLSError

NewClientError new client error

func (CLSError) Error

func (e CLSError) Error() string

func (CLSError) String

func (e CLSError) String() string

type CallBack

type CallBack interface {
	Success(result *Result)
	Fail(result *Result)
}

type ErrorMessage

type ErrorMessage struct {
	Code    string `json:"errorcode"`
	Message string `json:"errormessage"`
}

type Log

type Log struct {
	Time                 *int64         `protobuf:"varint,1,req,name=time" json:"time,omitempty"`
	Contents             []*Log_Content `protobuf:"bytes,2,rep,name=contents" json:"contents,omitempty"`
	XXX_NoUnkeyedLiteral struct{}       `json:"-"`
	XXX_unrecognized     []byte         `json:"-"`
	XXX_sizecache        int32          `json:"-"`
}

func NewCLSLog

func NewCLSLog(logTime int64, addLogMap map[string]string) *Log

NewCLSLog ...

func (*Log) Descriptor

func (*Log) Descriptor() ([]byte, []int)

func (*Log) GetContents

func (m *Log) GetContents() []*Log_Content

func (*Log) GetTime

func (m *Log) GetTime() int64

func (*Log) Marshal

func (m *Log) Marshal() (dAtA []byte, err error)

func (*Log) MarshalTo

func (m *Log) MarshalTo(dAtA []byte) (int, error)

func (*Log) MarshalToSizedBuffer

func (m *Log) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Log) ProtoMessage

func (*Log) ProtoMessage()

func (*Log) Reset

func (m *Log) Reset()

func (*Log) Size

func (m *Log) Size() (n int)

func (*Log) String

func (m *Log) String() string

func (*Log) Unmarshal

func (m *Log) Unmarshal(dAtA []byte) error

func (*Log) XXX_DiscardUnknown

func (m *Log) XXX_DiscardUnknown()

func (*Log) XXX_Marshal

func (m *Log) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Log) XXX_Merge

func (m *Log) XXX_Merge(src proto.Message)

func (*Log) XXX_Size

func (m *Log) XXX_Size() int

func (*Log) XXX_Unmarshal

func (m *Log) XXX_Unmarshal(b []byte) error

type LogGroup

type LogGroup struct {
	Logs                 []*Log    `protobuf:"bytes,1,rep,name=logs" json:"logs,omitempty"`
	ContextFlow          *string   `protobuf:"bytes,2,opt,name=contextFlow" json:"contextFlow,omitempty"`
	Filename             *string   `protobuf:"bytes,3,opt,name=filename" json:"filename,omitempty"`
	Source               *string   `protobuf:"bytes,4,opt,name=source" json:"source,omitempty"`
	LogTags              []*LogTag `protobuf:"bytes,5,rep,name=logTags" json:"logTags,omitempty"`
	XXX_NoUnkeyedLiteral struct{}  `json:"-"`
	XXX_unrecognized     []byte    `json:"-"`
	XXX_sizecache        int32     `json:"-"`
}

func (*LogGroup) Descriptor

func (*LogGroup) Descriptor() ([]byte, []int)

func (*LogGroup) GetContextFlow

func (m *LogGroup) GetContextFlow() string

func (*LogGroup) GetFilename

func (m *LogGroup) GetFilename() string

func (*LogGroup) GetLogTags

func (m *LogGroup) GetLogTags() []*LogTag

func (*LogGroup) GetLogs

func (m *LogGroup) GetLogs() []*Log

func (*LogGroup) GetSource

func (m *LogGroup) GetSource() string

func (*LogGroup) Marshal

func (m *LogGroup) Marshal() (dAtA []byte, err error)

func (*LogGroup) MarshalTo

func (m *LogGroup) MarshalTo(dAtA []byte) (int, error)

func (*LogGroup) MarshalToSizedBuffer

func (m *LogGroup) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*LogGroup) ProtoMessage

func (*LogGroup) ProtoMessage()

func (*LogGroup) Reset

func (m *LogGroup) Reset()

func (*LogGroup) Size

func (m *LogGroup) Size() (n int)

func (*LogGroup) String

func (m *LogGroup) String() string

func (*LogGroup) Unmarshal

func (m *LogGroup) Unmarshal(dAtA []byte) error

func (*LogGroup) XXX_DiscardUnknown

func (m *LogGroup) XXX_DiscardUnknown()

func (*LogGroup) XXX_Marshal

func (m *LogGroup) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*LogGroup) XXX_Merge

func (m *LogGroup) XXX_Merge(src proto.Message)

func (*LogGroup) XXX_Size

func (m *LogGroup) XXX_Size() int

func (*LogGroup) XXX_Unmarshal

func (m *LogGroup) XXX_Unmarshal(b []byte) error

type LogGroupList

type LogGroupList struct {
	LogGroupList         []*LogGroup `protobuf:"bytes,1,rep,name=logGroupList" json:"logGroupList,omitempty"`
	XXX_NoUnkeyedLiteral struct{}    `json:"-"`
	XXX_unrecognized     []byte      `json:"-"`
	XXX_sizecache        int32       `json:"-"`
}

func (*LogGroupList) Descriptor

func (*LogGroupList) Descriptor() ([]byte, []int)

func (*LogGroupList) GetLogGroupList

func (m *LogGroupList) GetLogGroupList() []*LogGroup

func (*LogGroupList) Marshal

func (m *LogGroupList) Marshal() (dAtA []byte, err error)

func (*LogGroupList) MarshalTo

func (m *LogGroupList) MarshalTo(dAtA []byte) (int, error)

func (*LogGroupList) MarshalToSizedBuffer

func (m *LogGroupList) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*LogGroupList) ProtoMessage

func (*LogGroupList) ProtoMessage()

func (*LogGroupList) Reset

func (m *LogGroupList) Reset()

func (*LogGroupList) Size

func (m *LogGroupList) Size() (n int)

func (*LogGroupList) String

func (m *LogGroupList) String() string

func (*LogGroupList) Unmarshal

func (m *LogGroupList) Unmarshal(dAtA []byte) error

func (*LogGroupList) XXX_DiscardUnknown

func (m *LogGroupList) XXX_DiscardUnknown()

func (*LogGroupList) XXX_Marshal

func (m *LogGroupList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*LogGroupList) XXX_Merge

func (m *LogGroupList) XXX_Merge(src proto.Message)

func (*LogGroupList) XXX_Size

func (m *LogGroupList) XXX_Size() int

func (*LogGroupList) XXX_Unmarshal

func (m *LogGroupList) XXX_Unmarshal(b []byte) error

type LogSendRetryQueue

type LogSendRetryQueue struct {
	// contains filtered or unexported fields
}

LogSendRetryQueue RetryQueue cache ProducerBatch and retry latter

func NewRetryQueue

func NewRetryQueue() *LogSendRetryQueue

NewRetryQueue ...

func (*LogSendRetryQueue) Len

func (retryQueue *LogSendRetryQueue) Len() int

func (*LogSendRetryQueue) Less

func (retryQueue *LogSendRetryQueue) Less(i, j int) bool

func (*LogSendRetryQueue) Pop

func (retryQueue *LogSendRetryQueue) Pop() interface{}

func (*LogSendRetryQueue) Push

func (retryQueue *LogSendRetryQueue) Push(x interface{})

func (*LogSendRetryQueue) Swap

func (retryQueue *LogSendRetryQueue) Swap(i, j int)

type LogTag

type LogTag struct {
	Key                  *string  `protobuf:"bytes,1,req,name=key" json:"key,omitempty"`
	Value                *string  `protobuf:"bytes,2,req,name=value" json:"value,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*LogTag) Descriptor

func (*LogTag) Descriptor() ([]byte, []int)

func (*LogTag) GetKey

func (m *LogTag) GetKey() string

func (*LogTag) GetValue

func (m *LogTag) GetValue() string

func (*LogTag) Marshal

func (m *LogTag) Marshal() (dAtA []byte, err error)

func (*LogTag) MarshalTo

func (m *LogTag) MarshalTo(dAtA []byte) (int, error)

func (*LogTag) MarshalToSizedBuffer

func (m *LogTag) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*LogTag) ProtoMessage

func (*LogTag) ProtoMessage()

func (*LogTag) Reset

func (m *LogTag) Reset()

func (*LogTag) Size

func (m *LogTag) Size() (n int)

func (*LogTag) String

func (m *LogTag) String() string

func (*LogTag) Unmarshal

func (m *LogTag) Unmarshal(dAtA []byte) error

func (*LogTag) XXX_DiscardUnknown

func (m *LogTag) XXX_DiscardUnknown()

func (*LogTag) XXX_Marshal

func (m *LogTag) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*LogTag) XXX_Merge

func (m *LogTag) XXX_Merge(src proto.Message)

func (*LogTag) XXX_Size

func (m *LogTag) XXX_Size() int

func (*LogTag) XXX_Unmarshal

func (m *LogTag) XXX_Unmarshal(b []byte) error

type Log_Content

type Log_Content struct {
	Key                  *string  `protobuf:"bytes,1,req,name=key" json:"key,omitempty"`
	Value                *string  `protobuf:"bytes,2,req,name=value" json:"value,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*Log_Content) Descriptor

func (*Log_Content) Descriptor() ([]byte, []int)

func (*Log_Content) GetKey

func (m *Log_Content) GetKey() string

func (*Log_Content) GetValue

func (m *Log_Content) GetValue() string

func (*Log_Content) Marshal

func (m *Log_Content) Marshal() (dAtA []byte, err error)

func (*Log_Content) MarshalTo

func (m *Log_Content) MarshalTo(dAtA []byte) (int, error)

func (*Log_Content) MarshalToSizedBuffer

func (m *Log_Content) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Log_Content) ProtoMessage

func (*Log_Content) ProtoMessage()

func (*Log_Content) Reset

func (m *Log_Content) Reset()

func (*Log_Content) Size

func (m *Log_Content) Size() (n int)

func (*Log_Content) String

func (m *Log_Content) String() string

func (*Log_Content) Unmarshal

func (m *Log_Content) Unmarshal(dAtA []byte) error

func (*Log_Content) XXX_DiscardUnknown

func (m *Log_Content) XXX_DiscardUnknown()

func (*Log_Content) XXX_Marshal

func (m *Log_Content) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Log_Content) XXX_Merge

func (m *Log_Content) XXX_Merge(src proto.Message)

func (*Log_Content) XXX_Size

func (m *Log_Content) XXX_Size() int

func (*Log_Content) XXX_Unmarshal

func (m *Log_Content) XXX_Unmarshal(b []byte) error

type Options

type Options struct {
	Host         string
	SecretID     string
	SecretKEY    string
	SecretToken  string
	Timeout      int
	IdleConn     int
	CompressType string
}

type ProducerBatch

type ProducerBatch struct {
	// contains filtered or unexported fields
}

func NewProducerBatch

func NewProducerBatch(topicID string, config *AsyncProducerClientConfig, callBackFunc CallBack, logData interface{}) *ProducerBatch

NewProducerBatch 初始化Producer batch

type Result

type Result struct {
	// contains filtered or unexported fields
}

func NewResult

func NewResult() *Result

NewResult init result

func (*Result) GetErrorCode

func (result *Result) GetErrorCode() string

func (*Result) GetErrorMessage

func (result *Result) GetErrorMessage() string

func (*Result) GetRequestId

func (result *Result) GetRequestId() string

func (*Result) GetReservedAttempts

func (result *Result) GetReservedAttempts() []*Attempt

func (*Result) GetTimeStampMs

func (result *Result) GetTimeStampMs() int64

func (*Result) IsSuccessful

func (result *Result) IsSuccessful() bool

type SendThreadPool

type SendThreadPool struct {
	// contains filtered or unexported fields
}

func NewSendThreadPool

func NewSendThreadPool(worker *Worker) *SendThreadPool

type TimerSendLogTask

type TimerSendLogTask struct {
	// contains filtered or unexported fields
}

func NewTimerSendLogTask

func NewTimerSendLogTask(logAccumulator *Accumulator, retryQueue *LogSendRetryQueue, ioWorker *Worker, threadPool *SendThreadPool) *TimerSendLogTask

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

func NewWorker

func NewWorker(clsClient *CLSClient, retryQueue *LogSendRetryQueue, maxSendWorkerCount int64, producer *AsyncProducerClient) *Worker

NewWorker ...

type ZstdDecoderParams

type ZstdDecoderParams struct {
}

type ZstdEncoderParams

type ZstdEncoderParams struct {
	Level int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL