修改producer为公开结构体

master
wujianhua 1 year ago
parent 7e67dda70b
commit 1e610cd927
  1. 12
      sls/producer.go
  2. 2
      sls/producer_test.go

@ -58,13 +58,13 @@ func WithAccessSecret(as string) Option {
type Option func(alc *options) type Option func(alc *options)
type slsProducer struct { type Producer struct {
pr *producer.Producer pr *producer.Producer
opts *options opts *options
} }
// NewSLSProducer 创建一个高性能的sls制造者 // NewSLSProducer 创建一个高性能的sls制造者
func NewSLSProducer(opts ...Option) *slsProducer { func NewSLSProducer(opts ...Option) *Producer {
opt := defaultOptions() opt := defaultOptions()
for _, o := range opts { for _, o := range opts {
@ -78,19 +78,19 @@ func NewSLSProducer(opts ...Option) *slsProducer {
producerInstance := producer.InitProducer(producerConfig) producerInstance := producer.InitProducer(producerConfig)
producerInstance.Start() producerInstance.Start()
return &slsProducer{ return &Producer{
pr: producerInstance, pr: producerInstance,
opts: opt, opts: opt,
} }
} }
// Close 程序结束时,必须调用该方法,不然可能导致部分日志丢失 // Close 程序结束时,必须调用该方法,不然可能导致部分日志丢失
func (s *slsProducer) Close() error { func (s *Producer) Close() error {
return s.pr.Close(5000) return s.pr.Close(5000)
} }
// SendLog 发送普通日志 // SendLog 发送普通日志
func (s *slsProducer) SendLog(topic, source string, log *sls.Log) error { func (s *Producer) SendLog(topic, source string, log *sls.Log) error {
err := s.pr.SendLog(s.opts.project, s.opts.logstore, topic, source, log) err := s.pr.SendLog(s.opts.project, s.opts.logstore, topic, source, log)
if err != nil { if err != nil {
return err return err
@ -99,7 +99,7 @@ func (s *slsProducer) SendLog(topic, source string, log *sls.Log) error {
} }
// SendRequestLog 发送特定的请求日志,使用该方法时,不需要指定project和logstore // SendRequestLog 发送特定的请求日志,使用该方法时,不需要指定project和logstore
func (s *slsProducer) SendRequestLog(env enum.Env, req *Request) error { func (s *Producer) SendRequestLog(env enum.Env, req *Request) error {
log := GenerateLog(req) log := GenerateLog(req)
err := s.pr.SendLog("bcpm-log", getLogStore(env), "request-go", req.RemoteAddr, log) err := s.pr.SendLog("bcpm-log", getLogStore(env), "request-go", req.RemoteAddr, log)

@ -7,7 +7,7 @@ import (
"gitea.drugeyes.vip/pharnexbase/utils/enum" "gitea.drugeyes.vip/pharnexbase/utils/enum"
) )
var pd *slsProducer var pd *Producer
func init() { func init() {
pd = NewSLSProducer( pd = NewSLSProducer(

Loading…
Cancel
Save