11{-# LANGUAGE AllowAmbiguousTypes #-}
2+ {-# LANGUAGE ConstraintKinds #-}
3+ {-# LANGUAGE FlexibleContexts #-}
24{-# LANGUAGE FlexibleInstances #-}
35{-# LANGUAGE ImportQualifiedPost #-}
46{-# LANGUAGE NamedFieldPuns #-}
57{-# LANGUAGE ScopedTypeVariables #-}
8+ {-# LANGUAGE TypeFamilies #-}
9+ {-# LANGUAGE TypeOperators #-}
10+ {-# LANGUAGE UndecidableInstances #-}
611{-# LANGUAGE NoFieldSelectors #-}
712-- Remove after inlining V2.Counter
813{-# OPTIONS_GHC -Wno-orphans #-}
914
1015module Prometheus.V3.Metric.Counter (
1116 V2. Counter ,
17+ V2. Counter' ,
1218 register ,
1319 new ,
1420
1521 -- * Methods
1622 IsCounter ,
23+ IsNumCounter ,
1724 inc ,
1825 add ,
1926 reset ,
@@ -40,12 +47,16 @@ import UnliftIO.Exception (Exception, withException)
4047
4148
4249-- | An alias for @'Registry.register' (new ...)@.
43- register :: MetricName -> Description -> V2. Counter
50+ register ::
51+ (Num a , Ord a , ToSampleValue a , V2. HasCounterBackend a ) =>
52+ MetricName -> Description -> (V2. Counter' a )
4453register name description = Registry. register $ new name description
4554{-# INLINE register #-}
4655
4756
48- new :: MetricName -> Description -> Metric V2. Counter
57+ new ::
58+ (Num a , V2. HasCounterBackend a ) =>
59+ MetricName -> Description -> Metric (V2. Counter' a )
4960new name description =
5061 Metric
5162 { name
@@ -54,42 +65,54 @@ new name description =
5465 }
5566
5667
57- instance IsMetric V2. Counter where
68+ instance ( SampleValueNum a , Ord a , V2. HasCounterBackend a ) => IsMetric ( V2. Counter' a ) where
5869 getMetricType _ = MetricTypeCounter
5970 getMetricSamples counter = do
6071 n <- sample counter
6172 pure [defaultSample{value = toSampleValue n}]
6273
6374
6475class IsCounter c where
65- getCounter :: c -> IO V2. Counter
66- instance IsCounter V2. Counter where
76+ type CounterElem c
77+ getCounter :: c -> IO (V2. Counter' (CounterElem c ))
78+ instance IsCounter (V2. Counter' a ) where
79+ type CounterElem (V2. Counter' a ) = a
6780 getCounter = pure
68- instance IsCounter (IO V2. Counter ) where
81+ instance IsCounter (IO (V2. Counter' a )) where
82+ type CounterElem (IO (V2. Counter' a )) = a
6983 getCounter = id
7084
7185
72- inc :: (IsCounter c , MonadIO m ) => c -> m ()
86+ type IsNumCounter c a =
87+ ( IsCounter c
88+ , a ~ CounterElem c
89+ , V2. HasCounterBackend a
90+ , Num a
91+ , Ord a
92+ )
93+
94+
95+ inc :: (IsNumCounter c a , MonadIO m ) => c -> m ()
7396inc c = liftIO $ V2. inc =<< getCounter c
7497{-# INLINE inc #-}
7598
7699
77- add :: (IsCounter c , MonadIO m ) => c -> Int -> m ()
100+ add :: (IsNumCounter c a , MonadIO m ) => c -> a -> m ()
78101add c n = liftIO $ V2. add n =<< getCounter c
79102{-# INLINE add #-}
80103
81104
82- reset :: (IsCounter c , MonadIO m ) => c -> m ()
105+ reset :: (IsNumCounter c a , MonadIO m ) => c -> m ()
83106reset c = liftIO $ V2. set 0 =<< getCounter c
84107{-# INLINE reset #-}
85108
86109
87- sample :: (IsCounter c , MonadIO m ) => c -> m Int
110+ sample :: (IsNumCounter c a , MonadIO m ) => c -> m a
88111sample c = liftIO $ fmap V2. unCounterSample . V2. sample =<< getCounter c
89112{-# INLINE sample #-}
90113
91114
92- addAndSample :: (IsCounter c , MonadIO m ) => c -> Int -> m Int
115+ addAndSample :: (IsNumCounter c a , MonadIO m ) => c -> a -> m a
93116addAndSample c n = liftIO $ fmap V2. unCounterSample . V2. addAndSample n =<< getCounter c
94117{-# INLINE addAndSample #-}
95118
@@ -99,7 +122,7 @@ addAndSample c n = liftIO $ fmap V2.unCounterSample . V2.addAndSample n =<< getC
99122-- | Count the number of times a particular exception is thrown.
100123countExceptions ::
101124 forall e c m a .
102- (IsCounter c , Exception e , MonadUnliftIO m ) =>
125+ (IsNumCounter c a , Exception e , MonadUnliftIO m ) =>
103126 c -> m a -> m a
104127countExceptions c = countExceptionsWhere c (\ (_ :: e ) -> True )
105128{-# INLINE countExceptions #-}
@@ -109,7 +132,7 @@ countExceptions c = countExceptionsWhere c (\(_ :: e) -> True)
109132-- include the exception in the count.
110133countExceptionsWhere ::
111134 forall e c m a .
112- (IsCounter c , Exception e , MonadUnliftIO m ) =>
135+ (IsNumCounter c a , Exception e , MonadUnliftIO m ) =>
113136 c -> (e -> Bool ) -> m a -> m a
114137countExceptionsWhere c f io = io `withException` \ e -> when (f e) (inc c)
115138{-# INLINE countExceptionsWhere #-}
0 commit comments