ridley-extras (empty) → 0.1.0.0
raw patch · 6 files changed
+148/−0 lines, 6 filesdep +basedep +ekg-prometheus-adapterdep +microlenssetup-changed
Dependencies added: base, ekg-prometheus-adapter, microlens, mtl, prometheus, ridley, ridley-extras, shelly, text, transformers
Files
- LICENSE +30/−0
- README.md +7/−0
- Setup.hs +2/−0
- ridley-extras.cabal +48/−0
- src/System/Metrics/Prometheus/Ridley/Metrics/FD.hs +59/−0
- test/Spec.hs +2/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,7 @@+# ridley-extras++A collection of useful `CustomMetric` which don't belong to the main `ridley` package.++## What's included++* Monitor open file descriptors;
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ ridley-extras.cabal view
@@ -0,0 +1,48 @@+name: ridley-extras+version: 0.1.0.0+synopsis: Handy metrics that doesn't belong to ridley.+-- description:+homepage: https://github.com/iconnect/ridley/ridley-extras#readme+license: BSD3+license-file: LICENSE+author: Author name here+maintainer: example@example.com+copyright: 2017 Author name here+category: Web+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++flag lib-Werror+ manual: True+ default: False+ description: Enable -Werror++library+ hs-source-dirs: src+ exposed-modules: System.Metrics.Prometheus.Ridley.Metrics.FD+ build-depends: base >= 4.7 && < 5,+ text,+ prometheus < 0.5.0.0,+ shelly < 1.7.0.0,+ microlens,+ ekg-prometheus-adapter < 0.2.0.0,+ mtl,+ transformers,+ ridley < 0.4.0.0+ if flag(lib-Werror)+ ghc-options: -Wall -Werror+ default-language: Haskell2010++test-suite ridley-extras-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , ridley-extras+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/githubuser/ridley-extras
+ src/System/Metrics/Prometheus/Ridley/Metrics/FD.hs view
@@ -0,0 +1,59 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE CPP #-}+module System.Metrics.Prometheus.Ridley.Metrics.FD where++import Control.Monad.IO.Class+import Data.Maybe (fromMaybe)+import Data.Monoid+import qualified Data.Text as T+import Lens.Micro+import Shelly+import qualified System.Metrics.Prometheus.Metric.Gauge as P+import qualified System.Metrics.Prometheus.RegistryT as P+import System.Metrics.Prometheus.Ridley.Types+import System.Posix.Types (ProcessID)+import System.Remote.Monitoring.Prometheus (labels)+import Text.Read (readMaybe)++--------------------------------------------------------------------------------+getOpenFD_unix :: ProcessID -> IO Double+getOpenFD_unix pid = do+ rawOutput <- shelly $ silently $ escaping False $+ T.strip <$> run "ls" ["-l", "/proc/" <> T.pack (show pid) <> "/fd", "|"+ ,"wc", "-l"+ ]+ return $ fromMaybe 0.0 (readMaybe . T.unpack $ rawOutput)++--------------------------------------------------------------------------------+getOpenFD_darwin :: ProcessID -> IO Double+getOpenFD_darwin pid = do+ rawOutput <- shelly $ silently $ escaping False $+ T.strip <$> run "lsof" ["-p", T.pack (show pid), "|"+ ,"wc", "-l"+ ]+ return $ fromMaybe 0.0 (readMaybe . T.unpack $ rawOutput)++--------------------------------------------------------------------------------+updateOpenFD :: ProcessID -> P.Gauge -> Bool -> IO ()+updateOpenFD pid gauge _ = do+#ifdef darwin_HOST_OS+ openFd <- getOpenFD_darwin pid+#else+ openFd <- getOpenFD_unix pid+#endif+ P.set openFd gauge++--------------------------------------------------------------------------------+-- | Monitors the number of open file descriptors for a given `ProcessID`.+processOpenFD :: MonadIO m+ => ProcessID+ -> RidleyOptions+ -> P.RegistryT m RidleyMetricHandler+processOpenFD pid opts = do+ let popts = opts ^. prometheusOptions+ openFD <- P.registerGauge "process_open_fd" (popts ^. labels)+ return RidleyMetricHandler {+ metric = openFD+ , updateMetric = updateOpenFD pid+ , flush = False+ }
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"