2015-09-26 17:36:40 +02: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 !notime
|
2014-07-28 12:37:01 +02:00
|
|
|
// +build !notime
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-11-14 11:53:51 +01:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2014-07-28 12:37:01 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type timeCollector struct {
|
2021-11-12 11:45:31 +01:00
|
|
|
now typedDesc
|
|
|
|
zone typedDesc
|
|
|
|
clocksourcesAvailable typedDesc
|
|
|
|
clocksourceCurrent typedDesc
|
|
|
|
logger log.Logger
|
2014-07-28 12:37:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("time", defaultEnabled, NewTimeCollector)
|
2014-07-28 12:37:01 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
// NewTimeCollector returns a new Collector exposing the current system time in
|
|
|
|
// seconds since epoch.
|
2019-12-31 17:19:37 +01:00
|
|
|
func NewTimeCollector(logger log.Logger) (Collector, error) {
|
2021-06-20 09:17:16 +02:00
|
|
|
const subsystem = "time"
|
2014-11-25 03:00:17 +01:00
|
|
|
return &timeCollector{
|
2021-11-12 11:45:31 +01:00
|
|
|
now: typedDesc{prometheus.NewDesc(
|
2021-06-20 09:17:16 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "seconds"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"System time in seconds since epoch (1970).",
|
|
|
|
nil, nil,
|
2021-11-12 11:45:31 +01:00
|
|
|
), prometheus.GaugeValue},
|
|
|
|
zone: typedDesc{prometheus.NewDesc(
|
2021-06-20 09:17:16 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "zone_offset_seconds"),
|
|
|
|
"System time zone offset in seconds.",
|
|
|
|
[]string{"time_zone"}, nil,
|
2021-11-12 11:45:31 +01:00
|
|
|
), prometheus.GaugeValue},
|
|
|
|
clocksourcesAvailable: typedDesc{prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, subsystem, "clocksource_available_info"),
|
|
|
|
"Available clocksources read from '/sys/devices/system/clocksource'.",
|
|
|
|
[]string{"device", "clocksource"}, nil,
|
|
|
|
), prometheus.GaugeValue},
|
|
|
|
clocksourceCurrent: typedDesc{prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, subsystem, "clocksource_current_info"),
|
|
|
|
"Current clocksource read from '/sys/devices/system/clocksource'.",
|
|
|
|
[]string{"device", "clocksource"}, nil,
|
|
|
|
), prometheus.GaugeValue},
|
2019-12-31 17:19:37 +01:00
|
|
|
logger: logger,
|
2014-11-25 03:00:17 +01:00
|
|
|
}, nil
|
2014-07-28 12:37:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:21:31 +01:00
|
|
|
func (c *timeCollector) Update(ch chan<- prometheus.Metric) error {
|
2021-06-20 09:17:16 +02:00
|
|
|
now := time.Now()
|
|
|
|
nowSec := float64(now.UnixNano()) / 1e9
|
|
|
|
zone, zoneOffset := now.Zone()
|
|
|
|
|
|
|
|
level.Debug(c.logger).Log("msg", "Return time", "now", nowSec)
|
2021-11-12 11:45:31 +01:00
|
|
|
ch <- c.now.mustNewConstMetric(nowSec)
|
2021-06-20 09:17:16 +02:00
|
|
|
level.Debug(c.logger).Log("msg", "Zone offset", "offset", zoneOffset, "time_zone", zone)
|
2021-11-12 11:45:31 +01:00
|
|
|
ch <- c.zone.mustNewConstMetric(float64(zoneOffset), zone)
|
|
|
|
return c.update(ch)
|
2014-07-28 12:37:01 +02:00
|
|
|
}
|