it_worker365

   ::  ::  ::  ::  :: 管理

HTTPOutputConfig 统计信息收集

是否收集统计信息,统计输出间隔是多少

if o.config.Stats {
    o.queueStats = NewGorStat("output_http", o.config.StatsMs)
}

统计信息收集

func (o *HTTPOutput) PluginWrite(msg *Message) (n int, err error) {
    if !isRequestPayload(msg.Meta) {
        return len(msg.Data), nil
    }

    select {
    case <-o.stop:
        return 0, ErrorStopped
    case o.queue <- msg:
    }

    if o.config.Stats {
        o.queueStats.Write(len(o.queue))
    }
    if len(o.queue) > 0 {
        // try to start a new worker to serve
        if atomic.LoadInt32(&o.activeWorkers) < int32(o.config.WorkersMax) {
            go o.startWorker()
            atomic.AddInt32(&o.activeWorkers, 1)
        }
    }
    return len(msg.Data) + len(msg.Meta), nil
}

NewGorStat统计类

func NewGorStat(statName string, rateMs int) (s *GorStat) {
    s = new(GorStat)
    s.statName = statName
    s.rateMs = rateMs
    s.latest = 0
    s.mean = 0
    s.max = 0
    s.count = 0

    if Settings.Stats {
        go s.reportStats()
    }
    return
}

写入时做统计

func (s *GorStat) Write(latest int) {
    if Settings.Stats {
        if latest > s.max {
            s.max = latest
        }
        if latest != 0 {
            s.mean = ((s.mean * s.count) + latest) / (s.count + 1)
        }
        s.latest = latest
        s.count = s.count + 1
    }
}

打印输出,简单的sleep

func (s *GorStat) reportStats() {
    Debug(0, "\n", s.statName+":latest,mean,max,count,count/second,gcount")
    for {
        Debug(0, "\n", s)
        s.Reset()
        time.Sleep(time.Duration(s.rateMs) * time.Millisecond)
    }
}
posted on 2021-08-07 23:32  it_worker365  阅读(119)  评论(0编辑  收藏  举报