+type CounterVec struct {
+ Vec *prometheus.CounterVec
+ Opts CounterOpts
+ Labels []string
+}
+
+type GaugeVec struct {
+ Vec *prometheus.GaugeVec
+ Opts CounterOpts
+ Labels []string
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type MetricGroupsCacheCounterRegisterer interface {
+ RegisterCounter(CounterOpts) Counter
+}
+
+type MetricGroupsCacheCounterRegistererFunc func(CounterOpts) Counter
+
+func (fn MetricGroupsCacheCounterRegistererFunc) RegisterCounter(copts CounterOpts) Counter {
+ return fn(copts)
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type MetricGroupsCacheGaugeRegisterer interface {
+ RegisterGauge(CounterOpts) Gauge
+}
+
+type MetricGroupsCacheGaugeRegistererFunc func(CounterOpts) Gauge
+
+func (fn MetricGroupsCacheGaugeRegistererFunc) RegisterGauge(copts CounterOpts) Gauge {
+ return fn(copts)
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+
+type MetricGroupsCache struct {
+ sync.RWMutex //This is for map locking
+ counters map[string]Counter
+ gauges map[string]Gauge
+ regcnt MetricGroupsCacheCounterRegisterer
+ reggau MetricGroupsCacheGaugeRegisterer
+}
+
+func (met *MetricGroupsCache) Registerer(regcnt MetricGroupsCacheCounterRegisterer, reggau MetricGroupsCacheGaugeRegisterer) {
+ met.regcnt = regcnt
+ met.reggau = reggau
+}
+
+func (met *MetricGroupsCache) cReg(metric string) Counter {
+ if met.regcnt != nil {
+ cntr := met.regcnt.RegisterCounter(CounterOpts{Name: metric, Help: "Amount of " + metric + "(auto)"})
+ met.counters[metric] = cntr
+ return cntr
+ }
+ return nil
+}
+func (met *MetricGroupsCache) gReg(metric string) Gauge {
+ if met.reggau != nil {
+ gaug := met.reggau.RegisterGauge(CounterOpts{Name: metric, Help: "Amount of " + metric + "(auto)"})
+ met.gauges[metric] = gaug
+ return gaug
+ }
+ return nil
+}
+
+func (met *MetricGroupsCache) CIs(metric string) bool {
+ met.Lock()
+ defer met.Unlock()
+ _, ok := met.counters[metric]
+ return ok
+}
+
+func (met *MetricGroupsCache) CGet(metric string) Counter {
+ met.Lock()
+ defer met.Unlock()
+ cntr, ok := met.counters[metric]
+ if !ok {
+ cntr = met.cReg(metric)
+ }
+ return cntr
+}
+
+func (met *MetricGroupsCache) CInc(metric string) {
+ met.Lock()
+ defer met.Unlock()
+ cntr, ok := met.counters[metric]
+ if !ok {
+ cntr = met.cReg(metric)
+ }
+ cntr.Inc()
+}
+
+func (met *MetricGroupsCache) CAdd(metric string, val float64) {
+ met.Lock()
+ defer met.Unlock()
+ cntr, ok := met.counters[metric]
+ if !ok {
+ cntr = met.cReg(metric)
+ }
+ cntr.Add(val)
+}
+
+func (met *MetricGroupsCache) GIs(metric string) bool {
+ met.Lock()
+ defer met.Unlock()
+ _, ok := met.gauges[metric]
+ return ok
+}
+
+func (met *MetricGroupsCache) GGet(metric string) Gauge {
+ met.Lock()
+ defer met.Unlock()
+ gaug, ok := met.gauges[metric]
+ if !ok {
+ gaug = met.gReg(metric)
+ }
+ return gaug
+}
+
+func (met *MetricGroupsCache) GSet(metric string, val float64) {
+ met.Lock()
+ defer met.Unlock()
+ gaug, ok := met.gauges[metric]
+ if !ok {
+ gaug = met.gReg(metric)
+ }
+ gaug.Set(val)
+}
+
+func (met *MetricGroupsCache) GAdd(metric string, val float64) {
+ met.Lock()
+ defer met.Unlock()
+ gaug, ok := met.gauges[metric]
+ if !ok {
+ gaug = met.gReg(metric)
+ }
+ gaug.Add(val)
+}
+
+func (met *MetricGroupsCache) GInc(metric string) {
+ met.Lock()
+ defer met.Unlock()
+ gaug, ok := met.gauges[metric]
+ if !ok {
+ gaug = met.gReg(metric)
+ }
+ gaug.Inc()
+}
+
+func (met *MetricGroupsCache) GDec(metric string) {
+ met.Lock()
+ defer met.Unlock()
+ gaug, ok := met.gauges[metric]
+ if !ok {
+ gaug = met.gReg(metric)
+ }
+ gaug.Dec()
+}
+
+func (met *MetricGroupsCache) combineCounterGroupsWithPrefix(prefix string, srcs ...map[string]Counter) {
+ for _, src := range srcs {
+ for k, v := range src {
+ met.counters[prefix+k] = v
+ }
+ }
+}
+
+func (met *MetricGroupsCache) CombineCounterGroupsWithPrefix(prefix string, srcs ...map[string]Counter) {
+ met.Lock()
+ defer met.Unlock()
+ met.combineCounterGroupsWithPrefix(prefix, srcs...)
+}
+
+func (met *MetricGroupsCache) CombineCounterGroups(srcs ...map[string]Counter) {
+ met.Lock()
+ defer met.Unlock()
+ met.combineCounterGroupsWithPrefix("", srcs...)
+}
+
+func (met *MetricGroupsCache) combineGaugeGroupsWithPrefix(prefix string, srcs ...map[string]Gauge) {
+ for _, src := range srcs {
+ for k, v := range src {
+ met.gauges[prefix+k] = v
+ }
+ }
+}
+
+func (met *MetricGroupsCache) CombineGaugeGroupsWithPrefix(prefix string, srcs ...map[string]Gauge) {
+ met.Lock()
+ defer met.Unlock()
+ met.combineGaugeGroupsWithPrefix(prefix, srcs...)
+}
+
+func (met *MetricGroupsCache) CombineGaugeGroups(srcs ...map[string]Gauge) {
+ met.Lock()
+ defer met.Unlock()
+ met.combineGaugeGroupsWithPrefix("", srcs...)
+}
+
+func NewMetricGroupsCache() *MetricGroupsCache {
+ entry := &MetricGroupsCache{}
+ entry.counters = make(map[string]Counter)
+ entry.gauges = make(map[string]Gauge)
+ entry.regcnt = nil
+ entry.reggau = nil
+ return entry
+}
+
+func NewMetricGroupsCacheWithRegisterers(regcnt MetricGroupsCacheCounterRegisterer, reggau MetricGroupsCacheGaugeRegisterer) *MetricGroupsCache {
+ entry := NewMetricGroupsCache()
+ entry.regcnt = regcnt
+ entry.reggau = reggau
+ return entry
+}
+
+//-----------------------------------------------------------------------------
+// All counters/gauges registered via Metrics instances:
+// Counter names are build from: namespace, subsystem, metric and possible labels
+//-----------------------------------------------------------------------------
+var globalLock sync.Mutex
+var cache_allcounters map[string]Counter
+var cache_allgauges map[string]Gauge
+var cache_allcountervects map[string]CounterVec
+var cache_allgaugevects map[string]GaugeVec
+
+func init() {
+ cache_allcounters = make(map[string]Counter)
+ cache_allgauges = make(map[string]Gauge)
+ cache_allcountervects = make(map[string]CounterVec)
+ cache_allgaugevects = make(map[string]GaugeVec)
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type Metrics struct {
+ Namespace string
+}
+