{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module OpenTelemetry.Resource.Host.Detector (
  detectHost,
  builtInHostDetectors,
  HostDetector,
) where

import Control.Monad
import qualified Data.Text as T
import Network.BSD
import OpenTelemetry.Resource.Host
import System.Info (arch)


adaptedArch :: T.Text
adaptedArch :: Text
adaptedArch = case String
arch of
  String
"aarch64" -> Text
"arm64"
  String
"arm" -> Text
"arm32"
  String
"x86_64" -> Text
"amd64"
  String
"i386" -> Text
"x86"
  String
"ia64" -> Text
"ia64"
  String
"powerpc" -> Text
"ppc32"
  String
"powerpc64" -> Text
"ppc64"
  String
"powerpc64le" -> Text
"ppc64"
  String
other -> String -> Text
T.pack String
other


-- | Detect as much host information as possible
detectHost :: IO Host
detectHost :: IO Host
detectHost = do
  Maybe Host
mhost <- forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM forall {f :: * -> *} {a}.
Applicative f =>
Maybe a -> f (Maybe a) -> f (Maybe a)
go forall a. Maybe a
Nothing [HostDetector]
builtInHostDetectors
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ case Maybe Host
mhost of
    Maybe Host
Nothing -> Maybe Text
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Host
Host forall a. Maybe a
Nothing forall a. Maybe a
Nothing forall a. Maybe a
Nothing forall a. Maybe a
Nothing forall a. Maybe a
Nothing forall a. Maybe a
Nothing forall a. Maybe a
Nothing
    Just Host
host -> Host
host
  where
    go :: Maybe a -> f (Maybe a) -> f (Maybe a)
go Maybe a
Nothing f (Maybe a)
hostDetector = f (Maybe a)
hostDetector
    go mhost :: Maybe a
mhost@(Just a
_host) f (Maybe a)
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
mhost


{- | A set of detectors for e.g. AWS, GCP, and other cloud providers.

 Currently only emits hostName and hostArch. Additional detectors are
 welcome via PR.
-}
builtInHostDetectors :: [HostDetector]
builtInHostDetectors :: [HostDetector]
builtInHostDetectors =
  [ -- TODO
    -- AWS support
    -- GCP support
    -- any other user contributed
    HostDetector
fallbackHostDetector
  ]


type HostDetector = IO (Maybe Host)


fallbackHostDetector :: HostDetector
fallbackHostDetector :: HostDetector
fallbackHostDetector =
  forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
    let hostId :: Maybe a
hostId = forall a. Maybe a
Nothing
        hostType :: Maybe a
hostType = forall a. Maybe a
Nothing
        hostArch :: Maybe Text
hostArch = forall a. a -> Maybe a
Just Text
adaptedArch
        hostImageName :: Maybe a
hostImageName = forall a. Maybe a
Nothing
        hostImageId :: Maybe a
hostImageId = forall a. Maybe a
Nothing
        hostImageVersion :: Maybe a
hostImageVersion = forall a. Maybe a
Nothing
    Maybe Text
hostName <- forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO String
getHostName
    forall (f :: * -> *) a. Applicative f => a -> f a
pure Host {Maybe Text
forall a. Maybe a
hostId :: Maybe Text
hostName :: Maybe Text
hostType :: Maybe Text
hostArch :: Maybe Text
hostImageName :: Maybe Text
hostImageId :: Maybe Text
hostImageVersion :: Maybe Text
hostName :: Maybe Text
hostImageVersion :: forall a. Maybe a
hostImageId :: forall a. Maybe a
hostImageName :: forall a. Maybe a
hostArch :: Maybe Text
hostType :: forall a. Maybe a
hostId :: forall a. Maybe a
..}