khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | // Copyright 2014 The Prometheus Authors |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package prometheus |
| 15 | |
| 16 | import ( |
| 17 | "encoding/json" |
| 18 | "expvar" |
| 19 | ) |
| 20 | |
| 21 | type expvarCollector struct { |
| 22 | exports map[string]*Desc |
| 23 | } |
| 24 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame^] | 25 | // NewExpvarCollector is the obsolete version of collectors.NewExpvarCollector. |
| 26 | // See there for documentation. |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 27 | // |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame^] | 28 | // Deprecated: Use collectors.NewExpvarCollector instead. |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 29 | func NewExpvarCollector(exports map[string]*Desc) Collector { |
| 30 | return &expvarCollector{ |
| 31 | exports: exports, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Describe implements Collector. |
| 36 | func (e *expvarCollector) Describe(ch chan<- *Desc) { |
| 37 | for _, desc := range e.exports { |
| 38 | ch <- desc |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Collect implements Collector. |
| 43 | func (e *expvarCollector) Collect(ch chan<- Metric) { |
| 44 | for name, desc := range e.exports { |
| 45 | var m Metric |
| 46 | expVar := expvar.Get(name) |
| 47 | if expVar == nil { |
| 48 | continue |
| 49 | } |
| 50 | var v interface{} |
| 51 | labels := make([]string, len(desc.variableLabels)) |
| 52 | if err := json.Unmarshal([]byte(expVar.String()), &v); err != nil { |
| 53 | ch <- NewInvalidMetric(desc, err) |
| 54 | continue |
| 55 | } |
| 56 | var processValue func(v interface{}, i int) |
| 57 | processValue = func(v interface{}, i int) { |
| 58 | if i >= len(labels) { |
| 59 | copiedLabels := append(make([]string, 0, len(labels)), labels...) |
| 60 | switch v := v.(type) { |
| 61 | case float64: |
| 62 | m = MustNewConstMetric(desc, UntypedValue, v, copiedLabels...) |
| 63 | case bool: |
| 64 | if v { |
| 65 | m = MustNewConstMetric(desc, UntypedValue, 1, copiedLabels...) |
| 66 | } else { |
| 67 | m = MustNewConstMetric(desc, UntypedValue, 0, copiedLabels...) |
| 68 | } |
| 69 | default: |
| 70 | return |
| 71 | } |
| 72 | ch <- m |
| 73 | return |
| 74 | } |
| 75 | vm, ok := v.(map[string]interface{}) |
| 76 | if !ok { |
| 77 | return |
| 78 | } |
| 79 | for lv, val := range vm { |
| 80 | labels[i] = lv |
| 81 | processValue(val, i+1) |
| 82 | } |
| 83 | } |
| 84 | processValue(v, 0) |
| 85 | } |
| 86 | } |