Repository navigation
Add new V3 API following Prometheus spec #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brandonchinn178
wants to merge
11
commits into
bitnomial:master
Choose a base branch
from
brandonchinn178:v3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8c59abd
Add new Prometheus.V3 API
brandonchinn178 9b715d8
Make Gauge polymorphic
brandonchinn178 708f4b3
Make Counter polymorphic
brandonchinn178 00f0ddc
Highlight requirement to use OPAQUE in Counter.register
brandonchinn178 20d7012
Fix histogram bucket initializers
brandonchinn178 9185f6c
Make sample values always Double; render with Fixed
brandonchinn178 ceda89a
Use IORef instead of MVar for Labelled.metricMap
brandonchinn178 e7e7df4
Add bounds to new dependencies
brandonchinn178 a34646c
Escape HELP text
brandonchinn178 60983b0
Error if metric registered multiple times
brandonchinn178 da2a913
Use getMonotonicTime for withDuration
brandonchinn178 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| -- | A new implementation of the API, following Prometheus's standard for writing | ||
| -- client libraries: https://prometheus.io/docs/instrumenting/writing_clientlibs/ | ||
| -- | ||
| -- When releasing prometheus-3.0: | ||
| -- * Rename this as Prometheus | ||
| -- * Remove old 'System.Metrics.Prometheus' modules | ||
| -- * Update README/Cabal description for new API | ||
| -- * Delete this preamble | ||
| -- | ||
| -- == Usage | ||
| -- | ||
| -- The recommended usage is to initialize the metrics as top-level global | ||
| -- variables in the same file as the functions they're instrumenting. | ||
| -- | ||
| -- Note: globally registered metrics are only registered when it's used for the | ||
| -- first time, due to laziness. | ||
| -- | ||
| -- @ | ||
| -- {-# LANGUAGE OverloadedStrings #-} | ||
| -- | ||
| -- import Prometheus.V3 qualified as Prom | ||
| -- import Prometheus.V3.Metric.Counter qualified as Counter | ||
| -- | ||
| -- fooCounter :: Prom.Counter | ||
| -- fooCounter = Counter.register "foo_total" "Number of foos" | ||
| -- {-# OPAQUE fooCounter #-} | ||
| -- | ||
| -- requestsCounter :: Prom.Labelled 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 #-} | ||
| -- | ||
| -- myHandler req = do | ||
| -- status <- run req | ||
| -- | ||
| -- Counter.inc fooCounter | ||
| -- Counter.inc (Counter.labels (requestMethod req, status) requestsCounter) | ||
| -- | ||
| -- -- Cache it, for a hot loop | ||
| -- c <- Counter.labels (requestMethod req, status) requestsCounter | ||
| -- Counter.inc c | ||
| -- | ||
| -- main :: IO () | ||
| -- main = do | ||
| -- Prom.serveMetrics 9090 -- Serves metrics on separate thread | ||
| -- @ | ||
| -- | ||
| -- Alternatively, you could initialize metrics in IO and pass it to your | ||
| -- functions as normal (e.g. Reader monad, as a function arg, etc.): | ||
| -- | ||
| -- @ | ||
| -- registry <- Prom.newRegistry | ||
| -- fooCounter <- Prom.registerTo registry $ Counter.new "foo_counter" "" | ||
| -- @ | ||
| -- | ||
| -- For advanced use-cases, you can also construct your own | ||
| -- 'Prometheus.V3.Collector.Collector' manually and register it to the registry. | ||
| module Prometheus.V3 ( | ||
| -- * Metric types | ||
| Metric, | ||
| Counter, | ||
| Gauge, | ||
| Histogram, | ||
|
|
||
| -- * Labels | ||
| Labelled, | ||
| withLabels, | ||
| IsLabelValue (..), | ||
|
|
||
| -- * Registry | ||
| globalRegistry, | ||
| registerTo, | ||
| unregisterFrom, | ||
| register, | ||
|
|
||
| -- * Serving metrics | ||
| serveMetrics, | ||
| ) where | ||
|
|
||
| import Prometheus.V3.Http.Serve | ||
| import Prometheus.V3.Label | ||
| import Prometheus.V3.Metric.Base | ||
| import Prometheus.V3.Metric.Counter (Counter) | ||
| import Prometheus.V3.Metric.Gauge (Gauge) | ||
| import Prometheus.V3.Metric.Histogram (Histogram) | ||
| import Prometheus.V3.Metric.Labelled | ||
| import Prometheus.V3.Registry | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| {-# LANGUAGE NamedFieldPuns #-} | ||
| {-# LANGUAGE OverloadedRecordDot #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
| {-# LANGUAGE TypeFamilies #-} | ||
| {-# LANGUAGE NoFieldSelectors #-} | ||
|
|
||
| module Prometheus.V3.Collector ( | ||
| Collector (..), | ||
|
|
||
| -- * Re-exports | ||
| MetricName, | ||
| Description, | ||
| MetricType (..), | ||
| Sample (..), | ||
| SampleValue, | ||
| defaultSample, | ||
| ToSampleValue (..), | ||
| ) where | ||
|
|
||
| import Prometheus.V3.Metric.Base ( | ||
| Description, | ||
| MetricName, | ||
| MetricType (..), | ||
| ) | ||
| import Prometheus.V3.Sample ( | ||
| Sample (..), | ||
| SampleValue, | ||
| ToSampleValue (..), | ||
| defaultSample, | ||
| ) | ||
|
|
||
|
|
||
| data Collector = Collector | ||
| { name :: MetricName | ||
| , description :: Description | ||
| , type_ :: MetricType | ||
| , getSamples :: IO [Sample] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.