Skip to content

Commit

Permalink
Changed Readable and ReadWriteLogRecord to be concrete types
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlauer1 committed Jul 15, 2024
1 parent 0c82615 commit 0ab77ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
14 changes: 7 additions & 7 deletions api/src/OpenTelemetry/Internal/Logging/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ createImmutableLogRecord attributeLimits LogRecordArguments {..} = do


-- | WARNING: this function should only be used to emit logs from the hs-opentelemetry-api library. DO NOT USE this function in any other context.
logDroppedAttributes :: (MonadIO m) => m LogRecord
logDroppedAttributes :: (MonadIO m) => m ReadWriteLogRecord
logDroppedAttributes = emitOTelLogRecord H.empty Warn "At least 1 attribute was discarded due to the attribute limits set in the logger provider."


-- | WARNING: this function should only be used to emit logs from the hs-opentelemetry-api library. DO NOT USE this function in any other context.
emitOTelLogRecord :: (MonadIO m) => H.HashMap Text LA.AnyValue -> SeverityNumber -> Text -> m LogRecord
emitOTelLogRecord :: (MonadIO m) => H.HashMap Text LA.AnyValue -> SeverityNumber -> Text -> m ReadWriteLogRecord
emitOTelLogRecord attrs severity body = do
glp <- getGlobalLoggerProvider
let gl =
Expand All @@ -173,10 +173,10 @@ emitLogRecord
:: (MonadIO m)
=> Logger
-> LogRecordArguments
-> m LogRecord
-> m ReadWriteLogRecord
emitLogRecord l args = do
ilr <- createImmutableLogRecord (loggerProviderAttributeLimits $ loggerProvider l) args
liftIO $ mkLogRecord l ilr
liftIO $ mkReadWriteLogRecord l ilr


{- | Add an attribute to a @LogRecord@.
Expand All @@ -199,7 +199,7 @@ 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 :: (ReadWriteLogRecord r, MonadIO m, ToValue a) => r -> Text -> a -> m ()
addAttribute :: (IsReadWriteLogRecord r, MonadIO m, ToValue a) => r -> Text -> a -> m ()
addAttribute lr k v =
let attributeLimits = readLogRecordAttributeLimits lr
in liftIO $
Expand All @@ -223,7 +223,7 @@ This function may be slightly more performant than repeatedly calling 'addAttrib
This is not an atomic modification
-}
addAttributes :: (ReadWriteLogRecord r, MonadIO m, ToValue a) => r -> HashMap Text a -> m ()
addAttributes :: (IsReadWriteLogRecord r, MonadIO m, ToValue a) => r -> HashMap Text a -> m ()
addAttributes lr attrs =
let attributeLimits = readLogRecordAttributeLimits lr
in liftIO $
Expand All @@ -244,5 +244,5 @@ addAttributes lr attrs =
using it to copy / otherwise use the data to further enrich
instrumentation.
-}
logRecordGetAttributes :: (ReadableLogRecord r, MonadIO m) => r -> m LogAttributes
logRecordGetAttributes :: (IsReadableLogRecord r, MonadIO m) => r -> m LogAttributes
logRecordGetAttributes lr = liftIO $ logRecordAttributes <$> readLogRecord lr
49 changes: 32 additions & 17 deletions api/src/OpenTelemetry/Internal/Logging/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
module OpenTelemetry.Internal.Logging.Types (
LoggerProvider (..),
Logger (..),
LogRecord,
mkLogRecord,
ReadableLogRecord (..),
ReadWriteLogRecord (..),
ReadWriteLogRecord,
mkReadWriteLogRecord,
ReadableLogRecord,
mkReadableLogRecord,
IsReadableLogRecord (..),
IsReadWriteLogRecord (..),
ImmutableLogRecord (..),
LogRecordArguments (..),
emptyLogRecordArguments,
Expand Down Expand Up @@ -55,11 +57,18 @@ data Logger = Logger
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.
Uses an IORef under the hood to allow mutability.
-}
data LogRecord = LogRecord Logger (IORef ImmutableLogRecord)
data ReadWriteLogRecord = ReadWriteLogRecord Logger (IORef ImmutableLogRecord)


mkLogRecord :: Logger -> ImmutableLogRecord -> IO LogRecord
mkLogRecord l = fmap (LogRecord l) . newIORef
mkReadWriteLogRecord :: Logger -> ImmutableLogRecord -> IO ReadWriteLogRecord
mkReadWriteLogRecord l = fmap (ReadWriteLogRecord l) . newIORef


newtype ReadableLogRecord = ReadableLogRecord {readableLogRecord :: ReadWriteLogRecord}


mkReadableLogRecord :: Logger -> ImmutableLogRecord -> IO ReadableLogRecord
mkReadableLogRecord l = fmap ReadableLogRecord . mkReadWriteLogRecord l


{- | This is a typeclass representing @LogRecord@s that can be read from.
Expand All @@ -70,7 +79,7 @@ The trace context fields MUST be populated from the resolved Context (either the
Counts for attributes due to collection limits MUST be available for exporters to report as described in the transformation to non-OTLP formats specification.
-}
class ReadableLogRecord r where
class IsReadableLogRecord r where
-- | Reads the current state of the @LogRecord@ from its internal @IORef@. The implementation mirrors @readIORef@.
readLogRecord :: r -> IO ImmutableLogRecord

Expand All @@ -97,7 +106,7 @@ A function receiving this as an argument MUST additionally be able to modify the
- SpanId
- TraceFlags
-}
class (ReadableLogRecord r) => ReadWriteLogRecord r where
class (IsReadableLogRecord r) => IsReadWriteLogRecord r where
-- | Reads the attribute limits from the @LoggerProvider@ that emitted the @LogRecord@. These are needed to add more attributes.
readLogRecordAttributeLimits :: r -> AttributeLimits

Expand All @@ -110,16 +119,22 @@ class (ReadableLogRecord r) => ReadWriteLogRecord r where
atomicModifyLogRecord :: r -> (ImmutableLogRecord -> (ImmutableLogRecord, b)) -> IO b


instance ReadableLogRecord LogRecord where
readLogRecord (LogRecord _ ref) = readIORef ref
readLogRecordInstrumentationScope (LogRecord (Logger {loggerInstrumentationScope}) _) = loggerInstrumentationScope
readLogRecordResource (LogRecord Logger {loggerProvider = LoggerProvider {loggerProviderResource}} _) = loggerProviderResource
instance IsReadableLogRecord ReadableLogRecord where
readLogRecord = readLogRecord . readableLogRecord
readLogRecordInstrumentationScope = readLogRecordInstrumentationScope . readableLogRecord
readLogRecordResource = readLogRecordResource . readableLogRecord


instance IsReadableLogRecord ReadWriteLogRecord where
readLogRecord (ReadWriteLogRecord _ ref) = readIORef ref
readLogRecordInstrumentationScope (ReadWriteLogRecord (Logger {loggerInstrumentationScope}) _) = loggerInstrumentationScope
readLogRecordResource (ReadWriteLogRecord Logger {loggerProvider = LoggerProvider {loggerProviderResource}} _) = loggerProviderResource


instance ReadWriteLogRecord LogRecord where
readLogRecordAttributeLimits (LogRecord Logger {loggerProvider = LoggerProvider {loggerProviderAttributeLimits}} _) = loggerProviderAttributeLimits
modifyLogRecord (LogRecord _ ref) = modifyIORef ref
atomicModifyLogRecord (LogRecord _ ref) = atomicModifyIORef ref
instance IsReadWriteLogRecord ReadWriteLogRecord where
readLogRecordAttributeLimits (ReadWriteLogRecord Logger {loggerProvider = LoggerProvider {loggerProviderAttributeLimits}} _) = loggerProviderAttributeLimits
modifyLogRecord (ReadWriteLogRecord _ ref) = modifyIORef ref
atomicModifyLogRecord (ReadWriteLogRecord _ ref) = atomicModifyIORef ref


data ImmutableLogRecord = ImmutableLogRecord
Expand Down
9 changes: 6 additions & 3 deletions api/src/OpenTelemetry/Logging/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ module OpenTelemetry.Logging.Core (
makeLogger,

-- * @LogRecord@ operations
LogRecord,
ReadableLogRecord (..),
ReadWriteLogRecord (..),
ReadableLogRecord,
ReadWriteLogRecord,
IsReadableLogRecord (..),
IsReadWriteLogRecord (..),
LogRecordArguments (..),
AnyValue (..),
ToValue (..),
SeverityNumber (..),
toShortName,
emitLogRecord,
Expand Down

0 comments on commit 0ab77ca

Please sign in to comment.