@@ -33,6 +33,8 @@ import Snap.Http.Server (Config, ConfigLog (..), defaultConfig, setAcc
33
33
import System.Metrics (Value (.. ), sampleAll )
34
34
import qualified System.Remote.Monitoring as EKG
35
35
36
+ import Debug.Trace
37
+
36
38
\end {code}
37
39
% endif
38
40
@@ -50,12 +52,14 @@ data Metric
50
52
| Metric
51
53
{ mName :: ! Text
52
54
, mType :: ! Text
53
- , mValue :: ! Number
55
+ , mLabel :: ! (Maybe Text )
56
+ , mNumber :: ! Number
54
57
}
55
58
56
59
instance A. ToJSON Metric where
57
60
toJSON NoMetric = A. Null
58
- toJSON (Metric n t v) = A. object [" name" .= n, " type" .= t, " value" .= v]
61
+ toJSON (Metric n t Nothing v) = A. object [" name" .= n, " type" .= t, " value" .= v]
62
+ toJSON (Metric n t (Just l) v) = A. object [" name" .= n, " type" .= t, " label" .= l, " value" .= v]
59
63
60
64
data Number
61
65
= NumberInt Integer
@@ -94,17 +98,28 @@ spawnPrometheus ekg host port prometheusOutput = Async.async $
94
98
[ case sv of
95
99
Counter c -> renderNamedValue sk (int64Dec c)
96
100
Gauge g -> renderNamedValue sk (int64Dec g)
97
- Label l -> if isFloat l
98
- then renderNamedValue sk (byteString $ encodeUtf8 l)
99
- else mempty
101
+ Label l -> trace (" renderSamples " <> T. unpack l) $
102
+ if " {" `T.isPrefixOf` l
103
+ then renderLabel sk l
104
+ else renderNamedValue sk (byteString $ encodeUtf8 l)
100
105
_ -> mempty
101
106
| (sk,sv) <- samples ]
107
+
102
108
renderNamedValue :: Text -> Builder -> Builder
103
109
renderNamedValue nm bld =
104
110
(byteString $ prepareName nm)
105
111
<> charUtf8 ' '
106
112
<> bld
107
113
<> charUtf8 ' \n '
114
+
115
+ renderLabel :: Text -> Text -> Builder
116
+ renderLabel nm l = trace " renderLabel" $
117
+ (byteString $ prepareName nm)
118
+ <> charUtf8 ' '
119
+ <> byteString (textToUtf8ByteString l)
120
+ <> charUtf8 ' '
121
+ <> charUtf8 ' 1'
122
+ <> charUtf8 ' \n '
108
123
prepareName nm = encodeUtf8 $ T. filter (flip elem ([' a' .. ' z' ]++ [' A' .. ' Z' ]++ [' _' ])) $ T. replace " " " _" $ T. replace " -" " _" $ T. replace " ." " _" nm
109
124
isFloat v = case double v of
110
125
Right (_n, " " ) -> True -- only floating point number parsed, no leftover
@@ -136,7 +151,8 @@ spawnPrometheus ekg host port prometheusOutput = Async.async $
136
151
intMetric sk v =
137
152
Metric { mName = maybe " " id $ T. stripPrefix (ns <> " ." ) sk
138
153
, mType = " int" -- All values are Int64.
139
- , mValue = NumberInt (fromIntegral v)
154
+ , mLabel = Nothing
155
+ , mNumber = NumberInt (fromIntegral v)
140
156
}
141
157
142
158
-- We cannot make any assumptions about the format of 'sk' in other samples,
@@ -146,26 +162,34 @@ spawnPrometheus ekg host port prometheusOutput = Async.async $
146
162
{ namespace = " common"
147
163
, metrics =
148
164
[ case sv of
149
- Counter c -> mkMetric sk $ NumberInt (fromIntegral c)
150
- Gauge g -> mkMetric sk $ NumberInt (fromIntegral g)
165
+ Counter c -> mkMetric sk Nothing $ NumberInt (fromIntegral c)
166
+ Gauge g -> mkMetric sk Nothing $ NumberInt (fromIntegral g)
151
167
Label l -> case double l of
152
- Left _ -> NoMetric
153
- Right (r, _) -> mkMetric sk $ NumberReal r
168
+ Right (r, _) ->
169
+ mkMetric sk Nothing $ NumberReal r
170
+ Left _ ->
171
+ case T. uncons l of
172
+ Just (' {' , _) -> mkMetric sk (Just l) (NumberInt 1 )
173
+ _ -> NoMetric
154
174
_ -> NoMetric
155
175
| (sk, sv) <- samples
156
176
]
157
177
}
158
178
where
159
- mkMetric sk number =
179
+ mkMetric sk condTxt number =
160
180
let (withoutType, typeSuffix) = stripTypeSuffix sk number
161
- in Metric { mName = withoutType, mType = typeSuffix, mValue = number }
181
+ in Metric { mName = withoutType, mType = typeSuffix, mLabel = condTxt, mNumber = number }
162
182
stripTypeSuffix sk number =
163
183
let types = [" us" , " ns" , " s" , " B" , " int" , " real" ]
164
184
parts = T. splitOn " ." sk
165
185
typeSuffix = if not . null $ parts then last parts else " "
166
186
in if typeSuffix `elem` types
167
187
then (fromJust $ T. stripSuffix (" ." <> typeSuffix) sk, typeSuffix)
168
188
else case number of
169
- NumberInt _ -> (sk, " int" )
170
- NumberReal _ -> (sk, " real" )
189
+ NumberInt _ -> (sk, " int" )
190
+ NumberReal _ -> (sk, " real" )
191
+
192
+ textToUtf8ByteString :: Text -> ByteString
193
+ textToUtf8ByteString txt = encodeUtf8 txt
194
+
171
195
\end {code}
0 commit comments