Skip to content
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

Add prometheus metrics support #30

Open
arianvp opened this issue Feb 3, 2022 · 7 comments
Open

Add prometheus metrics support #30

arianvp opened this issue Feb 3, 2022 · 7 comments
Assignees

Comments

@arianvp
Copy link
Member

arianvp commented Feb 3, 2022

I was able to add prometheus metrics supports with this super small snippet:

import Data.Proxy (Proxy)
import Data.Text (Text)
import qualified Data.Text as Text
import Network.Wai (Application, Request)
import Network.Wai.Middleware.Prometheus (instrumentHandlerValue)
import Servant.Ekg
  ( APIEndpoint (APIEndpoint),
    HasEndpoint (getEndpoint),
  )

-- | A middleware that records latency metrics for all endpoints in 'api'
instrumentServantHandlers :: HasEndpoint api => Proxy api -> Application -> Application
instrumentServantHandlers proxy app = do
  instrumentHandlerValue (makeLabel proxy) app

makeLabel :: HasEndpoint api => Proxy api -> Request -> Text
makeLabel proxy req = case getEndpoint proxy req of
  Nothing -> "<unknown>"
  Just (APIEndpoint pathParts _method) -> "/" <> Text.intercalate "/" pathParts
type GetMetrics = "metrics" :> Get '[PlainText] Text
handleGetMetrics :: Handler Text
handleGetMetrics = do
  Encoding.decodeUtf8With Encoding.lenientDecode <$> Prometheus.exportMetricsAsText

It only depends on the HasEndpoint typeclass. I'm wondering if maybe we should move HasEndpoint into the servant core package to make Prometheus support possible without people having to pull in the ekg dependency? cc @haskell-servant/maintainers

IDK if servant-prometheus deserves its only library or should just be a cookbook with the above code.

@arianvp arianvp self-assigned this Feb 3, 2022
@arianvp
Copy link
Member Author

arianvp commented Mar 1, 2022

Thoughts @haskell-servant/maintainers ?

edit: Oh I already mentioned the group. Sorry for the double ping. that was not my intention

@tchoutri
Copy link

tchoutri commented Mar 1, 2022

eh I guess 26 days is an acceptable period for re-ping ;)

@tchoutri
Copy link

tchoutri commented Mar 1, 2022

@arianvp here are my thoughts:

  1. This overall very good
  2. Yes we must avoid depending on the whole of ekg (especially snap) when providing this prometheus implementation.
  3. Does it need a separate library? Eeeh I'm not convinced, we're not in the JavaScript world and this snippet clearly doesn't cross the Fairbairn threshold. We have two options:
    1. put it in a Cookbook for now, if it grows a lot we can think about a new library.
    2. put it in servant-ekg, and if it grows too much we can think about a new library.

@alpmestan
Copy link

Could your functions be merged into servant-ekg?

@arianvp
Copy link
Member Author

arianvp commented Mar 1, 2022

They could but I'd prefer not to pull in ekg when using prometheus as my functions don't use anything ekg related. Just te HasEndpoint typeclass https://hackage.haskell.org/package/servant-ekg-0.3.1/docs/Servant-Ekg.html#t:HasEndpoint

@tchoutri
Copy link

tchoutri commented Mar 1, 2022

Ah, if HasEndpoint is the only one, maybe we could upstream it outside of servant-ekg.

@worm2fed
Copy link

worm2fed commented Aug 6, 2023

You might be interested
https://github.com/worm2fed/servant-prometheus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants