Skip to content

Skip tracing unless eventlog is enabled #1658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ghcide/ghcide.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ library
filepath,
fingertree,
ghc-exactprint,
ghc-trace-events,
Glob,
haddock-library ^>= 1.10.0,
hashable,
Expand Down
50 changes: 23 additions & 27 deletions ghcide/src/Development/IDE/Core/Tracing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module Development.IDE.Core.Tracing
, startTelemetry
, measureMemory
, getInstrumentCached
,otTracedProvider,otSetUri)
, otTracedProvider
, otSetUri
)
where

import Control.Concurrent.Async (Async, async)
Expand All @@ -26,6 +28,7 @@ import Data.IORef (modifyIORef', newIORef,
readIORef, writeIORef)
import Data.String (IsString (fromString))
import Data.Text.Encoding (encodeUtf8)
import Debug.Trace.Flags (userTracingEnabled)
import Development.IDE.Core.RuleTypes (GhcSession (GhcSession),
GhcSessionDeps (GhcSessionDeps),
GhcSessionIO (GhcSessionIO))
Expand All @@ -36,19 +39,17 @@ import Development.IDE.Types.Shake (Key (..), Value,
Values)
import Development.Shake (Action, actionBracket)
import Foreign.Storable (Storable (sizeOf))
import GHC.RTS.Flags
import HeapSize (recursiveSize, runHeapsize)
import Ide.PluginUtils (installSigUsr1Handler)
import Ide.Types (PluginId (..))
import Language.LSP.Types (NormalizedFilePath,
fromNormalizedFilePath)
import Numeric.Natural (Natural)
import OpenTelemetry.Eventlog (Instrument, SpanInFlight,
import OpenTelemetry.Eventlog (Instrument, SpanInFlight (..),
Synchronicity (Asynchronous),
addEvent, beginSpan, endSpan,
mkValueObserver, observe,
setTag, withSpan, withSpan_)
import System.IO.Unsafe (unsafePerformIO)

-- | Trace a handler using OpenTelemetry. Adds various useful info into tags in the OpenTelemetry span.
otTracedHandler
Expand All @@ -57,27 +58,20 @@ otTracedHandler
-> String -- ^ Message label
-> (SpanInFlight -> m a)
-> m a
otTracedHandler requestType label act =
let !name =
if null label
then requestType
else requestType <> ":" <> show label
-- Add an event so all requests can be quickly seen in the viewer without searching
in do
runInIO <- askRunInIO
liftIO $ withSpan (fromString name) (\sp -> addEvent sp "" (fromString $ name <> " received") >> runInIO (act sp))
otTracedHandler requestType label act
| userTracingEnabled = do
let !name =
if null label
then requestType
else requestType <> ":" <> show label
-- Add an event so all requests can be quickly seen in the viewer without searching
runInIO <- askRunInIO
liftIO $ withSpan (fromString name) (\sp -> addEvent sp "" (fromString $ name <> " received") >> runInIO (act sp))
| otherwise = act (SpanInFlight 0)

otSetUri :: SpanInFlight -> Uri -> IO ()
otSetUri sp (Uri t) = setTag sp "uri" (encodeUtf8 t)

{-# NOINLINE isTracingEnabled #-}
isTracingEnabled :: Bool
isTracingEnabled = unsafePerformIO $ do
flags <- getTraceFlags
case tracing flags of
TraceNone -> return False
_ -> return True

-- | Trace a Shake action using opentelemetry.
otTracedAction
:: Show k
Expand All @@ -87,7 +81,7 @@ otTracedAction
-> Action a -- ^ The action
-> Action a
otTracedAction key file success act
| isTracingEnabled =
| userTracingEnabled =
actionBracket
(do
sp <- beginSpan (fromString (show key))
Expand All @@ -106,11 +100,13 @@ otTracedProvider :: MonadUnliftIO m => PluginId -> ByteString -> m a -> m a
#else
otTracedProvider :: MonadUnliftIO m => PluginId -> String -> m a -> m a
#endif
otTracedProvider (PluginId pluginName) provider act = do
runInIO <- askRunInIO
liftIO $ withSpan (provider <> " provider") $ \sp -> do
setTag sp "plugin" (encodeUtf8 pluginName)
runInIO act
otTracedProvider (PluginId pluginName) provider act
| userTracingEnabled = do
runInIO <- askRunInIO
liftIO $ withSpan (provider <> " provider") $ \sp -> do
setTag sp "plugin" (encodeUtf8 pluginName)
runInIO act
| otherwise = act

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