Skip to content

Commit 36a65b0

Browse files
authored
Skip tracing unless eventlog is enabled (haskell#1658)
* Skip tracing unless eventlog is enabled This was done only for otTracedAction but not for otTracedHandler and otTracedProvider * fix syntax * make up a SpanInFlight * reuse userTracingEnabled
1 parent 6fce454 commit 36a65b0

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

ghcide/ghcide.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ library
5252
filepath,
5353
fingertree,
5454
ghc-exactprint,
55+
ghc-trace-events,
5556
Glob,
5657
haddock-library ^>= 1.10.0,
5758
hashable,

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

+23-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ module Development.IDE.Core.Tracing
66
, startTelemetry
77
, measureMemory
88
, getInstrumentCached
9-
,otTracedProvider,otSetUri)
9+
, otTracedProvider
10+
, otSetUri
11+
)
1012
where
1113

1214
import Control.Concurrent.Async (Async, async)
@@ -26,6 +28,7 @@ import Data.IORef (modifyIORef', newIORef,
2628
readIORef, writeIORef)
2729
import Data.String (IsString (fromString))
2830
import Data.Text.Encoding (encodeUtf8)
31+
import Debug.Trace.Flags (userTracingEnabled)
2932
import 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)
3740
import Development.Shake (Action, actionBracket)
3841
import Foreign.Storable (Storable (sizeOf))
39-
import GHC.RTS.Flags
4042
import HeapSize (recursiveSize, runHeapsize)
4143
import Ide.PluginUtils (installSigUsr1Handler)
4244
import Ide.Types (PluginId (..))
4345
import Language.LSP.Types (NormalizedFilePath,
4446
fromNormalizedFilePath)
4547
import 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.
5455
otTracedHandler
@@ -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

7072
otSetUri :: SpanInFlight -> Uri -> IO ()
7173
otSetUri 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.
8276
otTracedAction
8377
:: Show k
@@ -87,7 +81,7 @@ otTracedAction
8781
-> Action a -- ^ The action
8882
-> Action a
8983
otTracedAction 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
107101
otTracedProvider :: 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

115111
startTelemetry :: Bool -> Logger -> Var Values -> IO ()
116112
startTelemetry allTheTime logger stateRef = do

0 commit comments

Comments
 (0)