Skip to content

Commit 63e703b

Browse files
committed
First running testcase
1 parent 7927f6f commit 63e703b

File tree

4 files changed

+128
-15
lines changed

4 files changed

+128
-15
lines changed

cabal.project

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ packages:
3232
trace-resources
3333
trace-forward
3434

35+
source-repository-package
36+
type: git
37+
location: https://github.com/jutaro/cardano-prometheus-tracker.git
38+
tag: af73c6217850ac6bf07ad26d8faa985f391eddf3
39+
3540
-- Needed when cross compiling
3641
extra-packages: alex
3742

trace-dispatcher/src/Cardano/Logging/Prometheus/TCPServer.hs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
-- | Run a simple Prometheus TCP server, responding *only* to the '/metrics' URL with current Node metrics
2-
module Cardano.Logging.Prometheus.TCPServer (runPrometheusSimple) where
2+
module Cardano.Logging.Prometheus.TCPServer (spawnPrometheusSimple, runPrometheusSimple) where
33

44
import Cardano.Logging.Prometheus.Exposition (renderExpositionFromSample)
55
import Cardano.Logging.Prometheus.NetworkRun
66

7-
import Control.Concurrent.Async (async, link)
7+
import Control.Concurrent.Async (Async, async, link)
88
import qualified Control.Exception as E
99
import Control.Monad (when)
1010
import Data.ByteString (ByteString)
1111
import Data.ByteString.Builder
1212
import qualified Data.ByteString.Char8 as BC
13+
import Data.Functor (($>))
1314
import Data.Int (Int64)
1415
import Data.List (find, intersperse)
1516
import Data.Text.Lazy (Text)
@@ -23,16 +24,22 @@ import System.Metrics as EKG (Store, sampleAll)
2324
import System.Posix.Types (EpochTime)
2425
import System.PosixCompat.Time (epochTime)
2526

26-
27-
-- Will provide a 'Just errormessage' iff creating the Prometheus server failed
28-
runPrometheusSimple :: EKG.Store -> (Bool, Maybe HostName, PortNumber) -> IO (Maybe String)
29-
runPrometheusSimple ekgStore (noSuffixes, mHost, portNo) =
27+
spawnPrometheusSimple :: EKG.Store -> (Bool, Maybe HostName, PortNumber) -> IO (Either String (Async ()))
28+
spawnPrometheusSimple ekgStore (noSuffixes, mHost, portNo) =
3029
E.try createRunner >>= \case
31-
Left (E.SomeException e) -> pure (Just $ E.displayException e)
32-
Right runner -> async runner >>= link >> pure Nothing
30+
Left (E.SomeException e) -> pure (Left $ E.displayException e)
31+
Right runner -> Right <$> async runner
3332
where
3433
getCurrentExposition = renderExpositionFromSample noSuffixes <$> sampleAll ekgStore
35-
createRunner = mkTCPServerRunner (defaultRunParams "PrometheusSimple") mHost portNo (serveAccepted getCurrentExposition)
34+
createRunner =
35+
putStrLn ("Port Number: " <> show portNo) >>
36+
mkTCPServerRunner (defaultRunParams "PrometheusSimple") mHost portNo (serveAccepted getCurrentExposition)
37+
38+
-- Will provide a 'Just errormessage' iff creating the Prometheus server failed
39+
runPrometheusSimple :: EKG.Store -> (Bool, Maybe HostName, PortNumber) -> IO (Maybe String)
40+
runPrometheusSimple ekgStore args = spawnPrometheusSimple ekgStore args >>= \case
41+
Left err -> pure $ Just err
42+
Right as -> link as $> Nothing
3643

3744
-- serves an incoming connection; will release socket upon remote close, inactivity timeout or runRecvMaxSize bytes received
3845
serveAccepted :: IO Text -> TimeoutServer ()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Cardano.Logging (metricsFormatter)
2+
import Cardano.Logging.Configuration (configureTracers)
3+
import Cardano.Logging.Prometheus.TCPServer (spawnPrometheusSimple)
4+
import Cardano.Logging.Trace (traceWith)
5+
import Cardano.Logging.Tracer.EKG (ekgTracer)
6+
import Cardano.Logging.Types
7+
8+
import Control.Concurrent (threadDelay)
9+
import Control.Concurrent.Async (cancel)
10+
import Control.Monad (unless)
11+
import Data.Aeson
12+
import qualified Data.Map.Internal as Map
13+
import Data.Text (pack)
14+
import Network.HTTP.Client (defaultManagerSettings, newManager)
15+
import Network.HTTP.PrometheusTracker (scrapeOnce)
16+
import Network.HTTP.PrometheusTracker.Types (MetricsMap (MM), MetricsValue (MVDouble))
17+
import System.Exit (die)
18+
import System.Metrics (newStore)
19+
import System.Posix.Signals
20+
21+
newtype Measure = Measure Int
22+
23+
instance LogFormatting Measure where
24+
forMachine _dtal (Measure count) =
25+
mconcat
26+
[ "count" .= String (pack $ show count)
27+
]
28+
asMetrics (Measure count) =
29+
[ DoubleM "measure" (fromIntegral count)]
30+
31+
instance MetaTrace Measure where
32+
namespaceFor (Measure _count) = Namespace [] ["Count"]
33+
severityFor (Namespace [] ["Count"]) _ = Just Info
34+
privacyFor (Namespace [] ["Count"]) _ = Just Public
35+
documentFor (Namespace [] ["Count"]) = Just "A counter"
36+
metricsDocFor (Namespace [] ["Count"]) =
37+
[("count", "an integer")]
38+
allNamespaces = [Namespace [] ["Count"]]
39+
40+
{- Thread #1:
41+
- Run the prometheus simple server
42+
- Trace a metric
43+
- Spawn Thread #2
44+
- Wait
45+
46+
Thread #2:
47+
- Scape the metrics
48+
- Ensure that we see the expected metric and its value in the list
49+
-}
50+
main :: IO ()
51+
main = do
52+
store <- newStore
53+
let host = "localhost"
54+
let port = 9090
55+
Right metricsServerThread <- spawnPrometheusSimple store (True, Just host, port)
56+
pretracer <- ekgTracer emptyTraceConfig store
57+
let tracer = metricsFormatter pretracer :: Trace IO Measure
58+
confState <- emptyConfigReflection
59+
configureTracers confState emptyTraceConfig [tracer]
60+
traceWith tracer (Measure 42)
61+
_ <- installHandler sigTERM (Catch (cancel metricsServerThread)) Nothing
62+
manager <- newManager defaultManagerSettings
63+
_ <- threadDelay (3 * 1000000)
64+
MM metricsMap <- scrapeOnce manager ("http://" <> host <> ":" <> show port <> "/metrics")
65+
MVDouble value <- maybe (die "'measure' metric not found in the scape list") pure (Map.lookup "measure" metricsMap)
66+
unless (value == 42) $ die ("Unexpected value: " <> show value)
67+
putStrLn "Got correct metric value ✔"
68+
cancel metricsServerThread

trace-dispatcher/trace-dispatcher.cabal

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ library
9898
else
9999
build-depends: unix
100100

101+
test-suite trace-dispatcher-prometheus-simple-test
102+
import: project-config
103+
type: exitcode-stdio-1.0
104+
hs-source-dirs: test
105+
main-is: trace-dispatcher-prometheus-simple-test.hs
106+
build-depends: base >=4.12 && <5
107+
, aeson
108+
, async
109+
, bytestring
110+
, containers
111+
, deepseq
112+
, ekg-core
113+
, generic-data
114+
, hostname
115+
, http-client == 0.7.19
116+
, stm
117+
, tasty
118+
, tasty-hunit
119+
, tasty-quickcheck
120+
, text
121+
, time
122+
, trace-dispatcher
123+
, unordered-containers
124+
, utf8-string
125+
, unix
126+
, yaml
127+
, QuickCheck
128+
, cardano-prometheus-tracker
101129

102130
test-suite trace-dispatcher-test
103131
import: project-config
@@ -109,35 +137,40 @@ test-suite trace-dispatcher-test
109137
Cardano.Logging.Test.Config
110138
Cardano.Logging.Test.Tracer
111139
Cardano.Logging.Test.Script
112-
Cardano.Logging.Test.Unit.TestObjects
113140
Cardano.Logging.Test.Unit.Aggregation
114-
Cardano.Logging.Test.Unit.Trivial
115-
Cardano.Logging.Test.Unit.Routing
116-
Cardano.Logging.Test.Unit.EKG
117141
Cardano.Logging.Test.Unit.Configuration
118142
Cardano.Logging.Test.Unit.DataPoint
119-
Cardano.Logging.Test.Unit.FrequencyLimiting
120143
Cardano.Logging.Test.Unit.Documentation
144+
Cardano.Logging.Test.Unit.EKG
145+
Cardano.Logging.Test.Unit.FrequencyLimiting
146+
Cardano.Logging.Test.Unit.PrometheusSimple
147+
Cardano.Logging.Test.Unit.Routing
148+
Cardano.Logging.Test.Unit.TestObjects
149+
Cardano.Logging.Test.Unit.Trivial
121150

122151
build-depends: base >=4.12 && <5
123152
, aeson
153+
, async
124154
, bytestring
125155
, containers
126156
, deepseq
127157
, ekg-core
128158
, generic-data
129159
, hostname
130-
, text
160+
, http-client == 0.7.19
131161
, stm
132162
, tasty
133163
, tasty-hunit
134164
, tasty-quickcheck
165+
, text
135166
, time
136167
, trace-dispatcher
137168
, unordered-containers
138169
, utf8-string
170+
, unix
139171
, yaml
140172
, QuickCheck
173+
, cardano-prometheus-tracker
141174

142175

143176
benchmark trace-dispatcher-bench

0 commit comments

Comments
 (0)