github.com/google/cloudprober@v0.11.3/sysvars/runtime.go (about)

     1  // Copyright 2017-2021 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sysvars
    16  
    17  import (
    18  	"runtime"
    19  	"time"
    20  
    21  	"github.com/google/cloudprober/logger"
    22  	"github.com/google/cloudprober/metrics"
    23  )
    24  
    25  func runtimeVars(dataChan chan *metrics.EventMetrics, l *logger.Logger) {
    26  	m := &runtime.MemStats{}
    27  	runtime.ReadMemStats(m)
    28  	ts := time.Now()
    29  	osRuntimeVars(dataChan, l)
    30  	counterRuntimeVars(dataChan, ts, m, l)
    31  	gaugeRuntimeVars(dataChan, ts, m, l)
    32  }
    33  
    34  // counterRuntimeVars exports counter runtime stats, stats that grow through
    35  // the lifetime of the process. These stats are exported as CUMULATIVE
    36  // EventMetrics.
    37  func counterRuntimeVars(dataChan chan *metrics.EventMetrics, ts time.Time, m *runtime.MemStats, l *logger.Logger) {
    38  	em := metrics.NewEventMetrics(ts).
    39  		AddLabel("ptype", "sysvars").
    40  		AddLabel("probe", "sysvars")
    41  
    42  	// Time since this module started.
    43  	timeSince := time.Since(startTime).Seconds()
    44  	em.AddMetric("uptime_msec", metrics.NewFloat(timeSince*1000))
    45  
    46  	// GC memory stats
    47  	em.AddMetric("gc_time_msec", metrics.NewFloat(float64(m.PauseTotalNs)/1e6))
    48  	em.AddMetric("mallocs", metrics.NewInt(int64(m.Mallocs)))
    49  	em.AddMetric("frees", metrics.NewInt(int64(m.Frees)))
    50  
    51  	dataChan <- em
    52  	l.Debug(em.String())
    53  }
    54  
    55  // gaugeRuntimeVars exports gauge runtime stats, stats that represent the
    56  // current state and may go up or down. These stats are exported as GAUGE
    57  // EventMetrics.
    58  func gaugeRuntimeVars(dataChan chan *metrics.EventMetrics, ts time.Time, m *runtime.MemStats, l *logger.Logger) {
    59  	em := metrics.NewEventMetrics(ts).
    60  		AddLabel("ptype", "sysvars").
    61  		AddLabel("probe", "sysvars")
    62  	em.Kind = metrics.GAUGE
    63  
    64  	// Number of goroutines
    65  	em.AddMetric("goroutines", metrics.NewInt(int64(runtime.NumGoroutine())))
    66  	// Overall memory being used by the Go runtime (in bytes).
    67  	em.AddMetric("mem_stats_sys_bytes", metrics.NewInt(int64(m.Sys)))
    68  
    69  	dataChan <- em
    70  	l.Debug(em.String())
    71  }