Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repository cardano-haskell-packages
-- you need to run if you change them
index-state:
, hackage.haskell.org 2025-06-24T21:06:59Z
, cardano-haskell-packages 2025-09-18T12:21:32Z
, cardano-haskell-packages 2025-10-07T11:20:00Z

packages:
cardano-node
Expand Down
15 changes: 9 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
cardano-automation = {
url = "github:input-output-hk/cardano-automation";
inputs = {
hackageNix.follows = "hackageNix";
haskellNix.follows = "haskellNix";
nixpkgs.follows = "nixpkgs";
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Cardano.Logging (metricsFormatter)
import Cardano.Logging.Configuration (configureTracers)
import Cardano.Logging.Prometheus.TCPServer (runPrometheusSimple)
import Cardano.Logging.Trace (traceWith)
import Cardano.Logging.Tracer.EKG (ekgTracer)
import Cardano.Logging.Types

import Control.Concurrent (threadDelay)
import Control.Monad (unless)
import Data.Aeson
import qualified Data.Map.Internal as Map
import Data.Text (pack)
import Network.HTTP.Client (defaultManagerSettings, newManager)
import Network.HTTP.PrometheusTracker (scrapeOnce)
import Network.HTTP.PrometheusTracker.Types (MetricsMap (MM), MetricsValue (MVDouble))
import System.Exit (die, exitSuccess)
import System.Metrics (newStore)

newtype Measure = Measure Int

instance LogFormatting Measure where
forMachine _dtal (Measure count) =
mconcat
[ "count" .= String (pack $ show count)
]
asMetrics (Measure count) =
[ DoubleM "measure" (fromIntegral count)]

instance MetaTrace Measure where
namespaceFor (Measure _count) = Namespace [] ["Count"]
severityFor (Namespace [] ["Count"]) _ = Just Info
privacyFor (Namespace [] ["Count"]) _ = Just Public
documentFor (Namespace [] ["Count"]) = Just "A counter"
metricsDocFor (Namespace [] ["Count"]) =
[("count", "an integer")]
allNamespaces = [Namespace [] ["Count"]]

{- Thread #1:
- Run the prometheus simple server
- Trace a metric
- Spawn Thread #2
- Wait

Thread #2:
- Scrape the metrics
- Ensure that we see the expected metric and its value in the list
-}
main :: IO ()
main = do
store <- newStore
let host = "localhost"
let port = 9090
runPrometheusSimple store (True, Just host, port) >>= handleSpawn
pretracer <- ekgTracer emptyTraceConfig store
let tracer = metricsFormatter pretracer :: Trace IO Measure
confState <- emptyConfigReflection
configureTracers confState emptyTraceConfig [tracer]
traceWith tracer (Measure 42)
manager <- newManager defaultManagerSettings
_ <- threadDelay (3 * 1000000)
MM metricsMap <- scrapeOnce manager ("http://" <> host <> ":" <> show port <> "/metrics")
MVDouble value <- maybe (die "'measure' metric not found in the scape list") pure (Map.lookup "measure" metricsMap)
unless (value == 42) $ die ("Unexpected value: " <> show value)
putStrLn "Got correct metric value ✔" where

handleSpawn :: Maybe String -> IO ()
handleSpawn Nothing = pure ()
handleSpawn (Just err) = do
putStrLn $ "Couldn't spawn prometheus-simple server!\n" <> err
exitSuccess
35 changes: 29 additions & 6 deletions trace-dispatcher/trace-dispatcher.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ library
else
build-depends: unix

test-suite trace-dispatcher-prometheus-simple-test
import: project-config
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: trace-dispatcher-prometheus-simple-test.hs
build-depends: base >=4.12 && <5
, aeson
, cardano-prometheus-tracker
, containers
, ekg-core
, http-client
, text
, trace-dispatcher

ghc-options:
-threaded
-Wunused-packages

if !(os(linux) || os(darwin) || os(freebsd))
buildable: False

test-suite trace-dispatcher-test
import: project-config
Expand All @@ -109,35 +129,38 @@ test-suite trace-dispatcher-test
Cardano.Logging.Test.Config
Cardano.Logging.Test.Tracer
Cardano.Logging.Test.Script
Cardano.Logging.Test.Unit.TestObjects
Cardano.Logging.Test.Unit.Aggregation
Cardano.Logging.Test.Unit.Trivial
Cardano.Logging.Test.Unit.Routing
Cardano.Logging.Test.Unit.EKG
Cardano.Logging.Test.Unit.Configuration
Cardano.Logging.Test.Unit.DataPoint
Cardano.Logging.Test.Unit.FrequencyLimiting
Cardano.Logging.Test.Unit.Documentation
Cardano.Logging.Test.Unit.EKG
Cardano.Logging.Test.Unit.FrequencyLimiting
Cardano.Logging.Test.Unit.Routing
Cardano.Logging.Test.Unit.TestObjects
Cardano.Logging.Test.Unit.Trivial

build-depends: base >=4.12 && <5
, aeson
, async
, bytestring
, containers
, deepseq
, ekg-core
, generic-data
, hostname
, text
, stm
, tasty
, tasty-hunit
, tasty-quickcheck
, text
, time
, trace-dispatcher
, unordered-containers
, utf8-string
, unix-compat
, yaml
, QuickCheck
, cardano-prometheus-tracker


benchmark trace-dispatcher-bench
Expand Down
Loading