Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

87 řádky
2.2 KiB

  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. package prometheus
  14. import (
  15. "encoding/json"
  16. "expvar"
  17. )
  18. type expvarCollector struct {
  19. exports map[string]*Desc
  20. }
  21. // NewExpvarCollector is the obsolete version of collectors.NewExpvarCollector.
  22. // See there for documentation.
  23. //
  24. // Deprecated: Use collectors.NewExpvarCollector instead.
  25. func NewExpvarCollector(exports map[string]*Desc) Collector {
  26. return &expvarCollector{
  27. exports: exports,
  28. }
  29. }
  30. // Describe implements Collector.
  31. func (e *expvarCollector) Describe(ch chan<- *Desc) {
  32. for _, desc := range e.exports {
  33. ch <- desc
  34. }
  35. }
  36. // Collect implements Collector.
  37. func (e *expvarCollector) Collect(ch chan<- Metric) {
  38. for name, desc := range e.exports {
  39. var m Metric
  40. expVar := expvar.Get(name)
  41. if expVar == nil {
  42. continue
  43. }
  44. var v interface{}
  45. labels := make([]string, len(desc.variableLabels))
  46. if err := json.Unmarshal([]byte(expVar.String()), &v); err != nil {
  47. ch <- NewInvalidMetric(desc, err)
  48. continue
  49. }
  50. var processValue func(v interface{}, i int)
  51. processValue = func(v interface{}, i int) {
  52. if i >= len(labels) {
  53. copiedLabels := append(make([]string, 0, len(labels)), labels...)
  54. switch v := v.(type) {
  55. case float64:
  56. m = MustNewConstMetric(desc, UntypedValue, v, copiedLabels...)
  57. case bool:
  58. if v {
  59. m = MustNewConstMetric(desc, UntypedValue, 1, copiedLabels...)
  60. } else {
  61. m = MustNewConstMetric(desc, UntypedValue, 0, copiedLabels...)
  62. }
  63. default:
  64. return
  65. }
  66. ch <- m
  67. return
  68. }
  69. vm, ok := v.(map[string]interface{})
  70. if !ok {
  71. return
  72. }
  73. for lv, val := range vm {
  74. labels[i] = lv
  75. processValue(val, i+1)
  76. }
  77. }
  78. processValue(v, 0)
  79. }
  80. }