Skip to content

Commit

Permalink
Updated to match changes to SemanticsConfig.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlauer1 committed May 24, 2024
1 parent 77b4c5f commit e6b7fc4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Network.HTTP.Types
import OpenTelemetry.Context (Context, lookupSpan)
import OpenTelemetry.Context.ThreadLocal
import OpenTelemetry.Propagator
import OpenTelemetry.SemConvStabilityOptIn
import OpenTelemetry.SemanticsConfig
import OpenTelemetry.Trace.Core


Expand Down Expand Up @@ -120,10 +120,10 @@ instrumentRequest conf ctxt req = do
(\h -> (\v -> ("http.request.header." <> T.decodeUtf8 (foldedCase h), toAttribute (T.decodeUtf8 v))) <$> lookup h (requestHeaders req))
$ requestHeadersToRecord conf

semConvStabilityOptIn <- liftIO getSemConvStabilityOptIn
case semConvStabilityOptIn of
semanticsOptions <- liftIO getSemanticsOptions
case httpOption semanticsOptions of
Stable -> addStableAttributes
Both -> addStableAttributes >> addOldAttributes
StableAndOld -> addStableAttributes >> addOldAttributes
Old -> addOldAttributes

hdrs <- inject (getTracerProviderPropagators $ getTracerTracerProvider tracer) ctxt $ requestHeaders req
Expand Down Expand Up @@ -184,8 +184,8 @@ instrumentResponse conf ctxt resp = do
(\h -> (\v -> ("http.response.header." <> T.decodeUtf8 (foldedCase h), toAttribute (T.decodeUtf8 v))) <$> lookup h (responseHeaders resp))
$ responseHeadersToRecord conf

semConvStabilityOptIn <- liftIO getSemConvStabilityOptIn
case semConvStabilityOptIn of
semanticsOptions <- liftIO getSemanticsOptions
case httpOption semanticsOptions of
Stable -> addStableAttributes
Both -> addStableAttributes >> addOldAttributes
StableAndOld -> addStableAttributes >> addOldAttributes
Old -> addOldAttributes
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import qualified Database.MySQL.Base as MySQL
import qualified Database.Persist.MySQL as Orig
import Database.Persist.Sql
import qualified OpenTelemetry.Instrumentation.Persistent as Otel
import OpenTelemetry.SemConvStabilityOptIn
import OpenTelemetry.SemanticsConfig
import qualified OpenTelemetry.Trace.Core as Otel
import Text.Read (readMaybe)

Expand Down Expand Up @@ -141,11 +141,11 @@ openMySQLConn tp attrs [email protected] {connectUser, connectPort, connectOp
, (maybe "net.peer.name" (const "net.sock.peer.addr") (readMaybe connectHost :: Maybe IP), fromString connectHost)
]

semConvStabilityOptIn <- getSemConvStabilityOptIn
semanticsOptions <- getSemanticsOptions
let attrs' =
case semConvStabilityOptIn of
case httpOption semanticsOptions of
Stable -> addStableAttributes attrs
Both -> addStableAttributes . addOldAttributes $ attrs
StableAndOld -> addStableAttributes . addOldAttributes $ attrs
Old -> addOldAttributes attrs
(conn, backend) <- Orig.openMySQLConn ci logFunc
backend' <- Otel.wrapSqlBackend' tp attrs' backend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import Database.PostgreSQL.Simple.Internal (
)
import GHC.Stack
import OpenTelemetry.Resource ((.=), (.=?))
import OpenTelemetry.SemConvStabilityOptIn
import OpenTelemetry.SemanticsConfig
import OpenTelemetry.Trace.Core
import OpenTelemetry.Trace.Monad
import Text.Read (readMaybe)
Expand All @@ -99,7 +99,6 @@ staticConnectionAttributes Connection {connectionHandle} = liftIO $ do
<*> LibPQ.host pqConn
<*> LibPQ.port pqConn

semConvStabilityOptIn <- getSemConvStabilityOptIn
let stableMaybeAttributes =
[ "db.system" .= toAttribute ("postgresql" :: T.Text)
, "db.user" .=? (TE.decodeUtf8 <$> mUser)
Expand Down Expand Up @@ -129,12 +128,13 @@ staticConnectionAttributes Connection {connectionHandle} = liftIO $ do
Just (IPv6 ipv6) -> "net.peer.ip" .= T.pack (show ipv6)
]

semanticsOptions <- getSemanticsOptions
pure $
H.fromList $
catMaybes $
case semConvStabilityOptIn of
case httpOption semanticsOptions of
Stable -> stableMaybeAttributes
Both -> stableMaybeAttributes `union` oldMaybeAttributes
StableAndOld -> stableMaybeAttributes `union` oldMaybeAttributes
Old -> oldMaybeAttributes

{-
Expand Down
19 changes: 11 additions & 8 deletions instrumentation/wai/src/OpenTelemetry/Instrumentation/Wai.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import OpenTelemetry.Attributes (lookupAttribute)
import qualified OpenTelemetry.Context as Context
import OpenTelemetry.Context.ThreadLocal
import OpenTelemetry.Propagator
import OpenTelemetry.SemConvStabilityOptIn
import OpenTelemetry.SemanticsConfig
import OpenTelemetry.Trace.Core
import System.IO.Unsafe

Expand Down Expand Up @@ -61,20 +61,21 @@ newOpenTelemetryWaiMiddleware' tp =
let path_ = T.decodeUtf8 $ rawPathInfo req
-- peer = remoteHost req
parentContextM
semConvStabilityOptIn <- getSemConvStabilityOptIn

semanticsOptions <- getSemanticsOptions
let args =
defaultSpanArguments
{ kind = Server
, attributes =
case semConvStabilityOptIn of
case httpOption semanticsOptions of
Stable ->
usefulCallsite
`H.union` [
( "user_agent.original"
, toAttribute $ maybe "" T.decodeUtf8 (lookup hUserAgent $ requestHeaders req)
)
]
Both ->
StableAndOld ->
usefulCallsite
`H.union` [
( "user_agent.original"
Expand All @@ -85,6 +86,7 @@ newOpenTelemetryWaiMiddleware' tp =
}
inSpan'' tracer path_ args $ \requestSpan -> do
ctxt <- getContext

let addStableAttributes = do
addAttributes
requestSpan
Expand Down Expand Up @@ -164,9 +166,10 @@ newOpenTelemetryWaiMiddleware' tp =
SockAddrUnix path ->
[ ("net.peer.name", toAttribute $ T.pack path)
]
case semConvStabilityOptIn of

case httpOption semanticsOptions of
Stable -> addStableAttributes
Both -> addOldAttributes >> addStableAttributes
StableAndOld -> addOldAttributes >> addStableAttributes
Old -> addOldAttributes

let req' =
Expand All @@ -186,13 +189,13 @@ newOpenTelemetryWaiMiddleware' tp =
AttributeValue (TextAttribute route) -> updateName requestSpan route
_ -> pure ()

case semConvStabilityOptIn of
case httpOption semanticsOptions of
Stable ->
addAttributes
requestSpan
[ ("http.response.status_code", toAttribute $ statusCode $ responseStatus resp)
]
Both ->
StableAndOld ->
addAttributes
requestSpan
[ ("http.response.status_code", toAttribute $ statusCode $ responseStatus resp)
Expand Down
12 changes: 6 additions & 6 deletions instrumentation/yesod/src/OpenTelemetry/Instrumentation/Yesod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import qualified OpenTelemetry.Context as Context
import OpenTelemetry.Context.ThreadLocal
import OpenTelemetry.Contrib.SpanTraversals
import OpenTelemetry.Instrumentation.Wai (requestContext)
import OpenTelemetry.SemConvStabilityOptIn
import OpenTelemetry.SemanticsConfig
import OpenTelemetry.Trace.Core hiding (inSpan, inSpan', inSpan'')
import OpenTelemetry.Trace.Monad
import UnliftIO.Exception
Expand Down Expand Up @@ -217,7 +217,7 @@ openTelemetryYesodMiddleware
openTelemetryYesodMiddleware rr m = do
req <- waiRequest
mr <- getCurrentRoute
semConvStabilityOptIn <- liftIO getSemConvStabilityOptIn
semanticsOptions <- liftIO getSemanticsOptions
let mspan = requestContext req >>= Context.lookupSpan
sharedAttributes =
H.fromList $
Expand All @@ -231,15 +231,15 @@ openTelemetryYesodMiddleware rr m = do
Just ("http.handler", toAttribute $ nameRender rr r)
, do
ff <- lookup "X-Forwarded-For" $ requestHeaders req
case semConvStabilityOptIn of
case httpOption semanticsOptions of
Stable -> Just ("client.address", toAttribute $ T.decodeUtf8 ff)
Both -> Just ("client.address", toAttribute $ T.decodeUtf8 ff)
StableAndOld -> Just ("client.address", toAttribute $ T.decodeUtf8 ff)
Old -> Nothing
, do
ff <- lookup "X-Forwarded-For" $ requestHeaders req
case semConvStabilityOptIn of
case httpOption semanticsOptions of
Stable -> Nothing
Both -> Just ("http.client_ip", toAttribute $ T.decodeUtf8 ff)
StableAndOld -> Just ("http.client_ip", toAttribute $ T.decodeUtf8 ff)
Old -> Just ("http.client_ip", toAttribute $ T.decodeUtf8 ff)
]
args =
Expand Down

0 comments on commit e6b7fc4

Please sign in to comment.