Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

129 linhas
5.4 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. // Collector is the interface implemented by anything that can be used by
  15. // Prometheus to collect metrics. A Collector has to be registered for
  16. // collection. See Registerer.Register.
  17. //
  18. // The stock metrics provided by this package (Gauge, Counter, Summary,
  19. // Histogram, Untyped) are also Collectors (which only ever collect one metric,
  20. // namely itself). An implementer of Collector may, however, collect multiple
  21. // metrics in a coordinated fashion and/or create metrics on the fly. Examples
  22. // for collectors already implemented in this library are the metric vectors
  23. // (i.e. collection of multiple instances of the same Metric but with different
  24. // label values) like GaugeVec or SummaryVec, and the ExpvarCollector.
  25. type Collector interface {
  26. // Describe sends the super-set of all possible descriptors of metrics
  27. // collected by this Collector to the provided channel and returns once
  28. // the last descriptor has been sent. The sent descriptors fulfill the
  29. // consistency and uniqueness requirements described in the Desc
  30. // documentation.
  31. //
  32. // It is valid if one and the same Collector sends duplicate
  33. // descriptors. Those duplicates are simply ignored. However, two
  34. // different Collectors must not send duplicate descriptors.
  35. //
  36. // Sending no descriptor at all marks the Collector as “unchecked”,
  37. // i.e. no checks will be performed at registration time, and the
  38. // Collector may yield any Metric it sees fit in its Collect method.
  39. //
  40. // This method idempotently sends the same descriptors throughout the
  41. // lifetime of the Collector. It may be called concurrently and
  42. // therefore must be implemented in a concurrency safe way.
  43. //
  44. // If a Collector encounters an error while executing this method, it
  45. // must send an invalid descriptor (created with NewInvalidDesc) to
  46. // signal the error to the registry.
  47. Describe(chan<- *Desc)
  48. // Collect is called by the Prometheus registry when collecting
  49. // metrics. The implementation sends each collected metric via the
  50. // provided channel and returns once the last metric has been sent. The
  51. // descriptor of each sent metric is one of those returned by Describe
  52. // (unless the Collector is unchecked, see above). Returned metrics that
  53. // share the same descriptor must differ in their variable label
  54. // values.
  55. //
  56. // This method may be called concurrently and must therefore be
  57. // implemented in a concurrency safe way. Blocking occurs at the expense
  58. // of total performance of rendering all registered metrics. Ideally,
  59. // Collector implementations support concurrent readers.
  60. Collect(chan<- Metric)
  61. }
  62. // DescribeByCollect is a helper to implement the Describe method of a custom
  63. // Collector. It collects the metrics from the provided Collector and sends
  64. // their descriptors to the provided channel.
  65. //
  66. // If a Collector collects the same metrics throughout its lifetime, its
  67. // Describe method can simply be implemented as:
  68. //
  69. // func (c customCollector) Describe(ch chan<- *Desc) {
  70. // DescribeByCollect(c, ch)
  71. // }
  72. //
  73. // However, this will not work if the metrics collected change dynamically over
  74. // the lifetime of the Collector in a way that their combined set of descriptors
  75. // changes as well. The shortcut implementation will then violate the contract
  76. // of the Describe method. If a Collector sometimes collects no metrics at all
  77. // (for example vectors like CounterVec, GaugeVec, etc., which only collect
  78. // metrics after a metric with a fully specified label set has been accessed),
  79. // it might even get registered as an unchecked Collector (cf. the Register
  80. // method of the Registerer interface). Hence, only use this shortcut
  81. // implementation of Describe if you are certain to fulfill the contract.
  82. //
  83. // The Collector example demonstrates a use of DescribeByCollect.
  84. func DescribeByCollect(c Collector, descs chan<- *Desc) {
  85. metrics := make(chan Metric)
  86. go func() {
  87. c.Collect(metrics)
  88. close(metrics)
  89. }()
  90. for m := range metrics {
  91. descs <- m.Desc()
  92. }
  93. }
  94. // selfCollector implements Collector for a single Metric so that the Metric
  95. // collects itself. Add it as an anonymous field to a struct that implements
  96. // Metric, and call init with the Metric itself as an argument.
  97. type selfCollector struct {
  98. self Metric
  99. }
  100. // init provides the selfCollector with a reference to the metric it is supposed
  101. // to collect. It is usually called within the factory function to create a
  102. // metric. See example.
  103. func (c *selfCollector) init(self Metric) {
  104. c.self = self
  105. }
  106. // Describe implements Collector.
  107. func (c *selfCollector) Describe(ch chan<- *Desc) {
  108. ch <- c.self.Desc()
  109. }
  110. // Collect implements Collector.
  111. func (c *selfCollector) Collect(ch chan<- Metric) {
  112. ch <- c.self
  113. }
  114. // collectorMetric is a metric that is also a collector.
  115. // Because of selfCollector, most (if not all) Metrics in
  116. // this package are also collectors.
  117. type collectorMetric interface {
  118. Metric
  119. Collector
  120. }