go.temporal.io/server/common/metrics/runtime.go

128 LOC · 66 covered · 62 uncovered · 10 ranges · 64 concepts · 2 introducers · 12 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package metrics
2
3 import (
4 "runtime"
5 "sync/atomic"
6 "time"
7
8 "go.temporal.io/server/common/build"
9 "go.temporal.io/server/common/headers"
10 "go.temporal.io/server/common/log"
11 )
12
13 const (
14 // buildInfoMetricName is the emitted build information metric's name.
15 buildInfoMetricName = "build_information"
16
17 // buildAgeMetricName is the emitted build age metric's name.
18 buildAgeMetricName = "build_age"
19 )
20
21 // RuntimeMetricsReporter A struct containing the state of the RuntimeMetricsReporter.
22 type RuntimeMetricsReporter struct {
23 handler Handler
24 buildInfoHandler Handler
25 reportInterval time.Duration
26 started int32
27 quit chan struct{}
28 logger log.Logger
29 lastNumGC uint32
30 buildTime time.Time
31 }
32
33 // NewRuntimeMetricsReporter Creates a new RuntimeMetricsReporter.
34 func NewRuntimeMetricsReporter(
35 handler Handler,
36 reportInterval time.Duration,
37 logger log.Logger,
38 instanceID string,
39 > ) *RuntimeMetricsReporter { fx.go ×44
40 > if len(instanceID) > 0 {
41 handler = handler.WithTags(StringTag(instance, instanceID))
42 }
43 > var memstats runtime.MemStats fx.go ×44
44 > runtime.ReadMemStats(&memstats)
45 >
46 > return &RuntimeMetricsReporter{
47 > handler: handler,
48 > reportInterval: reportInterval,
49 > logger: logger,
50 > lastNumGC: memstats.NumGC,
51 > quit: make(chan struct{}),
52 > buildTime: build.InfoData.GitTime,
53 > buildInfoHandler: handler.WithTags(
54 > StringTag(gitRevisionTag, build.InfoData.GitRevision),
55 > StringTag(buildDateTag, build.InfoData.GitTime.Format(time.RFC3339)),
56 > StringTag(buildPlatformTag, build.InfoData.GoArch),
57 > StringTag(goVersionTag, build.InfoData.GoVersion),
58 > StringTag(buildVersionTag, headers.ServerVersion),
59 > ),
60 > }
61 }
62
63 // report Sends runtime metrics to the local metrics collector.
64 > func (r *RuntimeMetricsReporter) report() { fx.go ×44
65 > var memStats runtime.MemStats
66 > runtime.ReadMemStats(&memStats)
67 >
68 > NumGoRoutinesGauge.With(r.handler).Record(float64(runtime.NumGoroutine()))
69 > GoMaxProcsGauge.With(r.handler).Record(float64(runtime.GOMAXPROCS(0)))
70 > MemoryAllocatedGauge.With(r.handler).Record(float64(memStats.Alloc))
71 > MemoryHeapGauge.With(r.handler).Record(float64(memStats.HeapAlloc))
72 > MemoryHeapObjectsGauge.With(r.handler).Record(float64(memStats.HeapObjects))
73 > MemoryHeapIdleGauge.With(r.handler).Record(float64(memStats.HeapIdle))
74 > MemoryHeapInuseGauge.With(r.handler).Record(float64(memStats.HeapInuse))
75 > MemoryHeapReleasedGauge.With(r.handler).Record(float64(memStats.HeapReleased))
76 > MemoryStackGauge.With(r.handler).Record(float64(memStats.StackInuse))
77 > MemoryMallocsGauge.With(r.handler).Record(float64(memStats.Mallocs))
78 > MemoryFreesGauge.With(r.handler).Record(float64(memStats.Frees))
79 >
80 > NumGCGauge.With(r.handler).Record(float64(memStats.NumGC))
81 > GcPauseNsTotal.With(r.handler).Record(float64(memStats.PauseTotalNs))
82 >
83 > // memStats.NumGC is a perpetually incrementing counter (unless it wraps at 2^32)
84 > num := memStats.NumGC
85 > lastNum := atomic.SwapUint32(&r.lastNumGC, num) // reset for the next iteration
86 > if delta := num - lastNum; delta > 0 {
87 > NumGCCounter.With(r.handler).Record(int64(delta))
88 > if delta > 255 {
89 // too many GCs happened, the timestamps buffer got wrapped around. Report only the last 256
90 lastNum = num - 256
91 }
92 > for i := lastNum; i != num; i++ { fx.go ×44
93 > pause := memStats.PauseNs[i%256]
94 > GcPauseMsTimer.With(r.handler).Record(time.Duration(pause))
95 > }
96 }
97
98 // report build info
99 > r.buildInfoHandler.Gauge(buildInfoMetricName).Record(1.0) fx.go ×44
100 > r.buildInfoHandler.Gauge(buildAgeMetricName).Record(float64(time.Since(r.buildTime)))
101 }
102
103 // Start Starts the reporter thread that periodically emits metrics.
104 > func (r *RuntimeMetricsReporter) Start() { fx.go ×44
105 > if !atomic.CompareAndSwapInt32(&r.started, 0, 1) {
106 return
107 }
108 > r.report() fx.go ×44
109 > go func() {
110 > ticker := time.NewTicker(r.reportInterval)
111 > for {
112 > select {
113 case <-ticker.C:
114 r.report()
115 > case <-r.quit: service.go ×8
116 > ticker.Stop()
117 > return
118 }
119 }
120 }()
121 > r.logger.Info("RuntimeMetricsReporter started") fx.go ×44
122 }
123
124 // Stop Stops reporting of runtime metrics. The reporter cannot be started again after it's been stopped.
125 > func (r *RuntimeMetricsReporter) Stop() { service.go ×8
126 > close(r.quit)
127 > r.logger.Info("RuntimeMetricsReporter stopped")
128 > }