{- |
 Module      :  OpenTelemetry.Resource.Process
 Copyright   :  (c) Ian Duncan, 2021
 License     :  BSD-3
 Description :  Standard resources and detectors for system processes
 Maintainer  :  Ian Duncan
 Stability   :  experimental
 Portability :  non-portable (GHC extensions)
-}
module OpenTelemetry.Resource.Process where

import Data.Text (Text)
import OpenTelemetry.Attributes.Key (unkey)
import OpenTelemetry.Resource
import qualified OpenTelemetry.SemanticConventions as SC


{- |  An operating system process.

@since 0.0.1.0
-}
data Process = Process
  { Process -> Maybe Int
processPid :: Maybe Int
  {- ^ Process identifier (PID).

  Example: @1234@
  -}
  , Process -> Maybe Text
processExecutableName :: Maybe Text
  {- ^ The name of the process executable. On Linux based systems, can be set to the @Name@ in @proc/[pid]/status@. On Windows, can be set to the base name of @GetProcessImageFileNameW@.

  Example: @otelcol@
  -}
  , Process -> Maybe Text
processExecutablePath :: Maybe Text
  {- ^ The full path to the process executable. On Linux based systems, can be set to the target of @proc/[pid]/exe@. On Windows, can be set to the result of @GetProcessImageFileNameW@.

  Example: @/usr/bin/cmd/otelcol@
  -}
  , Process -> Maybe Text
processCommand :: Maybe Text
  {- ^ The command used to launch the process (i.e. the command name). On Linux based systems, can be set to the zeroth string in @proc/[pid]/cmdline@. On Windows, can be set to the first parameter extracted from @GetCommandLineW@.

  Example: @cmd/otelcol@
  -}
  , Process -> Maybe Text
processCommandLine :: Maybe Text
  {- ^ The full command used to launch the process as a single string representing the full command. On Windows, can be set to the result of @GetCommandLineW@. Do not set this if you have to assemble it just for monitoring; use @process.command_args@ instead.

  Example: @C:\cmd\otecol --config="my directory\config.yaml"@
  -}
  , Process -> Maybe [Text]
processCommandArgs :: Maybe [Text]
  {- ^ All the command arguments (including the command/executable itself) as received by the process. On Linux-based systems (and some other Unixoid systems supporting procfs), can be set according to the list of null-delimited strings extracted from @proc/[pid]/cmdline@. For libc-based executables, this would be the full argv vector passed to main.

  Example: @[cmd/otecol, --config=config.yaml]@
  -}
  , Process -> Maybe Text
processOwner :: Maybe Text
  {- ^ The username of the user that owns the process.

  Example: @root@
  -}
  , Process -> Maybe Text
processCreationTime :: Maybe Text
  {- ^ The date and time the process was created, in ISO 8601 format.

  Example: @"2023-11-21T09:25:34.853Z"@
  -}
  , Process -> Maybe Int
processArgsCount :: Maybe Int
  -- ^ Length of the process.command_args array. Opt-In.
  , Process -> Maybe Int
processParentPid :: Maybe Int
  -- ^ Parent Process identifier (PPID). Opt-In.
  , Process -> Maybe Text
processWorkingDirectory :: Maybe Text
  -- ^ The working directory of the process. Opt-In.
  , Process -> Maybe Bool
processInteractive :: Maybe Bool
  -- ^ Whether the process is connected to an interactive shell. Opt-In.
  , Process -> Maybe Text
processTitle :: Maybe Text
  -- ^ Process title (proctitle). Opt-In.
  , Process -> Maybe Text
processLinuxCgroup :: Maybe Text
  -- ^ The control group associated with the process. Opt-In.
  }


instance ToResource Process where
  toResource :: Process -> Resource
toResource Process {Maybe Bool
Maybe Int
Maybe [Text]
Maybe Text
processPid :: Process -> Maybe Int
processExecutableName :: Process -> Maybe Text
processExecutablePath :: Process -> Maybe Text
processCommand :: Process -> Maybe Text
processCommandLine :: Process -> Maybe Text
processCommandArgs :: Process -> Maybe [Text]
processOwner :: Process -> Maybe Text
processCreationTime :: Process -> Maybe Text
processArgsCount :: Process -> Maybe Int
processParentPid :: Process -> Maybe Int
processWorkingDirectory :: Process -> Maybe Text
processInteractive :: Process -> Maybe Bool
processTitle :: Process -> Maybe Text
processLinuxCgroup :: Process -> Maybe Text
processPid :: Maybe Int
processExecutableName :: Maybe Text
processExecutablePath :: Maybe Text
processCommand :: Maybe Text
processCommandLine :: Maybe Text
processCommandArgs :: Maybe [Text]
processOwner :: Maybe Text
processCreationTime :: Maybe Text
processArgsCount :: Maybe Int
processParentPid :: Maybe Int
processWorkingDirectory :: Maybe Text
processInteractive :: Maybe Bool
processTitle :: Maybe Text
processLinuxCgroup :: Maybe Text
..} =
    Maybe Text -> [Maybe (Text, Attribute)] -> Resource
mkResourceWithSchema
      (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
semConvSchemaUrl)
      [ AttributeKey Int64 -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Int64
SC.process_pid Text -> Maybe Int -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Int
processPid
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_executable_name Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processExecutableName
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_executable_path Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processExecutablePath
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_command Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processCommand
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_commandLine Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processCommandLine
      , AttributeKey [Text] -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey [Text]
SC.process_commandArgs Text -> Maybe [Text] -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe [Text]
processCommandArgs
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_owner Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processOwner
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_creation_time Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processCreationTime
      , AttributeKey Int64 -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Int64
SC.process_argsCount Text -> Maybe Int -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Int
processArgsCount
      , AttributeKey Int64 -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Int64
SC.process_parentPid Text -> Maybe Int -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Int
processParentPid
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_workingDirectory Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processWorkingDirectory
      , AttributeKey Bool -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Bool
SC.process_interactive Text -> Maybe Bool -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Bool
processInteractive
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_title Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processTitle
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_linux_cgroup Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processLinuxCgroup
      ]


{- | The single (language) runtime instance which is monitored.

@since 0.0.1.0
-}
data ProcessRuntime = ProcessRuntime
  { ProcessRuntime -> Maybe Text
processRuntimeName :: Maybe Text
  {- ^ The name of the runtime of this process. For compiled native binaries, this SHOULD be the name of the compiler.

  Example: @OpenJDK Runtime Environment@
  -}
  , ProcessRuntime -> Maybe Text
processRuntimeVersion :: Maybe Text
  {- ^ The version of the runtime of this process, as returned by the runtime without modification.

  Example: @14.0.2@
  -}
  , ProcessRuntime -> Maybe Text
processRuntimeDescription :: Maybe Text
  {- ^ An additional description about the runtime of the process, for example a specific vendor customization of the runtime environment.

  Example: @Eclipse OpenJ9 Eclipse OpenJ9 VM openj9-0.21.0@
  -}
  }


instance ToResource ProcessRuntime where
  toResource :: ProcessRuntime -> Resource
toResource ProcessRuntime {Maybe Text
processRuntimeName :: ProcessRuntime -> Maybe Text
processRuntimeVersion :: ProcessRuntime -> Maybe Text
processRuntimeDescription :: ProcessRuntime -> Maybe Text
processRuntimeName :: Maybe Text
processRuntimeVersion :: Maybe Text
processRuntimeDescription :: Maybe Text
..} =
    Maybe Text -> [Maybe (Text, Attribute)] -> Resource
mkResourceWithSchema
      (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
semConvSchemaUrl)
      [ AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_runtime_name Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processRuntimeName
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_runtime_version Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processRuntimeVersion
      , AttributeKey Text -> Text
forall a. AttributeKey a -> Text
unkey AttributeKey Text
SC.process_runtime_description Text -> Maybe Text -> Maybe (Text, Attribute)
forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe Text
processRuntimeDescription
      ]