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