1+ {-# LANGUAGE ConstraintKinds #-}
2+ {-# LANGUAGE FlexibleContexts #-}
13{-# LANGUAGE FlexibleInstances #-}
24{-# LANGUAGE ImportQualifiedPost #-}
35{-# LANGUAGE NamedFieldPuns #-}
6+ {-# LANGUAGE TypeFamilies #-}
7+ {-# LANGUAGE TypeOperators #-}
48{-# LANGUAGE NoFieldSelectors #-}
59-- Remove after inlining V2.Gauge
610{-# OPTIONS_GHC -Wno-orphans #-}
@@ -13,6 +17,7 @@ module Prometheus.V3.Metric.Gauge (
1317
1418 -- * Methods
1519 IsGauge ,
20+ IsNumGauge ,
1621 inc ,
1722 add ,
1823 dec ,
@@ -36,22 +41,23 @@ import Prometheus.V3.Metric.Base
3641import Prometheus.V3.Metric.Labelled (labels )
3742import Prometheus.V3.Registry qualified as Registry
3843import Prometheus.V3.Sample
44+ import Prometheus.V3.Utils.FromRealFrac (FromRealFrac , fromRealFrac )
3945import System.Metrics.Prometheus.Metric.Gauge qualified as V2
4046import UnliftIO (MonadUnliftIO )
4147import UnliftIO.Exception (bracket_ )
4248
4349
4450-- | An alias for @'Registry.register' (new ...)@.
45- register :: MetricName -> Description -> V2. Gauge
51+ register :: ( Num a , ToSampleValue a ) => MetricName -> Description -> V2. Gauge a
4652register name description = Registry. register $ new name description
4753{-# INLINE register #-}
4854
4955
50- new :: MetricName -> Description -> Metric V2. Gauge
56+ new :: ( Num a ) => MetricName -> Description -> Metric ( V2. Gauge a )
5157new name description = newAt name description 0
5258
5359
54- newAt :: MetricName -> Description -> Double -> Metric V2. Gauge
60+ newAt :: ( Num a ) => MetricName -> Description -> a -> Metric ( V2. Gauge a )
5561newAt name description val =
5662 Metric
5763 { name
@@ -63,74 +69,84 @@ newAt name description val =
6369 }
6470
6571
66- instance IsMetric V2. Gauge where
72+ instance ( SampleValueNum a ) => IsMetric ( V2. Gauge a ) where
6773 getMetricType _ = MetricTypeGauge
6874 getMetricSamples gauge = do
6975 n <- sample gauge
7076 pure [defaultSample{value = toSampleValue n}]
7177
7278
7379class IsGauge g where
74- getGauge :: g -> IO V2. Gauge
75- instance IsGauge V2. Gauge where
80+ type GaugeElem g
81+ getGauge :: g -> IO (V2. Gauge (GaugeElem g ))
82+ instance IsGauge (V2. Gauge a ) where
83+ type GaugeElem (V2. Gauge a ) = a
7684 getGauge = pure
77- instance IsGauge (IO V2. Gauge ) where
85+ instance IsGauge (IO (V2. Gauge a )) where
86+ type GaugeElem (IO (V2. Gauge a )) = a
7887 getGauge = id
7988
8089
81- inc :: (IsGauge g , MonadIO m ) => g -> m ()
90+ type IsNumGauge g a = (IsGauge g , a ~ GaugeElem g , Num a )
91+
92+
93+ inc :: (IsNumGauge g a , MonadIO m ) => g -> m ()
8294inc g = liftIO $ V2. inc =<< getGauge g
8395{-# INLINE inc #-}
8496
8597
86- add :: (IsGauge g , MonadIO m ) => g -> Double -> m ()
98+ add :: (IsNumGauge g a , MonadIO m ) => g -> a -> m ()
8799add g x = liftIO $ V2. add x =<< getGauge g
88100{-# INLINE add #-}
89101
90102
91- dec :: (IsGauge g , MonadIO m ) => g -> m ()
103+ dec :: (IsNumGauge g a , MonadIO m ) => g -> m ()
92104dec g = liftIO $ V2. dec =<< getGauge g
93105{-# INLINE dec #-}
94106
95107
96- sub :: (IsGauge g , MonadIO m ) => g -> Double -> m ()
108+ sub :: (IsNumGauge g a , MonadIO m ) => g -> a -> m ()
97109sub g x = liftIO $ V2. sub x =<< getGauge g
98110{-# INLINE sub #-}
99111
100112
101- set :: (IsGauge g , MonadIO m ) => g -> Double -> m ()
113+ set :: (IsNumGauge g a , MonadIO m ) => g -> a -> m ()
102114set g x = liftIO $ V2. set x =<< getGauge g
103115{-# INLINE set #-}
104116
105117
106- sample :: (IsGauge g , MonadIO m ) => g -> m Double
118+ sample :: (IsNumGauge g a , MonadIO m ) => g -> m a
107119sample g = liftIO $ fmap V2. unGaugeSample . V2. sample =<< getGauge g
108120{-# INLINE sample #-}
109121
110122
111- modifyAndSample :: (IsGauge g , MonadIO m ) => g -> (Double -> Double ) -> m Double
123+ modifyAndSample :: (IsNumGauge g a , MonadIO m ) => g -> (a -> a ) -> m a
112124modifyAndSample g f = liftIO $ fmap V2. unGaugeSample . V2. modifyAndSample f =<< getGauge g
113125{-# INLINE modifyAndSample #-}
114126
115127
116128{- ---- Helpers -----}
117129
118130-- | Set to the current number of seconds since the epoch.
119- setToCurrentTime :: (IsGauge g , MonadIO m ) => g -> m ()
131+ setToCurrentTime ::
132+ (IsGauge g , FromRealFrac (GaugeElem g ), MonadIO m ) =>
133+ g -> m ()
120134setToCurrentTime g = do
121135 now <- liftIO getPOSIXTime
122136 g' <- liftIO $ getGauge g
123- set g' (realToFrac now)
137+ set g' (fromRealFrac now)
124138{-# INLINE setToCurrentTime #-}
125139
126140
127141-- | Increment the gauge when the given action is running and decrement when it's exited.
128- trackInProgress :: (IsGauge g , MonadUnliftIO m ) => g -> m a -> m a
142+ trackInProgress :: (IsNumGauge g a , MonadUnliftIO m ) => g -> m a -> m a
129143trackInProgress g = bracket_ (inc g) (dec g)
130144{-# INLINE trackInProgress #-}
131145
132146
133147-- | Set the gauge to the duration of the given action.
134- time :: (IsGauge g , MonadUnliftIO m ) => g -> m a -> m a
135- time g = withDuration (set g)
148+ time ::
149+ (IsGauge g , FromRealFrac (GaugeElem g ), MonadUnliftIO m ) =>
150+ g -> m a -> m a
151+ time g = withDuration (set g . fromRealFrac)
136152{-# INLINE time #-}
0 commit comments