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 15, 2024
1 parent f931c5c commit db00438
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 @@ -84,7 +84,7 @@ Creating two @Logger@s with the same identity but different @libraryAttributes@
data Logger = Logger
{ loggerInstrumentationScope :: InstrumentationLibrary
-- ^ Details about the library that the @Logger@ instruments.
, loggerProvider :: LoggerProvider
, loggerLoggerProvider :: LoggerProvider
-- ^ The @LoggerProvider@ that created this @Logger@. All configuration for the @Logger@ is contained in the @LoggerProvider@.
}

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


instance IsReadWriteLogRecord ReadWriteLogRecord where
readLogRecordAttributeLimits (ReadWriteLogRecord Logger {loggerProvider = LoggerProvider {loggerProviderAttributeLimits}} _) = loggerProviderAttributeLimits
readLogRecordAttributeLimits (ReadWriteLogRecord Logger {loggerLoggerProvider = LoggerProvider {loggerProviderAttributeLimits}} _) = loggerProviderAttributeLimits
modifyLogRecord (ReadWriteLogRecord _ ref) = modifyIORef ref
atomicModifyLogRecord (ReadWriteLogRecord _ 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 @@ -40,7 +40,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 @@ -173,7 +173,7 @@ makeLogger
-> InstrumentationLibrary
-- ^ The library that the @Logger@ instruments. This uniquely identifies the @Logger@.
-> Logger
makeLogger loggerProvider loggerInstrumentationScope = Logger {..}
makeLogger loggerLoggerProvider loggerInstrumentationScope = Logger {..}


createImmutableLogRecord
Expand Down Expand Up @@ -236,18 +236,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
-> LogRecordArguments
-> m ReadWriteLogRecord
emitLogRecord l args = do
ilr <- createImmutableLogRecord (loggerProviderAttributeLimits $ loggerProvider l) args
liftIO $ mkReadWriteLogRecord l ilr
let LoggerProvider {loggerProviderProcessors, loggerProviderAttributeLimits} = loggerLoggerProvider l

ilr <- createImmutableLogRecord loggerProviderAttributeLimits args
lr <- liftIO $ mkReadWriteLogRecord 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 db00438

Please sign in to comment.