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