@@ -6,7 +6,9 @@ module Development.IDE.Core.Tracing
66 , startTelemetry
77 , measureMemory
88 , getInstrumentCached
9- ,otTracedProvider ,otSetUri )
9+ , otTracedProvider
10+ , otSetUri
11+ )
1012where
1113
1214import Control.Concurrent.Async (Async , async )
@@ -26,6 +28,7 @@ import Data.IORef (modifyIORef', newIORef,
2628 readIORef , writeIORef )
2729import Data.String (IsString (fromString ))
2830import Data.Text.Encoding (encodeUtf8 )
31+ import Debug.Trace.Flags (userTracingEnabled )
2932import Development.IDE.Core.RuleTypes (GhcSession (GhcSession ),
3033 GhcSessionDeps (GhcSessionDeps ),
3134 GhcSessionIO (GhcSessionIO ))
@@ -36,19 +39,17 @@ import Development.IDE.Types.Shake (Key (..), Value,
3639 Values )
3740import Development.Shake (Action , actionBracket )
3841import Foreign.Storable (Storable (sizeOf ))
39- import GHC.RTS.Flags
4042import HeapSize (recursiveSize , runHeapsize )
4143import Ide.PluginUtils (installSigUsr1Handler )
4244import Ide.Types (PluginId (.. ))
4345import Language.LSP.Types (NormalizedFilePath ,
4446 fromNormalizedFilePath )
4547import Numeric.Natural (Natural )
46- import OpenTelemetry.Eventlog (Instrument , SpanInFlight ,
48+ import OpenTelemetry.Eventlog (Instrument , SpanInFlight ( .. ) ,
4749 Synchronicity (Asynchronous ),
4850 addEvent , beginSpan , endSpan ,
4951 mkValueObserver , observe ,
5052 setTag , withSpan , withSpan_ )
51- import System.IO.Unsafe (unsafePerformIO )
5253
5354-- | Trace a handler using OpenTelemetry. Adds various useful info into tags in the OpenTelemetry span.
5455otTracedHandler
@@ -57,27 +58,20 @@ otTracedHandler
5758 -> String -- ^ Message label
5859 -> (SpanInFlight -> m a )
5960 -> m a
60- otTracedHandler requestType label act =
61- let ! name =
62- if null label
63- then requestType
64- else requestType <> " :" <> show label
65- -- Add an event so all requests can be quickly seen in the viewer without searching
66- in do
67- runInIO <- askRunInIO
68- liftIO $ withSpan (fromString name) (\ sp -> addEvent sp " " (fromString $ name <> " received" ) >> runInIO (act sp))
61+ otTracedHandler requestType label act
62+ | userTracingEnabled = do
63+ let ! name =
64+ if null label
65+ then requestType
66+ else requestType <> " :" <> show label
67+ -- Add an event so all requests can be quickly seen in the viewer without searching
68+ runInIO <- askRunInIO
69+ liftIO $ withSpan (fromString name) (\ sp -> addEvent sp " " (fromString $ name <> " received" ) >> runInIO (act sp))
70+ | otherwise = act (SpanInFlight 0 )
6971
7072otSetUri :: SpanInFlight -> Uri -> IO ()
7173otSetUri sp (Uri t) = setTag sp " uri" (encodeUtf8 t)
7274
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-
8175-- | Trace a Shake action using opentelemetry.
8276otTracedAction
8377 :: Show k
@@ -87,7 +81,7 @@ otTracedAction
8781 -> Action a -- ^ The action
8882 -> Action a
8983otTracedAction key file success act
90- | isTracingEnabled =
84+ | userTracingEnabled =
9185 actionBracket
9286 (do
9387 sp <- beginSpan (fromString (show key))
@@ -106,11 +100,13 @@ otTracedProvider :: MonadUnliftIO m => PluginId -> ByteString -> m a -> m a
106100#else
107101otTracedProvider :: MonadUnliftIO m => PluginId -> String -> m a -> m a
108102#endif
109- otTracedProvider (PluginId pluginName) provider act = do
110- runInIO <- askRunInIO
111- liftIO $ withSpan (provider <> " provider" ) $ \ sp -> do
112- setTag sp " plugin" (encodeUtf8 pluginName)
113- runInIO act
103+ otTracedProvider (PluginId pluginName) provider act
104+ | userTracingEnabled = do
105+ runInIO <- askRunInIO
106+ liftIO $ withSpan (provider <> " provider" ) $ \ sp -> do
107+ setTag sp " plugin" (encodeUtf8 pluginName)
108+ runInIO act
109+ | otherwise = act
114110
115111startTelemetry :: Bool -> Logger -> Var Values -> IO ()
116112startTelemetry allTheTime logger stateRef = do
0 commit comments