sls_output.go 4.8 KB
Newer Older
O
ob-robot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
package sls

import (
	"fmt"
	"time"

	"github.com/aliyun/aliyun-log-go-sdk"
	"github.com/cenkalti/backoff"
	"github.com/golang/protobuf/proto"
	log "github.com/sirupsen/logrus"

	"github.com/oceanbase/obagent/monitor/message"
)

type Config struct {
	AccessKeyID     string        `yaml:"accessKeyId"`
	AccessKeySecret string        `yaml:"accessKeySecret"`
	Endpoint        string        `yaml:"endpoint"`
	RequestTimeout  time.Duration `yaml:"requestTimeout"`
	ProjectName     string        `yaml:"projectName"`
	LogStoreName    string        `yaml:"logStoreName"`
	Topic           string        `yaml:"topic"`
	Source          string        `yaml:"source"`
	FieldMap        FieldMap      `yaml:"fieldMap"`
	Retry           Retry         `yaml:"retry"`
}

type FieldMap struct {
	Name   string            `yaml:"name"`
	Tags   map[string]string `yaml:"tags"`
	Fields map[string]string `yaml:"fields"`
}

type Retry struct {
	InitialInterval time.Duration
	MaxInterval     time.Duration
	MaxElapsedTime  time.Duration
}

type SLSOutput struct {
	config       *Config
	client       sls.ClientInterface
	logStore     *sls.LogStore
	batch        []*message.Message
	done         chan struct{}
	retryBackoff backoff.BackOff
}

var DefaultConfig = Config{
	RequestTimeout: time.Second * 5,
	Retry: Retry{
		MaxInterval:     time.Minute,
		InitialInterval: time.Millisecond * 200,
		MaxElapsedTime:  time.Minute * 5,
	},
}

func NewSLSOutput(config *Config) *SLSOutput {
	sls.RetryOnServerErrorEnabled = false // disable sls internal retry
	client := &sls.Client{
		Endpoint:        config.Endpoint,
		AccessKeyID:     config.AccessKeyID,
		AccessKeySecret: config.AccessKeySecret,
		RequestTimeOut:  config.RequestTimeout,
	}
	retryBackoff := backoff.NewExponentialBackOff()
	retryBackoff.MaxInterval = config.Retry.MaxInterval
	retryBackoff.InitialInterval = config.Retry.InitialInterval
	retryBackoff.MaxElapsedTime = config.Retry.MaxElapsedTime
	retryBackoff.RandomizationFactor = 0.1
	return &SLSOutput{
		config:       config,
		client:       client,
		done:         make(chan struct{}),
		retryBackoff: retryBackoff,
	}
}

func (s *SLSOutput) Start(in <-chan []*message.Message) error {
	store, err := s.client.GetLogStore(s.config.ProjectName, s.config.LogStoreName)
	if err != nil {
		log.Errorf("get logStore failed, err: %s", err)
		return err
	}
	s.logStore = store
	for {
		select {
		case batch := <-in:
			logGroup := s.toLogGroup(batch)
			if len(logGroup.Logs) == 0 {
				continue
			}
			s.writeLogGroup(logGroup)
		case <-s.done:
			log.Info("SLS Output exited")
			return nil
		}
	}
}

func (s *SLSOutput) toLogGroup(batch []*message.Message) *sls.LogGroup {
	logs := make([]*sls.Log, 0, len(batch))
	for _, msg := range batch {
		logs = append(logs, s.toLog(msg))
	}
	return &sls.LogGroup{
		Topic:  proto.String(s.config.Topic),
		Source: proto.String(s.config.Source),
		Logs:   logs,
		//LogTags: s.constantTags,
	}
}

func (s *SLSOutput) toLog(msg *message.Message) *sls.Log {
	content := make([]*sls.LogContent, 0, len(msg.Tags())+len(msg.Fields()))
	fieldMap := s.config.FieldMap
	for _, fieldEntry := range msg.Fields() {
		if fieldName, ok := fieldMap.Fields[fieldEntry.Name]; ok {
			content = append(content, &sls.LogContent{
				Key:   proto.String(fieldName),
				Value: proto.String(fmt.Sprint(fieldEntry.Value)),
			})
		}
	}
	for _, tagEntry := range msg.Tags() {
		if fieldName, ok := fieldMap.Tags[tagEntry.Name]; ok {
			content = append(content, &sls.LogContent{
				Key:   proto.String(fieldName),
				Value: proto.String(fmt.Sprint(tagEntry.Value)),
			})
		}
	}
	if fieldMap.Name != "" {
		content = append(content, &sls.LogContent{
			Key:   proto.String(fieldMap.Name),
			Value: proto.String(msg.GetName()),
		})
	}
	return &sls.Log{
		Time:     proto.Uint32(uint32(msg.GetTime().Unix())),
		Contents: content,
	}
}

func (s *SLSOutput) writeLogGroup(logs *sls.LogGroup) {
	s.retry(func() error {
		err := s.logStore.PutLogs(logs)
		if err != nil {
			log.WithError(err).Warn("write log group failed")
		}
		return err
	})
}

func (s *SLSOutput) retry(fn func() error) {
	_ = backoff.Retry(func() error {
		err := fn()
		if err == nil {
			return nil
		}
		select {
		case <-s.done:
			return backoff.Permanent(err)
		default:
		}
		if canRetry(err) {
			return err
		}
		return backoff.Permanent(err)
	}, s.retryBackoff)
}

func canRetry(err error) bool {
	if err == nil {
		return false
	}
	if slsErr, ok := err.(*sls.Error); ok {
		if slsErr.Code == sls.WRITE_QUOTA_EXCEED {
			return true
		}
		return slsErr.HTTPCode >= 500
	} else if slsErr, ok := err.(*sls.BadResponseError); ok {
		return slsErr.HTTPCode >= 500
	} else {
		return false
	}
}

func (s *SLSOutput) Stop() {
	defer func() {
		err := recover()
		if err != nil {
			log.Errorf("recover from panic: %v", err)
		}
	}()
	err := s.client.Close()
	if err != nil {
		log.Errorf("close SLS Client got error: %v", err)
	}
	close(s.done)
}