]> git.immae.eu Git - github/fretlink/haskell-graylog.git/commitdiff
Merge pull request #1 from fretlink/support-additional-meta v2019-01-15
authorThomas Crevoisier <crevoisier.thomas@gmail.com>
Tue, 15 Jan 2019 14:30:19 +0000 (15:30 +0100)
committerGitHub <noreply@github.com>
Tue, 15 Jan 2019 14:30:19 +0000 (15:30 +0100)
Support additional meta in GELF messages

graylog.cabal
src/Graylog/Gelf.hs
stack.yaml

index 5618d7c60e95d1c2661c6f7cbf0585d8848b4210..dc848626bc558a11065b8fa89fcd4d63de64991e 100644 (file)
@@ -17,7 +17,7 @@ source-repository head
     type:               git
     location:           https://github.com/AndrewRademacher/haskell-graylog.git
 
-library 
+library
    hs-source-dirs:      src
    default-language:    Haskell2010
 
@@ -29,7 +29,7 @@ library
    ghc-options:         -Wall -rtsopts
 
    build-depends:       base            ==4.*
-   
+
                      ,  aeson
                      ,  aeson-casing
                      ,  bytestring
@@ -38,6 +38,7 @@ library
                      ,  scientific
                      ,  text
                      ,  time
+                     ,  unordered-containers
                      ,  vector
 
 test-suite test-state
index ee17e3d347919b8f7deda25d5f362a745740504a..8ffe24fff75dbb1af61984ecf1682860207037df 100644 (file)
@@ -1,17 +1,21 @@
 {-# LANGUAGE DeriveDataTypeable #-}
 {-# LANGUAGE DeriveGeneric      #-}
 {-# LANGUAGE OverloadedStrings  #-}
+{-# LANGUAGE RecordWildCards    #-}
 
 -- | Default formatting for Graylog messages,
 -- see http://docs.graylog.org/en/latest/pages/gelf.html
 module Graylog.Gelf where
 
-import           Data.Aeson        (ToJSON (..), Value (..), genericToJSON,
-                                    toJSON)
-import           Data.Aeson.Casing
-import           Data.Text         (Text)
+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
 import           Data.Typeable
+import           GHC.Exts            (toList)
 import           GHC.Generics
 
 data GELF
@@ -24,11 +28,44 @@ data GELF
       , _gelfLevel        :: Maybe SyslogLevel
       , _gelfLine         :: Maybe Word
       , _gelfFile         :: Maybe 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 = genericToJSON $ aesonPrefix snakeCase
+  toJSON GELF{..} = object $ [ "version"        .= _gelfVersion
+                             , "host"           .= _gelfHost
+                             , "short_message"  .= _gelfShortMessage
+                             , "full_message"   .= _gelfFullMessage
+                             , "timestamp"      .= _gelfTimestamp
+                             , "level"          .= _gelfLevel
+                             , "line"           .= _gelfLine
+                             , "file"           .= _gelfFile
+                             ] <> toList (toJSON <$> _gelfMeta)
 
 --
 
@@ -69,4 +106,4 @@ simpleGelf
    -> Text     -- ^ Short message
    -> GELF
 simpleGelf host short =
-   GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing
+  GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing mempty
index 55ab633cdb7a0c50a371c1519282f84602cd0e74..2ccd75c3c3d0b1bf72458bdbd0226f4201c0cc84 100644 (file)
@@ -2,7 +2,7 @@
 # For more information, see: http://docs.haskellstack.org/en/stable/yaml_configuration/
 
 # Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
-resolver: lts-5.4
+resolver: lts-11.5
 
 # Local packages, usually specified by relative directory name
 packages: