]> git.immae.eu Git - github/fretlink/haskell-graylog.git/commitdiff
Allow sending numbers in additional fields support-additional-meta 1/head
authorThomas Crevoisier <thomas.crevoisier@fretlink.com>
Fri, 30 Nov 2018 16:01:18 +0000 (17:01 +0100)
committerThomas Crevoisier <thomas.crevoisier@fretlink.com>
Fri, 30 Nov 2018 16:04:41 +0000 (17:04 +0100)
src/Graylog/Gelf.hs

index b6e7ec09297175c44ebf8a92ca17b1da3e05c7ea..8ffe24fff75dbb1af61984ecf1682860207037df 100644 (file)
@@ -7,9 +7,10 @@
 -- see http://docs.graylog.org/en/latest/pages/gelf.html
 module Graylog.Gelf where
 
-import           Data.Aeson          (ToJSON (..), Value (..), object,
-                                      toJSON, (.=))
+import           Data.Aeson          (ToJSON (..), Value (..), object, toJSON,
+                                      (.=))
 import           Data.HashMap.Strict (HashMap)
+import           Data.Scientific     (Scientific)
 import           Data.Semigroup      ((<>))
 import           Data.Text           (Text)
 import           Data.Time
@@ -27,10 +28,34 @@ data GELF
       , _gelfLevel        :: Maybe SyslogLevel
       , _gelfLine         :: Maybe Word
       , _gelfFile         :: Maybe Text
-      , _gelfMeta         :: HashMap Text Text
+      , _gelfMeta         :: HashMap Text MetaValue
       }
    deriving (Show, Typeable, Generic)
 
+data MetaValue
+  = TextValue Text
+  | NumberValue Scientific
+  deriving (Show)
+
+instance ToJSON MetaValue where
+  toJSON (TextValue txt) = toJSON txt
+  toJSON (NumberValue n) = toJSON n
+
+class ToMeta a where
+  toMeta :: a -> MetaValue
+
+instance ToMeta Text where
+  toMeta = TextValue
+
+instance ToMeta Scientific where
+  toMeta = NumberValue
+
+instance ToMeta Integer where
+  toMeta = NumberValue . fromInteger
+
+instance ToMeta Int where
+  toMeta = toMeta . toInteger
+
 instance ToJSON GELF where
   toJSON GELF{..} = object $ [ "version"        .= _gelfVersion
                              , "host"           .= _gelfHost
@@ -40,7 +65,7 @@ instance ToJSON GELF where
                              , "level"          .= _gelfLevel
                              , "line"           .= _gelfLine
                              , "file"           .= _gelfFile
-                             ] <> toList (String <$> _gelfMeta)
+                             ] <> toList (toJSON <$> _gelfMeta)
 
 --