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.
 
 

568 lines
18 KiB

  1. // Copyright 2021 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. //go:build go1.17
  14. // +build go1.17
  15. package prometheus
  16. import (
  17. "math"
  18. "runtime"
  19. "runtime/metrics"
  20. "strings"
  21. "sync"
  22. "github.com/prometheus/client_golang/prometheus/internal"
  23. dto "github.com/prometheus/client_model/go"
  24. "google.golang.org/protobuf/proto"
  25. )
  26. const (
  27. // constants for strings referenced more than once.
  28. goGCHeapTinyAllocsObjects = "/gc/heap/tiny/allocs:objects"
  29. goGCHeapAllocsObjects = "/gc/heap/allocs:objects"
  30. goGCHeapFreesObjects = "/gc/heap/frees:objects"
  31. goGCHeapFreesBytes = "/gc/heap/frees:bytes"
  32. goGCHeapAllocsBytes = "/gc/heap/allocs:bytes"
  33. goGCHeapObjects = "/gc/heap/objects:objects"
  34. goGCHeapGoalBytes = "/gc/heap/goal:bytes"
  35. goMemoryClassesTotalBytes = "/memory/classes/total:bytes"
  36. goMemoryClassesHeapObjectsBytes = "/memory/classes/heap/objects:bytes"
  37. goMemoryClassesHeapUnusedBytes = "/memory/classes/heap/unused:bytes"
  38. goMemoryClassesHeapReleasedBytes = "/memory/classes/heap/released:bytes"
  39. goMemoryClassesHeapFreeBytes = "/memory/classes/heap/free:bytes"
  40. goMemoryClassesHeapStacksBytes = "/memory/classes/heap/stacks:bytes"
  41. goMemoryClassesOSStacksBytes = "/memory/classes/os-stacks:bytes"
  42. goMemoryClassesMetadataMSpanInuseBytes = "/memory/classes/metadata/mspan/inuse:bytes"
  43. goMemoryClassesMetadataMSPanFreeBytes = "/memory/classes/metadata/mspan/free:bytes"
  44. goMemoryClassesMetadataMCacheInuseBytes = "/memory/classes/metadata/mcache/inuse:bytes"
  45. goMemoryClassesMetadataMCacheFreeBytes = "/memory/classes/metadata/mcache/free:bytes"
  46. goMemoryClassesProfilingBucketsBytes = "/memory/classes/profiling/buckets:bytes"
  47. goMemoryClassesMetadataOtherBytes = "/memory/classes/metadata/other:bytes"
  48. goMemoryClassesOtherBytes = "/memory/classes/other:bytes"
  49. )
  50. // rmNamesForMemStatsMetrics represents runtime/metrics names required to populate goRuntimeMemStats from like logic.
  51. var rmNamesForMemStatsMetrics = []string{
  52. goGCHeapTinyAllocsObjects,
  53. goGCHeapAllocsObjects,
  54. goGCHeapFreesObjects,
  55. goGCHeapAllocsBytes,
  56. goGCHeapObjects,
  57. goGCHeapGoalBytes,
  58. goMemoryClassesTotalBytes,
  59. goMemoryClassesHeapObjectsBytes,
  60. goMemoryClassesHeapUnusedBytes,
  61. goMemoryClassesHeapReleasedBytes,
  62. goMemoryClassesHeapFreeBytes,
  63. goMemoryClassesHeapStacksBytes,
  64. goMemoryClassesOSStacksBytes,
  65. goMemoryClassesMetadataMSpanInuseBytes,
  66. goMemoryClassesMetadataMSPanFreeBytes,
  67. goMemoryClassesMetadataMCacheInuseBytes,
  68. goMemoryClassesMetadataMCacheFreeBytes,
  69. goMemoryClassesProfilingBucketsBytes,
  70. goMemoryClassesMetadataOtherBytes,
  71. goMemoryClassesOtherBytes,
  72. }
  73. func bestEffortLookupRM(lookup []string) []metrics.Description {
  74. ret := make([]metrics.Description, 0, len(lookup))
  75. for _, rm := range metrics.All() {
  76. for _, m := range lookup {
  77. if m == rm.Name {
  78. ret = append(ret, rm)
  79. }
  80. }
  81. }
  82. return ret
  83. }
  84. type goCollector struct {
  85. base baseGoCollector
  86. // mu protects updates to all fields ensuring a consistent
  87. // snapshot is always produced by Collect.
  88. mu sync.Mutex
  89. // Contains all samples that has to retrieved from runtime/metrics (not all of them will be exposed).
  90. sampleBuf []metrics.Sample
  91. // sampleMap allows lookup for MemStats metrics and runtime/metrics histograms for exact sums.
  92. sampleMap map[string]*metrics.Sample
  93. // rmExposedMetrics represents all runtime/metrics package metrics
  94. // that were configured to be exposed.
  95. rmExposedMetrics []collectorMetric
  96. rmExactSumMapForHist map[string]string
  97. // With Go 1.17, the runtime/metrics package was introduced.
  98. // From that point on, metric names produced by the runtime/metrics
  99. // package could be generated from runtime/metrics names. However,
  100. // these differ from the old names for the same values.
  101. //
  102. // This field exists to export the same values under the old names
  103. // as well.
  104. msMetrics memStatsMetrics
  105. msMetricsEnabled bool
  106. }
  107. type rmMetricDesc struct {
  108. metrics.Description
  109. }
  110. func matchRuntimeMetricsRules(rules []internal.GoCollectorRule) []rmMetricDesc {
  111. var descs []rmMetricDesc
  112. for _, d := range metrics.All() {
  113. var (
  114. deny = true
  115. desc rmMetricDesc
  116. )
  117. for _, r := range rules {
  118. if !r.Matcher.MatchString(d.Name) {
  119. continue
  120. }
  121. deny = r.Deny
  122. }
  123. if deny {
  124. continue
  125. }
  126. desc.Description = d
  127. descs = append(descs, desc)
  128. }
  129. return descs
  130. }
  131. func defaultGoCollectorOptions() internal.GoCollectorOptions {
  132. return internal.GoCollectorOptions{
  133. RuntimeMetricSumForHist: map[string]string{
  134. "/gc/heap/allocs-by-size:bytes": goGCHeapAllocsBytes,
  135. "/gc/heap/frees-by-size:bytes": goGCHeapFreesBytes,
  136. },
  137. RuntimeMetricRules: []internal.GoCollectorRule{
  138. //{Matcher: regexp.MustCompile("")},
  139. },
  140. }
  141. }
  142. // NewGoCollector is the obsolete version of collectors.NewGoCollector.
  143. // See there for documentation.
  144. //
  145. // Deprecated: Use collectors.NewGoCollector instead.
  146. func NewGoCollector(opts ...func(o *internal.GoCollectorOptions)) Collector {
  147. opt := defaultGoCollectorOptions()
  148. for _, o := range opts {
  149. o(&opt)
  150. }
  151. exposedDescriptions := matchRuntimeMetricsRules(opt.RuntimeMetricRules)
  152. // Collect all histogram samples so that we can get their buckets.
  153. // The API guarantees that the buckets are always fixed for the lifetime
  154. // of the process.
  155. var histograms []metrics.Sample
  156. for _, d := range exposedDescriptions {
  157. if d.Kind == metrics.KindFloat64Histogram {
  158. histograms = append(histograms, metrics.Sample{Name: d.Name})
  159. }
  160. }
  161. if len(histograms) > 0 {
  162. metrics.Read(histograms)
  163. }
  164. bucketsMap := make(map[string][]float64)
  165. for i := range histograms {
  166. bucketsMap[histograms[i].Name] = histograms[i].Value.Float64Histogram().Buckets
  167. }
  168. // Generate a collector for each exposed runtime/metrics metric.
  169. metricSet := make([]collectorMetric, 0, len(exposedDescriptions))
  170. // SampleBuf is used for reading from runtime/metrics.
  171. // We are assuming the largest case to have stable pointers for sampleMap purposes.
  172. sampleBuf := make([]metrics.Sample, 0, len(exposedDescriptions)+len(opt.RuntimeMetricSumForHist)+len(rmNamesForMemStatsMetrics))
  173. sampleMap := make(map[string]*metrics.Sample, len(exposedDescriptions))
  174. for _, d := range exposedDescriptions {
  175. namespace, subsystem, name, ok := internal.RuntimeMetricsToProm(&d.Description)
  176. if !ok {
  177. // Just ignore this metric; we can't do anything with it here.
  178. // If a user decides to use the latest version of Go, we don't want
  179. // to fail here. This condition is tested in TestExpectedRuntimeMetrics.
  180. continue
  181. }
  182. sampleBuf = append(sampleBuf, metrics.Sample{Name: d.Name})
  183. sampleMap[d.Name] = &sampleBuf[len(sampleBuf)-1]
  184. var m collectorMetric
  185. if d.Kind == metrics.KindFloat64Histogram {
  186. _, hasSum := opt.RuntimeMetricSumForHist[d.Name]
  187. unit := d.Name[strings.IndexRune(d.Name, ':')+1:]
  188. m = newBatchHistogram(
  189. NewDesc(
  190. BuildFQName(namespace, subsystem, name),
  191. d.Description.Description,
  192. nil,
  193. nil,
  194. ),
  195. internal.RuntimeMetricsBucketsForUnit(bucketsMap[d.Name], unit),
  196. hasSum,
  197. )
  198. } else if d.Cumulative {
  199. m = NewCounter(CounterOpts{
  200. Namespace: namespace,
  201. Subsystem: subsystem,
  202. Name: name,
  203. Help: d.Description.Description,
  204. },
  205. )
  206. } else {
  207. m = NewGauge(GaugeOpts{
  208. Namespace: namespace,
  209. Subsystem: subsystem,
  210. Name: name,
  211. Help: d.Description.Description,
  212. })
  213. }
  214. metricSet = append(metricSet, m)
  215. }
  216. // Add exact sum metrics to sampleBuf if not added before.
  217. for _, h := range histograms {
  218. sumMetric, ok := opt.RuntimeMetricSumForHist[h.Name]
  219. if !ok {
  220. continue
  221. }
  222. if _, ok := sampleMap[sumMetric]; ok {
  223. continue
  224. }
  225. sampleBuf = append(sampleBuf, metrics.Sample{Name: sumMetric})
  226. sampleMap[sumMetric] = &sampleBuf[len(sampleBuf)-1]
  227. }
  228. var (
  229. msMetrics memStatsMetrics
  230. msDescriptions []metrics.Description
  231. )
  232. if !opt.DisableMemStatsLikeMetrics {
  233. msMetrics = goRuntimeMemStats()
  234. msDescriptions = bestEffortLookupRM(rmNamesForMemStatsMetrics)
  235. // Check if metric was not exposed before and if not, add to sampleBuf.
  236. for _, mdDesc := range msDescriptions {
  237. if _, ok := sampleMap[mdDesc.Name]; ok {
  238. continue
  239. }
  240. sampleBuf = append(sampleBuf, metrics.Sample{Name: mdDesc.Name})
  241. sampleMap[mdDesc.Name] = &sampleBuf[len(sampleBuf)-1]
  242. }
  243. }
  244. return &goCollector{
  245. base: newBaseGoCollector(),
  246. sampleBuf: sampleBuf,
  247. sampleMap: sampleMap,
  248. rmExposedMetrics: metricSet,
  249. rmExactSumMapForHist: opt.RuntimeMetricSumForHist,
  250. msMetrics: msMetrics,
  251. msMetricsEnabled: !opt.DisableMemStatsLikeMetrics,
  252. }
  253. }
  254. // Describe returns all descriptions of the collector.
  255. func (c *goCollector) Describe(ch chan<- *Desc) {
  256. c.base.Describe(ch)
  257. for _, i := range c.msMetrics {
  258. ch <- i.desc
  259. }
  260. for _, m := range c.rmExposedMetrics {
  261. ch <- m.Desc()
  262. }
  263. }
  264. // Collect returns the current state of all metrics of the collector.
  265. func (c *goCollector) Collect(ch chan<- Metric) {
  266. // Collect base non-memory metrics.
  267. c.base.Collect(ch)
  268. if len(c.sampleBuf) == 0 {
  269. return
  270. }
  271. // Collect must be thread-safe, so prevent concurrent use of
  272. // sampleBuf elements. Just read into sampleBuf but write all the data
  273. // we get into our Metrics or MemStats.
  274. //
  275. // This lock also ensures that the Metrics we send out are all from
  276. // the same updates, ensuring their mutual consistency insofar as
  277. // is guaranteed by the runtime/metrics package.
  278. //
  279. // N.B. This locking is heavy-handed, but Collect is expected to be called
  280. // relatively infrequently. Also the core operation here, metrics.Read,
  281. // is fast (O(tens of microseconds)) so contention should certainly be
  282. // low, though channel operations and any allocations may add to that.
  283. c.mu.Lock()
  284. defer c.mu.Unlock()
  285. // Populate runtime/metrics sample buffer.
  286. metrics.Read(c.sampleBuf)
  287. // Collect all our runtime/metrics user chose to expose from sampleBuf (if any).
  288. for i, metric := range c.rmExposedMetrics {
  289. // We created samples for exposed metrics first in order, so indexes match.
  290. sample := c.sampleBuf[i]
  291. // N.B. switch on concrete type because it's significantly more efficient
  292. // than checking for the Counter and Gauge interface implementations. In
  293. // this case, we control all the types here.
  294. switch m := metric.(type) {
  295. case *counter:
  296. // Guard against decreases. This should never happen, but a failure
  297. // to do so will result in a panic, which is a harsh consequence for
  298. // a metrics collection bug.
  299. v0, v1 := m.get(), unwrapScalarRMValue(sample.Value)
  300. if v1 > v0 {
  301. m.Add(unwrapScalarRMValue(sample.Value) - m.get())
  302. }
  303. m.Collect(ch)
  304. case *gauge:
  305. m.Set(unwrapScalarRMValue(sample.Value))
  306. m.Collect(ch)
  307. case *batchHistogram:
  308. m.update(sample.Value.Float64Histogram(), c.exactSumFor(sample.Name))
  309. m.Collect(ch)
  310. default:
  311. panic("unexpected metric type")
  312. }
  313. }
  314. if c.msMetricsEnabled {
  315. // ms is a dummy MemStats that we populate ourselves so that we can
  316. // populate the old metrics from it if goMemStatsCollection is enabled.
  317. var ms runtime.MemStats
  318. memStatsFromRM(&ms, c.sampleMap)
  319. for _, i := range c.msMetrics {
  320. ch <- MustNewConstMetric(i.desc, i.valType, i.eval(&ms))
  321. }
  322. }
  323. }
  324. // unwrapScalarRMValue unwraps a runtime/metrics value that is assumed
  325. // to be scalar and returns the equivalent float64 value. Panics if the
  326. // value is not scalar.
  327. func unwrapScalarRMValue(v metrics.Value) float64 {
  328. switch v.Kind() {
  329. case metrics.KindUint64:
  330. return float64(v.Uint64())
  331. case metrics.KindFloat64:
  332. return v.Float64()
  333. case metrics.KindBad:
  334. // Unsupported metric.
  335. //
  336. // This should never happen because we always populate our metric
  337. // set from the runtime/metrics package.
  338. panic("unexpected unsupported metric")
  339. default:
  340. // Unsupported metric kind.
  341. //
  342. // This should never happen because we check for this during initialization
  343. // and flag and filter metrics whose kinds we don't understand.
  344. panic("unexpected unsupported metric kind")
  345. }
  346. }
  347. // exactSumFor takes a runtime/metrics metric name (that is assumed to
  348. // be of kind KindFloat64Histogram) and returns its exact sum and whether
  349. // its exact sum exists.
  350. //
  351. // The runtime/metrics API for histograms doesn't currently expose exact
  352. // sums, but some of the other metrics are in fact exact sums of histograms.
  353. func (c *goCollector) exactSumFor(rmName string) float64 {
  354. sumName, ok := c.rmExactSumMapForHist[rmName]
  355. if !ok {
  356. return 0
  357. }
  358. s, ok := c.sampleMap[sumName]
  359. if !ok {
  360. return 0
  361. }
  362. return unwrapScalarRMValue(s.Value)
  363. }
  364. func memStatsFromRM(ms *runtime.MemStats, rm map[string]*metrics.Sample) {
  365. lookupOrZero := func(name string) uint64 {
  366. if s, ok := rm[name]; ok {
  367. return s.Value.Uint64()
  368. }
  369. return 0
  370. }
  371. // Currently, MemStats adds tiny alloc count to both Mallocs AND Frees.
  372. // The reason for this is because MemStats couldn't be extended at the time
  373. // but there was a desire to have Mallocs at least be a little more representative,
  374. // while having Mallocs - Frees still represent a live object count.
  375. // Unfortunately, MemStats doesn't actually export a large allocation count,
  376. // so it's impossible to pull this number out directly.
  377. tinyAllocs := lookupOrZero(goGCHeapTinyAllocsObjects)
  378. ms.Mallocs = lookupOrZero(goGCHeapAllocsObjects) + tinyAllocs
  379. ms.Frees = lookupOrZero(goGCHeapFreesObjects) + tinyAllocs
  380. ms.TotalAlloc = lookupOrZero(goGCHeapAllocsBytes)
  381. ms.Sys = lookupOrZero(goMemoryClassesTotalBytes)
  382. ms.Lookups = 0 // Already always zero.
  383. ms.HeapAlloc = lookupOrZero(goMemoryClassesHeapObjectsBytes)
  384. ms.Alloc = ms.HeapAlloc
  385. ms.HeapInuse = ms.HeapAlloc + lookupOrZero(goMemoryClassesHeapUnusedBytes)
  386. ms.HeapReleased = lookupOrZero(goMemoryClassesHeapReleasedBytes)
  387. ms.HeapIdle = ms.HeapReleased + lookupOrZero(goMemoryClassesHeapFreeBytes)
  388. ms.HeapSys = ms.HeapInuse + ms.HeapIdle
  389. ms.HeapObjects = lookupOrZero(goGCHeapObjects)
  390. ms.StackInuse = lookupOrZero(goMemoryClassesHeapStacksBytes)
  391. ms.StackSys = ms.StackInuse + lookupOrZero(goMemoryClassesOSStacksBytes)
  392. ms.MSpanInuse = lookupOrZero(goMemoryClassesMetadataMSpanInuseBytes)
  393. ms.MSpanSys = ms.MSpanInuse + lookupOrZero(goMemoryClassesMetadataMSPanFreeBytes)
  394. ms.MCacheInuse = lookupOrZero(goMemoryClassesMetadataMCacheInuseBytes)
  395. ms.MCacheSys = ms.MCacheInuse + lookupOrZero(goMemoryClassesMetadataMCacheFreeBytes)
  396. ms.BuckHashSys = lookupOrZero(goMemoryClassesProfilingBucketsBytes)
  397. ms.GCSys = lookupOrZero(goMemoryClassesMetadataOtherBytes)
  398. ms.OtherSys = lookupOrZero(goMemoryClassesOtherBytes)
  399. ms.NextGC = lookupOrZero(goGCHeapGoalBytes)
  400. // N.B. GCCPUFraction is intentionally omitted. This metric is not useful,
  401. // and often misleading due to the fact that it's an average over the lifetime
  402. // of the process.
  403. // See https://github.com/prometheus/client_golang/issues/842#issuecomment-861812034
  404. // for more details.
  405. ms.GCCPUFraction = 0
  406. }
  407. // batchHistogram is a mutable histogram that is updated
  408. // in batches.
  409. type batchHistogram struct {
  410. selfCollector
  411. // Static fields updated only once.
  412. desc *Desc
  413. hasSum bool
  414. // Because this histogram operates in batches, it just uses a
  415. // single mutex for everything. updates are always serialized
  416. // but Write calls may operate concurrently with updates.
  417. // Contention between these two sources should be rare.
  418. mu sync.Mutex
  419. buckets []float64 // Inclusive lower bounds, like runtime/metrics.
  420. counts []uint64
  421. sum float64 // Used if hasSum is true.
  422. }
  423. // newBatchHistogram creates a new batch histogram value with the given
  424. // Desc, buckets, and whether or not it has an exact sum available.
  425. //
  426. // buckets must always be from the runtime/metrics package, following
  427. // the same conventions.
  428. func newBatchHistogram(desc *Desc, buckets []float64, hasSum bool) *batchHistogram {
  429. // We need to remove -Inf values. runtime/metrics keeps them around.
  430. // But -Inf bucket should not be allowed for prometheus histograms.
  431. if buckets[0] == math.Inf(-1) {
  432. buckets = buckets[1:]
  433. }
  434. h := &batchHistogram{
  435. desc: desc,
  436. buckets: buckets,
  437. // Because buckets follows runtime/metrics conventions, there's
  438. // 1 more value in the buckets list than there are buckets represented,
  439. // because in runtime/metrics, the bucket values represent *boundaries*,
  440. // and non-Inf boundaries are inclusive lower bounds for that bucket.
  441. counts: make([]uint64, len(buckets)-1),
  442. hasSum: hasSum,
  443. }
  444. h.init(h)
  445. return h
  446. }
  447. // update updates the batchHistogram from a runtime/metrics histogram.
  448. //
  449. // sum must be provided if the batchHistogram was created to have an exact sum.
  450. // h.buckets must be a strict subset of his.Buckets.
  451. func (h *batchHistogram) update(his *metrics.Float64Histogram, sum float64) {
  452. counts, buckets := his.Counts, his.Buckets
  453. h.mu.Lock()
  454. defer h.mu.Unlock()
  455. // Clear buckets.
  456. for i := range h.counts {
  457. h.counts[i] = 0
  458. }
  459. // Copy and reduce buckets.
  460. var j int
  461. for i, count := range counts {
  462. h.counts[j] += count
  463. if buckets[i+1] == h.buckets[j+1] {
  464. j++
  465. }
  466. }
  467. if h.hasSum {
  468. h.sum = sum
  469. }
  470. }
  471. func (h *batchHistogram) Desc() *Desc {
  472. return h.desc
  473. }
  474. func (h *batchHistogram) Write(out *dto.Metric) error {
  475. h.mu.Lock()
  476. defer h.mu.Unlock()
  477. sum := float64(0)
  478. if h.hasSum {
  479. sum = h.sum
  480. }
  481. dtoBuckets := make([]*dto.Bucket, 0, len(h.counts))
  482. totalCount := uint64(0)
  483. for i, count := range h.counts {
  484. totalCount += count
  485. if !h.hasSum {
  486. if count != 0 {
  487. // N.B. This computed sum is an underestimate.
  488. sum += h.buckets[i] * float64(count)
  489. }
  490. }
  491. // Skip the +Inf bucket, but only for the bucket list.
  492. // It must still count for sum and totalCount.
  493. if math.IsInf(h.buckets[i+1], 1) {
  494. break
  495. }
  496. // Float64Histogram's upper bound is exclusive, so make it inclusive
  497. // by obtaining the next float64 value down, in order.
  498. upperBound := math.Nextafter(h.buckets[i+1], h.buckets[i])
  499. dtoBuckets = append(dtoBuckets, &dto.Bucket{
  500. CumulativeCount: proto.Uint64(totalCount),
  501. UpperBound: proto.Float64(upperBound),
  502. })
  503. }
  504. out.Histogram = &dto.Histogram{
  505. Bucket: dtoBuckets,
  506. SampleCount: proto.Uint64(totalCount),
  507. SampleSum: proto.Float64(sum),
  508. }
  509. return nil
  510. }