Skip to content

Commit

Permalink
fully evaluate before opening span
Browse files Browse the repository at this point in the history
This will make metrics more accurate because the cost of computing the
values which form the query/statement is no longer factored into the
span.
  • Loading branch information
avieth committed Nov 17, 2022
1 parent 315f8f8 commit 6b003e5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ library
, text
, unliftio
, vault
, deepseq
default-language: Haskell2010

test-suite hs-opentelemetry-persistent-test
Expand All @@ -59,4 +60,5 @@ test-suite hs-opentelemetry-persistent-test
, text
, unliftio
, vault
, deepseq
default-language: Haskell2010
1 change: 1 addition & 0 deletions instrumentation/persistent/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
- resourcet
- unliftio # TODO, unliftio-core
- mtl
- deepseq

library:
# ghc-options: -Wall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module OpenTelemetry.Instrumentation.Persistent
) where
import OpenTelemetry.Trace.Core
import OpenTelemetry.Context
import Control.DeepSeq (deepseq)
import Data.Acquire.Internal
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Database.Persist.Sql
import Database.Persist.SqlBackend (setConnHooks, SqlBackendHooks (hookGetStatement), emptySqlBackendHooks, MkSqlBackendArgs (connRDBMS), getRDBMS, getConnVault, modifyConnVault)
import Database.Persist.SqlBackend (setConnHooks, emptySqlBackendHooks, MkSqlBackendArgs (connRDBMS), getRDBMS, getConnVault, modifyConnVault)
import Database.Persist.SqlBackend.Internal
import Control.Monad.IO.Class
import System.IO.Unsafe (unsafePerformIO)
Expand Down Expand Up @@ -60,7 +61,7 @@ wrapSqlBackend attrs conn_ = do
let hooks = emptySqlBackendHooks
{ hookGetStatement = \conn sql stmt -> do
pure $ Statement
{ stmtQuery = \ps -> do
{ stmtQuery = \ps -> ps `deepseq` do
ctxt <- getContext
let spanCreator = do
s <- createSpan
Expand Down Expand Up @@ -88,7 +89,7 @@ wrapSqlBackend attrs conn_ = do
)
(stmtQueryAcquireF f)

, stmtExecute = \ps -> do
, stmtExecute = \ps -> ps `deepseq` do
inSpan' t sql (defaultSpanArguments { kind = Client, attributes = ("db.statement", toAttribute sql) : attrs }) $ \s -> do
annotateBasics s conn
stmtExecute stmt ps
Expand Down Expand Up @@ -129,4 +130,4 @@ annotateBasics :: MonadIO m => Span -> SqlBackend -> m ()
annotateBasics span conn = do
addAttributes span
[ ("db.system", toAttribute $ getRDBMS conn)
]
]

0 comments on commit 6b003e5

Please sign in to comment.