core-telemetry 0.2.3.7 → 0.2.5.0
raw patch · 2 files changed
+58/−10 lines, 2 filesdep +timedep ~core-dataPVP ok
version bump matches the API change (PVP)
Dependencies added: time
Dependency ranges changed: core-data
API changes (from Hackage documentation)
+ Core.Telemetry.Observability: class Telemetry σ
+ Core.Telemetry.Observability: clearTrace :: Program τ ()
+ Core.Telemetry.Observability: instance Core.Telemetry.Observability.Telemetry Data.Time.Clock.Internal.UTCTime.UTCTime
+ Core.Telemetry.Observability: instance Core.Telemetry.Observability.Telemetry σ => Core.Telemetry.Observability.Telemetry (GHC.Maybe.Maybe σ)
+ Core.Telemetry.Observability: metric :: Telemetry σ => Rope -> σ -> MetricValue
Files
core-telemetry.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: core-telemetry-version: 0.2.3.7+version: 0.2.5.0 synopsis: Advanced telemetry description: This is part of a library to help build command-line programs, both tools and longer-running daemons.@@ -52,7 +52,7 @@ async , base >=4.11 && <5 , bytestring- , core-data >=0.3.3.1+ , core-data >=0.3.4.0 , core-program >=0.5.0.2 , core-text >=0.3.7.1 , exceptions@@ -66,6 +66,7 @@ , stm , template-haskell >=2.14 && <3 , text+ , time , unix , zlib default-language: Haskell2010
lib/Core/Telemetry/Observability.hs view
@@ -1,6 +1,7 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralisedNewtypeDeriving #-}+{-# LANGUAGE ImportQualifiedPost #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE RankNTypes #-}@@ -160,28 +161,31 @@ -- * Events sendEvent, clearMetrics,+ clearTrace, ) where import Control.Concurrent.MVar (modifyMVar_, newMVar, readMVar) import Control.Concurrent.STM (atomically) import Control.Concurrent.STM.TQueue (writeTQueue)-import qualified Control.Exception.Safe as Safe+import Control.Exception.Safe qualified as Safe+import Core.Data.Clock import Core.Data.Structures (Map, emptyMap, insertKeyValue)+import Core.Encoding.External import Core.Encoding.Json import Core.Program.Arguments import Core.Program.Context-import Core.Data.Clock import Core.Program.Logging import Core.System.Base (SomeException, liftIO) import Core.Telemetry.Identifiers import Core.Text.Rope import Core.Text.Utilities (oxford, quote)-import qualified Data.ByteString as B (ByteString)-import qualified Data.ByteString.Lazy as L (ByteString)-import qualified Data.List as List (foldl')+import Data.ByteString qualified as B (ByteString)+import Data.ByteString.Lazy qualified as L (ByteString)+import Data.List qualified as List (foldl') import Data.Scientific (Scientific)-import qualified Data.Text as T (Text)-import qualified Data.Text.Lazy as U (Text)+import Data.Text qualified as T (Text)+import Data.Text.Lazy qualified as U (Text)+import Data.Time.Clock (UTCTime) import GHC.Int import GHC.Word import System.Random (randomIO)@@ -229,6 +233,14 @@ pure datum' ) +{- |+Adaptor class to take primitive values and send them as metrics. The+underlying types are either strings, numbers, or boolean so any instance will+need to externalize and then convert to one of these three.++(this class is what allows us to act pass in what look like polymorphic lists+of metrics to 'telemetry' and 'sendEvent')+-} class Telemetry σ where metric :: Rope -> σ -> MetricValue @@ -297,6 +309,22 @@ metric k v = MetricValue (JsonKey k) v {- |+Strip the constructor off if the value is Just, and send `null` if Nothing.++@since 0.2.5+-}+instance Telemetry σ => Telemetry (Maybe σ) where+ metric k v = case v of+ Nothing -> MetricValue (JsonKey k) JsonNull+ Just v' -> metric k v'++{- |+@since 0.2.5+-}+instance Telemetry UTCTime where+ metric k v = MetricValue (JsonKey k) (JsonString (formatExternal (intoTime v)))++{- | Activate the telemetry subsystem for use within the 'Core.Program.Execute.Program' monad. @@ -600,7 +628,7 @@ ] @ -The 'metric' function is a method provided by instances of the 'Telemtetry'+The 'metric' function is a method provided by instances of the 'Telemetry' typeclass which is mostly a wrapper around constructing key/value pairs suitable to be sent as measurements up to an observability service. -}@@ -729,3 +757,22 @@ modifyMVar_ v (\datum -> pure datum{attachedMetadataFrom = emptyMap})++{- |+Reset the program context so that the currently executing program is no longer+within a trace or span.++This is specifically for the occasion where you have forked a new thread but+have not yet received the event which would occasion starting a new trace.++@since 0.2.4+-}+clearTrace :: Program τ ()+clearTrace = do+ context <- getContext++ liftIO $ do+ let v = currentDatumFrom context+ modifyMVar_+ v+ (\_ -> pure emptyDatum)