-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathExampleV3.hs
More file actions
72 lines (54 loc) · 1.74 KB
/
Copy pathExampleV3.hs
File metadata and controls
72 lines (54 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoFieldSelectors #-}
import Control.Concurrent (threadDelay)
import Control.Monad (forever)
import Data.Hashable (Hashable)
import GHC.Generics (Generic)
import qualified Prometheus.V3 as Prom
import qualified Prometheus.V3.Metric.Counter as Counter
type Status = Int
data Method = GET | POST
deriving (Eq, Generic, Hashable)
instance Prom.IsLabelValue Method where
toLabelValue = \case
GET -> "GET"
POST -> "POST"
fooCounter :: Prom.Counter
fooCounter = Counter.register "foo_total" "Number of foos"
{-# OPAQUE fooCounter #-}
requestsCounter :: Prom.Labelled (Method, Status) Prom.Counter
requestsCounter =
Prom.register
. Prom.withLabels ("method", "status") labels
$ Counter.new "requests_total" "Number of requests"
where
labels =
[ (method, status)
| method <- [GET, POST]
, status <- [200, 404, 500]
]
{-# OPAQUE requestsCounter #-}
data Request = Request
{ method :: Method
}
myHandler :: Request -> IO ()
myHandler req = do
status <- pure 200
Counter.inc fooCounter
Counter.inc (Counter.labels (req.method, status) requestsCounter)
-- Cache it, for a hot loop
c <- Counter.labels (req.method, status) requestsCounter
Counter.inc c
main :: IO ()
main = do
Prom.serveMetrics 9090 -- Serves metrics on separate thread
myHandler Request{method = GET}
myHandler Request{method = GET}
putStrLn ">>> Server running."
putStrLn ">>> Run `curl localhost:9090` to see metrics"
putStrLn ">>> Press Ctrl-C to exit"
forever $ threadDelay 10000000