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 for slot_no #537

Merged
merged 1 commit into from
Mar 8, 2021
Merged
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
7 changes: 6 additions & 1 deletion cardano-db-sync-extended/app/cardano-db-sync-extended.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Cardano.Prelude
import Cardano.Config.Git.Rev (gitRev)

import Cardano.DbSync (runDbSyncNode)
import Cardano.DbSync.Metrics (withMetricSetters)
import Cardano.DbSync.Plugin.Extended (extendedDbSyncNodePlugin)

import Cardano.Sync.Config
Expand All @@ -29,7 +30,11 @@ main = do
cmd <- Opt.execParser opts
case cmd of
CmdVersion -> runVersionCommand
CmdRun params -> runDbSyncNode extendedDbSyncNodePlugin params
CmdRun params -> do
prometheusPort <- dncPrometheusPort <$> readSyncNodeConfig (enpConfigFile params)

withMetricSetters prometheusPort $ \metricsSetters ->
runDbSyncNode metricsSetters extendedDbSyncNodePlugin params

-- -------------------------------------------------------------------------------------------------

Expand Down
7 changes: 6 additions & 1 deletion cardano-db-sync/app/cardano-db-sync.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Cardano.Prelude
import Cardano.Config.Git.Rev (gitRev)

import Cardano.DbSync (defDbSyncNodePlugin, runDbSyncNode)
import Cardano.DbSync.Metrics (withMetricSetters)

import Cardano.Sync.Config
import Cardano.Sync.Config.Types
Expand All @@ -29,7 +30,11 @@ main = do
cmd <- Opt.execParser opts
case cmd of
CmdVersion -> runVersionCommand
CmdRun params -> runDbSyncNode defDbSyncNodePlugin params
CmdRun params -> do
prometheusPort <- dncPrometheusPort <$> readSyncNodeConfig (enpConfigFile params)

withMetricSetters prometheusPort $ \metricsSetters ->
runDbSyncNode metricsSetters defDbSyncNodePlugin params

-- -------------------------------------------------------------------------------------------------

Expand Down
27 changes: 16 additions & 11 deletions cardano-db-sync/src/Cardano/DbSync.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Cardano.Prelude hiding (Nat, option, (%))

import Control.Monad.Trans.Maybe (MaybeT (..))

import Cardano.Api (SlotNo (..))
import Cardano.Slotting.Slot (EpochNo (..), SlotNo (..))

import Cardano.BM.Trace (Trace)

Expand All @@ -37,20 +37,24 @@ import Cardano.DbSync.Plugin.Default (defDbSyncNodePlugin)
import Cardano.DbSync.Rollback (unsafeRollback)
import Cardano.Sync.Database (runDbThread)

import Cardano.Sync (Block (..), SyncDataLayer (..), SyncNodePlugin (..),
import Cardano.Sync (Block (..), MetricSetters, SyncDataLayer (..), SyncNodePlugin (..),
configureLogging, runSyncNode)
import Cardano.Sync.Config.Types (ConfigFile (..), GenesisFile (..), LedgerStateDir (..),
MigrationDir (..), NetworkName (..), SocketPath (..), SyncCommand (..),
SyncNodeParams (..))
import Cardano.Sync.Tracing.ToObjectOrphans ()

import Control.Monad.Extra (whenJust)

import Database.Persist.Postgresql (withPostgresqlConn)

import Database.Persist.Sql (SqlBackend)

import Ouroboros.Network.Block (BlockNo (..))


runDbSyncNode :: (SqlBackend -> SyncNodePlugin) -> SyncNodeParams -> IO ()
runDbSyncNode mkPlugin params = do
runDbSyncNode :: MetricSetters -> (SqlBackend -> SyncNodePlugin) -> SyncNodeParams -> IO ()
runDbSyncNode metricsSetters mkPlugin params = do

-- Read the PG connection info
pgConfig <- DB.readPGPassFileEnv Nothing
Expand All @@ -65,11 +69,10 @@ runDbSyncNode mkPlugin params = do
DB.runIohkLogging trce $ withPostgresqlConn connectionString $ \backend ->
lift $ do
-- For testing and debugging.
case enpMaybeRollback params of
Just slotNo -> void $ unsafeRollback trce slotNo
Nothing -> pure ()
whenJust (enpMaybeRollback params) $ \ slotNo ->
void $ unsafeRollback trce slotNo

runSyncNode (mkSyncDataLayer trce backend) trce (mkPlugin backend)
runSyncNode (mkSyncDataLayer trce backend) metricsSetters trce (mkPlugin backend)
params (insertValidateGenesisDist backend) runDbThread

-- -------------------------------------------------------------------------------------------------
Expand All @@ -82,11 +85,13 @@ mkSyncDataLayer trce backend =
, sdlGetLatestBlock =
runMaybeT $ do
block <- MaybeT $ DB.runDbNoLogging DB.queryLatestBlock
-- The EpochNo, SlotNo and BlockNo can only be zero for the Byron
-- era, but we need to make the types match, hence `fromMaybe`.
pure $ Block
{ bHash = DB.blockHash block
, bEpochNo = DB.blockEpochNo block
, bSlotNo = DB.blockSlotNo block
, bBlockNo = DB.blockBlockNo block
, bEpochNo = EpochNo . fromMaybe 0 $ DB.blockEpochNo block
, bSlotNo = SlotNo . fromMaybe 0 $ DB.blockSlotNo block
, bBlockNo = BlockNo . fromMaybe 0 $ DB.blockBlockNo block
}
, sdlGetLatestSlotNo = SlotNo <$> DB.runDbNoLogging DB.queryLatestSlotNo
}
51 changes: 39 additions & 12 deletions cardano-db-sync/src/Cardano/DbSync/Metrics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,56 @@
module Cardano.DbSync.Metrics
( Metrics (..)
, makeMetrics
, withMetricSetters
, withMetricsServer
) where

import Cardano.Prelude

import Cardano.Slotting.Slot (SlotNo (..))

import Cardano.Sync.Types (MetricSetters (..))

import Ouroboros.Network.Block (BlockNo (..))

import System.Metrics.Prometheus.Concurrent.RegistryT (RegistryT (..), registerGauge,
runRegistryT, unRegistryT)
import System.Metrics.Prometheus.Http.Scrape (serveMetricsT)
import System.Metrics.Prometheus.Metric.Gauge (Gauge)

import qualified System.Metrics.Prometheus.Metric.Gauge as Gauge

data Metrics = Metrics
Copy link
Contributor

@erikd erikd Mar 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it a layer? To me "layer" implies that if we have say 3 layers, A, B and C, that A calls into B and then B calls into C. That is not the case here. Its not a layer, its a Metrics component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ mDbHeight :: !Gauge
, mNodeHeight :: !Gauge
, mQueuePre :: !Gauge
, mQueuePost :: !Gauge
, mQueuePostWrite :: !Gauge
{ mNodeBlockHeight :: !Gauge
-- ^ The block tip number of the remote node.
, mDbQueueLength :: !Gauge
-- ^ The number of @DbAction@ remaining for the database.
, mDbBlockHeight :: !Gauge
-- ^ The block tip number in the database.
, mDbSlotHeight :: !Gauge
-- ^ The slot tip number in the database.
}

-- This enables us to be much more flexibile with what we actually measure.
withMetricSetters :: Int -> (MetricSetters -> IO a) -> IO a
withMetricSetters prometheusPort action =
withMetricsServer prometheusPort $ \metrics -> do
action $
MetricSetters
{ metricsSetNodeBlockHeight = \ (BlockNo nodeHeight) ->
Gauge.set (fromIntegral nodeHeight) $ mNodeBlockHeight metrics
, metricsSetDbQueueLength = \ queuePostWrite ->
Gauge.set (fromIntegral queuePostWrite) $ mDbQueueLength metrics
, metricsSetDbBlockHeight = \ (BlockNo blockNo) ->
Gauge.set (fromIntegral blockNo) $ mDbBlockHeight metrics
, metricsSetDbSlotHeight = \ (SlotNo slotNo) ->
Gauge.set (fromIntegral slotNo) $ mDbSlotHeight metrics
}

withMetricsServer :: Int -> (Metrics -> IO a) -> IO a
withMetricsServer port action = do
-- Using both `RegistryT` and `bracket` here is overkill. Unfortunately the
-- Prometheus API requires the use of a `Registry` and this seems to be the
-- least sucky way of doing it.
(metrics, registry) <- runRegistryT $ (,) <$> makeMetrics <*> RegistryT ask
bracket
(async $ runReaderT (unRegistryT $ serveMetricsT port []) registry)
Expand All @@ -34,9 +63,7 @@ withMetricsServer port action = do
makeMetrics :: RegistryT IO Metrics
makeMetrics =
Metrics
<$> registerGauge "db_block_height" mempty
<*> registerGauge "remote_tip_height" mempty
<*> registerGauge "action_queue_length_pre" mempty
<*> registerGauge "action_queue_length_post" mempty
<*> registerGauge "action_queue_length_post_write" mempty

<$> registerGauge "cardano_db_sync_node_block_height" mempty
<*> registerGauge "cardano_db_sync_db_queue_length" mempty
<*> registerGauge "cardano_db_sync_db_block_height" mempty
<*> registerGauge "cardano_db_sync_db_slot_height" mempty
3 changes: 1 addition & 2 deletions cardano-sync/cardano-sync.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ library
Cardano.Sync.Error

Cardano.Sync.LedgerState
Cardano.Sync.Metrics

Cardano.Sync.Era.Byron.Util
Cardano.Sync.Era.Cardano.Util
Expand All @@ -57,6 +56,7 @@ library
Cardano.Sync.Era.Shelley.Generic.StakeCred
Cardano.Sync.Era.Shelley.Generic.StakeDist

Cardano.Sync.Metrics
Cardano.Sync.Plugin

Cardano.Sync.StateQuery
Expand Down Expand Up @@ -97,7 +97,6 @@ library
, ouroboros-consensus-shelley
, ouroboros-network
, ouroboros-network-framework
, prometheus
, shelley-spec-ledger
, stm
, text
Expand Down
Loading