From 8e35e0423a8d063f39229eea849f703ff5d9f8b5 Mon Sep 17 00:00:00 2001 From: evanlauer1 Date: Tue, 11 Jun 2024 16:21:22 -0700 Subject: [PATCH] Refactored attribute limits to be controlled by the LoggerProvider --- api/src/OpenTelemetry/Internal/Logging/Types.hs | 7 +++++-- api/src/OpenTelemetry/LogAttributes.hs | 5 +++++ api/src/OpenTelemetry/Logging/Core.hs | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/api/src/OpenTelemetry/Internal/Logging/Types.hs b/api/src/OpenTelemetry/Internal/Logging/Types.hs index 37de7931..752e6dd9 100644 --- a/api/src/OpenTelemetry/Internal/Logging/Types.hs +++ b/api/src/OpenTelemetry/Internal/Logging/Types.hs @@ -11,19 +11,22 @@ module OpenTelemetry.Internal.Logging.Types ( severityInt, ) where +import qualified Data.HashMap.Strict as H import Data.Int (Int64) import Data.Text (Text) +import OpenTelemetry.Attributes (AttributeLimits) import OpenTelemetry.Common (Timestamp, TraceFlags) import OpenTelemetry.Context.Types import OpenTelemetry.Internal.Common.Types (InstrumentationLibrary) import OpenTelemetry.Internal.Trace.Id (SpanId, TraceId) -import OpenTelemetry.LogAttributes (LogAttributes) +import OpenTelemetry.LogAttributes (AnyValue, LogAttributes) import OpenTelemetry.Resource (MaterializedResources) -- | @Logger@s can be created from @LoggerProvider@s data LoggerProvider = LoggerProvider { loggerProviderResource :: MaterializedResources + , loggerProviderAttributeLimits :: AttributeLimits } @@ -125,7 +128,7 @@ data LogRecordArguments body = LogRecordArguments , severityText :: Maybe Text , severityNumber :: Maybe Int , body :: body - , attributes :: LogAttributes + , attributes :: H.HashMap Text AnyValue } diff --git a/api/src/OpenTelemetry/LogAttributes.hs b/api/src/OpenTelemetry/LogAttributes.hs index ea16051e..90f48d04 100644 --- a/api/src/OpenTelemetry/LogAttributes.hs +++ b/api/src/OpenTelemetry/LogAttributes.hs @@ -162,6 +162,11 @@ instance (ToValue a) => ToValue (H.HashMap Text a) where toValue = HashMapValue . fmap toValue +instance ToValue AnyValue where + toValue :: AnyValue -> AnyValue + toValue = id + + unsafeMergeLogAttributesIgnoringLimits :: LogAttributes -> LogAttributes -> LogAttributes unsafeMergeLogAttributesIgnoringLimits (LogAttributes l lc ld) (LogAttributes r rc rd) = LogAttributes (l <> r) (lc + rc) (ld + rd) diff --git a/api/src/OpenTelemetry/Logging/Core.hs b/api/src/OpenTelemetry/Logging/Core.hs index 4ae639c2..835b6bb1 100644 --- a/api/src/OpenTelemetry/Logging/Core.hs +++ b/api/src/OpenTelemetry/Logging/Core.hs @@ -28,15 +28,18 @@ import Control.Applicative import Control.Monad.Trans import Control.Monad.Trans.Maybe import Data.Coerce +import qualified Data.HashMap.Strict as H import Data.IORef import Data.Maybe import GHC.IO (unsafePerformIO) +import OpenTelemetry.Attributes (AttributeLimits) import OpenTelemetry.Common import OpenTelemetry.Context import OpenTelemetry.Context.ThreadLocal import OpenTelemetry.Internal.Common.Types import OpenTelemetry.Internal.Logging.Types import OpenTelemetry.Internal.Trace.Types +import OpenTelemetry.LogAttributes import OpenTelemetry.Resource (MaterializedResources, emptyMaterializedResources) import System.Clock @@ -45,7 +48,10 @@ getCurrentTimestamp :: (MonadIO m) => m Timestamp getCurrentTimestamp = liftIO $ coerce @(IO TimeSpec) @(IO Timestamp) $ getTime Realtime -data LoggerProviderOptions = LoggerProviderOptions {loggerProviderOptionsResource :: MaterializedResources} +data LoggerProviderOptions = LoggerProviderOptions + { loggerProviderOptionsResource :: MaterializedResources + , loggerProviderOptionsAttributeLimits :: AttributeLimits + } {- | Options for creating a @LoggerProvider@ with no resources and default limits. @@ -124,5 +130,9 @@ emitLogRecord Logger {..} LogRecordArguments {..} = do , logRecordBody = body , logRecordResource = loggerProviderResource loggerProvider , logRecordInstrumentationScope = loggerInstrumentationScope - , logRecordAttributes = attributes + , logRecordAttributes = + addAttributes + (loggerProviderAttributeLimits loggerProvider) + emptyAttributes + attributes }