github.com/theQRL/go-zond@v0.1.1/metrics/influxdb/influxdb.go (about)

     1  package influxdb
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/theQRL/go-zond/metrics"
     7  )
     8  
     9  func readMeter(namespace, name string, i interface{}) (string, map[string]interface{}) {
    10  	switch metric := i.(type) {
    11  	case metrics.Counter:
    12  		measurement := fmt.Sprintf("%s%s.count", namespace, name)
    13  		fields := map[string]interface{}{
    14  			"value": metric.Snapshot().Count(),
    15  		}
    16  		return measurement, fields
    17  	case metrics.CounterFloat64:
    18  		measurement := fmt.Sprintf("%s%s.count", namespace, name)
    19  		fields := map[string]interface{}{
    20  			"value": metric.Snapshot().Count(),
    21  		}
    22  		return measurement, fields
    23  	case metrics.Gauge:
    24  		measurement := fmt.Sprintf("%s%s.gauge", namespace, name)
    25  		fields := map[string]interface{}{
    26  			"value": metric.Snapshot().Value(),
    27  		}
    28  		return measurement, fields
    29  	case metrics.GaugeFloat64:
    30  		measurement := fmt.Sprintf("%s%s.gauge", namespace, name)
    31  		fields := map[string]interface{}{
    32  			"value": metric.Snapshot().Value(),
    33  		}
    34  		return measurement, fields
    35  	case metrics.GaugeInfo:
    36  		ms := metric.Snapshot()
    37  		measurement := fmt.Sprintf("%s%s.gauge", namespace, name)
    38  		fields := map[string]interface{}{
    39  			"value": ms.Value().String(),
    40  		}
    41  		return measurement, fields
    42  	case metrics.Histogram:
    43  		ms := metric.Snapshot()
    44  		if ms.Count() <= 0 {
    45  			break
    46  		}
    47  		ps := ms.Percentiles([]float64{0.25, 0.5, 0.75, 0.95, 0.99, 0.999, 0.9999})
    48  		measurement := fmt.Sprintf("%s%s.histogram", namespace, name)
    49  		fields := map[string]interface{}{
    50  			"count":    ms.Count(),
    51  			"max":      ms.Max(),
    52  			"mean":     ms.Mean(),
    53  			"min":      ms.Min(),
    54  			"stddev":   ms.StdDev(),
    55  			"variance": ms.Variance(),
    56  			"p25":      ps[0],
    57  			"p50":      ps[1],
    58  			"p75":      ps[2],
    59  			"p95":      ps[3],
    60  			"p99":      ps[4],
    61  			"p999":     ps[5],
    62  			"p9999":    ps[6],
    63  		}
    64  		return measurement, fields
    65  	case metrics.Meter:
    66  		ms := metric.Snapshot()
    67  		measurement := fmt.Sprintf("%s%s.meter", namespace, name)
    68  		fields := map[string]interface{}{
    69  			"count": ms.Count(),
    70  			"m1":    ms.Rate1(),
    71  			"m5":    ms.Rate5(),
    72  			"m15":   ms.Rate15(),
    73  			"mean":  ms.RateMean(),
    74  		}
    75  		return measurement, fields
    76  	case metrics.Timer:
    77  		ms := metric.Snapshot()
    78  		ps := ms.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999})
    79  
    80  		measurement := fmt.Sprintf("%s%s.timer", namespace, name)
    81  		fields := map[string]interface{}{
    82  			"count":    ms.Count(),
    83  			"max":      ms.Max(),
    84  			"mean":     ms.Mean(),
    85  			"min":      ms.Min(),
    86  			"stddev":   ms.StdDev(),
    87  			"variance": ms.Variance(),
    88  			"p50":      ps[0],
    89  			"p75":      ps[1],
    90  			"p95":      ps[2],
    91  			"p99":      ps[3],
    92  			"p999":     ps[4],
    93  			"p9999":    ps[5],
    94  			"m1":       ms.Rate1(),
    95  			"m5":       ms.Rate5(),
    96  			"m15":      ms.Rate15(),
    97  			"meanrate": ms.RateMean(),
    98  		}
    99  		return measurement, fields
   100  	case metrics.ResettingTimer:
   101  		t := metric.Snapshot()
   102  		if t.Count() == 0 {
   103  			break
   104  		}
   105  		ps := t.Percentiles([]float64{0.50, 0.95, 0.99})
   106  		measurement := fmt.Sprintf("%s%s.span", namespace, name)
   107  		fields := map[string]interface{}{
   108  			"count": t.Count(),
   109  			"max":   t.Max(),
   110  			"mean":  t.Mean(),
   111  			"min":   t.Min(),
   112  			"p50":   int(ps[0]),
   113  			"p95":   int(ps[1]),
   114  			"p99":   int(ps[2]),
   115  		}
   116  		return measurement, fields
   117  	}
   118  	return "", nil
   119  }