Skip to content

Commit

Permalink
cardano-node: NodeVersionTracer
Browse files Browse the repository at this point in the history
Added for metrics

trace-dispatcher: With PrometheusM Label type.

cardano-node: With new cardano_build_info metric

Patches
  • Loading branch information
jutaro committed May 5, 2024
1 parent 7b5e65e commit f5e6c54
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions cardano-node/cardano-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ library
Cardano.Node.Tracing.Tracers.KESInfo
Cardano.Node.Tracing.Tracers.NodeToClient
Cardano.Node.Tracing.Tracers.NodeToNode
Cardano.Node.Tracing.Tracers.NodeVersion
Cardano.Node.Tracing.Tracers.NonP2P
Cardano.Node.Tracing.Tracers.P2P
Cardano.Node.Tracing.Tracers.Peer
Expand Down
3 changes: 3 additions & 0 deletions cardano-node/src/Cardano/Node/Tracing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Cardano.Node.Tracing.StateRep (NodeState)
import Cardano.Node.Tracing.Tracers.ConsensusStartupException
(ConsensusStartupException (..))
import Cardano.Node.Tracing.Tracers.Peer (PeerT)
import Cardano.Node.Tracing.Tracers.NodeVersion (NodeVersionTrace)

import qualified Ouroboros.Consensus.Network.NodeToClient as NodeToClient
import qualified Ouroboros.Consensus.Network.NodeToNode as NodeToNode
import qualified Ouroboros.Consensus.Node.Tracers as Consensus
Expand Down Expand Up @@ -48,6 +50,7 @@ data Tracers peer localPeer blk p2p = Tracers
, startupTracer :: !(Tracer IO (StartupTrace blk))
, shutdownTracer :: !(Tracer IO ShutdownTrace)
, nodeInfoTracer :: !(Tracer IO NodeInfo)
, nodeVersionTracer :: !(Tracer IO NodeVersionTrace)
, nodeStartupInfoTracer :: !(Tracer IO NodeStartupInfo)
, nodeStateTracer :: !(Tracer IO NodeState)
, resourcesTracer :: !(Tracer IO ResourceStats)
Expand Down
6 changes: 6 additions & 0 deletions cardano-node/src/Cardano/Node/Tracing/Consistency.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import Cardano.Node.Tracing.Tracers.Diffusion ()
import Cardano.Node.Tracing.Tracers.KESInfo ()
import Cardano.Node.Tracing.Tracers.NodeToClient ()
import Cardano.Node.Tracing.Tracers.NodeToNode ()
import Cardano.Node.Tracing.Tracers.NodeVersion (NodeVersionTrace)

import Cardano.Node.Tracing.Tracers.NonP2P ()
import Cardano.Node.Tracing.Tracers.P2P ()
import Cardano.Node.Tracing.Tracers.Peer
Expand Down Expand Up @@ -130,6 +132,9 @@ getAllNamespaces =
(allNamespaces :: [Namespace (StartupTrace blk)])
shutdownNS = map (nsGetTuple . nsReplacePrefix ["Shutdown"])
(allNamespaces :: [Namespace ShutdownTrace])
nodeVersionNS = map (nsGetTuple . nsReplacePrefix ["Version"])
(allNamespaces :: [Namespace NodeVersionTrace])

chainDBNS = map (nsGetTuple . nsReplacePrefix ["ChainDB"])
(allNamespaces :: [Namespace (ChainDB.TraceEvent blk)])
replayBlockNS = map (nsGetTuple . nsReplacePrefix ["ChainDB", "ReplayBlock"])
Expand Down Expand Up @@ -366,6 +371,7 @@ getAllNamespaces =
<> resourcesNS
<> startupNS
<> shutdownNS
<> nodeVersionNS
<> chainDBNS
<> replayBlockNS
-- Consensus
Expand Down
9 changes: 9 additions & 0 deletions cardano-node/src/Cardano/Node/Tracing/Documentation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import Cardano.Node.Tracing.Tracers.ForgingThreadStats (ForgeThreadSta
import Cardano.Node.Tracing.Tracers.KESInfo ()
import Cardano.Node.Tracing.Tracers.NodeToClient ()
import Cardano.Node.Tracing.Tracers.NodeToNode ()
import Cardano.Node.Tracing.Tracers.NodeVersion (NodeVersionTrace)

import Cardano.Node.Tracing.Tracers.NonP2P ()
import Cardano.Node.Tracing.Tracers.P2P ()
import Cardano.Node.Tracing.Tracers.Peer
Expand Down Expand Up @@ -192,6 +194,12 @@ docTracersFirstPhase condConfigFileName = do
nodeStartupInfoDpDoc <- documentTracer
(nodeStartupInfoDp :: Trace IO NodeStartupInfo)

nodeVersionTr <- mkCardanoTracer
trBase trForward mbTrEKG
["Version"]
configureTracers configReflection trConfig [nodeVersionTr]
nodeVersionDoc <- documentTracer (nodeVersionTr :: Trace IO NodeVersionTrace)

-- State tracer
stateTr <- mkCardanoTracer
trBase trForward mbTrEKG
Expand Down Expand Up @@ -677,6 +685,7 @@ docTracersFirstPhase condConfigFileName = do
<> resourcesTrDoc
<> startupTrDoc
<> shutdownTrDoc
<> nodeVersionDoc
<> peersTrDoc
<> chainDBTrDoc
<> replayBlockTrDoc
Expand Down
9 changes: 9 additions & 0 deletions cardano-node/src/Cardano/Node/Tracing/Tracers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import Cardano.Node.Tracing.Tracers.ForgingThreadStats (forgeThreadSta
import Cardano.Node.Tracing.Tracers.KESInfo
import Cardano.Node.Tracing.Tracers.NodeToClient ()
import Cardano.Node.Tracing.Tracers.NodeToNode ()
import Cardano.Node.Tracing.Tracers.NodeVersion (getNodeVersion)

import Cardano.Node.Tracing.Tracers.NonP2P ()
import Cardano.Node.Tracing.Tracers.P2P ()
import Cardano.Node.Tracing.Tracers.Peer ()
Expand Down Expand Up @@ -125,6 +127,10 @@ mkDispatchTracers nodeKernel trBase trForward mbTrEKG trDataPoint trConfig enabl
!chainDBTr <- mkCardanoTracer' trBase trForward mbTrEKG ["ChainDB"]
withAddedToCurrentChainEmptyLimited
configureTracers configReflection trConfig [chainDBTr]

!nodeVersionTr <- mkCardanoTracer trBase trForward mbTrEKG ["Version"]
configureTracers configReflection trConfig [nodeVersionTr]

-- Filter out replayed blocks for this tracer
let chainDBTr' = filterTrace
(\case (_, ChainDB.TraceLedgerReplayEvent
Expand Down Expand Up @@ -170,6 +176,8 @@ mkDispatchTracers nodeKernel trBase trForward mbTrEKG trDataPoint trConfig enabl

traceEffectiveConfiguration trBase trForward trConfig

traceWith nodeVersionTr getNodeVersion

pure Tracers
{
chainDBTracer = Tracer (traceWith chainDBTr')
Expand All @@ -188,6 +196,7 @@ mkDispatchTracers nodeKernel trBase trForward mbTrEKG trDataPoint trConfig enabl
, nodeStartupInfoTracer = Tracer (traceWith nodeStartupInfoDP)
, nodeStateTracer = Tracer (traceWith stateTr)
<> Tracer (traceWith nodeStateDP)
, nodeVersionTracer = Tracer (traceWith nodeVersionTr)
, resourcesTracer = Tracer (traceWith resourcesTr)
, peersTracer = Tracer (traceWith peersTr)
<> Tracer (traceNodePeers nodePeersDP)
Expand Down
130 changes: 130 additions & 0 deletions cardano-node/src/Cardano/Node/Tracing/Tracers/NodeVersion.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}

{-# OPTIONS_GHC -Wno-name-shadowing #-}

module Cardano.Node.Tracing.Tracers.NodeVersion
(
NodeVersionTrace (..)
, getNodeVersion
)
where

import Cardano.Logging
import Data.Aeson (toJSON, (.=))
import Data.Text (Text, pack)

import Data.Version (Version (..), showVersion)

import Cardano.Git.Rev (gitRev)

import Paths_cardano_node (version)
import System.Info (arch, compilerName, compilerVersion, os)



data NodeVersionTrace = NodeVersionTrace
{ applicationName :: Text
, applicationVersion :: Version
, osName :: Text
, architecture :: Text
, compilerName :: Text
, compilerVersion :: Version
, gitRevision :: Text
} deriving (Eq, Show)

getNodeVersion :: NodeVersionTrace
getNodeVersion =
let applicationName = "cardano-node"
applicationVersion = version
osName = pack os
architecture = pack arch
compilerName = pack System.Info.compilerName
compilerVersion = System.Info.compilerVersion

gitRevision = $(gitRev)
in NodeVersionTrace {..}

instance MetaTrace NodeVersionTrace where
namespaceFor NodeVersionTrace {} =
Namespace [] ["NodeVersion"]
severityFor (Namespace _ ["NodeVersion"]) _ = Just Info
severityFor _ _ = Nothing

documentFor (Namespace _ ["NodeVersion"]) = Just "Node version information"

documentFor _ = Nothing

metricsDocFor (Namespace _ ["NodeVersion"]) =
[("cardano_version_major", "Cardano node version information")
,("cardano_version_minor", "Cardano node version information")
,("cardano_version_patch", "Cardano node version information")
,("haskell_compiler_major", "Cardano compiler version information")
,("haskell_compiler_minor", "Cardano compiler version information")
--,("haskell_compiler_patch", "Cardano compiler version information")
,("cardano_build_info", "Cardano node build info")
]
metricsDocFor _ = []

allNamespaces = [Namespace [] ["NodeVersion"]]

instance LogFormatting NodeVersionTrace where
forHuman NodeVersionTrace {..} = mconcat
[ "cardano-node ", pack (showVersion applicationVersion)
, " git rev ", gitRevision
, " - ", pack os, "-", pack arch
, " - ", compilerName, "-", pack (showVersion compilerVersion)
]

forMachine _dtal NodeVersionTrace {..} = mconcat

[ "applicationName" .= applicationName
, "applicationVersion" .= toJSON applicationVersion
, "gitRevision" .= gitRevision
, "osName" .= osName
, "architecture" .= architecture
, "compilerName" .= compilerName
, "compilerVersion" .= toJSON compilerVersion
]

asMetrics nvt@NodeVersionTrace {..} =
[ IntM "cardano_version_major" (fromIntegral (getMajor applicationVersion))
, IntM "cardano_version_minor" (fromIntegral (getMinor applicationVersion))
, IntM "cardano_version_patch" (fromIntegral (getPatch applicationVersion))
, IntM "haskell_compiler_major" (fromIntegral (getMajor compilerVersion))
, IntM "haskell_compiler_minor" (fromIntegral (getMinor compilerVersion))
--, IntM "haskell_compiler_patch" (fromIntegral (getPatch compilerVersion))
, PrometheusM "cardano_build_info" (getCardanoBuildInfo nvt)
]

getCardanoBuildInfo :: NodeVersionTrace -> [(Text,Text)]
getCardanoBuildInfo NodeVersionTrace {..} =
[ ("version_major", pack (show (getMajor applicationVersion)))
, ("version_minor", pack (show (getMinor applicationVersion)))
, ("version_patch", pack (show (getPatch applicationVersion)))
, ("version", pack (showVersion applicationVersion))
, ("revision", gitRevision)
, ("compiler_name", compilerName)
, ("compiler_version", pack (showVersion compilerVersion))
, ("compiler_version_major", pack (show (getMajor compilerVersion)))
, ("compiler_version_minor", pack (show (getMinor compilerVersion)))
-- , ("compiler_version_patch", pack (show (getPatch compilerVersion)))
, ("architecture", architecture)
, ("os_name", osName)
]

getMajor :: Version -> Int
getMajor (Version (x:_) _) = x
getMajor _ = 0

getMinor :: Version -> Int
getMinor (Version (_:x:_) _) = x
getMinor _ = 0

getPatch :: Version -> Int
getPatch (Version (_:_:x:_) _) = x
getPatch _ = 0



3 changes: 3 additions & 0 deletions cardano-node/src/Cardano/Node/Tracing/Tracers/Startup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import Cardano.Logging
import Cardano.Node.Configuration.POM (NodeConfiguration, ncProtocol)
import Cardano.Node.Configuration.Socket
import Cardano.Node.Protocol (SomeConsensusProtocol (..))

import Cardano.Node.Startup

import Cardano.Slotting.Slot (EpochSize (..))
import qualified Ouroboros.Consensus.BlockchainTime.WallClock.Types as WCT
import Ouroboros.Consensus.Byron.Ledger.Conversions (fromByronEpochSlots,
Expand Down Expand Up @@ -54,6 +56,7 @@ import Data.Text (Text, pack)
import Data.Time (getCurrentTime)
import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds)
import Data.Version (showVersion)

import Network.Socket (SockAddr)

import Paths_cardano_node (version)
Expand Down
4 changes: 4 additions & 0 deletions cardano-node/src/Cardano/Tracing/Tracers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ nullTracersP2P = Tracers
, nodeInfoTracer = nullTracer
, nodeStartupInfoTracer = nullTracer
, nodeStateTracer = nullTracer
, nodeVersionTracer = nullTracer
, resourcesTracer = nullTracer
, peersTracer = nullTracer
}
Expand All @@ -180,6 +181,7 @@ nullTracersNonP2P = Tracers
, nodeInfoTracer = nullTracer
, nodeStartupInfoTracer = nullTracer
, nodeStateTracer = nullTracer
, nodeVersionTracer = nullTracer
, resourcesTracer = nullTracer
, peersTracer = nullTracer
}
Expand Down Expand Up @@ -345,6 +347,7 @@ mkTracers blockConfig tOpts@(TracingOnLegacy trSel) tr nodeKern ekgDirect enable
, nodeInfoTracer = nullTracer
, nodeStartupInfoTracer = nullTracer
, nodeStateTracer = nullTracer
, nodeVersionTracer = nullTracer
, resourcesTracer = nullTracer
, peersTracer = nullTracer
}
Expand Down Expand Up @@ -503,6 +506,7 @@ mkTracers _ _ _ _ _ enableP2P =
, nodeInfoTracer = nullTracer
, nodeStartupInfoTracer = nullTracer
, nodeStateTracer = nullTracer
, nodeVersionTracer = nullTracer
, resourcesTracer = nullTracer
, peersTracer = nullTracer
}
Expand Down
1 change: 1 addition & 0 deletions nix/workbench/service/tracing.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ let
"TxSubmission.Remote".severity = "Notice";
"TxSubmission.TxInbound".severity = "Debug";
"TxSubmission.TxOutbound".severity = "Notice";
"Version.NodeVersion".severity = "Info";
};
};

Expand Down
11 changes: 11 additions & 0 deletions trace-dispatcher/src/Cardano/Logging/Tracer/EKG.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ ekgTracer storeOrServer = liftIO $ do
(DoubleM name theDouble) = do
label <- modifyMVar rgsLabels (setFunc Metrics.createLabel getLabel name)
Label.set label ((pack . show) theDouble)
setIt _rgsGauges rgsLabels _rgsCounters _namespace
(PrometheusM name keyLabels) = do
label <- modifyMVar rgsLabels (setFunc Metrics.createLabel getLabel name)
Label.set label (presentPrometheusM keyLabels)

setIt _rgsGauges _rgsLabels rgsCounters _namespace
(CounterM name mbInt) = do
counter <- modifyMVar rgsCounters (setFunc Metrics.createCounter getCounter name)
Expand All @@ -84,3 +89,9 @@ ekgTracer storeOrServer = liftIO $ do
Right server -> creator2 name server
let rgsMap' = Map.insert name gauge rgsMap
pure (rgsMap', gauge)

presentPrometheusM :: [(Text, Text)] -> Text
presentPrometheusM labels = "{" <> foldr (\(k, v) acc -> k <> "=\"" <> v <>
(if k == fst (last labels)
then "\" " <> acc
else "\", " <> acc)) "" labels <> "} 1"
7 changes: 7 additions & 0 deletions trace-dispatcher/src/Cardano/Logging/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,20 @@ data Metric
-- | A counter metric.
-- Text is used to name the metric
| CounterM Text (Maybe Int)
-- | A prometheus metric with key label pairs.
-- Text is used to name the metric
-- [(Text, Text)] is used to represent the key label pairs
-- The value of the metric will always be "1"
| PrometheusM Text [(Text, Text)]
deriving (Show, Eq)


getMetricName :: Metric -> Text
getMetricName (IntM name _) = name
getMetricName (DoubleM name _) = name
getMetricName (CounterM name _) = name
getMetricName (PrometheusM name _) = name


-- | A helper function for creating an empty |Object|.
emptyObject :: HM.HashMap Text a
Expand Down

0 comments on commit f5e6c54

Please sign in to comment.