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.
package metrics
import (
"runtime"
"sync/atomic"
"time"
"go.temporal.io/server/common/build"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/log"
)
const (
// buildInfoMetricName is the emitted build information metric's name.
buildInfoMetricName = "build_information"
// buildAgeMetricName is the emitted build age metric's name.
buildAgeMetricName = "build_age"
)
// RuntimeMetricsReporter A struct containing the state of the RuntimeMetricsReporter.
type RuntimeMetricsReporter struct {
handler Handler
buildInfoHandler Handler
reportInterval time.Duration
started int32
quit chan struct{}
logger log.Logger
lastNumGC uint32
buildTime time.Time
}
// NewRuntimeMetricsReporter Creates a new RuntimeMetricsReporter.
func NewRuntimeMetricsReporter(
handler Handler,
reportInterval time.Duration,
logger log.Logger,
instanceID string,
if len(instanceID) > 0 {
handler = handler.WithTags(StringTag(instance, instanceID))
}
runtime.ReadMemStats(&memstats)
return &RuntimeMetricsReporter{
handler: handler,
reportInterval: reportInterval,
logger: logger,
lastNumGC: memstats.NumGC,
quit: make(chan struct{}),
buildTime: build.InfoData.GitTime,
buildInfoHandler: handler.WithTags(
StringTag(gitRevisionTag, build.InfoData.GitRevision),
StringTag(buildDateTag, build.InfoData.GitTime.Format(time.RFC3339)),
StringTag(buildPlatformTag, build.InfoData.GoArch),
StringTag(goVersionTag, build.InfoData.GoVersion),
StringTag(buildVersionTag, headers.ServerVersion),
),
}
}
// report Sends runtime metrics to the local metrics collector.
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
NumGoRoutinesGauge.With(r.handler).Record(float64(runtime.NumGoroutine()))
GoMaxProcsGauge.With(r.handler).Record(float64(runtime.GOMAXPROCS(0)))
MemoryAllocatedGauge.With(r.handler).Record(float64(memStats.Alloc))
MemoryHeapGauge.With(r.handler).Record(float64(memStats.HeapAlloc))
MemoryHeapObjectsGauge.With(r.handler).Record(float64(memStats.HeapObjects))
MemoryHeapIdleGauge.With(r.handler).Record(float64(memStats.HeapIdle))
MemoryHeapInuseGauge.With(r.handler).Record(float64(memStats.HeapInuse))
MemoryHeapReleasedGauge.With(r.handler).Record(float64(memStats.HeapReleased))
MemoryStackGauge.With(r.handler).Record(float64(memStats.StackInuse))
MemoryMallocsGauge.With(r.handler).Record(float64(memStats.Mallocs))
MemoryFreesGauge.With(r.handler).Record(float64(memStats.Frees))
NumGCGauge.With(r.handler).Record(float64(memStats.NumGC))
GcPauseNsTotal.With(r.handler).Record(float64(memStats.PauseTotalNs))
// memStats.NumGC is a perpetually incrementing counter (unless it wraps at 2^32)
num := memStats.NumGC
lastNum := atomic.SwapUint32(&r.lastNumGC, num) // reset for the next iteration
if delta := num - lastNum; delta > 0 {
NumGCCounter.With(r.handler).Record(int64(delta))
if delta > 255 {
// too many GCs happened, the timestamps buffer got wrapped around. Report only the last 256
lastNum = num - 256
}
pause := memStats.PauseNs[i%256]
GcPauseMsTimer.With(r.handler).Record(time.Duration(pause))
}
}
// report build info
r.buildInfoHandler.Gauge(buildAgeMetricName).Record(float64(time.Since(r.buildTime)))
}
// Start Starts the reporter thread that periodically emits metrics.
if !atomic.CompareAndSwapInt32(&r.started, 0, 1) {
return
}
go func() {
ticker := time.NewTicker(r.reportInterval)
for {
select {
case <-ticker.C:
r.report()
ticker.Stop()
return
}
}
}()
}
// Stop Stops reporting of runtime metrics. The reporter cannot be started again after it's been stopped.
close(r.quit)
r.logger.Info("RuntimeMetricsReporter stopped")
}