2016-12-27 12:46:25 +01:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-10-03 13:35:24 +02:00
|
|
|
//go:build (darwin || linux || openbsd) && !nomeminfo
|
2018-01-04 12:23:26 +01:00
|
|
|
// +build darwin linux openbsd
|
2016-12-27 12:46:25 +01:00
|
|
|
// +build !nomeminfo
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-07 22:27:52 +02:00
|
|
|
"strings"
|
2016-12-27 12:46:25 +01:00
|
|
|
|
2020-11-14 11:53:51 +01:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2016-12-27 12:46:25 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
memInfoSubsystem = "memory"
|
|
|
|
)
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
type meminfoCollector struct {
|
|
|
|
logger log.Logger
|
|
|
|
}
|
2016-12-27 12:46:25 +01:00
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("meminfo", defaultEnabled, NewMeminfoCollector)
|
2016-12-27 12:46:25 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 18:14:17 +01:00
|
|
|
// NewMeminfoCollector returns a new Collector exposing memory stats.
|
2019-12-31 17:19:37 +01:00
|
|
|
func NewMeminfoCollector(logger log.Logger) (Collector, error) {
|
|
|
|
return &meminfoCollector{logger}, nil
|
2016-12-27 12:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update calls (*meminfoCollector).getMemInfo to get the platform specific
|
|
|
|
// memory metrics.
|
2017-02-28 19:47:20 +01:00
|
|
|
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) error {
|
2018-09-07 22:27:52 +02:00
|
|
|
var metricType prometheus.ValueType
|
2016-12-27 12:46:25 +01:00
|
|
|
memInfo, err := c.getMemInfo()
|
|
|
|
if err != nil {
|
2020-06-15 22:27:14 +02:00
|
|
|
return fmt.Errorf("couldn't get meminfo: %w", err)
|
2016-12-27 12:46:25 +01:00
|
|
|
}
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Debug(c.logger).Log("msg", "Set node_mem", "memInfo", memInfo)
|
2016-12-27 12:46:25 +01:00
|
|
|
for k, v := range memInfo {
|
2018-09-07 22:27:52 +02:00
|
|
|
if strings.HasSuffix(k, "_total") {
|
|
|
|
metricType = prometheus.CounterValue
|
|
|
|
} else {
|
|
|
|
metricType = prometheus.GaugeValue
|
|
|
|
}
|
2016-12-27 12:46:25 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, memInfoSubsystem, k),
|
2016-12-27 12:46:25 +01:00
|
|
|
fmt.Sprintf("Memory information field %s.", k),
|
|
|
|
nil, nil,
|
|
|
|
),
|
2018-09-07 22:27:52 +02:00
|
|
|
metricType, v,
|
2016-12-27 12:46:25 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|