registry.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. "bytes"
  16. "errors"
  17. "fmt"
  18. "os"
  19. "sort"
  20. "sync"
  21. "github.com/golang/protobuf/proto"
  22. dto "github.com/prometheus/client_model/go"
  23. )
  24. const (
  25. // Capacity for the channel to collect metrics and descriptors.
  26. capMetricChan = 1000
  27. capDescChan = 10
  28. )
  29. // DefaultRegisterer and DefaultGatherer are the implementations of the
  30. // Registerer and Gatherer interface a number of convenience functions in this
  31. // package act on. Initially, both variables point to the same Registry, which
  32. // has a process collector (see NewProcessCollector) and a Go collector (see
  33. // NewGoCollector) already registered. This approach to keep default instances
  34. // as global state mirrors the approach of other packages in the Go standard
  35. // library. Note that there are caveats. Change the variables with caution and
  36. // only if you understand the consequences. Users who want to avoid global state
  37. // altogether should not use the convenience function and act on custom
  38. // instances instead.
  39. var (
  40. defaultRegistry = NewRegistry()
  41. DefaultRegisterer Registerer = defaultRegistry
  42. DefaultGatherer Gatherer = defaultRegistry
  43. )
  44. func init() {
  45. MustRegister(NewProcessCollector(os.Getpid(), ""))
  46. MustRegister(NewGoCollector())
  47. }
  48. // NewRegistry creates a new vanilla Registry without any Collectors
  49. // pre-registered.
  50. func NewRegistry() *Registry {
  51. return &Registry{
  52. collectorsByID: map[uint64]Collector{},
  53. descIDs: map[uint64]struct{}{},
  54. dimHashesByName: map[string]uint64{},
  55. }
  56. }
  57. // NewPedanticRegistry returns a registry that checks during collection if each
  58. // collected Metric is consistent with its reported Desc, and if the Desc has
  59. // actually been registered with the registry.
  60. //
  61. // Usually, a Registry will be happy as long as the union of all collected
  62. // Metrics is consistent and valid even if some metrics are not consistent with
  63. // their own Desc or a Desc provided by their registered Collector. Well-behaved
  64. // Collectors and Metrics will only provide consistent Descs. This Registry is
  65. // useful to test the implementation of Collectors and Metrics.
  66. func NewPedanticRegistry() *Registry {
  67. r := NewRegistry()
  68. r.pedanticChecksEnabled = true
  69. return r
  70. }
  71. // Registerer is the interface for the part of a registry in charge of
  72. // registering and unregistering. Users of custom registries should use
  73. // Registerer as type for registration purposes (rather then the Registry type
  74. // directly). In that way, they are free to use custom Registerer implementation
  75. // (e.g. for testing purposes).
  76. type Registerer interface {
  77. // Register registers a new Collector to be included in metrics
  78. // collection. It returns an error if the descriptors provided by the
  79. // Collector are invalid or if they — in combination with descriptors of
  80. // already registered Collectors — do not fulfill the consistency and
  81. // uniqueness criteria described in the documentation of metric.Desc.
  82. //
  83. // If the provided Collector is equal to a Collector already registered
  84. // (which includes the case of re-registering the same Collector), the
  85. // returned error is an instance of AlreadyRegisteredError, which
  86. // contains the previously registered Collector.
  87. //
  88. // It is in general not safe to register the same Collector multiple
  89. // times concurrently.
  90. Register(Collector) error
  91. // MustRegister works like Register but registers any number of
  92. // Collectors and panics upon the first registration that causes an
  93. // error.
  94. MustRegister(...Collector)
  95. // Unregister unregisters the Collector that equals the Collector passed
  96. // in as an argument. (Two Collectors are considered equal if their
  97. // Describe method yields the same set of descriptors.) The function
  98. // returns whether a Collector was unregistered.
  99. //
  100. // Note that even after unregistering, it will not be possible to
  101. // register a new Collector that is inconsistent with the unregistered
  102. // Collector, e.g. a Collector collecting metrics with the same name but
  103. // a different help string. The rationale here is that the same registry
  104. // instance must only collect consistent metrics throughout its
  105. // lifetime.
  106. Unregister(Collector) bool
  107. }
  108. // Gatherer is the interface for the part of a registry in charge of gathering
  109. // the collected metrics into a number of MetricFamilies. The Gatherer interface
  110. // comes with the same general implication as described for the Registerer
  111. // interface.
  112. type Gatherer interface {
  113. // Gather calls the Collect method of the registered Collectors and then
  114. // gathers the collected metrics into a lexicographically sorted slice
  115. // of MetricFamily protobufs. Even if an error occurs, Gather attempts
  116. // to gather as many metrics as possible. Hence, if a non-nil error is
  117. // returned, the returned MetricFamily slice could be nil (in case of a
  118. // fatal error that prevented any meaningful metric collection) or
  119. // contain a number of MetricFamily protobufs, some of which might be
  120. // incomplete, and some might be missing altogether. The returned error
  121. // (which might be a MultiError) explains the details. In scenarios
  122. // where complete collection is critical, the returned MetricFamily
  123. // protobufs should be disregarded if the returned error is non-nil.
  124. Gather() ([]*dto.MetricFamily, error)
  125. }
  126. // Register registers the provided Collector with the DefaultRegisterer.
  127. //
  128. // Register is a shortcut for DefaultRegisterer.Register(c). See there for more
  129. // details.
  130. func Register(c Collector) error {
  131. return DefaultRegisterer.Register(c)
  132. }
  133. // MustRegister registers the provided Collectors with the DefaultRegisterer and
  134. // panics if any error occurs.
  135. //
  136. // MustRegister is a shortcut for DefaultRegisterer.MustRegister(cs...). See
  137. // there for more details.
  138. func MustRegister(cs ...Collector) {
  139. DefaultRegisterer.MustRegister(cs...)
  140. }
  141. // Unregister removes the registration of the provided Collector from the
  142. // DefaultRegisterer.
  143. //
  144. // Unregister is a shortcut for DefaultRegisterer.Unregister(c). See there for
  145. // more details.
  146. func Unregister(c Collector) bool {
  147. return DefaultRegisterer.Unregister(c)
  148. }
  149. // GathererFunc turns a function into a Gatherer.
  150. type GathererFunc func() ([]*dto.MetricFamily, error)
  151. // Gather implements Gatherer.
  152. func (gf GathererFunc) Gather() ([]*dto.MetricFamily, error) {
  153. return gf()
  154. }
  155. // AlreadyRegisteredError is returned by the Register method if the Collector to
  156. // be registered has already been registered before, or a different Collector
  157. // that collects the same metrics has been registered before. Registration fails
  158. // in that case, but you can detect from the kind of error what has
  159. // happened. The error contains fields for the existing Collector and the
  160. // (rejected) new Collector that equals the existing one. This can be used to
  161. // find out if an equal Collector has been registered before and switch over to
  162. // using the old one, as demonstrated in the example.
  163. type AlreadyRegisteredError struct {
  164. ExistingCollector, NewCollector Collector
  165. }
  166. func (err AlreadyRegisteredError) Error() string {
  167. return "duplicate metrics collector registration attempted"
  168. }
  169. // MultiError is a slice of errors implementing the error interface. It is used
  170. // by a Gatherer to report multiple errors during MetricFamily gathering.
  171. type MultiError []error
  172. func (errs MultiError) Error() string {
  173. if len(errs) == 0 {
  174. return ""
  175. }
  176. buf := &bytes.Buffer{}
  177. fmt.Fprintf(buf, "%d error(s) occurred:", len(errs))
  178. for _, err := range errs {
  179. fmt.Fprintf(buf, "\n* %s", err)
  180. }
  181. return buf.String()
  182. }
  183. // MaybeUnwrap returns nil if len(errs) is 0. It returns the first and only
  184. // contained error as error if len(errs is 1). In all other cases, it returns
  185. // the MultiError directly. This is helpful for returning a MultiError in a way
  186. // that only uses the MultiError if needed.
  187. func (errs MultiError) MaybeUnwrap() error {
  188. switch len(errs) {
  189. case 0:
  190. return nil
  191. case 1:
  192. return errs[0]
  193. default:
  194. return errs
  195. }
  196. }
  197. // Registry registers Prometheus collectors, collects their metrics, and gathers
  198. // them into MetricFamilies for exposition. It implements both Registerer and
  199. // Gatherer. The zero value is not usable. Create instances with NewRegistry or
  200. // NewPedanticRegistry.
  201. type Registry struct {
  202. mtx sync.RWMutex
  203. collectorsByID map[uint64]Collector // ID is a hash of the descIDs.
  204. descIDs map[uint64]struct{}
  205. dimHashesByName map[string]uint64
  206. pedanticChecksEnabled bool
  207. }
  208. // Register implements Registerer.
  209. func (r *Registry) Register(c Collector) error {
  210. var (
  211. descChan = make(chan *Desc, capDescChan)
  212. newDescIDs = map[uint64]struct{}{}
  213. newDimHashesByName = map[string]uint64{}
  214. collectorID uint64 // Just a sum of all desc IDs.
  215. duplicateDescErr error
  216. )
  217. go func() {
  218. c.Describe(descChan)
  219. close(descChan)
  220. }()
  221. r.mtx.Lock()
  222. defer r.mtx.Unlock()
  223. // Conduct various tests...
  224. for desc := range descChan {
  225. // Is the descriptor valid at all?
  226. if desc.err != nil {
  227. return fmt.Errorf("descriptor %s is invalid: %s", desc, desc.err)
  228. }
  229. // Is the descID unique?
  230. // (In other words: Is the fqName + constLabel combination unique?)
  231. if _, exists := r.descIDs[desc.id]; exists {
  232. duplicateDescErr = fmt.Errorf("descriptor %s already exists with the same fully-qualified name and const label values", desc)
  233. }
  234. // If it is not a duplicate desc in this collector, add it to
  235. // the collectorID. (We allow duplicate descs within the same
  236. // collector, but their existence must be a no-op.)
  237. if _, exists := newDescIDs[desc.id]; !exists {
  238. newDescIDs[desc.id] = struct{}{}
  239. collectorID += desc.id
  240. }
  241. // Are all the label names and the help string consistent with
  242. // previous descriptors of the same name?
  243. // First check existing descriptors...
  244. if dimHash, exists := r.dimHashesByName[desc.fqName]; exists {
  245. if dimHash != desc.dimHash {
  246. return fmt.Errorf("a previously registered descriptor with the same fully-qualified name as %s has different label names or a different help string", desc)
  247. }
  248. } else {
  249. // ...then check the new descriptors already seen.
  250. if dimHash, exists := newDimHashesByName[desc.fqName]; exists {
  251. if dimHash != desc.dimHash {
  252. return fmt.Errorf("descriptors reported by collector have inconsistent label names or help strings for the same fully-qualified name, offender is %s", desc)
  253. }
  254. } else {
  255. newDimHashesByName[desc.fqName] = desc.dimHash
  256. }
  257. }
  258. }
  259. // Did anything happen at all?
  260. if len(newDescIDs) == 0 {
  261. return errors.New("collector has no descriptors")
  262. }
  263. if existing, exists := r.collectorsByID[collectorID]; exists {
  264. return AlreadyRegisteredError{
  265. ExistingCollector: existing,
  266. NewCollector: c,
  267. }
  268. }
  269. // If the collectorID is new, but at least one of the descs existed
  270. // before, we are in trouble.
  271. if duplicateDescErr != nil {
  272. return duplicateDescErr
  273. }
  274. // Only after all tests have passed, actually register.
  275. r.collectorsByID[collectorID] = c
  276. for hash := range newDescIDs {
  277. r.descIDs[hash] = struct{}{}
  278. }
  279. for name, dimHash := range newDimHashesByName {
  280. r.dimHashesByName[name] = dimHash
  281. }
  282. return nil
  283. }
  284. // Unregister implements Registerer.
  285. func (r *Registry) Unregister(c Collector) bool {
  286. var (
  287. descChan = make(chan *Desc, capDescChan)
  288. descIDs = map[uint64]struct{}{}
  289. collectorID uint64 // Just a sum of the desc IDs.
  290. )
  291. go func() {
  292. c.Describe(descChan)
  293. close(descChan)
  294. }()
  295. for desc := range descChan {
  296. if _, exists := descIDs[desc.id]; !exists {
  297. collectorID += desc.id
  298. descIDs[desc.id] = struct{}{}
  299. }
  300. }
  301. r.mtx.RLock()
  302. if _, exists := r.collectorsByID[collectorID]; !exists {
  303. r.mtx.RUnlock()
  304. return false
  305. }
  306. r.mtx.RUnlock()
  307. r.mtx.Lock()
  308. defer r.mtx.Unlock()
  309. delete(r.collectorsByID, collectorID)
  310. for id := range descIDs {
  311. delete(r.descIDs, id)
  312. }
  313. // dimHashesByName is left untouched as those must be consistent
  314. // throughout the lifetime of a program.
  315. return true
  316. }
  317. // MustRegister implements Registerer.
  318. func (r *Registry) MustRegister(cs ...Collector) {
  319. for _, c := range cs {
  320. if err := r.Register(c); err != nil {
  321. panic(err)
  322. }
  323. }
  324. }
  325. // Gather implements Gatherer.
  326. func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
  327. var (
  328. metricChan = make(chan Metric, capMetricChan)
  329. metricHashes = map[uint64]struct{}{}
  330. dimHashes = map[string]uint64{}
  331. wg sync.WaitGroup
  332. errs MultiError // The collected errors to return in the end.
  333. registeredDescIDs map[uint64]struct{} // Only used for pedantic checks
  334. )
  335. r.mtx.RLock()
  336. metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
  337. // Scatter.
  338. // (Collectors could be complex and slow, so we call them all at once.)
  339. wg.Add(len(r.collectorsByID))
  340. go func() {
  341. wg.Wait()
  342. close(metricChan)
  343. }()
  344. for _, collector := range r.collectorsByID {
  345. go func(collector Collector) {
  346. defer wg.Done()
  347. collector.Collect(metricChan)
  348. }(collector)
  349. }
  350. // In case pedantic checks are enabled, we have to copy the map before
  351. // giving up the RLock.
  352. if r.pedanticChecksEnabled {
  353. registeredDescIDs = make(map[uint64]struct{}, len(r.descIDs))
  354. for id := range r.descIDs {
  355. registeredDescIDs[id] = struct{}{}
  356. }
  357. }
  358. r.mtx.RUnlock()
  359. // Drain metricChan in case of premature return.
  360. defer func() {
  361. for range metricChan {
  362. }
  363. }()
  364. // Gather.
  365. for metric := range metricChan {
  366. // This could be done concurrently, too, but it required locking
  367. // of metricFamiliesByName (and of metricHashes if checks are
  368. // enabled). Most likely not worth it.
  369. desc := metric.Desc()
  370. dtoMetric := &dto.Metric{}
  371. if err := metric.Write(dtoMetric); err != nil {
  372. errs = append(errs, fmt.Errorf(
  373. "error collecting metric %v: %s", desc, err,
  374. ))
  375. continue
  376. }
  377. metricFamily, ok := metricFamiliesByName[desc.fqName]
  378. if ok {
  379. if metricFamily.GetHelp() != desc.help {
  380. errs = append(errs, fmt.Errorf(
  381. "collected metric %s %s has help %q but should have %q",
  382. desc.fqName, dtoMetric, desc.help, metricFamily.GetHelp(),
  383. ))
  384. continue
  385. }
  386. // TODO(beorn7): Simplify switch once Desc has type.
  387. switch metricFamily.GetType() {
  388. case dto.MetricType_COUNTER:
  389. if dtoMetric.Counter == nil {
  390. errs = append(errs, fmt.Errorf(
  391. "collected metric %s %s should be a Counter",
  392. desc.fqName, dtoMetric,
  393. ))
  394. continue
  395. }
  396. case dto.MetricType_GAUGE:
  397. if dtoMetric.Gauge == nil {
  398. errs = append(errs, fmt.Errorf(
  399. "collected metric %s %s should be a Gauge",
  400. desc.fqName, dtoMetric,
  401. ))
  402. continue
  403. }
  404. case dto.MetricType_SUMMARY:
  405. if dtoMetric.Summary == nil {
  406. errs = append(errs, fmt.Errorf(
  407. "collected metric %s %s should be a Summary",
  408. desc.fqName, dtoMetric,
  409. ))
  410. continue
  411. }
  412. case dto.MetricType_UNTYPED:
  413. if dtoMetric.Untyped == nil {
  414. errs = append(errs, fmt.Errorf(
  415. "collected metric %s %s should be Untyped",
  416. desc.fqName, dtoMetric,
  417. ))
  418. continue
  419. }
  420. case dto.MetricType_HISTOGRAM:
  421. if dtoMetric.Histogram == nil {
  422. errs = append(errs, fmt.Errorf(
  423. "collected metric %s %s should be a Histogram",
  424. desc.fqName, dtoMetric,
  425. ))
  426. continue
  427. }
  428. default:
  429. panic("encountered MetricFamily with invalid type")
  430. }
  431. } else {
  432. metricFamily = &dto.MetricFamily{}
  433. metricFamily.Name = proto.String(desc.fqName)
  434. metricFamily.Help = proto.String(desc.help)
  435. // TODO(beorn7): Simplify switch once Desc has type.
  436. switch {
  437. case dtoMetric.Gauge != nil:
  438. metricFamily.Type = dto.MetricType_GAUGE.Enum()
  439. case dtoMetric.Counter != nil:
  440. metricFamily.Type = dto.MetricType_COUNTER.Enum()
  441. case dtoMetric.Summary != nil:
  442. metricFamily.Type = dto.MetricType_SUMMARY.Enum()
  443. case dtoMetric.Untyped != nil:
  444. metricFamily.Type = dto.MetricType_UNTYPED.Enum()
  445. case dtoMetric.Histogram != nil:
  446. metricFamily.Type = dto.MetricType_HISTOGRAM.Enum()
  447. default:
  448. errs = append(errs, fmt.Errorf(
  449. "empty metric collected: %s", dtoMetric,
  450. ))
  451. continue
  452. }
  453. metricFamiliesByName[desc.fqName] = metricFamily
  454. }
  455. if err := checkMetricConsistency(metricFamily, dtoMetric, metricHashes, dimHashes); err != nil {
  456. errs = append(errs, err)
  457. continue
  458. }
  459. if r.pedanticChecksEnabled {
  460. // Is the desc registered at all?
  461. if _, exist := registeredDescIDs[desc.id]; !exist {
  462. errs = append(errs, fmt.Errorf(
  463. "collected metric %s %s with unregistered descriptor %s",
  464. metricFamily.GetName(), dtoMetric, desc,
  465. ))
  466. continue
  467. }
  468. if err := checkDescConsistency(metricFamily, dtoMetric, desc); err != nil {
  469. errs = append(errs, err)
  470. continue
  471. }
  472. }
  473. metricFamily.Metric = append(metricFamily.Metric, dtoMetric)
  474. }
  475. return normalizeMetricFamilies(metricFamiliesByName), errs.MaybeUnwrap()
  476. }
  477. // Gatherers is a slice of Gatherer instances that implements the Gatherer
  478. // interface itself. Its Gather method calls Gather on all Gatherers in the
  479. // slice in order and returns the merged results. Errors returned from the
  480. // Gather calles are all returned in a flattened MultiError. Duplicate and
  481. // inconsistent Metrics are skipped (first occurrence in slice order wins) and
  482. // reported in the returned error.
  483. //
  484. // Gatherers can be used to merge the Gather results from multiple
  485. // Registries. It also provides a way to directly inject existing MetricFamily
  486. // protobufs into the gathering by creating a custom Gatherer with a Gather
  487. // method that simply returns the existing MetricFamily protobufs. Note that no
  488. // registration is involved (in contrast to Collector registration), so
  489. // obviously registration-time checks cannot happen. Any inconsistencies between
  490. // the gathered MetricFamilies are reported as errors by the Gather method, and
  491. // inconsistent Metrics are dropped. Invalid parts of the MetricFamilies
  492. // (e.g. syntactically invalid metric or label names) will go undetected.
  493. type Gatherers []Gatherer
  494. // Gather implements Gatherer.
  495. func (gs Gatherers) Gather() ([]*dto.MetricFamily, error) {
  496. var (
  497. metricFamiliesByName = map[string]*dto.MetricFamily{}
  498. metricHashes = map[uint64]struct{}{}
  499. dimHashes = map[string]uint64{}
  500. errs MultiError // The collected errors to return in the end.
  501. )
  502. for i, g := range gs {
  503. mfs, err := g.Gather()
  504. if err != nil {
  505. if multiErr, ok := err.(MultiError); ok {
  506. for _, err := range multiErr {
  507. errs = append(errs, fmt.Errorf("[from Gatherer #%d] %s", i+1, err))
  508. }
  509. } else {
  510. errs = append(errs, fmt.Errorf("[from Gatherer #%d] %s", i+1, err))
  511. }
  512. }
  513. for _, mf := range mfs {
  514. existingMF, exists := metricFamiliesByName[mf.GetName()]
  515. if exists {
  516. if existingMF.GetHelp() != mf.GetHelp() {
  517. errs = append(errs, fmt.Errorf(
  518. "gathered metric family %s has help %q but should have %q",
  519. mf.GetName(), mf.GetHelp(), existingMF.GetHelp(),
  520. ))
  521. continue
  522. }
  523. if existingMF.GetType() != mf.GetType() {
  524. errs = append(errs, fmt.Errorf(
  525. "gathered metric family %s has type %s but should have %s",
  526. mf.GetName(), mf.GetType(), existingMF.GetType(),
  527. ))
  528. continue
  529. }
  530. } else {
  531. existingMF = &dto.MetricFamily{}
  532. existingMF.Name = mf.Name
  533. existingMF.Help = mf.Help
  534. existingMF.Type = mf.Type
  535. metricFamiliesByName[mf.GetName()] = existingMF
  536. }
  537. for _, m := range mf.Metric {
  538. if err := checkMetricConsistency(existingMF, m, metricHashes, dimHashes); err != nil {
  539. errs = append(errs, err)
  540. continue
  541. }
  542. existingMF.Metric = append(existingMF.Metric, m)
  543. }
  544. }
  545. }
  546. return normalizeMetricFamilies(metricFamiliesByName), errs.MaybeUnwrap()
  547. }
  548. // metricSorter is a sortable slice of *dto.Metric.
  549. type metricSorter []*dto.Metric
  550. func (s metricSorter) Len() int {
  551. return len(s)
  552. }
  553. func (s metricSorter) Swap(i, j int) {
  554. s[i], s[j] = s[j], s[i]
  555. }
  556. func (s metricSorter) Less(i, j int) bool {
  557. if len(s[i].Label) != len(s[j].Label) {
  558. // This should not happen. The metrics are
  559. // inconsistent. However, we have to deal with the fact, as
  560. // people might use custom collectors or metric family injection
  561. // to create inconsistent metrics. So let's simply compare the
  562. // number of labels in this case. That will still yield
  563. // reproducible sorting.
  564. return len(s[i].Label) < len(s[j].Label)
  565. }
  566. for n, lp := range s[i].Label {
  567. vi := lp.GetValue()
  568. vj := s[j].Label[n].GetValue()
  569. if vi != vj {
  570. return vi < vj
  571. }
  572. }
  573. // We should never arrive here. Multiple metrics with the same
  574. // label set in the same scrape will lead to undefined ingestion
  575. // behavior. However, as above, we have to provide stable sorting
  576. // here, even for inconsistent metrics. So sort equal metrics
  577. // by their timestamp, with missing timestamps (implying "now")
  578. // coming last.
  579. if s[i].TimestampMs == nil {
  580. return false
  581. }
  582. if s[j].TimestampMs == nil {
  583. return true
  584. }
  585. return s[i].GetTimestampMs() < s[j].GetTimestampMs()
  586. }
  587. // normalizeMetricFamilies returns a MetricFamily slice with empty
  588. // MetricFamilies pruned and the remaining MetricFamilies sorted by name within
  589. // the slice, with the contained Metrics sorted within each MetricFamily.
  590. func normalizeMetricFamilies(metricFamiliesByName map[string]*dto.MetricFamily) []*dto.MetricFamily {
  591. for _, mf := range metricFamiliesByName {
  592. sort.Sort(metricSorter(mf.Metric))
  593. }
  594. names := make([]string, 0, len(metricFamiliesByName))
  595. for name, mf := range metricFamiliesByName {
  596. if len(mf.Metric) > 0 {
  597. names = append(names, name)
  598. }
  599. }
  600. sort.Strings(names)
  601. result := make([]*dto.MetricFamily, 0, len(names))
  602. for _, name := range names {
  603. result = append(result, metricFamiliesByName[name])
  604. }
  605. return result
  606. }
  607. // checkMetricConsistency checks if the provided Metric is consistent with the
  608. // provided MetricFamily. It also hashed the Metric labels and the MetricFamily
  609. // name. If the resulting hash is alread in the provided metricHashes, an error
  610. // is returned. If not, it is added to metricHashes. The provided dimHashes maps
  611. // MetricFamily names to their dimHash (hashed sorted label names). If dimHashes
  612. // doesn't yet contain a hash for the provided MetricFamily, it is
  613. // added. Otherwise, an error is returned if the existing dimHashes in not equal
  614. // the calculated dimHash.
  615. func checkMetricConsistency(
  616. metricFamily *dto.MetricFamily,
  617. dtoMetric *dto.Metric,
  618. metricHashes map[uint64]struct{},
  619. dimHashes map[string]uint64,
  620. ) error {
  621. // Type consistency with metric family.
  622. if metricFamily.GetType() == dto.MetricType_GAUGE && dtoMetric.Gauge == nil ||
  623. metricFamily.GetType() == dto.MetricType_COUNTER && dtoMetric.Counter == nil ||
  624. metricFamily.GetType() == dto.MetricType_SUMMARY && dtoMetric.Summary == nil ||
  625. metricFamily.GetType() == dto.MetricType_HISTOGRAM && dtoMetric.Histogram == nil ||
  626. metricFamily.GetType() == dto.MetricType_UNTYPED && dtoMetric.Untyped == nil {
  627. return fmt.Errorf(
  628. "collected metric %s %s is not a %s",
  629. metricFamily.GetName(), dtoMetric, metricFamily.GetType(),
  630. )
  631. }
  632. // Is the metric unique (i.e. no other metric with the same name and the same label values)?
  633. h := hashNew()
  634. h = hashAdd(h, metricFamily.GetName())
  635. h = hashAddByte(h, separatorByte)
  636. dh := hashNew()
  637. // Make sure label pairs are sorted. We depend on it for the consistency
  638. // check.
  639. sort.Sort(LabelPairSorter(dtoMetric.Label))
  640. for _, lp := range dtoMetric.Label {
  641. h = hashAdd(h, lp.GetValue())
  642. h = hashAddByte(h, separatorByte)
  643. dh = hashAdd(dh, lp.GetName())
  644. dh = hashAddByte(dh, separatorByte)
  645. }
  646. if _, exists := metricHashes[h]; exists {
  647. return fmt.Errorf(
  648. "collected metric %s %s was collected before with the same name and label values",
  649. metricFamily.GetName(), dtoMetric,
  650. )
  651. }
  652. if dimHash, ok := dimHashes[metricFamily.GetName()]; ok {
  653. if dimHash != dh {
  654. return fmt.Errorf(
  655. "collected metric %s %s has label dimensions inconsistent with previously collected metrics in the same metric family",
  656. metricFamily.GetName(), dtoMetric,
  657. )
  658. }
  659. } else {
  660. dimHashes[metricFamily.GetName()] = dh
  661. }
  662. metricHashes[h] = struct{}{}
  663. return nil
  664. }
  665. func checkDescConsistency(
  666. metricFamily *dto.MetricFamily,
  667. dtoMetric *dto.Metric,
  668. desc *Desc,
  669. ) error {
  670. // Desc help consistency with metric family help.
  671. if metricFamily.GetHelp() != desc.help {
  672. return fmt.Errorf(
  673. "collected metric %s %s has help %q but should have %q",
  674. metricFamily.GetName(), dtoMetric, metricFamily.GetHelp(), desc.help,
  675. )
  676. }
  677. // Is the desc consistent with the content of the metric?
  678. lpsFromDesc := make([]*dto.LabelPair, 0, len(dtoMetric.Label))
  679. lpsFromDesc = append(lpsFromDesc, desc.constLabelPairs...)
  680. for _, l := range desc.variableLabels {
  681. lpsFromDesc = append(lpsFromDesc, &dto.LabelPair{
  682. Name: proto.String(l),
  683. })
  684. }
  685. if len(lpsFromDesc) != len(dtoMetric.Label) {
  686. return fmt.Errorf(
  687. "labels in collected metric %s %s are inconsistent with descriptor %s",
  688. metricFamily.GetName(), dtoMetric, desc,
  689. )
  690. }
  691. sort.Sort(LabelPairSorter(lpsFromDesc))
  692. for i, lpFromDesc := range lpsFromDesc {
  693. lpFromMetric := dtoMetric.Label[i]
  694. if lpFromDesc.GetName() != lpFromMetric.GetName() ||
  695. lpFromDesc.Value != nil && lpFromDesc.GetValue() != lpFromMetric.GetValue() {
  696. return fmt.Errorf(
  697. "labels in collected metric %s %s are inconsistent with descriptor %s",
  698. metricFamily.GetName(), dtoMetric, desc,
  699. )
  700. }
  701. }
  702. return nil
  703. }