Skip to content

Commit 51fe548

Browse files
committed
new Prometheus option metricsNoSuffix
1 parent 91d1f1a commit 51fe548

File tree

12 files changed

+85
-77
lines changed

12 files changed

+85
-77
lines changed

cardano-node/src/Cardano/Node/Tracing/API.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ initTraceDispatcher nc p networkMagic nodeKernel p2pMode = do
100100
ekgTrace <- ekgTracer trConfig ekgStore
101101

102102
forM_ prometheusSimple $
103-
uncurry (runPrometheusSimple ekgStore)
103+
runPrometheusSimple ekgStore
104104

105105
stdoutTrace <- standardTracer
106106

@@ -133,12 +133,12 @@ initTraceDispatcher nc p networkMagic nodeKernel p2pMode = do
133133
where
134134
-- This backend can only be used globally, i.e. will always apply to the namespace root.
135135
-- Multiple definitions, especially with differing ports, are considered a *misconfiguration*.
136-
prometheusSimple :: Maybe (Maybe HostName, PortNumber)
136+
prometheusSimple :: Maybe (Bool, Maybe HostName, PortNumber)
137137
prometheusSimple =
138-
listToMaybe [ (mHost, portNo)
139-
| options <- Map.elems (tcOptions trConfig)
140-
, ConfBackend backends' <- options
141-
, PrometheusSimple mHost portNo <- backends'
138+
listToMaybe [ (noSuff, mHost, portNo)
139+
| options <- Map.elems (tcOptions trConfig)
140+
, ConfBackend backends' <- options
141+
, PrometheusSimple noSuff mHost portNo <- backends'
142142
]
143143

144144
forwarderBackendEnabled =

cardano-tracer/bench/cardano-tracer-bench.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ main = do
148148
, logging = NE.fromList [LoggingParams root FileMode format]
149149
, rotation = Nothing
150150
, verbosity = Nothing
151-
, metricsComp = Nothing
151+
, metricsNoSuffix = Nothing
152152
, metricsHelp = Nothing
153153
, hasForwarding = Nothing
154154
, resourceFreq = Nothing

cardano-tracer/src/Cardano/Tracer/Configuration.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ data TracerConfig = TracerConfig
145145
, Maybe [[Text]]
146146
, Log.TraceOptionForwarder
147147
))
148-
, logging :: !(NonEmpty LoggingParams) -- ^ Logging parameters.
149-
, rotation :: !(Maybe RotationParams) -- ^ Rotation parameters.
150-
, verbosity :: !(Maybe Verbosity) -- ^ Verbosity of the tracer itself.
151-
, metricsComp :: !(Maybe (Map Text Text)) -- ^ Metrics compatibility map from metrics name to metrics name
152-
, metricsHelp :: !(Maybe FileOrMap) -- ^ JSON file or object containing a key-value map "metric name -> help text" for Prometheus "# HELP " annotations
153-
, resourceFreq :: !(Maybe Int) -- ^ Frequency (1/millisecond) for gathering resource data.
154-
, ekgRequestFull :: !(Maybe Bool) -- ^ Request full set of metrics always, vs. deltas only (safer, but more overhead); default: False
148+
, logging :: !(NonEmpty LoggingParams) -- ^ Logging parameters.
149+
, rotation :: !(Maybe RotationParams) -- ^ Rotation parameters.
150+
, verbosity :: !(Maybe Verbosity) -- ^ Verbosity of the tracer itself.
151+
, metricsNoSuffix :: !(Maybe Bool) -- ^ Prometheus ONLY: Dropping metrics name suffixes (like "_int") increases similiarity with old system names - if desired; default: False
152+
, metricsHelp :: !(Maybe FileOrMap) -- ^ Prometheus ONLY: JSON file or object containing a key-value map "metric name -> help text" for "# HELP " annotations
153+
, resourceFreq :: !(Maybe Int) -- ^ Frequency (1/millisecond) for gathering resource data.
154+
, ekgRequestFull :: !(Maybe Bool) -- ^ Request full set of metrics always, vs. deltas only (safer, but more overhead); default: False
155155
}
156156
deriving stock (Eq, Show, Generic)
157157
deriving anyclass (FromJSON, ToJSON)

cardano-tracer/src/Cardano/Tracer/Handlers/Metrics/Prometheus.hs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Cardano.Tracer.Handlers.Metrics.Prometheus
66
( runPrometheusServer
77
) where
88

9-
import Cardano.Logging.Prometheus.Exposition (MetricName, renderExpositionFromSampleWith)
9+
import Cardano.Logging.Prometheus.Exposition (renderExpositionFromSampleWith)
1010
import Cardano.Tracer.Configuration
1111
import Cardano.Tracer.Environment
1212
import Cardano.Tracer.Handlers.Metrics.Utils
@@ -17,7 +17,6 @@ import Prelude hiding (head)
1717
import qualified Data.ByteString as ByteString
1818
import Data.ByteString.Builder (stringUtf8)
1919
import Data.Functor ((<&>))
20-
import Data.Map.Strict (Map)
2120
import Data.Text (Text)
2221
import qualified Data.Text.Lazy as TL
2322
import Data.Text.Lazy.Builder (Builder)
@@ -72,20 +71,22 @@ runPrometheusServer tracerEnv endpoint computeRoutes_autoUpdate = do
7271
{ ttPrometheusEndpoint = endpoint
7372
}
7473
runSettings (setEndpoint endpoint defaultSettings) do
75-
renderPrometheus computeRoutes_autoUpdate metricsComp teMetricsHelp where
74+
renderPrometheus computeRoutes_autoUpdate noSuffix teMetricsHelp
75+
where
76+
TracerEnv
77+
{ teTracer
78+
, teConfig = TracerConfig { metricsNoSuffix }
79+
, teMetricsHelp
80+
} = tracerEnv
7681

77-
TracerEnv
78-
{ teTracer
79-
, teConfig = TracerConfig { metricsComp }
80-
, teMetricsHelp
81-
} = tracerEnv
82+
noSuffix = or @Maybe metricsNoSuffix
8283

8384
renderPrometheus
8485
:: IO RouteDictionary
85-
-> Maybe (Map Text Text)
86+
-> Bool
8687
-> [(Text, Builder)]
8788
-> Application
88-
renderPrometheus computeRoutes_autoUpdate metricsComp helpTextDict request send = do
89+
renderPrometheus computeRoutes_autoUpdate noSuffix helpTextDict request send = do
8990
routeDictionary :: RouteDictionary <-
9091
computeRoutes_autoUpdate
9192

@@ -105,7 +106,7 @@ renderPrometheus computeRoutes_autoUpdate metricsComp helpTextDict request send
105106

106107
route:_
107108
| Just (store :: EKG.Store, _) <- lookup route (getRouteDictionary routeDictionary)
108-
-> do metrics <- getMetricsFromNode metricsComp helpTextDict store
109+
-> do metrics <- getMetricsFromNode noSuffix helpTextDict store
109110
send $ responseBuilder status200
110111
(if wantsOpenMetrics then contentHdrOpenMetrics else contentHdrUtf8Text)
111112
(TL.encodeUtf8Builder metrics)
@@ -116,9 +117,9 @@ renderPrometheus computeRoutes_autoUpdate metricsComp helpTextDict request send
116117
<> stringUtf8 (show route)
117118

118119
getMetricsFromNode
119-
:: Maybe (Map MetricName MetricName)
120+
:: Bool
120121
-> [(Text, Builder)]
121122
-> EKG.Store
122123
-> IO TL.Text
123-
getMetricsFromNode metricsComp helpTextDict ekgStore =
124-
sampleAll ekgStore <&> renderExpositionFromSampleWith metricsComp helpTextDict
124+
getMetricsFromNode noSuffix helpTextDict ekgStore =
125+
sampleAll ekgStore <&> renderExpositionFromSampleWith helpTextDict noSuffix

cardano-tracer/test/Cardano/Tracer/Test/Acceptor.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ launchAcceptorsSimple mode localSock dpName = do
113113
, logging = NE.fromList [LoggingParams "/tmp/demo-acceptor" FileMode ForHuman]
114114
, rotation = Nothing
115115
, verbosity = Just Minimum
116-
, metricsComp = Nothing
116+
, metricsNoSuffix = Nothing
117117
, metricsHelp = Nothing
118118
, hasForwarding = Nothing
119119
, resourceFreq = Nothing

cardano-tracer/test/Cardano/Tracer/Test/DataPoint/Tests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ propDataPoint ts@TestSetup{..} rootDir localSock = do
9090
, logging = NE.fromList [LoggingParams rootDir FileMode ForHuman]
9191
, rotation = Nothing
9292
, verbosity = Just Minimum
93-
, metricsComp = Nothing
93+
, metricsNoSuffix = Nothing
9494
, metricsHelp = Nothing
9595
, hasForwarding = Nothing
9696
, resourceFreq = Nothing

cardano-tracer/test/Cardano/Tracer/Test/Logs/Tests.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ propLogs ts@TestSetup{..} format logRotLimitBytes logRotMaxAgeMinutes rootDir lo
8282
, rpKeepFilesNum = 10
8383
}
8484
, verbosity = Just Minimum
85-
, metricsComp = Nothing
85+
, metricsNoSuffix = Nothing
8686
, metricsHelp = Nothing
8787
, hasForwarding = Nothing
8888
, resourceFreq = Nothing
@@ -114,7 +114,7 @@ propMultiInit ts@TestSetup{..} format rootDir localSock1 localSock2 = do
114114
, logging = NE.fromList [LoggingParams root FileMode format]
115115
, rotation = Nothing
116116
, verbosity = Just Minimum
117-
, metricsComp = Nothing
117+
, metricsNoSuffix = Nothing
118118
, metricsHelp = Nothing
119119
, hasForwarding = Nothing
120120
, resourceFreq = Nothing
@@ -146,7 +146,7 @@ propMultiResp ts@TestSetup{..} format rootDir localSock = do
146146
, logging = NE.fromList [LoggingParams root FileMode format]
147147
, rotation = Nothing
148148
, verbosity = Just Minimum
149-
, metricsComp = Nothing
149+
, metricsNoSuffix = Nothing
150150
, metricsHelp = Nothing
151151
, hasForwarding = Nothing
152152
, resourceFreq = Nothing

cardano-tracer/test/Cardano/Tracer/Test/Restart/Tests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ mkConfig TestSetup{..} rootDir p = TracerConfig
9696
, logging = NE.fromList [LoggingParams rootDir FileMode ForMachine]
9797
, rotation = Nothing
9898
, verbosity = Just Minimum
99-
, metricsComp = Nothing
99+
, metricsNoSuffix = Nothing
100100
, metricsHelp = Nothing
101101
, hasForwarding = Nothing
102102
, resourceFreq = Nothing

nix/nixos/cardano-tracer-service.nix

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ let serviceConfigToJSON =
3737
epHost = "127.0.0.1";
3838
epPort = 3200; ## supervisord.portShiftPrometheus
3939
} // (cfg.prometheus or {});
40-
# Just an example for metrics compatibility mapping.
41-
# An entry means the first entry has the second entry as alias.
42-
# The Metrics is then avalable, both with the original and the mapped name.
43-
# Only one mapping per message is supported.
44-
# metricsComp = {
45-
# "Mempool.TxsInMempool" = "Mempool.TxsInMempool.Mapped";
46-
# "ChainDB.SlotNum" = "ChainDB.SlotNum.Mapped";
47-
# };
4840
} // pkgs.lib.optionalAttrs ((cfg.RTView or {}) != {})
4941
{
5042
hasRTView = cfg.RTView;
@@ -76,6 +68,7 @@ in pkgs.commonLib.defServiceModule
7668
prometheus = opt attrs {} "Prometheus overrides: see cardano-tracer documentation.";
7769
resourceFreq = mayOpt int "Frequency (1/ms) for tracing resource usage.";
7870
metricsHelp = mayOpt str "JSON file containing metrics help annotations for Prometheus";
71+
metricsNoSuffix = mayOpt bool "Drop suffixes like '_int' in Prometheus exposition, increasing similiarity with legacy system names.";
7972

8073
### Here be dragons, on the other hand..
8174
configFile = mayOpt str

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ module Cardano.Logging.Prometheus.Exposition
55
) where
66

77
import Data.Char
8+
import Data.Foldable (asum)
89
import qualified Data.HashMap.Strict as HM
910
import Data.List (find)
10-
import Data.Map.Strict (Map)
11-
import qualified Data.Map.Strict as M
11+
import Data.Maybe
1212
import Data.Text (Text)
1313
import qualified Data.Text as T
1414
import qualified Data.Text.Lazy as TL
@@ -21,29 +21,29 @@ import System.Metrics (Sample, Value (..))
2121
type MetricName = Text
2222

2323

24-
renderExpositionFromSample :: Sample -> TL.Text
25-
renderExpositionFromSample = renderExpositionFromSampleWith Nothing []
24+
renderExpositionFromSample :: Bool -> Sample -> TL.Text
25+
renderExpositionFromSample = renderExpositionFromSampleWith []
2626

2727
renderExpositionFromSampleWith
28-
:: Maybe (Map MetricName MetricName)
29-
-> [(MetricName, Builder)]
28+
:: [(MetricName, Builder)]
29+
-> Bool
3030
-> Sample
3131
-> TL.Text
32-
renderExpositionFromSampleWith renameMap helpTextDict =
33-
TB.toLazyText . (`mappend` buildEOF) . HM.foldlWithKey' buildMetric mempty
32+
renderExpositionFromSampleWith helpTextDict noSuffixes =
33+
TB.toLazyText . (<> buildEOF) . HM.foldlWithKey' buildMetric mempty
3434
where
3535
buildHelpText :: MetricName -> (Builder -> Builder)
3636
buildHelpText name = maybe
3737
(const mempty)
3838
(buildHelp . snd)
3939
(find ((`T.isInfixOf` name) . fst) helpTextDict)
4040

41-
-- implements the metricsComp config option
42-
replaceName :: MetricName -> MetricName
43-
replaceName =
44-
case renameMap of
45-
Nothing -> Prelude.id
46-
Just mmap -> \name -> M.findWithDefault name name mmap
41+
-- implements the metricsNoSuffix config option
42+
-- must strip all suffixes as per: trace-dispatcher/src/Cardano/Logging/Tracer/EKG.hs > ekgTracer > setIt
43+
stripSuffix :: MetricName -> MetricName
44+
stripSuffix
45+
| noSuffixes = \name -> fromMaybe name $ asum $ map (`T.stripSuffix` name) ["_int", "_counter", "_real"]
46+
| otherwise = id
4747

4848
prepareName :: MetricName -> MetricName
4949
prepareName =
@@ -55,37 +55,39 @@ renderExpositionFromSampleWith renameMap helpTextDict =
5555
-- the help annotation line
5656
buildHelp :: Builder -> Builder -> Builder
5757
buildHelp h n =
58-
TB.fromText "# HELP " `mappend` (n `mappend` (space `mappend` (h `mappend` newline)))
58+
TB.fromText "# HELP " <> n <> space <> h <> newline
5959

6060
buildMetric :: TB.Builder -> MetricName -> Value -> TB.Builder
6161
buildMetric acc mName mValue =
62-
acc `mappend` case mValue of
63-
Counter c -> annotate buildCounter `mappend` buildVal space (TB.decimal c)
64-
Gauge g -> annotate buildGauge `mappend` buildVal space (TB.decimal g)
62+
acc <> case mValue of
63+
Counter c -> annotate buildCounter <> buildVal space (TB.decimal c)
64+
Gauge g -> annotate buildGauge <> buildVal space (TB.decimal g)
6565
Label l
6666
| Just ('{', _) <- T.uncons l
67-
-> annotate buildInfo `mappend` buildVal mempty (TB.fromText l)
67+
-> annotate buildInfo <> buildVal mempty (TB.fromText l)
6868
| otherwise
69-
-> helpAnnotation `mappend` buildVal space (TB.fromText l)
70-
_ -> mempty
69+
-> helpAnnotation <> buildVal space (TB.fromText l)
70+
_ -> mempty
7171
where
72-
helpAnnotation = buildHelpText mName buildName
72+
helpAnnotation =
73+
buildHelpText mName buildName
7374

7475
-- annotates a metric in the order TYPE, UNIT, HELP
7576
-- TODO: UNIT annotation
7677
annotate annType =
77-
buildTypeAnn annType `mappend` helpAnnotation
78+
buildTypeAnn annType <> helpAnnotation
7879

79-
-- the name for exposition
80-
buildName = TB.fromText $ prepareName $ replaceName mName
80+
-- the metric name for exposition
81+
buildName =
82+
TB.fromText $ prepareName $ stripSuffix mName
8183

8284
-- the type annotation line
8385
buildTypeAnn t =
84-
TB.fromText "# TYPE " `mappend` (buildName `mappend` (t `mappend` newline))
86+
TB.fromText "# TYPE " <> buildName <> t <> newline
8587

8688
-- the actual metric line, optional spacing after name, because of labels: 'metric_name{label_value="foo"} 1'
8789
buildVal spacing v =
88-
buildName `mappend` (spacing `mappend` (v `mappend` newline))
90+
buildName <> spacing <> v <> newline
8991

9092
buildGauge, buildCounter, buildInfo, buildEOF, newline, space :: Builder
9193
buildGauge = TB.fromText " gauge"

0 commit comments

Comments
 (0)