Merge pull request #109 from xiaoziv/reader_code_refactor

reader code refactor
This commit is contained in:
ulricqin 2022-07-19 19:22:52 +08:00 committed by GitHub
commit 2ca1982036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 21 deletions

View File

@ -57,8 +57,8 @@ func (a *Agent) startMetricsAgent() error {
continue continue
} }
reader := NewInputReader(inp) reader := NewInputReader(name, inp)
reader.Start(name) reader.Start()
a.InputReaders[name] = reader a.InputReaders[name] = reader
log.Println("I! input:", name, "started") log.Println("I! input:", name, "started")

View File

@ -19,25 +19,28 @@ const agentHostnameLabelKey = "agent_hostname"
var metricReplacer = strings.NewReplacer("-", "_", ".", "_", " ", "_", "'", "_", "\"", "_") var metricReplacer = strings.NewReplacer("-", "_", ".", "_", " ", "_", "'", "_", "\"", "_")
type InputReader struct { type InputReader struct {
inputName string
input inputs.Input input inputs.Input
quitChan chan struct{} quitChan chan struct {
}
queue chan *types.Sample queue chan *types.Sample
} }
func NewInputReader(in inputs.Input) *InputReader { func NewInputReader(inputName string, in inputs.Input) *InputReader {
return &InputReader{ return &InputReader{
inputName: inputName,
input: in, input: in,
quitChan: make(chan struct{}, 1), quitChan: make(chan struct{}, 1),
queue: make(chan *types.Sample, config.Config.WriterOpt.ChanSize), queue: make(chan *types.Sample, config.Config.WriterOpt.ChanSize),
} }
} }
func (r *InputReader) Start(inputName string) { func (r *InputReader) Start() {
// start consumer goroutines // start consumer goroutines
go r.read(inputName) go r.read()
// start collector instance // start collector instance
go r.startInput(inputName) go r.startInput()
} }
func (r *InputReader) Stop() { func (r *InputReader) Stop() {
@ -45,7 +48,7 @@ func (r *InputReader) Stop() {
r.input.Drop() r.input.Drop()
} }
func (r *InputReader) startInput(inputName string) { func (r *InputReader) startInput() {
interval := config.GetInterval() interval := config.GetInterval()
if r.input.GetInterval() > 0 { if r.input.GetInterval() > 0 {
interval = time.Duration(r.input.GetInterval()) interval = time.Duration(r.input.GetInterval())
@ -62,23 +65,23 @@ func (r *InputReader) startInput(inputName string) {
var start time.Time var start time.Time
if config.Config.DebugMode { if config.Config.DebugMode {
start = time.Now() start = time.Now()
log.Println("D!", inputName, ": before gather once") log.Println("D!", r.inputName, ": before gather once")
} }
r.gatherOnce(inputName) r.gatherOnce()
if config.Config.DebugMode { if config.Config.DebugMode {
ms := time.Since(start).Milliseconds() ms := time.Since(start).Milliseconds()
log.Println("D!", inputName, ": after gather once,", "duration:", ms, "ms") log.Println("D!", r.inputName, ": after gather once,", "duration:", ms, "ms")
} }
} }
} }
} }
func (r *InputReader) gatherOnce(inputName string) { func (r *InputReader) gatherOnce() {
defer func() { defer func() {
if r := recover(); r != nil { if rc := recover(); rc != nil {
log.Println("E!", inputName, ": gather metrics panic:", r, string(runtimex.Stack(3))) log.Println("E!", r.inputName, ": gather metrics panic:", r, string(runtimex.Stack(3)))
} }
}() }()
@ -95,7 +98,7 @@ func (r *InputReader) gatherOnce(inputName string) {
} }
if config.Config.DebugMode { if config.Config.DebugMode {
log.Println("D!", inputName, ": gathered samples size:", size) log.Println("D!", r.inputName, ": gathered samples size:", size)
} }
now := time.Now() now := time.Now()
@ -145,7 +148,7 @@ func (r *InputReader) gatherOnce(inputName string) {
} }
} }
func (r *InputReader) read(inputName string) { func (r *InputReader) read() {
batch := config.Config.WriterOpt.Batch batch := config.Config.WriterOpt.Batch
if batch <= 0 { if batch <= 0 {
batch = 2000 batch = 2000