You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

135 rivejä
3.7 KiB

  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package metric
  15. import (
  16. "sync"
  17. "time"
  18. "go.opencensus.io/metric/metricdata"
  19. )
  20. // Registry creates and manages a set of gauges.
  21. // External synchronization is required if you want to add gauges to the same
  22. // registry from multiple goroutines.
  23. type Registry struct {
  24. gauges sync.Map
  25. }
  26. type gaugeType int
  27. const (
  28. gaugeInt64 gaugeType = iota
  29. gaugeFloat64
  30. derivedGaugeInt64
  31. derivedGaugeFloat64
  32. )
  33. // NewRegistry initializes a new Registry.
  34. func NewRegistry() *Registry {
  35. return &Registry{}
  36. }
  37. // AddFloat64Gauge creates and adds a new float64-valued gauge to this registry.
  38. func (r *Registry) AddFloat64Gauge(name, description string, unit metricdata.Unit, labelKeys ...string) (*Float64Gauge, error) {
  39. f := &Float64Gauge{
  40. g: gauge{
  41. gType: gaugeFloat64,
  42. },
  43. }
  44. _, err := r.initGauge(&f.g, labelKeys, name, description, unit)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return f, nil
  49. }
  50. // AddInt64Gauge creates and adds a new int64-valued gauge to this registry.
  51. func (r *Registry) AddInt64Gauge(name, description string, unit metricdata.Unit, labelKeys ...string) (*Int64Gauge, error) {
  52. i := &Int64Gauge{
  53. g: gauge{
  54. gType: gaugeInt64,
  55. },
  56. }
  57. _, err := r.initGauge(&i.g, labelKeys, name, description, unit)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return i, nil
  62. }
  63. // AddInt64DerivedGauge creates and adds a new derived int64-valued gauge to this registry.
  64. // A derived gauge is convenient form of gauge where the object associated with the gauge
  65. // provides its value by implementing func() int64.
  66. func (r *Registry) AddInt64DerivedGauge(name, description string, unit metricdata.Unit, labelKeys ...string) (*Int64DerivedGauge, error) {
  67. i := &Int64DerivedGauge{
  68. g: gauge{
  69. gType: derivedGaugeInt64,
  70. },
  71. }
  72. _, err := r.initGauge(&i.g, labelKeys, name, description, unit)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return i, nil
  77. }
  78. // AddFloat64DerivedGauge creates and adds a new derived float64-valued gauge to this registry.
  79. // A derived gauge is convenient form of gauge where the object associated with the gauge
  80. // provides its value by implementing func() float64.
  81. func (r *Registry) AddFloat64DerivedGauge(name, description string, unit metricdata.Unit, labelKeys ...string) (*Float64DerivedGauge, error) {
  82. f := &Float64DerivedGauge{
  83. g: gauge{
  84. gType: derivedGaugeFloat64,
  85. },
  86. }
  87. _, err := r.initGauge(&f.g, labelKeys, name, description, unit)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return f, nil
  92. }
  93. func (r *Registry) initGauge(g *gauge, labelKeys []string, name string, description string, unit metricdata.Unit) (*gauge, error) {
  94. val, ok := r.gauges.Load(name)
  95. if ok {
  96. existing := val.(*gauge)
  97. if existing.gType != g.gType {
  98. return nil, errGaugeExistsWithDiffType
  99. }
  100. }
  101. g.keys = labelKeys
  102. g.start = time.Now()
  103. g.desc = metricdata.Descriptor{
  104. Name: name,
  105. Description: description,
  106. Unit: unit,
  107. LabelKeys: labelKeys,
  108. }
  109. r.gauges.Store(name, g)
  110. return g, nil
  111. }
  112. // Read reads all gauges in this registry and returns their values as metrics.
  113. func (r *Registry) Read() []*metricdata.Metric {
  114. ms := []*metricdata.Metric{}
  115. r.gauges.Range(func(k, v interface{}) bool {
  116. g := v.(*gauge)
  117. ms = append(ms, g.read())
  118. return true
  119. })
  120. return ms
  121. }