Skip to content

Commit d2b15bc

Browse files
committed
Tracing: avoid calling actionBracket for no reason
1 parent 5afb65d commit d2b15bc

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

ghcide/src/Development/IDE/Core/Tracing.hs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Development.IDE.Types.Shake (Key (..), Value,
3636
Values)
3737
import Development.Shake (Action, actionBracket)
3838
import Foreign.Storable (Storable (sizeOf))
39+
import GHC.RTS.Flags
3940
import HeapSize (recursiveSize, runHeapsize)
4041
import Ide.PluginUtils (installSigUsr1Handler)
4142
import Ide.Types (PluginId (..))
@@ -47,6 +48,7 @@ import OpenTelemetry.Eventlog (Instrument, SpanInFlight,
4748
addEvent, beginSpan, endSpan,
4849
mkValueObserver, observe,
4950
setTag, withSpan, withSpan_)
51+
import System.IO.Unsafe (unsafePerformIO)
5052

5153
-- | Trace a handler using OpenTelemetry. Adds various useful info into tags in the OpenTelemetry span.
5254
otTracedHandler
@@ -68,6 +70,14 @@ otTracedHandler requestType label act =
6870
otSetUri :: SpanInFlight -> Uri -> IO ()
6971
otSetUri sp (Uri t) = setTag sp "uri" (encodeUtf8 t)
7072

73+
{-# NOINLINE isTracingEnabled #-}
74+
isTracingEnabled :: Bool
75+
isTracingEnabled = unsafePerformIO $ do
76+
flags <- getTraceFlags
77+
case tracing flags of
78+
TraceNone -> return False
79+
_ -> return True
80+
7181
-- | Trace a Shake action using opentelemetry.
7282
otTracedAction
7383
:: Show k
@@ -76,23 +86,26 @@ otTracedAction
7686
-> (a -> Bool) -- ^ Did this action succeed?
7787
-> Action a -- ^ The action
7888
-> Action a
79-
otTracedAction key file success act = actionBracket
80-
(do
81-
sp <- beginSpan (fromString (show key))
82-
setTag sp "File" (fromString $ fromNormalizedFilePath file)
83-
return sp
84-
)
85-
endSpan
86-
(\sp -> do
87-
res <- act
88-
unless (success res) $ setTag sp "error" "1"
89-
return res)
90-
91-
#if MIN_GHC_API_VERSION(8,8,0)
89+
otTracedAction key file success act
90+
| isTracingEnabled =
91+
actionBracket
92+
(do
93+
sp <- beginSpan (fromString (show key))
94+
setTag sp "File" (fromString $ fromNormalizedFilePath file)
95+
return sp
96+
)
97+
endSpan
98+
(\sp -> do
99+
res <- act
100+
unless (success res) $ setTag sp "error" "1"
101+
return res)
102+
| otherwise = act
103+
104+
92105
otTracedProvider :: MonadUnliftIO m => PluginId -> ByteString -> m a -> m a
93-
#else
94-
otTracedProvider :: MonadUnliftIO m => PluginId -> String -> m a -> m a
95-
#endif
106+
107+
108+
96109
otTracedProvider (PluginId pluginName) provider act = do
97110
runInIO <- askRunInIO
98111
liftIO $ withSpan (provider <> " provider") $ \sp -> do
@@ -220,3 +233,4 @@ repeatUntilJust nattempts action = do
220233
case res of
221234
Nothing -> repeatUntilJust (nattempts-1) action
222235
Just{} -> return res
236+

0 commit comments

Comments
 (0)