您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

110 行
3.1 KiB

  1. // Copyright 2017, 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. //
  15. package stats
  16. import (
  17. "sync"
  18. "sync/atomic"
  19. )
  20. // Measure represents a single numeric value to be tracked and recorded.
  21. // For example, latency, request bytes, and response bytes could be measures
  22. // to collect from a server.
  23. //
  24. // Measures by themselves have no outside effects. In order to be exported,
  25. // the measure needs to be used in a View. If no Views are defined over a
  26. // measure, there is very little cost in recording it.
  27. type Measure interface {
  28. // Name returns the name of this measure.
  29. //
  30. // Measure names are globally unique (among all libraries linked into your program).
  31. // We recommend prefixing the measure name with a domain name relevant to your
  32. // project or application.
  33. //
  34. // Measure names are never sent over the wire or exported to backends.
  35. // They are only used to create Views.
  36. Name() string
  37. // Description returns the human-readable description of this measure.
  38. Description() string
  39. // Unit returns the units for the values this measure takes on.
  40. //
  41. // Units are encoded according to the case-sensitive abbreviations from the
  42. // Unified Code for Units of Measure: http://unitsofmeasure.org/ucum.html
  43. Unit() string
  44. }
  45. // measureDescriptor is the untyped descriptor associated with each measure.
  46. // Int64Measure and Float64Measure wrap measureDescriptor to provide typed
  47. // recording APIs.
  48. // Two Measures with the same name will have the same measureDescriptor.
  49. type measureDescriptor struct {
  50. subs int32 // access atomically
  51. name string
  52. description string
  53. unit string
  54. }
  55. func (m *measureDescriptor) subscribe() {
  56. atomic.StoreInt32(&m.subs, 1)
  57. }
  58. func (m *measureDescriptor) subscribed() bool {
  59. return atomic.LoadInt32(&m.subs) == 1
  60. }
  61. var (
  62. mu sync.RWMutex
  63. measures = make(map[string]*measureDescriptor)
  64. )
  65. func registerMeasureHandle(name, desc, unit string) *measureDescriptor {
  66. mu.Lock()
  67. defer mu.Unlock()
  68. if stored, ok := measures[name]; ok {
  69. return stored
  70. }
  71. m := &measureDescriptor{
  72. name: name,
  73. description: desc,
  74. unit: unit,
  75. }
  76. measures[name] = m
  77. return m
  78. }
  79. // Measurement is the numeric value measured when recording stats. Each measure
  80. // provides methods to create measurements of their kind. For example, Int64Measure
  81. // provides M to convert an int64 into a measurement.
  82. type Measurement struct {
  83. v float64
  84. m Measure
  85. desc *measureDescriptor
  86. }
  87. // Value returns the value of the Measurement as a float64.
  88. func (m Measurement) Value() float64 {
  89. return m.v
  90. }
  91. // Measure returns the Measure from which this Measurement was created.
  92. func (m Measurement) Measure() Measure {
  93. return m.m
  94. }