Skip to content

Commit

Permalink
Updated test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlauer1 committed Jun 21, 2024
1 parent 4df5c2e commit c024ce3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 53 deletions.
16 changes: 15 additions & 1 deletion api/src/OpenTelemetry/Internal/Logging/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module OpenTelemetry.Internal.Logging.Types (
LogRecord (..),
ImmutableLogRecord (..),
LogRecordArguments (..),
emptyLogRecordArguments,
mkSeverityNumber,
shortName,
severityInt,
Expand All @@ -20,7 +21,7 @@ import OpenTelemetry.Common (Timestamp, TraceFlags)
import OpenTelemetry.Context.Types (Context)
import OpenTelemetry.Internal.Common.Types (InstrumentationLibrary)
import OpenTelemetry.Internal.Trace.Id (SpanId, TraceId)
import OpenTelemetry.LogAttributes (AnyValue, AttributeLimits, LogAttributes)
import OpenTelemetry.LogAttributes
import OpenTelemetry.Resource (MaterializedResources)


Expand Down Expand Up @@ -138,6 +139,19 @@ data LogRecordArguments body = LogRecordArguments
}


emptyLogRecordArguments :: body -> LogRecordArguments body
emptyLogRecordArguments body =
LogRecordArguments
{ timestamp = Nothing
, observedTimestamp = Nothing
, context = Nothing
, severityText = Nothing
, severityNumber = Nothing
, body = body
, attributes = snd $ getAttributes emptyAttributes
}


data SeverityNumber = SeverityNumber {shortName :: Maybe Text, severityInt :: !Int64} deriving (Read, Show)


Expand Down
77 changes: 25 additions & 52 deletions api/test/OpenTelemetry/Logging/CoreSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,44 @@ import Test.Hspec

spec :: Spec
spec = describe "Core" $ do
describe "getGlobalLoggerProvider" $ do
describe "The global logger provider" $ do
it "Returns a no-op LoggerProvider when not initialized" $ do
LoggerProvider {..} <- getGlobalLoggerProvider
loggerProviderResource `shouldBe` emptyMaterializedResources
loggerProviderAttributeLimits `shouldBe` LA.defaultAttributeLimits
describe "setGlobalLoggerProvider" $ do
it "works" $ do
lp <-
createLoggerProvider $
LoggerProviderOptions
{ loggerProviderOptionsResource =
materializeResources $
toResource
OperatingSystem
{ osType = "exampleOs"
, osDescription = Nothing
, osName = Nothing
, osVersion = Nothing
it "Allows a LoggerProvider to be set and returns that with subsequent calls to getGlobalLoggerProvider" $ do
let lp =
createLoggerProvider $
LoggerProviderOptions
{ loggerProviderOptionsResource =
materializeResources $
toResource
OperatingSystem
{ osType = "exampleOs"
, osDescription = Nothing
, osName = Nothing
, osVersion = Nothing
}
, loggerProviderOptionsAttributeLimits =
LA.AttributeLimits
{ attributeCountLimit = Just 50
, attributeLengthLimit = Just 50
}
, loggerProviderOptionsAttributeLimits =
LA.AttributeLimits
{ attributeCountLimit = Just 50
, attributeLengthLimit = Just 50
}
}
}

setGlobalLoggerProvider lp

glp <- getGlobalLoggerProvider
glp `shouldBe` lp
describe "addAttribute" $ do
it "works" $ do
lp <- getGlobalLoggerProvider
let l = makeLogger lp InstrumentationLibrary {libraryName = "exampleLibrary", libraryVersion = "", librarySchemaUrl = "", libraryAttributes = A.emptyAttributes}
lr <-
emitLogRecord
l
LogRecordArguments
{ timestamp = Nothing
, observedTimestamp = Nothing
, context = Nothing
, severityText = Nothing
, severityNumber = Nothing
, body = Nothing
, attributes =
H.fromList
[ ("something", "a thing")
]
}
lr <- emitLogRecord l $ (emptyLogRecordArguments ()) {attributes = H.fromList [("something", "a thing")]}

addAttribute lr "anotherThing" ("another thing" :: LA.AnyValue)

(_, attrs) <- LA.getAttributes <$> logRecordGetAttributes lr

attrs
`shouldBe` H.fromList
[ ("anotherThing", "another thing")
Expand All @@ -76,29 +63,15 @@ spec = describe "Core" $ do
it "works" $ do
lp <- getGlobalLoggerProvider
let l = makeLogger lp InstrumentationLibrary {libraryName = "exampleLibrary", libraryVersion = "", librarySchemaUrl = "", libraryAttributes = A.emptyAttributes}
lr <-
emitLogRecord
l
LogRecordArguments
{ timestamp = Nothing
, observedTimestamp = Nothing
, context = Nothing
, severityText = Nothing
, severityNumber = Nothing
, body = Nothing
, attributes =
H.fromList
[ ("something", "a thing")
]
}
lr <- emitLogRecord l $ (emptyLogRecordArguments ()) {attributes = H.fromList [("something", "a thing")]}

addAttributes lr $
H.fromList
[ ("anotherThing", "another thing" :: LA.AnyValue)
, ("twoThing", "the second another thing")
]

(_, attrs) <- LA.getAttributes <$> logRecordGetAttributes lr

attrs
`shouldBe` H.fromList
[ ("anotherThing", "another thing")
Expand Down

0 comments on commit c024ce3

Please sign in to comment.