Skip to content
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

Instrumentation for trace profiling #5260

Closed
wants to merge 15 commits into from
Closed
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
22 changes: 17 additions & 5 deletions etc/scripts/release.hs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,24 @@ rules global@Global{..} args = do
releaseCheckDir </> binaryExeFileName %> \out -> do
need [releaseBinDir </> binaryName </> stackExeFileName]

-- Run "git diff" so we can see what changes exist, in case things fail below
() <- cmd "git diff"
-- Symlinks in the repo cause a spurious git status change notification
-- on Windows, so we don't run this on Windows. Instead we rely on
-- Linux/Mac testing for that.

Stdout dirty <- cmd "git status --porcelain"
when (not gAllowDirty && not (null (trim dirty))) $
error ("Working tree is dirty. Use --" ++ allowDirtyOptName ++ " option to continue anyway.")
case platformOS of
Windows -> pure ()
_ -> do
-- Run "git diff" so we can see what changes exist, in case things fail below
() <- cmd "git diff"

Stdout dirty <- cmd "git status --porcelain"
when (not gAllowDirty && not (null (trim dirty))) $
error $ concat
[ "Working tree is dirty. Use --"
, allowDirtyOptName
, " option to continue anyway. Output:\n"
, show dirty
]
() <- cmd
[gProjectRoot </> releaseBinDir </> binaryName </> stackExeFileName]
(stackArgs global)
Expand Down
2 changes: 2 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ghc-options:
dependencies:
- Cabal
- aeson
- opentelemetry >= 0.5.0
- annotated-wl-pprint
- ansi-terminal
- array
Expand Down Expand Up @@ -275,6 +276,7 @@ executables:
ghc-options:
- -threaded
- -rtsopts
- -eventlog
dependencies:
- stack
when:
Expand Down
45 changes: 24 additions & 21 deletions src/Stack/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import Stack.Types.SourceMap

import Stack.Types.Compiler (compilerVersionText, getGhcVersion)
import System.Terminal (fixCodePage)
import OpenTelemetry.Eventlog

-- | Build.
--
Expand All @@ -56,7 +57,7 @@ import System.Terminal (fixCodePage)
build :: HasEnvConfig env
=> Maybe (Set (Path Abs File) -> IO ()) -- ^ callback after discovering all local files
-> RIO env ()
build msetLocalFiles = do
build msetLocalFiles = withSpan_ "Build.build" $ do
mcp <- view $ configL.to configModifyCodePage
ghcVersion <- view $ actualCompilerVersionL.to getGhcVersion
fixCodePage mcp ghcVersion $ do
Expand All @@ -69,26 +70,28 @@ build msetLocalFiles = do
checkSubLibraryDependencies (Map.elems $ smProject sourceMap)

boptsCli <- view $ envConfigL.to envConfigBuildOptsCLI
-- Set local files, necessary for file watching
stackYaml <- view stackYamlL
for_ msetLocalFiles $ \setLocalFiles -> do
files <-
if boptsCLIWatchAll boptsCli
then sequence [lpFiles lp | lp <- allLocals]
else forM allLocals $ \lp -> do
let pn = packageName (lpPackage lp)
case Map.lookup pn (smtTargets $ smTargets sourceMap) of
Nothing ->
pure Set.empty
Just (TargetAll _) ->
lpFiles lp
Just (TargetComps components) ->
lpFilesForComponents components lp
liftIO $ setLocalFiles $ Set.insert stackYaml $ Set.unions files
withSpan_ "Build.build_setLocalFiles" $ do
-- Set local files, necessary for file watching
stackYaml <- view stackYamlL
for_ msetLocalFiles $ \setLocalFiles -> do
files <-
if boptsCLIWatchAll boptsCli
then sequence [lpFiles lp | lp <- allLocals]
else forM allLocals $ \lp -> do
let pn = packageName (lpPackage lp)
case Map.lookup pn (smtTargets $ smTargets sourceMap) of
Nothing ->
pure Set.empty
Just (TargetAll _) ->
lpFiles lp
Just (TargetComps components) ->
lpFilesForComponents components lp
liftIO $ setLocalFiles $ Set.insert stackYaml $ Set.unions files

checkComponentsBuildable allLocals
withSpan_ "Build.build_checkComponentsBuildable" $ do
checkComponentsBuildable allLocals

installMap <- toInstallMap sourceMap
installMap <- withSpan_ "Build.Installed.toInstallMap" $ toInstallMap sourceMap
(installedMap, globalDumpPkgs, snapshotDumpPkgs, localDumpPkgs) <-
getInstalled installMap

Expand Down Expand Up @@ -235,7 +238,7 @@ splitObjsWarning = unwords
-- | Get the @BaseConfigOpts@ necessary for constructing configure options
mkBaseConfigOpts :: (HasEnvConfig env)
=> BuildOptsCLI -> RIO env BaseConfigOpts
mkBaseConfigOpts boptsCli = do
mkBaseConfigOpts boptsCli = withSpan_ "Build.mkBaseConfigOpts" $ do
bopts <- view buildOptsL
snapDBPath <- packageDatabaseDeps
localDBPath <- packageDatabaseLocal
Expand All @@ -260,7 +263,7 @@ loadPackage
-> [Text] -- ^ GHC options
-> [Text] -- ^ Cabal configure options
-> RIO env Package
loadPackage loc flags ghcOptions cabalConfigOpts = do
loadPackage loc flags ghcOptions cabalConfigOpts = withSpan_ "Build.loadPackage" $ do
compiler <- view actualCompilerVersionL
platform <- view platformL
let pkgConfig = PackageConfig
Expand Down
4 changes: 3 additions & 1 deletion src/Stack/Build/ConstructPlan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import System.IO (putStrLn)
import RIO.PrettyPrint
import RIO.Process (findExecutable, HasProcessContext (..))

import OpenTelemetry.Eventlog

data PackageInfo
=
-- | This indicates that the package is already installed, and
Expand Down Expand Up @@ -173,7 +175,7 @@ constructPlan :: forall env. HasEnvConfig env
-> InstalledMap
-> Bool
-> RIO env Plan
constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap initialBuildSteps = do
constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap initialBuildSteps = withSpan_ "Build.ConstructPlan.constructPlan" $ do
logDebug "Constructing the build plan"

when hasBaseInDeps $
Expand Down
18 changes: 10 additions & 8 deletions src/Stack/Build/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ import RIO.PrettyPrint
import RIO.Process
import Pantry.Internal.Companion

import OpenTelemetry.Eventlog

-- | Has an executable been built or not?
data ExecutableBuildStatus
= ExecutableBuilt
Expand Down Expand Up @@ -247,7 +249,7 @@ getSetupExe :: HasEnvConfig env
-> Path Abs File -- ^ SetupShim.hs input file
-> Path Abs Dir -- ^ temporary directory
-> RIO env (Maybe (Path Abs File))
getSetupExe setupHs setupShimHs tmpdir = do
getSetupExe setupHs setupShimHs tmpdir = withSpan_ "Build.Execute.getSetupExe" $ do
wc <- view $ actualCompilerVersionL.whichCompilerL
platformDir <- platformGhcRelDir
config <- view configL
Expand Down Expand Up @@ -480,7 +482,7 @@ executePlan :: HasEnvConfig env
-> Map PackageName Target
-> Plan
-> RIO env ()
executePlan boptsCli baseConfigOpts locals globalPackages snapshotPackages localPackages installedMap targets plan = do
executePlan boptsCli baseConfigOpts locals globalPackages snapshotPackages localPackages installedMap targets plan = withSpan_ "Build.Execute.executePlan" $ do
logDebug "Executing the build plan"
bopts <- view buildOptsL
withExecuteEnv bopts boptsCli baseConfigOpts locals globalPackages snapshotPackages localPackages mlargestPackageName
Expand Down Expand Up @@ -510,7 +512,7 @@ copyExecutables
=> Map Text InstallLocation
-> RIO env ()
copyExecutables exes | Map.null exes = return ()
copyExecutables exes = do
copyExecutables exes = withSpan_ "Build.Execute.copyExecutables" $ do
snapBin <- (</> bindirSuffix) `liftM` installationRootDeps
localBin <- (</> bindirSuffix) `liftM` installationRootLocal
compilerSpecific <- boptsInstallCompilerTool <$> view buildOptsL
Expand Down Expand Up @@ -669,7 +671,7 @@ unregisterPackages ::
-> Path Abs Dir
-> NonEmpty (GhcPkgId, (PackageIdentifier, Text))
-> RIO env ()
unregisterPackages cv localDB ids = do
unregisterPackages cv localDB ids = withSpan_ "Build.Execute.unregisterPackages" $ do
let logReason ident reason =
logInfo $
fromString (packageIdentifierString ident) <> ": unregistering" <>
Expand Down Expand Up @@ -844,7 +846,7 @@ ensureConfig :: HasEnvConfig env
-> Path Abs File -- ^ .cabal file
-> Task
-> RIO env Bool
ensureConfig newConfigCache pkgDir ExecuteEnv {..} announce cabal cabalfp task = do
ensureConfig newConfigCache pkgDir ExecuteEnv {..} announce cabal cabalfp task = withSpan_ "Build.Execute.ensureConfig" $ do
newCabalMod <- liftIO $ modificationTime <$> getFileStatus (toFilePath cabalfp)
setupConfigfp <- setupConfigFromDir pkgDir
newSetupConfigMod <- liftIO $ either (const Nothing) (Just . modificationTime) <$>
Expand Down Expand Up @@ -1129,7 +1131,7 @@ withSingleContext ActionContext {..} ee@ExecuteEnv {..} task@Task {..} mdeps msu
-> OutputType
-> ((KeepOutputOpen -> ExcludeTHLoading -> [String] -> RIO env ()) -> RIO env a)
-> RIO env a
withCabal package pkgDir outputType inner = do
withCabal package pkgDir outputType inner = withSpan_ "Build.Execute.withCabal" $ do
config <- view configL
unless (configAllowDifferentUser config) $
checkOwnership (pkgDir </> configWorkDir config)
Expand Down Expand Up @@ -1478,7 +1480,7 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap
return $ if b then Just pc else Nothing
_ -> return Nothing

copyPreCompiled (PrecompiledCache mlib sublibs exes) = do
copyPreCompiled (PrecompiledCache mlib sublibs exes) = withSpan_ "Build.Execute.copyPreCompiled" $ do
wc <- view $ actualCompilerVersionL.whichCompilerL
announceTask ee task "using precompiled package"

Expand Down Expand Up @@ -1586,7 +1588,7 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap
-> (Utf8Builder -> RIO env ())
-> Map Text ExecutableBuildStatus
-> RIO env Installed
realBuild cache package pkgDir cabal0 announce executableBuildStatuses = do
realBuild cache package pkgDir cabal0 announce executableBuildStatuses = withSpan_ "Build.Execute.realBuild" $ do
let cabal = cabal0 CloseOnException
wc <- view $ actualCompilerVersionL.whichCompilerL

Expand Down
6 changes: 4 additions & 2 deletions src/Stack/Build/Installed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import Stack.Types.GhcPkgId
import Stack.Types.Package
import Stack.Types.SourceMap

import OpenTelemetry.Eventlog

toInstallMap :: MonadIO m => SourceMap -> m InstallMap
toInstallMap sourceMap = do
projectInstalls <-
Expand All @@ -53,7 +55,7 @@ getInstalled :: HasEnvConfig env
, [DumpPackage] -- snapshot installed
, [DumpPackage] -- locally installed
)
getInstalled {-opts-} installMap = do
getInstalled {-opts-} installMap = withSpan_ "Build.Installed.getInstalled" $ do
logDebug "Finding out which packages are already installed"
snapDBPath <- packageDatabaseDeps
localDBPath <- packageDatabaseLocal
Expand Down Expand Up @@ -114,7 +116,7 @@ loadDatabase :: HasEnvConfig env
-> Maybe (InstalledPackageLocation, Path Abs Dir) -- ^ package database, Nothing for global
-> [LoadHelper] -- ^ from parent databases
-> RIO env ([LoadHelper], [DumpPackage])
loadDatabase installMap mdb lhs0 = do
loadDatabase installMap mdb lhs0 = withSpan_ "Build.Installed.loadDatabase" $ do
pkgexe <- getGhcPkgExe
(lhs1', dps) <- ghcPkgDump pkgexe (fmap snd (maybeToList mdb))
$ conduitDumpPackage .| sink
Expand Down
12 changes: 7 additions & 5 deletions src/Stack/Build/Source.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ import System.FilePath (takeFileName)
import System.IO.Error (isDoesNotExistError)
import System.PosixCompat.Files (modificationTime, getFileStatus)

import OpenTelemetry.Eventlog

-- | loads and returns project packages
projectLocalPackages :: HasEnvConfig env
=> RIO env [LocalPackage]
projectLocalPackages = do
projectLocalPackages = withSpan_ "Build.Source.projectLocalPackages" $ do
sm <- view $ envConfigL.to envConfigSourceMap
for (toList $ smProject sm) loadLocalPackage

-- | loads all local dependencies - project packages and local extra-deps
localDependencies :: HasEnvConfig env => RIO env [LocalPackage]
localDependencies = do
localDependencies = withSpan_ "Build.Source.localDependencies" $ do
bopts <- view $ configL.to configBuild
sourceMap <- view $ envConfigL . to envConfigSourceMap
forMaybeM (Map.elems $ smDeps sourceMap) $ \dp ->
Expand All @@ -69,7 +71,7 @@ loadSourceMap :: HasBuildConfig env
-> BuildOptsCLI
-> SMActual DumpedGlobalPackage
-> RIO env SourceMap
loadSourceMap smt boptsCli sma = do
loadSourceMap smt boptsCli sma = withSpan_ "Build.Source.loadSourceMap" $ do
bconfig <- view buildConfigL
let compiler = smaCompiler sma
project = M.map applyOptsFlagsPP $ smaProject sma
Expand Down Expand Up @@ -250,7 +252,7 @@ loadCommonPackage ::
forall env. (HasBuildConfig env, HasSourceMap env)
=> CommonPackage
-> RIO env Package
loadCommonPackage common = do
loadCommonPackage common = withSpan_ "Build.Source.loadCommonPackage" $ do
config <- getPackageConfig (cpFlags common) (cpGhcOptions common) (cpCabalConfigOpts common)
gpkg <- liftIO $ cpGPD common
return $ resolvePackage config gpkg
Expand All @@ -261,7 +263,7 @@ loadLocalPackage ::
forall env. (HasBuildConfig env, HasSourceMap env)
=> ProjectPackage
-> RIO env LocalPackage
loadLocalPackage pp = do
loadLocalPackage pp = withSpan_ "Build.Source.loadLocalPackage" $ do
sm <- view sourceMapL
let common = ppCommon pp
bopts <- view buildOptsL
Expand Down
12 changes: 7 additions & 5 deletions src/Stack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ import RIO.PrettyPrint (stylesUpdateL, useColorL)
import RIO.Process
import RIO.Time (toGregorian)

import OpenTelemetry.Eventlog

-- | If deprecated path exists, use it and print a warning.
-- Otherwise, return the new path.
tryDeprecatedPath
Expand Down Expand Up @@ -145,7 +147,7 @@ makeConcreteResolver
=> AbstractResolver
-> RIO env RawSnapshotLocation
makeConcreteResolver (ARResolver r) = pure r
makeConcreteResolver ar = do
makeConcreteResolver ar = withSpan_ "Config.makeConcreteResolver" $ do
r <-
case ar of
ARResolver r -> assert False $ makeConcreteResolver (ARResolver r)
Expand Down Expand Up @@ -434,7 +436,7 @@ getDefaultLocalProgramsBase configStackRoot configPlatform override =
-- | Load the configuration, using current directory, environment variables,
-- and defaults as necessary.
loadConfig :: HasRunner env => (Config -> RIO env a) -> RIO env a
loadConfig inner = do
loadConfig inner = withSpan_ "Config.loadConfig" $ do
mstackYaml <- view $ globalOptsL.to globalStackYaml
mproject <- loadProjectConfig mstackYaml
mresolver <- view $ globalOptsL.to globalResolver
Expand Down Expand Up @@ -480,7 +482,7 @@ loadConfig inner = do
withBuildConfig
:: RIO BuildConfig a
-> RIO Config a
withBuildConfig inner = do
withBuildConfig inner = withSpan_ "Config.withBuildConfig" $ do
config <- ask

-- If provided, turn the AbstractResolver from the command line
Expand Down Expand Up @@ -796,7 +798,7 @@ getExtraConfigs userConfigPath = do
loadConfigYaml
:: HasLogFunc env
=> (Value -> Yaml.Parser (WithJSONWarnings a)) -> Path Abs File -> RIO env a
loadConfigYaml parser path = do
loadConfigYaml parser path = withSpan_ "Config.loadConfigYaml" $ do
eres <- loadYaml parser path
case eres of
Left err -> liftIO $ throwM (ParseConfigFileException path err)
Expand Down Expand Up @@ -851,7 +853,7 @@ loadProjectConfig :: HasLogFunc env
=> StackYamlLoc
-- ^ Override stack.yaml
-> RIO env (ProjectConfig (Project, Path Abs File, ConfigMonoid))
loadProjectConfig mstackYaml = do
loadProjectConfig mstackYaml = withSpan_ "Config.loadProjectConfig" $ do
mfp <- getProjectConfig mstackYaml
case mfp of
PCProject fp -> do
Expand Down
8 changes: 5 additions & 3 deletions src/Stack/Lock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import Stack.SourceMap
import Stack.Types.Config
import Stack.Types.SourceMap

import OpenTelemetry.Eventlog

data LockedLocation a b = LockedLocation
{ llOriginal :: a
, llCompleted :: b
Expand Down Expand Up @@ -77,7 +79,7 @@ instance FromJSON (WithJSONWarnings (Unresolved Locked)) where
loadYamlThrow
:: HasLogFunc env
=> (Value -> Yaml.Parser (WithJSONWarnings a)) -> Path Abs File -> RIO env a
loadYamlThrow parser path = do
loadYamlThrow parser path = withSpan_ "Lock.loadYamlThrow" $ do
val <- liftIO $ Yaml.decodeFileThrow (toFilePath path)
case Yaml.parseEither parser val of
Left err -> throwIO $ Yaml.AesonException err
Expand All @@ -94,7 +96,7 @@ lockCachedWanted ::
-> Map PackageName (Bool -> RIO env DepPackage)
-> RIO env ( SMWanted, [CompletedPLI]))
-> RIO env SMWanted
lockCachedWanted stackFile resolver fillWanted = do
lockCachedWanted stackFile resolver fillWanted = withSpan_ "Lock.lockCacheWanted" $ do
lockFile <- liftIO $ addExtension ".lock" stackFile
let getLockExists = doesFileExist lockFile
lfb <- view lockFileBehaviorL
Expand All @@ -118,7 +120,7 @@ lockCachedWanted stackFile resolver fillWanted = do
slocCache = toMap $ lckSnapshotLocations locked
pkgLocCache = toMap $ lckPkgImmutableLocations locked
(snap, slocCompleted, pliCompleted) <-
loadAndCompleteSnapshotRaw resolver slocCache pkgLocCache
withSpan_ "Pantry.loadAndCompleteSnapshotRaw" $ loadAndCompleteSnapshotRaw resolver slocCache pkgLocCache
let compiler = snapshotCompiler snap
snPkgs = Map.mapWithKey (\n p h -> snapToDepPackage h n p) (snapshotPackages snap)
(wanted, prjCompleted) <- fillWanted pkgLocCache compiler snPkgs
Expand Down
Loading