2016-12-27 03:46:25 -08: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.
|
|
|
|
|
2023-01-03 11:16:40 -08:00
|
|
|
//go:build (darwin || linux || openbsd || netbsd) && !nomeminfo
|
|
|
|
// +build darwin linux openbsd netbsd
|
2016-12-27 03:46:25 -08:00
|
|
|
// +build !nomeminfo
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-07 13:27:52 -07:00
|
|
|
"strings"
|
2016-12-27 03:46:25 -08:00
|
|
|
|
2020-11-14 02:53:51 -08:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2016-12-27 03:46:25 -08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
memInfoSubsystem = "memory"
|
|
|
|
)
|
|
|
|
|
2019-12-31 08:19:37 -08:00
|
|
|
type meminfoCollector struct {
|
|
|
|
logger log.Logger
|
|
|
|
}
|
2016-12-27 03:46:25 -08:00
|
|
|
|
|
|
|
func init() {
|
2017-09-28 06:06:26 -07:00
|
|
|
registerCollector("meminfo", defaultEnabled, NewMeminfoCollector)
|
2016-12-27 03:46:25 -08:00
|
|
|
}
|
|
|
|
|
2016-12-27 09:14:17 -08:00
|
|
|
// NewMeminfoCollector returns a new Collector exposing memory stats.
|
2019-12-31 08:19:37 -08:00
|
|
|
func NewMeminfoCollector(logger log.Logger) (Collector, error) {
|
|
|
|
return &meminfoCollector{logger}, nil
|
2016-12-27 03:46:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update calls (*meminfoCollector).getMemInfo to get the platform specific
|
|
|
|
// memory metrics.
|
2017-02-28 10:47:20 -08:00
|
|
|
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) error {
|
2018-09-07 13:27:52 -07:00
|
|
|
var metricType prometheus.ValueType
|
2016-12-27 03:46:25 -08:00
|
|
|
memInfo, err := c.getMemInfo()
|
|
|
|
if err != nil {
|
2020-06-15 13:27:14 -07:00
|
|
|
return fmt.Errorf("couldn't get meminfo: %w", err)
|
2016-12-27 03:46:25 -08:00
|
|
|
}
|
2019-12-31 08:19:37 -08:00
|
|
|
level.Debug(c.logger).Log("msg", "Set node_mem", "memInfo", memInfo)
|
2016-12-27 03:46:25 -08:00
|
|
|
for k, v := range memInfo {
|
2018-09-07 13:27:52 -07:00
|
|
|
if strings.HasSuffix(k, "_total") {
|
|
|
|
metricType = prometheus.CounterValue
|
|
|
|
} else {
|
|
|
|
metricType = prometheus.GaugeValue
|
|
|
|
}
|
2016-12-27 03:46:25 -08:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2017-09-28 06:06:26 -07:00
|
|
|
prometheus.BuildFQName(namespace, memInfoSubsystem, k),
|
2016-12-27 03:46:25 -08:00
|
|
|
fmt.Sprintf("Memory information field %s.", k),
|
|
|
|
nil, nil,
|
|
|
|
),
|
2018-09-07 13:27:52 -07:00
|
|
|
metricType, v,
|
2016-12-27 03:46:25 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|