-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update HTTP Semantic Conventions to Stable Versions (#118)
* Work in Progress: Request and Response Attributes updated to match the latest stable semantics if the environment variable OTEL_SEMCONV_STABILITY_OPT_IN is "http" * Added Stable attributes to instrumentResponse * Refactored to fit new httpTracerProvider return type * Fixed missing "do" notation and removed unnecessary imports * Fix typo and update comment * Found unchanged http name * Found outdated http attribute names * Fixed cradle so HLS works * Added Settings File for application-wide environment variable configuration * Moved semConvStabilityOptIn and related types to OpenTelemetry.Settings * Changed code using HttpTracerProvider to work with changed return type * Fixed exports * Replaced general Settings.hs with specific SemConvStability.hs * Removed comment because the problem it referenced is solved * removed unused language pragma * fixed imports * Replaced outdated http conventions based on the value of OTEL_SEMCONV_STABILITY_OPT_IN * changed line about generation version * added unordered-containers to dependencies * Moved semConvStabilityOptIn so that it is in scope * Updated dependencies for testing * WIP testing for http-client * Generated by newer version * Removed WIP testing * Updated getSemConvStabilityOptIn so it parses environment variable as a comma-separated list * derives Show and Eq for testing purposes * Added tests for SemConvStabilityOptIn to make sure environment variable is read correctly * Updated Haddock to include blurb about the new HTTP semantic conventions * Removed hspec from dependencies * Auto-generated change. Removed hspec from build-depends * Added newline to end of file * Split off parsing logic from getSemConvStabilityOptIn to parseSemConvStabilityOptIn * Added blank line for readability * Changed pure to Just to improve readability and contrast with Nothing * Rewrote tests to eliminate boilerplate and increase readability * Refactored and Renamed SemanticsConfig.hs to future proof * Refactored SemanticsConfig so options are Enums for pattern matching instead of booleans * Updated SemanticsConfig tests. NOTE: does not currently pass memoization tests. * Updated to match changes to SemanticsConfig.hs * Fix infinite loop issue Co-authored-by: Ian Duncan <[email protected]> * changed from . to $ * Added haddocks * Formatting fixes --------- Co-authored-by: Ian Duncan <[email protected]>
- Loading branch information
1 parent
02653bb
commit 07f3ed5
Showing
35 changed files
with
3,013 additions
and
2,626 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module OpenTelemetry.SemanticsConfig ( | ||
SemanticsOptions (httpOption), | ||
HttpOption (..), | ||
getSemanticsOptions, | ||
getSemanticsOptions', | ||
) where | ||
|
||
import Control.Exception.Safe (throwIO, tryAny) | ||
import Data.IORef (newIORef, readIORef, writeIORef) | ||
import qualified Data.Text as T | ||
import System.Environment (lookupEnv) | ||
import System.IO.Unsafe (unsafePerformIO) | ||
|
||
|
||
{- | This is a record that contains options for whether the new stable semantics conventions should be emitted. | ||
Semantics conventions that have been declared stable: | ||
- [http](https://opentelemetry.io/blog/2023/http-conventions-declared-stable/#migration-plan) | ||
-} | ||
data SemanticsOptions = SemanticsOptions {httpOption :: HttpOption} | ||
|
||
|
||
-- | This option determines whether stable, old, or both kinds of http attributes are emitted. | ||
data HttpOption | ||
= Stable | ||
| StableAndOld | ||
| Old | ||
deriving (Show, Eq) | ||
|
||
|
||
-- | These are the default values emitted if OTEL_SEM_CONV_STABILITY_OPT_IN is unset or does not contain values for a specific category of option. | ||
defaultOptions :: SemanticsOptions | ||
defaultOptions = SemanticsOptions {httpOption = Old} | ||
|
||
|
||
-- | Detects the presence of "http/dup" or "http" in OTEL_SEMCONV_STABILITY_OPT_IN or uses the default option if they are not there. | ||
parseHttpOption :: (Foldable t) => t T.Text -> HttpOption | ||
parseHttpOption envs | ||
| "http/dup" `elem` envs = StableAndOld | ||
| "http" `elem` envs = Stable | ||
| otherwise = httpOption defaultOptions | ||
|
||
|
||
-- | Detects the presence of semantics options in OTEL_SEMCONV_STABILITY_OPT_IN or uses the defaultOptions if they are not present. | ||
parseSemanticsOptions :: Maybe String -> SemanticsOptions | ||
parseSemanticsOptions Nothing = defaultOptions | ||
parseSemanticsOptions (Just env) = SemanticsOptions {..} | ||
where | ||
envs = fmap T.strip $ T.splitOn "," $ T.pack env | ||
httpOption = parseHttpOption envs | ||
|
||
|
||
{- | Version of getSemanticsOptions that is not memoized. It is recommended to use getSemanticsOptions for efficiency purposes | ||
unless it is necessary to retrieve the value of OTEL_SEMCONV_STABILITY_OPT_IN every time getSemanticsOptions' is called. | ||
-} | ||
getSemanticsOptions' :: IO SemanticsOptions | ||
getSemanticsOptions' = parseSemanticsOptions <$> lookupEnv "OTEL_SEMCONV_STABILITY_OPT_IN" | ||
|
||
|
||
{- | 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 | ||
variable and parsing some stuff, we don't have to be concerned about thread-safety. | ||
-} | ||
memoize :: IO a -> IO (IO a) | ||
memoize action = do | ||
ref <- newIORef Nothing | ||
pure $ do | ||
mres <- readIORef ref | ||
res <- case mres of | ||
Just res -> pure res | ||
Nothing -> do | ||
res <- tryAny action | ||
writeIORef ref $ Just res | ||
pure res | ||
either throwIO pure res | ||
|
||
|
||
{- | Retrieves OTEL_SEMCONV_STABILITY_OPT_IN and parses it into SemanticsOptions. | ||
This uses the [global IORef trick](https://www.parsonsmatt.org/2021/04/21/global_ioref_in_template_haskell.html) | ||
to memoize the settings for efficiency. Note that getSemanticsOptions stores and returns the | ||
value of the first time it was called and will not change when OTEL_SEMCONV_STABILITY_OPT_IN | ||
is updated. Use getSemanticsOptions' to read OTEL_SEMCONV_STABILITY_OPT_IN every time the | ||
function is called. | ||
-} | ||
getSemanticsOptions :: IO SemanticsOptions | ||
getSemanticsOptions = unsafePerformIO $ memoize getSemanticsOptions' | ||
{-# NOINLINE getSemanticsOptions #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
instrumentation/conduit/src/OpenTelemetry/Instrumentation/Conduit.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
instrumentation/http-client/hs-opentelemetry-instrumentation-http-client.cabal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.