Skip to content

Commit

Permalink
Call processors' onEmit when a log is emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlauer1 committed Jul 10, 2024
1 parent 81072e0 commit cb620e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 3 additions & 3 deletions api/src/OpenTelemetry/Internal/Logging/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Creating two @Logger@s with the same identity but different @libraryAttributes@
data Logger body = Logger
{ loggerInstrumentationScope :: InstrumentationLibrary
-- ^ Details about the library that the @Logger@ instruments.
, loggerProvider :: LoggerProvider body
, loggerLoggerProvider :: LoggerProvider body
-- ^ The @LoggerProvider@ that created this @Logger@. All configuration for the @Logger@ is contained in the @LoggerProvider@.
}

Expand Down Expand Up @@ -149,11 +149,11 @@ class (ReadableLogRecord r) => ReadWriteLogRecord r where
instance ReadableLogRecord LogRecord where
readLogRecord (LogRecord _ ref) = readIORef ref
readLogRecordInstrumentationScope (LogRecord (Logger {loggerInstrumentationScope}) _) = loggerInstrumentationScope
readLogRecordResource (LogRecord Logger {loggerProvider = LoggerProvider {loggerProviderResource}} _) = loggerProviderResource
readLogRecordResource (LogRecord Logger {loggerLoggerProvider = LoggerProvider {loggerProviderResource}} _) = loggerProviderResource


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

Expand Down
20 changes: 15 additions & 5 deletions api/src/OpenTelemetry/Logging/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module OpenTelemetry.Logging.Core (

import Control.Applicative
import Control.Concurrent.Async
import Control.Monad (void, when)
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.Maybe
import Data.Coerce
Expand Down Expand Up @@ -170,7 +170,7 @@ makeLogger
-> InstrumentationLibrary
-- ^ The library that the @Logger@ instruments. This uniquely identifies the @Logger@.
-> Logger body
makeLogger loggerProvider loggerInstrumentationScope = Logger {..}
makeLogger loggerLoggerProvider loggerInstrumentationScope = Logger {..}


createImmutableLogRecord
Expand Down Expand Up @@ -233,18 +233,28 @@ emitOTelLogRecord attrs severity body = do
}


{- | Emits a LogRecord with properties specified by the passed in Logger and LogRecordArguments.
{- | Emits a @LogRecord@ with properties specified by the passed in Logger and LogRecordArguments.
If observedTimestamp is not set in LogRecordArguments, it will default to the current timestamp.
If context is not specified in LogRecordArguments it will default to the current context.
The emitted @LogRecord@ will be passed to any @LogRecordProcessor@s registered on the @LoggerProvider@
that created the @Logger@.
-}
emitLogRecord
:: (MonadIO m)
=> Logger body
-> LogRecordArguments body
-> m (LogRecord body)
emitLogRecord l args = do
ilr <- createImmutableLogRecord (loggerProviderAttributeLimits $ loggerProvider l) args
liftIO $ mkLogRecord l ilr
let LoggerProvider {loggerProviderProcessors, loggerProviderAttributeLimits} = loggerLoggerProvider l

ilr <- createImmutableLogRecord loggerProviderAttributeLimits args
lr <- liftIO $ mkLogRecord l ilr

ctxt <- getContext
mapM_ (\processor -> liftIO $ logRecordProcessorOnEmit processor lr ctxt) loggerProviderProcessors

pure lr


{- | Add an attribute to a @LogRecord@.
Expand Down

0 comments on commit cb620e4

Please sign in to comment.