Skip to content

Commit

Permalink
Added ReadableLogRecord and ReadWriteLogRecord interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlauer1 committed Jul 3, 2024
1 parent 22f054c commit 3c7470b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
31 changes: 28 additions & 3 deletions api/src/OpenTelemetry/Internal/Logging/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
module OpenTelemetry.Internal.Logging.Types (
LoggerProvider (..),
Logger (..),
LogRecord (..),
LogRecord,
mkLogRecord,
ReadableLogRecord (..),
ReadWriteLogRecord (..),
ImmutableLogRecord (..),
LogRecordArguments (..),
emptyLogRecordArguments,
Expand All @@ -14,7 +17,7 @@ module OpenTelemetry.Internal.Logging.Types (

import Data.Function (on)
import qualified Data.HashMap.Strict as H
import Data.IORef (IORef)
import Data.IORef (IORef, atomicModifyIORef, modifyIORef, newIORef, readIORef)
import Data.Int (Int64)
import Data.Text (Text)
import OpenTelemetry.Common (Timestamp, TraceFlags)
Expand Down Expand Up @@ -47,7 +50,29 @@ data Logger = Logger
{- | This is a data type that can represent logs from various sources: application log files, machine generated events, system logs, etc. [Specification outlined here.](https://opentelemetry.io/docs/specs/otel/logs/data-model/)
Existing log formats can be unambiguously mapped to this data type. Reverse mapping from this data type is also possible to the extent that the target log format has equivalent capabilities.
-}
data LogRecord a = LogRecord (IORef (ImmutableLogRecord a))
newtype LogRecord a = LogRecord (IORef (ImmutableLogRecord a))


mkLogRecord :: ImmutableLogRecord body -> IO (LogRecord body)
mkLogRecord = fmap LogRecord . newIORef


class ReadableLogRecord r where
readLogRecord :: r a -> IO (ImmutableLogRecord a)


class (ReadableLogRecord r) => ReadWriteLogRecord r where
modifyLogRecord :: r a -> (ImmutableLogRecord a -> ImmutableLogRecord a) -> IO ()
atomicModifyLogRecord :: r a -> (ImmutableLogRecord a -> (ImmutableLogRecord a, b)) -> IO b


instance ReadableLogRecord LogRecord where
readLogRecord (LogRecord ref) = readIORef ref


instance ReadWriteLogRecord LogRecord where
modifyLogRecord (LogRecord ref) = modifyIORef ref
atomicModifyLogRecord (LogRecord ref) = atomicModifyIORef ref


data ImmutableLogRecord body = ImmutableLogRecord
Expand Down
23 changes: 12 additions & 11 deletions api/src/OpenTelemetry/Logging/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module OpenTelemetry.Logging.Core (
makeLogger,

-- * @LogRecord@ operations
LogRecord (..),
LogRecord,
ReadableLogRecord (..),
ReadWriteLogRecord (..),
LogRecordArguments (..),
SeverityNumber (..),
toShortName,
Expand Down Expand Up @@ -193,8 +195,7 @@ emitLogRecord
-> m (LogRecord body)
emitLogRecord l args = do
ilr <- createImmutableLogRecord l args
lr <- liftIO $ newIORef ilr
pure $ LogRecord lr
liftIO $ mkLogRecord ilr


{- | Add an attribute to a @LogRecord@.
Expand All @@ -215,10 +216,10 @@ For example, the 'otel.library.name' attribute is used to record the instrumenta
Any additions to the 'otel.*' namespace MUST be approved as part of OpenTelemetry specification.
-}
addAttribute :: (MonadIO m, ToValue a) => LogRecord body -> Text -> a -> m ()
addAttribute (LogRecord lr) k v =
addAttribute :: (ReadWriteLogRecord r, MonadIO m, ToValue a) => r body -> Text -> a -> m ()
addAttribute lr k v =
liftIO $
modifyIORef'
modifyLogRecord
lr
( \ilr@ImmutableLogRecord {logRecordAttributes, logRecordLogger} ->
ilr
Expand All @@ -236,10 +237,10 @@ addAttribute (LogRecord lr) k v =
This function may be slightly more performant than repeatedly calling 'addAttribute'.
-}
addAttributes :: (MonadIO m, ToValue a) => LogRecord body -> HashMap Text a -> m ()
addAttributes (LogRecord lr) attrs =
addAttributes :: (ReadWriteLogRecord r, MonadIO m, ToValue a) => r body -> HashMap Text a -> m ()
addAttributes lr attrs =
liftIO $
modifyIORef'
modifyLogRecord
lr
( \ilr@ImmutableLogRecord {logRecordAttributes, logRecordLogger} ->
ilr
Expand All @@ -256,5 +257,5 @@ addAttributes (LogRecord lr) attrs =
using it to copy / otherwise use the data to further enrich
instrumentation.
-}
logRecordGetAttributes :: (MonadIO m) => LogRecord a -> m LogAttributes
logRecordGetAttributes (LogRecord lr) = liftIO $ logRecordAttributes <$> readIORef lr
logRecordGetAttributes :: (ReadableLogRecord r, MonadIO m) => r a -> m LogAttributes
logRecordGetAttributes lr = liftIO $ logRecordAttributes <$> readLogRecord lr

0 comments on commit 3c7470b

Please sign in to comment.