@@ -6,7 +6,9 @@ module Development.IDE.Core.Tracing
6
6
, startTelemetry
7
7
, measureMemory
8
8
, getInstrumentCached
9
- ,otTracedProvider ,otSetUri )
9
+ , otTracedProvider
10
+ , otSetUri
11
+ )
10
12
where
11
13
12
14
import Control.Concurrent.Async (Async , async )
@@ -26,6 +28,7 @@ import Data.IORef (modifyIORef', newIORef,
26
28
readIORef , writeIORef )
27
29
import Data.String (IsString (fromString ))
28
30
import Data.Text.Encoding (encodeUtf8 )
31
+ import Debug.Trace.Flags (userTracingEnabled )
29
32
import Development.IDE.Core.RuleTypes (GhcSession (GhcSession ),
30
33
GhcSessionDeps (GhcSessionDeps ),
31
34
GhcSessionIO (GhcSessionIO ))
@@ -36,19 +39,17 @@ import Development.IDE.Types.Shake (Key (..), Value,
36
39
Values )
37
40
import Development.Shake (Action , actionBracket )
38
41
import Foreign.Storable (Storable (sizeOf ))
39
- import GHC.RTS.Flags
40
42
import HeapSize (recursiveSize , runHeapsize )
41
43
import Ide.PluginUtils (installSigUsr1Handler )
42
44
import Ide.Types (PluginId (.. ))
43
45
import Language.LSP.Types (NormalizedFilePath ,
44
46
fromNormalizedFilePath )
45
47
import Numeric.Natural (Natural )
46
- import OpenTelemetry.Eventlog (Instrument , SpanInFlight ,
48
+ import OpenTelemetry.Eventlog (Instrument , SpanInFlight ( .. ) ,
47
49
Synchronicity (Asynchronous ),
48
50
addEvent , beginSpan , endSpan ,
49
51
mkValueObserver , observe ,
50
52
setTag , withSpan , withSpan_ )
51
- import System.IO.Unsafe (unsafePerformIO )
52
53
53
54
-- | Trace a handler using OpenTelemetry. Adds various useful info into tags in the OpenTelemetry span.
54
55
otTracedHandler
@@ -57,27 +58,20 @@ otTracedHandler
57
58
-> String -- ^ Message label
58
59
-> (SpanInFlight -> m a )
59
60
-> 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 )
69
71
70
72
otSetUri :: SpanInFlight -> Uri -> IO ()
71
73
otSetUri sp (Uri t) = setTag sp " uri" (encodeUtf8 t)
72
74
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
-
81
75
-- | Trace a Shake action using opentelemetry.
82
76
otTracedAction
83
77
:: Show k
@@ -87,7 +81,7 @@ otTracedAction
87
81
-> Action a -- ^ The action
88
82
-> Action a
89
83
otTracedAction key file success act
90
- | isTracingEnabled =
84
+ | userTracingEnabled =
91
85
actionBracket
92
86
(do
93
87
sp <- beginSpan (fromString (show key))
@@ -106,11 +100,13 @@ otTracedProvider :: MonadUnliftIO m => PluginId -> ByteString -> m a -> m a
106
100
#else
107
101
otTracedProvider :: MonadUnliftIO m => PluginId -> String -> m a -> m a
108
102
#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
114
110
115
111
startTelemetry :: Bool -> Logger -> Var Values -> IO ()
116
112
startTelemetry allTheTime logger stateRef = do
0 commit comments