Skip to content

Commit 998ce32

Browse files
authored
Merge pull request #646 from input-output-hk/jutaro/ekg-labels
Add Prometheus labels for Cardano build info display
2 parents ef5cd89 + c1fab33 commit 998ce32

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

plugins/backend-ekg/lobemo-backend-ekg.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.0
22
name: lobemo-backend-ekg
3-
version: 0.1.0.3
3+
version: 0.1.1.0
44
synopsis: provides a backend implementation to EKG
55
-- description:
66
homepage: https://github.com/input-output-hk/iohk-monitoring-framework

plugins/backend-ekg/src/Cardano/BM/Backend/Prometheus.lhs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ data Metric
5050
| Metric
5151
{ mName :: !Text
5252
, mType :: !Text
53-
, mValue :: !Number
53+
, mLabel :: !(Maybe Text)
54+
, mNumber :: !Number
5455
}
5556
5657
instance A.ToJSON Metric where
5758
toJSON NoMetric = A.Null
58-
toJSON (Metric n t v) = A.object ["name" .= n, "type" .= t, "value" .= v]
59+
toJSON (Metric n t Nothing v) = A.object ["name" .= n, "type" .= t, "value" .= v]
60+
toJSON (Metric n t (Just l) v) = A.object ["name" .= n, "type" .= t, "label" .= l, "value" .= v]
5961
6062
data Number
6163
= NumberInt Integer
@@ -94,17 +96,29 @@ spawnPrometheus ekg host port prometheusOutput = Async.async $
9496
[ case sv of
9597
Counter c -> renderNamedValue sk (int64Dec c)
9698
Gauge g -> renderNamedValue sk (int64Dec g)
97-
Label l -> if isFloat l
98-
then renderNamedValue sk (byteString $ encodeUtf8 l)
99-
else mempty
99+
Label l -> if "{" `T.isPrefixOf` l
100+
then renderLabel sk l
101+
else if (isFloat l)
102+
then renderNamedValue sk (byteString $ encodeUtf8 l)
103+
else mempty
100104
_ -> mempty
101105
| (sk,sv) <- samples ]
106+
102107
renderNamedValue :: Text -> Builder -> Builder
103108
renderNamedValue nm bld =
104109
(byteString $ prepareName nm)
105110
<> charUtf8 ' '
106111
<> bld
107112
<> charUtf8 '\n'
113+
114+
renderLabel :: Text -> Text -> Builder
115+
renderLabel nm l =
116+
(byteString $ prepareName nm)
117+
<> charUtf8 ' '
118+
<> byteString (textToUtf8ByteString l)
119+
<> charUtf8 ' '
120+
<> charUtf8 '1'
121+
<> charUtf8 '\n'
108122
prepareName nm = encodeUtf8 $ T.filter (flip elem (['a'..'z']++['A'..'Z']++['_'])) $ T.replace " " "_" $ T.replace "-" "_" $ T.replace "." "_" nm
109123
isFloat v = case double v of
110124
Right (_n, "") -> True -- only floating point number parsed, no leftover
@@ -136,7 +150,8 @@ spawnPrometheus ekg host port prometheusOutput = Async.async $
136150
intMetric sk v =
137151
Metric { mName = maybe "" id $ T.stripPrefix (ns <> ".") sk
138152
, mType = "int" -- All values are Int64.
139-
, mValue = NumberInt (fromIntegral v)
153+
, mLabel = Nothing
154+
, mNumber = NumberInt (fromIntegral v)
140155
}
141156
142157
-- We cannot make any assumptions about the format of 'sk' in other samples,
@@ -146,26 +161,34 @@ spawnPrometheus ekg host port prometheusOutput = Async.async $
146161
{ namespace = "common"
147162
, metrics =
148163
[ case sv of
149-
Counter c -> mkMetric sk $ NumberInt (fromIntegral c)
150-
Gauge g -> mkMetric sk $ NumberInt (fromIntegral g)
164+
Counter c -> mkMetric sk Nothing $ NumberInt (fromIntegral c)
165+
Gauge g -> mkMetric sk Nothing $ NumberInt (fromIntegral g)
151166
Label l -> case double l of
152-
Left _ -> NoMetric
153-
Right (r, _) -> mkMetric sk $ NumberReal r
167+
Right (r, _) ->
168+
mkMetric sk Nothing $ NumberReal r
169+
Left _ ->
170+
case T.uncons l of
171+
Just ('{', _) -> mkMetric sk (Just l) (NumberInt 1)
172+
_ -> NoMetric
154173
_ -> NoMetric
155174
| (sk, sv) <- samples
156175
]
157176
}
158177
where
159-
mkMetric sk number =
178+
mkMetric sk condTxt number =
160179
let (withoutType, typeSuffix) = stripTypeSuffix sk number
161-
in Metric { mName = withoutType, mType = typeSuffix, mValue = number }
180+
in Metric { mName = withoutType, mType = typeSuffix, mLabel = condTxt, mNumber = number }
162181
stripTypeSuffix sk number =
163182
let types = ["us", "ns", "s", "B", "int", "real"]
164183
parts = T.splitOn "." sk
165184
typeSuffix = if not . null $ parts then last parts else ""
166185
in if typeSuffix `elem` types
167186
then (fromJust $ T.stripSuffix ("." <> typeSuffix) sk, typeSuffix)
168187
else case number of
169-
NumberInt _ -> (sk, "int")
170-
NumberReal _ -> (sk, "real")
188+
NumberInt _ -> (sk, "int")
189+
NumberReal _ -> (sk, "real")
190+
191+
textToUtf8ByteString :: Text -> ByteString
192+
textToUtf8ByteString txt = encodeUtf8 txt
193+
171194
\end{code}

0 commit comments

Comments
 (0)