package metrics

import (
	"context"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/tidbcloud/pkgv2/goroutine"
)

var (
	Uptime = prometheus.NewDesc(
		prometheus.BuildFQName("dbaas", "system", "uptime"),
		"dbaas Uptime",
		[]string{},
		nil)
)

type UptimeCollector struct {
	readyTime int64
}

func NewUptimeCollector(readyTime int64) *UptimeCollector {
	return &UptimeCollector{readyTime: readyTime}
}

func (c *UptimeCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- Uptime
}

func (c *UptimeCollector) Collect(ch chan<- prometheus.Metric) {
	defer goroutine.Recover(context.TODO())

	ch <- prometheus.MustNewConstMetric(Uptime, prometheus.GaugeValue, float64(time.Now().Unix()-c.readyTime))
}
