Skip to content

Commit

Permalink
Merge pull request #4955 from input-output-hk/jutaro/metrics-mapping
Browse files Browse the repository at this point in the history
Backward compatibility for Prometheus metrics names
  • Loading branch information
deepfire authored Mar 22, 2023
2 parents c83b479 + a30f5ca commit 3888d54
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cardano-node/src/Cardano/Node/Tracing/Tracers/ChainDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ instance MetaTrace (ChainDB.TraceAddBlockEvent blk) where
, " of blocks that could be created over the span of the last @k@ blocks."
]
)
, ( "ChainDB.Slots"
, ( "ChainDB.SlotNum"
, "Number of slots in this chain fragment."
)
, ( "ChainDB.Blocks"
Expand All @@ -629,7 +629,7 @@ instance MetaTrace (ChainDB.TraceAddBlockEvent blk) where
, " of blocks that could be created over the span of the last @k@ blocks."
]
)
, ( "ChainDB.Slots"
, ( "ChainDB.SlotNum"
, "Number of slots in this chain fragment."
)
, ( "ChainDB.Blocks"
Expand Down
1 change: 1 addition & 0 deletions cardano-tracer/bench/cardano-tracer-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ main = do
, logging = NE.fromList [LoggingParams root FileMode format]
, rotation = Nothing
, verbosity = Nothing
, metricsComp = Nothing
}

generate num = replicate num . mkTraceObject <$> getCurrentTime
Expand Down
3 changes: 3 additions & 0 deletions cardano-tracer/src/Cardano/Tracer/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Data.List.Extra (notNull)
import Data.Maybe (catMaybes)
import Data.Map.Strict (Map)
import Data.Word (Word16, Word32, Word64)
import Data.Text (Text)
import Data.Yaml (decodeFileEither)
import GHC.Generics (Generic)
import System.Exit (die)
Expand Down Expand Up @@ -91,6 +93,7 @@ data TracerConfig = TracerConfig
, logging :: !(NonEmpty LoggingParams) -- ^ Logging parameters.
, rotation :: !(Maybe RotationParams) -- ^ Rotation parameters.
, verbosity :: !(Maybe Verbosity) -- ^ Verbosity of the tracer itself.
, metricsComp :: !(Maybe (Map Text Text)) -- ^ Metrics compatibility map from metrics name to metrics name
} deriving (Eq, Generic, FromJSON, ToJSON, Show)

-- | Read the tracer's configuration file.
Expand Down
24 changes: 20 additions & 4 deletions cardano-tracer/src/Cardano/Tracer/Handlers/Metrics/Prometheus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,19 @@ runPrometheusServer tracerEnv (Endpoint host port) = forever $ do
Just [nodeName] -> do
liftIO (askNodeId tracerEnv $ decodeUtf8 nodeName) >>= \case
Nothing -> writeText "No such a node!"
Just anId -> writeText =<< liftIO (getMetricsFromNode anId teAcceptedMetrics)
Just anId -> writeText =<< liftIO (getMetricsFromNode tracerEnv anId teAcceptedMetrics)
_ -> writeText "No such a node!"

type MetricName = Text
type MetricValue = Text
type MetricsList = [(MetricName, MetricValue)]

getMetricsFromNode
:: NodeId
:: TracerEnv
-> NodeId
-> AcceptedMetrics
-> IO Text
getMetricsFromNode nodeId acceptedMetrics =
getMetricsFromNode tracerEnv nodeId acceptedMetrics =
readTVarIO acceptedMetrics >>=
(\case
Nothing ->
Expand All @@ -124,7 +125,11 @@ getMetricsFromNode nodeId acceptedMetrics =
) . M.lookup nodeId
where
getListOfMetrics :: Sample -> MetricsList
getListOfMetrics = filter (not . T.null . fst) . map metricsWeNeed . HM.toList
getListOfMetrics =
metricsCompatibility
. filter (not . T.null . fst)
. map metricsWeNeed
. HM.toList

metricsWeNeed (mName, mValue) =
case mValue of
Expand All @@ -143,3 +148,14 @@ getMetricsFromNode nodeId acceptedMetrics =
. T.replace " " "_"
. T.replace "-" "_"
. T.replace "." "_"

metricsCompatibility :: MetricsList -> MetricsList
metricsCompatibility metricsList =
case metricsComp (teConfig tracerEnv) of
Nothing -> metricsList
Just mmap -> foldl (\ accu p'@(mn,mv) -> case M.lookup mn mmap of
Nothing -> p' : accu
Just rep -> p' : (rep,mv) : accu)
[]
metricsList

1 change: 1 addition & 0 deletions cardano-tracer/test/Cardano/Tracer/Test/Acceptor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ launchAcceptorsSimple mode localSock dpName = do
, logging = NE.fromList [LoggingParams "/tmp/demo-acceptor" FileMode ForHuman]
, rotation = Nothing
, verbosity = Just Minimum
, metricsComp = Nothing
}

-- | To be able to ask any 'DataPoint' by the name without knowing the actual type,
Expand Down
1 change: 1 addition & 0 deletions cardano-tracer/test/Cardano/Tracer/Test/DataPoint/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ propDataPoint ts@TestSetup{..} rootDir localSock = do
, logging = NE.fromList [LoggingParams rootDir FileMode ForHuman]
, rotation = Nothing
, verbosity = Just Minimum
, metricsComp = Nothing
}
3 changes: 3 additions & 0 deletions cardano-tracer/test/Cardano/Tracer/Test/Logs/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ propLogs ts@TestSetup{..} format rootDir localSock = do
, rpKeepFilesNum = 10
}
, verbosity = Just Minimum
, metricsComp = Nothing
}

propMultiInit :: TestSetup Identity -> LogFormat -> FilePath -> FilePath -> FilePath -> IO Property
Expand Down Expand Up @@ -107,6 +108,7 @@ propMultiInit ts@TestSetup{..} format rootDir localSock1 localSock2 = do
, logging = NE.fromList [LoggingParams root FileMode format]
, rotation = Nothing
, verbosity = Just Minimum
, metricsComp = Nothing
}

propMultiResp :: TestSetup Identity -> LogFormat -> FilePath -> FilePath -> IO Property
Expand Down Expand Up @@ -134,6 +136,7 @@ propMultiResp ts@TestSetup{..} format rootDir localSock = do
, logging = NE.fromList [LoggingParams root FileMode format]
, rotation = Nothing
, verbosity = Just Minimum
, metricsComp = Nothing
}

checkMultiResults :: FilePath -> IO Property
Expand Down
1 change: 1 addition & 0 deletions cardano-tracer/test/Cardano/Tracer/Test/Restart/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ mkConfig TestSetup{..} rootDir p = TracerConfig
, logging = NE.fromList [LoggingParams rootDir FileMode ForMachine]
, rotation = Nothing
, verbosity = Just Minimum
, metricsComp = Nothing
}
8 changes: 8 additions & 0 deletions nix/nixos/cardano-tracer-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ let serviceConfigToJSON =
epHost = "127.0.0.1";
epPort = 3200; ## supervisord.portShiftPrometheus
} // (cfg.prometheus or {});
# Just an example for metrics compatibility mapping.
# An entry means the first entry has the second entry as alias.
# The Metrics is then avalable, both with the original and the mapped name.
# Only one mapping per message is supported.
# metricsComp = {
# "Mempool.TxsInMempool" = "Mempool.TxsInMempool.Mapped";
# "ChainDB.SlotNum" = "ChainDB.SlotNum.Mapped";
# };
};
in pkgs.commonLib.defServiceModule
(lib: with lib;
Expand Down

0 comments on commit 3888d54

Please sign in to comment.