{-# LANGUAGE NamedFieldPuns #-}
module OpenTelemetry.Internal.Logs.Types (
LogRecordExporter,
LogRecordExporterArguments (..),
mkLogRecordExporter,
logRecordExporterExport,
logRecordExporterForceFlush,
logRecordExporterShutdown,
LogRecordProcessor (..),
LoggerProvider (..),
Logger (..),
ReadWriteLogRecord,
mkReadWriteLogRecord,
ReadableLogRecord,
mkReadableLogRecord,
IsReadableLogRecord (..),
IsReadWriteLogRecord (..),
ImmutableLogRecord (..),
LogRecordArguments (..),
emptyLogRecordArguments,
SeverityNumber (..),
toShortName,
) where
import Control.Concurrent (MVar, newMVar, withMVar)
import Control.Concurrent.Async
import Data.Function (on)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as H
import Data.IORef (IORef, atomicModifyIORef, modifyIORef, newIORef, readIORef)
import Data.Text (Text)
import Data.Vector (Vector)
import OpenTelemetry.Common (Timestamp, TraceFlags)
import OpenTelemetry.Context.Types (Context)
import OpenTelemetry.Internal.Common.Types (ExportResult, InstrumentationLibrary, ShutdownResult)
import OpenTelemetry.Internal.Trace.Id (SpanId, TraceId)
import OpenTelemetry.LogAttributes
import OpenTelemetry.Resource (MaterializedResources)
data LogRecordExporterArguments = LogRecordExporterArguments
{ LogRecordExporterArguments
-> Vector ReadableLogRecord -> IO ExportResult
logRecordExporterArgumentsExport :: Vector ReadableLogRecord -> IO ExportResult
, LogRecordExporterArguments -> IO ()
logRecordExporterArgumentsForceFlush :: IO ()
, LogRecordExporterArguments -> IO ()
logRecordExporterArgumentsShutdown :: IO ()
}
newtype LogRecordExporter = LogRecordExporter {LogRecordExporter -> MVar LogRecordExporterArguments
unExporter :: MVar LogRecordExporterArguments}
mkLogRecordExporter :: LogRecordExporterArguments -> IO LogRecordExporter
mkLogRecordExporter :: LogRecordExporterArguments -> IO LogRecordExporter
mkLogRecordExporter = (MVar LogRecordExporterArguments -> LogRecordExporter)
-> IO (MVar LogRecordExporterArguments) -> IO LogRecordExporter
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MVar LogRecordExporterArguments -> LogRecordExporter
LogRecordExporter (IO (MVar LogRecordExporterArguments) -> IO LogRecordExporter)
-> (LogRecordExporterArguments
-> IO (MVar LogRecordExporterArguments))
-> LogRecordExporterArguments
-> IO LogRecordExporter
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogRecordExporterArguments -> IO (MVar LogRecordExporterArguments)
forall a. a -> IO (MVar a)
newMVar
logRecordExporterExport :: LogRecordExporter -> Vector ReadableLogRecord -> IO ExportResult
logRecordExporterExport :: LogRecordExporter -> Vector ReadableLogRecord -> IO ExportResult
logRecordExporterExport LogRecordExporter
exporter Vector ReadableLogRecord
lrs = MVar LogRecordExporterArguments
-> (LogRecordExporterArguments -> IO ExportResult)
-> IO ExportResult
forall a b. MVar a -> (a -> IO b) -> IO b
withMVar (LogRecordExporter -> MVar LogRecordExporterArguments
unExporter LogRecordExporter
exporter) ((LogRecordExporterArguments -> IO ExportResult)
-> IO ExportResult)
-> (LogRecordExporterArguments -> IO ExportResult)
-> IO ExportResult
forall a b. (a -> b) -> a -> b
$ \LogRecordExporterArguments
e -> LogRecordExporterArguments
-> Vector ReadableLogRecord -> IO ExportResult
logRecordExporterArgumentsExport LogRecordExporterArguments
e Vector ReadableLogRecord
lrs
logRecordExporterForceFlush :: LogRecordExporter -> IO ()
logRecordExporterForceFlush :: LogRecordExporter -> IO ()
logRecordExporterForceFlush = (MVar LogRecordExporterArguments
-> (LogRecordExporterArguments -> IO ()) -> IO ())
-> (LogRecordExporterArguments -> IO ())
-> MVar LogRecordExporterArguments
-> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip MVar LogRecordExporterArguments
-> (LogRecordExporterArguments -> IO ()) -> IO ()
forall a b. MVar a -> (a -> IO b) -> IO b
withMVar LogRecordExporterArguments -> IO ()
logRecordExporterArgumentsForceFlush (MVar LogRecordExporterArguments -> IO ())
-> (LogRecordExporter -> MVar LogRecordExporterArguments)
-> LogRecordExporter
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogRecordExporter -> MVar LogRecordExporterArguments
unExporter
logRecordExporterShutdown :: LogRecordExporter -> IO ()
logRecordExporterShutdown :: LogRecordExporter -> IO ()
logRecordExporterShutdown = (MVar LogRecordExporterArguments
-> (LogRecordExporterArguments -> IO ()) -> IO ())
-> (LogRecordExporterArguments -> IO ())
-> MVar LogRecordExporterArguments
-> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip MVar LogRecordExporterArguments
-> (LogRecordExporterArguments -> IO ()) -> IO ()
forall a b. MVar a -> (a -> IO b) -> IO b
withMVar LogRecordExporterArguments -> IO ()
logRecordExporterArgumentsShutdown (MVar LogRecordExporterArguments -> IO ())
-> (LogRecordExporter -> MVar LogRecordExporterArguments)
-> LogRecordExporter
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogRecordExporter -> MVar LogRecordExporterArguments
unExporter
data LogRecordProcessor = LogRecordProcessor
{ LogRecordProcessor -> ReadWriteLogRecord -> Context -> IO ()
logRecordProcessorOnEmit :: ReadWriteLogRecord -> Context -> IO ()
, LogRecordProcessor -> IO (Async ShutdownResult)
logRecordProcessorShutdown :: IO (Async ShutdownResult)
, LogRecordProcessor -> IO ()
logRecordProcessorForceFlush :: IO ()
}
data LoggerProvider = LoggerProvider
{ LoggerProvider -> Vector LogRecordProcessor
loggerProviderProcessors :: Vector LogRecordProcessor
, LoggerProvider -> MaterializedResources
loggerProviderResource :: MaterializedResources
, LoggerProvider -> AttributeLimits
loggerProviderAttributeLimits :: AttributeLimits
}
data Logger = Logger
{ Logger -> InstrumentationLibrary
loggerInstrumentationScope :: InstrumentationLibrary
, Logger -> LoggerProvider
loggerLoggerProvider :: LoggerProvider
}
data ReadWriteLogRecord = ReadWriteLogRecord Logger (IORef ImmutableLogRecord)
mkReadWriteLogRecord :: Logger -> ImmutableLogRecord -> IO ReadWriteLogRecord
mkReadWriteLogRecord :: Logger -> ImmutableLogRecord -> IO ReadWriteLogRecord
mkReadWriteLogRecord Logger
l = (IORef ImmutableLogRecord -> ReadWriteLogRecord)
-> IO (IORef ImmutableLogRecord) -> IO ReadWriteLogRecord
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Logger -> IORef ImmutableLogRecord -> ReadWriteLogRecord
ReadWriteLogRecord Logger
l) (IO (IORef ImmutableLogRecord) -> IO ReadWriteLogRecord)
-> (ImmutableLogRecord -> IO (IORef ImmutableLogRecord))
-> ImmutableLogRecord
-> IO ReadWriteLogRecord
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ImmutableLogRecord -> IO (IORef ImmutableLogRecord)
forall a. a -> IO (IORef a)
newIORef
newtype ReadableLogRecord = ReadableLogRecord {ReadableLogRecord -> ReadWriteLogRecord
readableLogRecord :: ReadWriteLogRecord}
mkReadableLogRecord :: ReadWriteLogRecord -> ReadableLogRecord
mkReadableLogRecord :: ReadWriteLogRecord -> ReadableLogRecord
mkReadableLogRecord = ReadWriteLogRecord -> ReadableLogRecord
ReadableLogRecord
class IsReadableLogRecord r where
readLogRecord :: r -> IO ImmutableLogRecord
readLogRecordInstrumentationScope :: r -> InstrumentationLibrary
readLogRecordResource :: r -> MaterializedResources
class (IsReadableLogRecord r) => IsReadWriteLogRecord r where
readLogRecordAttributeLimits :: r -> AttributeLimits
modifyLogRecord :: r -> (ImmutableLogRecord -> ImmutableLogRecord) -> IO ()
atomicModifyLogRecord :: r -> (ImmutableLogRecord -> (ImmutableLogRecord, b)) -> IO b
instance IsReadableLogRecord ReadableLogRecord where
readLogRecord :: ReadableLogRecord -> IO ImmutableLogRecord
readLogRecord = ReadWriteLogRecord -> IO ImmutableLogRecord
forall r. IsReadableLogRecord r => r -> IO ImmutableLogRecord
readLogRecord (ReadWriteLogRecord -> IO ImmutableLogRecord)
-> (ReadableLogRecord -> ReadWriteLogRecord)
-> ReadableLogRecord
-> IO ImmutableLogRecord
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadableLogRecord -> ReadWriteLogRecord
readableLogRecord
readLogRecordInstrumentationScope :: ReadableLogRecord -> InstrumentationLibrary
readLogRecordInstrumentationScope = ReadWriteLogRecord -> InstrumentationLibrary
forall r. IsReadableLogRecord r => r -> InstrumentationLibrary
readLogRecordInstrumentationScope (ReadWriteLogRecord -> InstrumentationLibrary)
-> (ReadableLogRecord -> ReadWriteLogRecord)
-> ReadableLogRecord
-> InstrumentationLibrary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadableLogRecord -> ReadWriteLogRecord
readableLogRecord
readLogRecordResource :: ReadableLogRecord -> MaterializedResources
readLogRecordResource = ReadWriteLogRecord -> MaterializedResources
forall r. IsReadableLogRecord r => r -> MaterializedResources
readLogRecordResource (ReadWriteLogRecord -> MaterializedResources)
-> (ReadableLogRecord -> ReadWriteLogRecord)
-> ReadableLogRecord
-> MaterializedResources
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadableLogRecord -> ReadWriteLogRecord
readableLogRecord
instance IsReadableLogRecord ReadWriteLogRecord where
readLogRecord :: ReadWriteLogRecord -> IO ImmutableLogRecord
readLogRecord (ReadWriteLogRecord Logger
_ IORef ImmutableLogRecord
ref) = IORef ImmutableLogRecord -> IO ImmutableLogRecord
forall a. IORef a -> IO a
readIORef IORef ImmutableLogRecord
ref
readLogRecordInstrumentationScope :: ReadWriteLogRecord -> InstrumentationLibrary
readLogRecordInstrumentationScope (ReadWriteLogRecord (Logger {InstrumentationLibrary
loggerInstrumentationScope :: Logger -> InstrumentationLibrary
loggerInstrumentationScope :: InstrumentationLibrary
loggerInstrumentationScope}) IORef ImmutableLogRecord
_) = InstrumentationLibrary
loggerInstrumentationScope
readLogRecordResource :: ReadWriteLogRecord -> MaterializedResources
readLogRecordResource (ReadWriteLogRecord Logger {loggerLoggerProvider :: Logger -> LoggerProvider
loggerLoggerProvider = LoggerProvider {MaterializedResources
loggerProviderResource :: LoggerProvider -> MaterializedResources
loggerProviderResource :: MaterializedResources
loggerProviderResource}} IORef ImmutableLogRecord
_) = MaterializedResources
loggerProviderResource
instance IsReadWriteLogRecord ReadWriteLogRecord where
readLogRecordAttributeLimits :: ReadWriteLogRecord -> AttributeLimits
readLogRecordAttributeLimits (ReadWriteLogRecord Logger {loggerLoggerProvider :: Logger -> LoggerProvider
loggerLoggerProvider = LoggerProvider {AttributeLimits
loggerProviderAttributeLimits :: LoggerProvider -> AttributeLimits
loggerProviderAttributeLimits :: AttributeLimits
loggerProviderAttributeLimits}} IORef ImmutableLogRecord
_) = AttributeLimits
loggerProviderAttributeLimits
modifyLogRecord :: ReadWriteLogRecord
-> (ImmutableLogRecord -> ImmutableLogRecord) -> IO ()
modifyLogRecord (ReadWriteLogRecord Logger
_ IORef ImmutableLogRecord
ref) = IORef ImmutableLogRecord
-> (ImmutableLogRecord -> ImmutableLogRecord) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef IORef ImmutableLogRecord
ref
atomicModifyLogRecord :: forall b.
ReadWriteLogRecord
-> (ImmutableLogRecord -> (ImmutableLogRecord, b)) -> IO b
atomicModifyLogRecord (ReadWriteLogRecord Logger
_ IORef ImmutableLogRecord
ref) = IORef ImmutableLogRecord
-> (ImmutableLogRecord -> (ImmutableLogRecord, b)) -> IO b
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef IORef ImmutableLogRecord
ref
data ImmutableLogRecord = ImmutableLogRecord
{ ImmutableLogRecord -> Maybe Timestamp
logRecordTimestamp :: Maybe Timestamp
, ImmutableLogRecord -> Timestamp
logRecordObservedTimestamp :: Timestamp
, ImmutableLogRecord -> Maybe (TraceId, SpanId, TraceFlags)
logRecordTracingDetails :: Maybe (TraceId, SpanId, TraceFlags)
, ImmutableLogRecord -> Maybe Text
logRecordSeverityText :: Maybe Text
, ImmutableLogRecord -> Maybe SeverityNumber
logRecordSeverityNumber :: Maybe SeverityNumber
, ImmutableLogRecord -> AnyValue
logRecordBody :: AnyValue
, ImmutableLogRecord -> LogAttributes
logRecordAttributes :: LogAttributes
}
data LogRecordArguments = LogRecordArguments
{ LogRecordArguments -> Maybe Timestamp
timestamp :: Maybe Timestamp
, LogRecordArguments -> Maybe Timestamp
observedTimestamp :: Maybe Timestamp
, LogRecordArguments -> Maybe Context
context :: Maybe Context
, LogRecordArguments -> Maybe Text
severityText :: Maybe Text
, LogRecordArguments -> Maybe SeverityNumber
severityNumber :: Maybe SeverityNumber
, LogRecordArguments -> AnyValue
body :: AnyValue
, LogRecordArguments -> HashMap Text AnyValue
attributes :: HashMap Text AnyValue
}
emptyLogRecordArguments :: LogRecordArguments
emptyLogRecordArguments :: LogRecordArguments
emptyLogRecordArguments =
LogRecordArguments
{ timestamp :: Maybe Timestamp
timestamp = Maybe Timestamp
forall a. Maybe a
Nothing
, observedTimestamp :: Maybe Timestamp
observedTimestamp = Maybe Timestamp
forall a. Maybe a
Nothing
, context :: Maybe Context
context = Maybe Context
forall a. Maybe a
Nothing
, severityText :: Maybe Text
severityText = Maybe Text
forall a. Maybe a
Nothing
, severityNumber :: Maybe SeverityNumber
severityNumber = Maybe SeverityNumber
forall a. Maybe a
Nothing
, body :: AnyValue
body = AnyValue
NullValue
, attributes :: HashMap Text AnyValue
attributes = HashMap Text AnyValue
forall k v. HashMap k v
H.empty
}
data SeverityNumber
= Trace
| Trace2
| Trace3
| Trace4
| Debug
| Debug2
| Debug3
| Debug4
| Info
| Info2
| Info3
| Info4
| Warn
| Warn2
| Warn3
| Warn4
| Error
| Error2
| Error3
| Error4
| Fatal
| Fatal2
| Fatal3
| Fatal4
| Unknown !Int
instance Enum SeverityNumber where
toEnum :: Int -> SeverityNumber
toEnum Int
1 = SeverityNumber
Trace
toEnum Int
2 = SeverityNumber
Trace2
toEnum Int
3 = SeverityNumber
Trace3
toEnum Int
4 = SeverityNumber
Trace4
toEnum Int
5 = SeverityNumber
Debug
toEnum Int
6 = SeverityNumber
Debug2
toEnum Int
7 = SeverityNumber
Debug3
toEnum Int
8 = SeverityNumber
Debug4
toEnum Int
9 = SeverityNumber
Info
toEnum Int
10 = SeverityNumber
Info2
toEnum Int
11 = SeverityNumber
Info3
toEnum Int
12 = SeverityNumber
Info4
toEnum Int
13 = SeverityNumber
Warn
toEnum Int
14 = SeverityNumber
Warn2
toEnum Int
15 = SeverityNumber
Warn3
toEnum Int
16 = SeverityNumber
Warn4
toEnum Int
17 = SeverityNumber
Error
toEnum Int
18 = SeverityNumber
Error2
toEnum Int
19 = SeverityNumber
Error3
toEnum Int
20 = SeverityNumber
Error4
toEnum Int
21 = SeverityNumber
Fatal
toEnum Int
22 = SeverityNumber
Fatal2
toEnum Int
23 = SeverityNumber
Fatal3
toEnum Int
24 = SeverityNumber
Fatal4
toEnum Int
n = Int -> SeverityNumber
Unknown Int
n
fromEnum :: SeverityNumber -> Int
fromEnum SeverityNumber
Trace = Int
1
fromEnum SeverityNumber
Trace2 = Int
2
fromEnum SeverityNumber
Trace3 = Int
3
fromEnum SeverityNumber
Trace4 = Int
4
fromEnum SeverityNumber
Debug = Int
5
fromEnum SeverityNumber
Debug2 = Int
6
fromEnum SeverityNumber
Debug3 = Int
7
fromEnum SeverityNumber
Debug4 = Int
8
fromEnum SeverityNumber
Info = Int
9
fromEnum SeverityNumber
Info2 = Int
10
fromEnum SeverityNumber
Info3 = Int
11
fromEnum SeverityNumber
Info4 = Int
12
fromEnum SeverityNumber
Warn = Int
13
fromEnum SeverityNumber
Warn2 = Int
14
fromEnum SeverityNumber
Warn3 = Int
15
fromEnum SeverityNumber
Warn4 = Int
16
fromEnum SeverityNumber
Error = Int
17
fromEnum SeverityNumber
Error2 = Int
18
fromEnum SeverityNumber
Error3 = Int
19
fromEnum SeverityNumber
Error4 = Int
20
fromEnum SeverityNumber
Fatal = Int
21
fromEnum SeverityNumber
Fatal2 = Int
22
fromEnum SeverityNumber
Fatal3 = Int
23
fromEnum SeverityNumber
Fatal4 = Int
24
fromEnum (Unknown Int
n) = Int
n
instance Eq SeverityNumber where
== :: SeverityNumber -> SeverityNumber -> Bool
(==) = (Int -> Int -> Bool)
-> (SeverityNumber -> Int)
-> SeverityNumber
-> SeverityNumber
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
(==) SeverityNumber -> Int
forall a. Enum a => a -> Int
fromEnum
instance Ord SeverityNumber where
compare :: SeverityNumber -> SeverityNumber -> Ordering
compare = (Int -> Int -> Ordering)
-> (SeverityNumber -> Int)
-> SeverityNumber
-> SeverityNumber
-> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare SeverityNumber -> Int
forall a. Enum a => a -> Int
fromEnum
toShortName :: SeverityNumber -> Maybe Text
toShortName :: SeverityNumber -> Maybe Text
toShortName SeverityNumber
Trace = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"TRACE"
toShortName SeverityNumber
Trace2 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"TRACE2"
toShortName SeverityNumber
Trace3 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"TRACE3"
toShortName SeverityNumber
Trace4 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"TRACE4"
toShortName SeverityNumber
Debug = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"DEBUG"
toShortName SeverityNumber
Debug2 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"DEBUG2"
toShortName SeverityNumber
Debug3 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"DEBUG3"
toShortName SeverityNumber
Debug4 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"DEBUG4"
toShortName SeverityNumber
Info = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"INFO"
toShortName SeverityNumber
Info2 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"INFO2"
toShortName SeverityNumber
Info3 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"INFO3"
toShortName SeverityNumber
Info4 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"INFO4"
toShortName SeverityNumber
Warn = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"WARN"
toShortName SeverityNumber
Warn2 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"WARN2"
toShortName SeverityNumber
Warn3 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"WARN3"
toShortName SeverityNumber
Warn4 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"WARN4"
toShortName SeverityNumber
Error = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"ERROR"
toShortName SeverityNumber
Error2 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"ERROR2"
toShortName SeverityNumber
Error3 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"ERROR3"
toShortName SeverityNumber
Error4 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"ERROR4"
toShortName SeverityNumber
Fatal = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"FATAL"
toShortName SeverityNumber
Fatal2 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"FATAL2"
toShortName SeverityNumber
Fatal3 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"FATAL3"
toShortName SeverityNumber
Fatal4 = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"FATAL4"
toShortName (Unknown Int
_) = Maybe Text
forall a. Maybe a
Nothing