bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/stream.go (about)

     1  package collectors
     2  
     3  import (
     4  	"reflect"
     5  	"runtime"
     6  
     7  	"bosun.org/collect"
     8  	"bosun.org/metadata"
     9  	"bosun.org/opentsdb"
    10  	"bosun.org/util"
    11  )
    12  
    13  /* StreamCollector is useful for collectors that do not produces metrics at a
    14     preset interval. Instead it consummes directly from a channel provided by
    15     the collector and forwards it internally. */
    16  
    17  type StreamCollector struct {
    18  	F    func() <-chan *opentsdb.MultiDataPoint
    19  	name string
    20  	init func()
    21  
    22  	TagOverride
    23  }
    24  
    25  func (s *StreamCollector) Init() {
    26  	if s.init != nil {
    27  		s.init()
    28  	}
    29  }
    30  
    31  func (s *StreamCollector) Run(dpchan chan<- *opentsdb.DataPoint, quit <-chan struct{}) {
    32  	inputChan := s.F()
    33  	count := 0
    34  	for {
    35  		select {
    36  		case md := <-inputChan:
    37  			if !collect.DisableDefaultCollectors {
    38  				tags := opentsdb.TagSet{"collector": s.Name(), "os": runtime.GOOS}
    39  				Add(md, "scollector.collector.count", count, tags, metadata.Counter, metadata.Count, "Counter of metrics passed through.")
    40  			}
    41  
    42  			for _, dp := range *md {
    43  				if _, found := dp.Tags["host"]; !found {
    44  					dp.Tags["host"] = util.GetHostManager().GetHostName()
    45  				}
    46  				s.ApplyTagOverrides(dp.Tags)
    47  				dpchan <- dp
    48  				count++
    49  			}
    50  		case <-quit:
    51  			return
    52  		}
    53  	}
    54  }
    55  
    56  func (s *StreamCollector) Enabled() bool {
    57  	return true
    58  }
    59  
    60  func (s *StreamCollector) Name() string {
    61  	if s.name != "" {
    62  		return s.name
    63  	}
    64  	v := runtime.FuncForPC(reflect.ValueOf(s.F).Pointer())
    65  	return v.Name()
    66  
    67  }