From 77b4c5f36d18b86b3b16aa85af4741a8bcc992c1 Mon Sep 17 00:00:00 2001 From: evanlauer1 Date: Thu, 23 May 2024 17:45:40 -0700 Subject: [PATCH] Updated SemanticsConfig tests. NOTE: does not currently pass memoization tests. --- api/hs-opentelemetry-api.cabal | 2 +- api/src/OpenTelemetry/SemanticsConfig.hs | 3 ++ .../SemConvStabilityOptInSpec.hs | 32 -------------- api/test/OpenTelemetry/SemanticsConfigSpec.hs | 44 +++++++++++++++++++ api/test/Spec.hs | 4 +- 5 files changed, 50 insertions(+), 35 deletions(-) delete mode 100644 api/test/OpenTelemetry/SemConvStabilityOptInSpec.hs create mode 100644 api/test/OpenTelemetry/SemanticsConfigSpec.hs diff --git a/api/hs-opentelemetry-api.cabal b/api/hs-opentelemetry-api.cabal index 76cb67fe..28feafe7 100644 --- a/api/hs-opentelemetry-api.cabal +++ b/api/hs-opentelemetry-api.cabal @@ -101,7 +101,7 @@ test-suite hs-opentelemetry-api-test main-is: Spec.hs other-modules: OpenTelemetry.BaggageSpec - OpenTelemetry.SemConvStabilityOptInSpec + OpenTelemetry.SemanticsConfigSpec OpenTelemetry.Trace.SamplerSpec OpenTelemetry.Trace.TraceFlagsSpec Paths_hs_opentelemetry_api diff --git a/api/src/OpenTelemetry/SemanticsConfig.hs b/api/src/OpenTelemetry/SemanticsConfig.hs index 07949e56..38cf71d2 100644 --- a/api/src/OpenTelemetry/SemanticsConfig.hs +++ b/api/src/OpenTelemetry/SemanticsConfig.hs @@ -3,6 +3,7 @@ module OpenTelemetry.SemanticsConfig ( SemanticsOptions (httpOption), + HttpOption (..), getSemanticsOptions, getSemanticsOptions', ) where @@ -80,6 +81,8 @@ getSemanticsOptions' :: IO SemanticsOptions getSemanticsOptions' = parseSemanticsOptions <$> lookupEnv "OTEL_SEMCONV_STABILITY_OPT_IN" +-- ! Does not pass memoization tests yet + {- | Create a new memoized IO action using an 'IORef' under the surface. Note that the action may be run in multiple threads simultaneously, so this may not be thread safe (depending on the underlying action). For the sake of reading an environment diff --git a/api/test/OpenTelemetry/SemConvStabilityOptInSpec.hs b/api/test/OpenTelemetry/SemConvStabilityOptInSpec.hs deleted file mode 100644 index 10726191..00000000 --- a/api/test/OpenTelemetry/SemConvStabilityOptInSpec.hs +++ /dev/null @@ -1,32 +0,0 @@ -module OpenTelemetry.SemConvStabilityOptInSpec where - -import OpenTelemetry.SemConvStabilityOptIn -import System.Environment -import Test.Hspec - - -envVarName :: String -envVarName = "OTEL_SEMCONV_STABILITY_OPT_IN" - - -spec :: Spec -spec = do - describe "SemConvStabilityOptIn" $ do - it "defaults to 'Old' when env var has no value" $ do - unsetEnv envVarName - semConvStabilityOptIn <- getSemConvStabilityOptIn - semConvStabilityOptIn `shouldBe` Old - mapM_ - ( \(envVarVal, expectedVal) -> - it ("returns '" ++ show expectedVal ++ "' when env var is '" ++ show envVarVal ++ "'") $ do - setEnv envVarName envVarVal - semConvStabilityOptIn <- getSemConvStabilityOptIn - semConvStabilityOptIn `shouldBe` expectedVal - ) - [ ("http", Stable) - , ("http/du", Old) -- intentionally similar to both "http/dup" and "http" - , ("http/dup", Both) - , ("http/dup,http", Both) - , ("http,http/dup", Both) - , ("http,something-random,http/dup", Both) - ] diff --git a/api/test/OpenTelemetry/SemanticsConfigSpec.hs b/api/test/OpenTelemetry/SemanticsConfigSpec.hs new file mode 100644 index 00000000..bfd1f85b --- /dev/null +++ b/api/test/OpenTelemetry/SemanticsConfigSpec.hs @@ -0,0 +1,44 @@ +module OpenTelemetry.SemanticsConfigSpec where + +import OpenTelemetry.SemanticsConfig +import System.Environment +import Test.Hspec + + +envVarName :: String +envVarName = "OTEL_SEMCONV_STABILITY_OPT_IN" + + +spec :: Spec +spec = do + describe "SemanticsConfig" $ do + describe "HttpOption" $ do + it "defaults to 'Old' when env var has no value" $ do + unsetEnv envVarName + semanticsOptions <- getSemanticsOptions' + httpOption semanticsOptions `shouldBe` Old + mapM_ + ( \(envVarVal, expectedVal) -> + it ("returns " ++ show expectedVal ++ " when env var is " ++ show envVarVal) $ do + setEnv envVarName envVarVal + semanticsOptions <- getSemanticsOptions' + httpOption semanticsOptions `shouldBe` expectedVal + ) + [ ("http", Stable) + , ("http/du", Old) -- intentionally similar to both "http/dup" and "http" + , ("http/dup", StableAndOld) + , ("http/dup,http", StableAndOld) + , ("http,http/dup", StableAndOld) + , ("http,something-random,http/dup", StableAndOld) + ] + context "memoization" $ do + it "works" $ do + setEnv envVarName "http" + semanticsOptions <- getSemanticsOptions + httpOption semanticsOptions `shouldBe` Stable + it ("does not change when " ++ envVarName ++ " changes") $ do + setEnv envVarName "http" + semanticsOptions <- getSemanticsOptions + setEnv envVarName "http/dup" + semanticsOptions <- getSemanticsOptions + httpOption semanticsOptions `shouldBe` Stable -- and not StableAndOld because of memoization diff --git a/api/test/Spec.hs b/api/test/Spec.hs index 81211f31..ba2bb467 100644 --- a/api/test/Spec.hs +++ b/api/test/Spec.hs @@ -13,7 +13,7 @@ import OpenTelemetry.Attributes (lookupAttribute) import qualified OpenTelemetry.BaggageSpec as Baggage import OpenTelemetry.Context -import qualified OpenTelemetry.SemConvStabilityOptInSpec as SemConvStabilityOptIn +import qualified OpenTelemetry.SemanticsConfigSpec as SemanticsConfigSpec import OpenTelemetry.Trace.Core import qualified OpenTelemetry.Trace.SamplerSpec as Sampler import qualified OpenTelemetry.Trace.TraceFlagsSpec as TraceFlags @@ -55,4 +55,4 @@ main = hspec $ do Baggage.spec Sampler.spec TraceFlags.spec - SemConvStabilityOptIn.spec + SemanticsConfigSpec.spec