github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/metricsutil/registry_to_map.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package metricsutil
     6  
     7  import "github.com/rcrowley/go-metrics"
     8  
     9  // The code below is adapted from
    10  // https://github.com/rcrowley/go-metrics/blob/master/json.go
    11  // .
    12  
    13  // registryToMap returns a map representation of all the metrics in
    14  // the Registry.
    15  func registryToMap(r metrics.Registry) map[string]map[string]interface{} {
    16  	data := make(map[string]map[string]interface{})
    17  	r.Each(func(name string, i interface{}) {
    18  		values := make(map[string]interface{})
    19  		switch metric := i.(type) {
    20  		case metrics.Counter:
    21  			values["count"] = metric.Count()
    22  		case metrics.Gauge:
    23  			values["value"] = metric.Value()
    24  		case metrics.GaugeFloat64:
    25  			values["value"] = metric.Value()
    26  		case metrics.Healthcheck:
    27  			values["error"] = nil
    28  			metric.Check()
    29  			if err := metric.Error(); nil != err {
    30  				values["error"] = metric.Error().Error()
    31  			}
    32  		case metrics.Histogram:
    33  			h := metric.Snapshot()
    34  			ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    35  			values["count"] = h.Count()
    36  			values["min"] = h.Min()
    37  			values["max"] = h.Max()
    38  			values["mean"] = h.Mean()
    39  			values["stddev"] = h.StdDev()
    40  			values["median"] = ps[0]
    41  			values["75%"] = ps[1]
    42  			values["95%"] = ps[2]
    43  			values["99%"] = ps[3]
    44  			values["99.9%"] = ps[4]
    45  		case metrics.Meter:
    46  			m := metric.Snapshot()
    47  			values["count"] = m.Count()
    48  			values["1m.rate"] = m.Rate1()
    49  			values["5m.rate"] = m.Rate5()
    50  			values["15m.rate"] = m.Rate15()
    51  			values["mean.rate"] = m.RateMean()
    52  		case metrics.Timer:
    53  			t := metric.Snapshot()
    54  			ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    55  			values["count"] = t.Count()
    56  			values["min"] = t.Min()
    57  			values["max"] = t.Max()
    58  			values["mean"] = t.Mean()
    59  			values["stddev"] = t.StdDev()
    60  			values["median"] = ps[0]
    61  			values["75%"] = ps[1]
    62  			values["95%"] = ps[2]
    63  			values["99%"] = ps[3]
    64  			values["99.9%"] = ps[4]
    65  			values["1m.rate"] = t.Rate1()
    66  			values["5m.rate"] = t.Rate5()
    67  			values["15m.rate"] = t.Rate15()
    68  			values["mean.rate"] = t.RateMean()
    69  		}
    70  		data[name] = values
    71  	})
    72  	return data
    73  }
    74  
    75  // RegistryToInterfaceMap returns a map representation of all the
    76  // metrics in the Registry, but with the value type being interface{}.
    77  func RegistryToInterfaceMap(r metrics.Registry) map[string]interface{} {
    78  	metricsMap := registryToMap(r)
    79  	interfaceMap := make(map[string]interface{})
    80  	for k, v := range metricsMap {
    81  		interfaceMap[k] = v
    82  	}
    83  	return interfaceMap
    84  }