{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}

{- |
Module      : OpenTelemetry.Internal.Common.Types
Description : Internal types shared across API signals: InstrumentationLibrary, ShutdownResult, FlushResult.
Stability   : experimental
-}
module OpenTelemetry.Internal.Common.Types (
  InstrumentationLibrary (..),
  InstrumentationScope,
  instrumentationLibrary,
  instrumentationScope,
  withSchemaUrl,
  withLibraryAttributes,
  AnyValue (..),
  ToValue (..),
  ShutdownResult (..),
  worstShutdown,
  FlushResult (..),
  worstFlush,
  ExportResult (..),
  parseInstrumentationLibrary,
  detectInstrumentationLibrary,
) where

import Control.Exception (SomeException)
import Data.ByteString (ByteString)
import Data.Char (isAlphaNum, isDigit)
import Data.Data (Data)
import qualified Data.HashMap.Strict as H
import Data.Hashable (Hashable)
import Data.Int (Int64)
import Data.String (IsString (fromString))
import Data.Text (Text)
import qualified Data.Text as T
import GHC.Generics (Generic)
import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Syntax as TH
import OpenTelemetry.Attributes (Attributes, emptyAttributes)


{- | An instrumentation scope identifies the library or component providing
instrumentation. The OpenTelemetry specification renamed this concept from
\"Instrumentation Library\" to \"Instrumentation Scope\"; this type retains
the old constructor name for backwards compatibility but 'InstrumentationScope'
is the preferred type alias.

Spec: <https://opentelemetry.io/docs/specs/otel/common/instrumentation-scope/>

@since 0.0.1.0
-}
data InstrumentationLibrary = InstrumentationLibrary
  { InstrumentationLibrary -> Text
libraryName :: {-# UNPACK #-} !Text
  -- ^ The name of the instrumentation library
  , InstrumentationLibrary -> Text
libraryVersion :: {-# UNPACK #-} !Text
  -- ^ The version of the instrumented library
  , InstrumentationLibrary -> Text
librarySchemaUrl :: {-# UNPACK #-} !Text
  , InstrumentationLibrary -> Attributes
libraryAttributes :: Attributes
  }
  deriving (Eq InstrumentationLibrary
Eq InstrumentationLibrary =>
(InstrumentationLibrary -> InstrumentationLibrary -> Ordering)
-> (InstrumentationLibrary -> InstrumentationLibrary -> Bool)
-> (InstrumentationLibrary -> InstrumentationLibrary -> Bool)
-> (InstrumentationLibrary -> InstrumentationLibrary -> Bool)
-> (InstrumentationLibrary -> InstrumentationLibrary -> Bool)
-> (InstrumentationLibrary
    -> InstrumentationLibrary -> InstrumentationLibrary)
-> (InstrumentationLibrary
    -> InstrumentationLibrary -> InstrumentationLibrary)
-> Ord InstrumentationLibrary
InstrumentationLibrary -> InstrumentationLibrary -> Bool
InstrumentationLibrary -> InstrumentationLibrary -> Ordering
InstrumentationLibrary
-> InstrumentationLibrary -> InstrumentationLibrary
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: InstrumentationLibrary -> InstrumentationLibrary -> Ordering
compare :: InstrumentationLibrary -> InstrumentationLibrary -> Ordering
$c< :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
< :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
$c<= :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
<= :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
$c> :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
> :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
$c>= :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
>= :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
$cmax :: InstrumentationLibrary
-> InstrumentationLibrary -> InstrumentationLibrary
max :: InstrumentationLibrary
-> InstrumentationLibrary -> InstrumentationLibrary
$cmin :: InstrumentationLibrary
-> InstrumentationLibrary -> InstrumentationLibrary
min :: InstrumentationLibrary
-> InstrumentationLibrary -> InstrumentationLibrary
Ord, InstrumentationLibrary -> InstrumentationLibrary -> Bool
(InstrumentationLibrary -> InstrumentationLibrary -> Bool)
-> (InstrumentationLibrary -> InstrumentationLibrary -> Bool)
-> Eq InstrumentationLibrary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
== :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
$c/= :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
/= :: InstrumentationLibrary -> InstrumentationLibrary -> Bool
Eq, (forall x. InstrumentationLibrary -> Rep InstrumentationLibrary x)
-> (forall x.
    Rep InstrumentationLibrary x -> InstrumentationLibrary)
-> Generic InstrumentationLibrary
forall x. Rep InstrumentationLibrary x -> InstrumentationLibrary
forall x. InstrumentationLibrary -> Rep InstrumentationLibrary x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. InstrumentationLibrary -> Rep InstrumentationLibrary x
from :: forall x. InstrumentationLibrary -> Rep InstrumentationLibrary x
$cto :: forall x. Rep InstrumentationLibrary x -> InstrumentationLibrary
to :: forall x. Rep InstrumentationLibrary x -> InstrumentationLibrary
Generic, Int -> InstrumentationLibrary -> ShowS
[InstrumentationLibrary] -> ShowS
InstrumentationLibrary -> String
(Int -> InstrumentationLibrary -> ShowS)
-> (InstrumentationLibrary -> String)
-> ([InstrumentationLibrary] -> ShowS)
-> Show InstrumentationLibrary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InstrumentationLibrary -> ShowS
showsPrec :: Int -> InstrumentationLibrary -> ShowS
$cshow :: InstrumentationLibrary -> String
show :: InstrumentationLibrary -> String
$cshowList :: [InstrumentationLibrary] -> ShowS
showList :: [InstrumentationLibrary] -> ShowS
Show, (forall (m :: * -> *). Quote m => InstrumentationLibrary -> m Exp)
-> (forall (m :: * -> *).
    Quote m =>
    InstrumentationLibrary -> Code m InstrumentationLibrary)
-> Lift InstrumentationLibrary
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => InstrumentationLibrary -> m Exp
forall (m :: * -> *).
Quote m =>
InstrumentationLibrary -> Code m InstrumentationLibrary
$clift :: forall (m :: * -> *). Quote m => InstrumentationLibrary -> m Exp
lift :: forall (m :: * -> *). Quote m => InstrumentationLibrary -> m Exp
$cliftTyped :: forall (m :: * -> *).
Quote m =>
InstrumentationLibrary -> Code m InstrumentationLibrary
liftTyped :: forall (m :: * -> *).
Quote m =>
InstrumentationLibrary -> Code m InstrumentationLibrary
TH.Lift)


instance Hashable InstrumentationLibrary


instance IsString InstrumentationLibrary where
  fromString :: String -> InstrumentationLibrary
  fromString :: String -> InstrumentationLibrary
fromString String
str = Text -> Text -> Text -> Attributes -> InstrumentationLibrary
InstrumentationLibrary (String -> Text
forall a. IsString a => String -> a
fromString String
str) Text
"" Text
"" Attributes
emptyAttributes


{- | Preferred alias matching the current OpenTelemetry specification terminology
(\"Instrumentation Scope\"). Identical to 'InstrumentationLibrary'.

Spec: <https://opentelemetry.io/docs/specs/otel/common/instrumentation-scope/>

@since 0.4.0.0
-}
type InstrumentationScope = InstrumentationLibrary


{- | Create an 'InstrumentationScope' with a name and version.
Schema URL and attributes default to empty.

@
let scope = instrumentationScope "my-service" "1.2.0"
@

For more fields, chain with 'withSchemaUrl' or 'withLibraryAttributes':

@
let scope = instrumentationScope "my-service" "1.2.0"
          & withSchemaUrl "https:\/\/opentelemetry.io\/schemas\/1.25.0"
@

@since 0.4.0.0
-}
instrumentationScope :: Text -> Text -> InstrumentationScope
instrumentationScope :: Text -> Text -> InstrumentationLibrary
instrumentationScope = Text -> Text -> InstrumentationLibrary
instrumentationLibrary


{- | Create an 'InstrumentationLibrary' with a name and version.
Schema URL and attributes default to empty.

Prefer 'instrumentationScope' for new code.

@since 0.4.0.0
-}
instrumentationLibrary :: Text -> Text -> InstrumentationLibrary
instrumentationLibrary :: Text -> Text -> InstrumentationLibrary
instrumentationLibrary Text
name Text
version =
  InstrumentationLibrary
    { libraryName :: Text
libraryName = Text
name
    , libraryVersion :: Text
libraryVersion = Text
version
    , librarySchemaUrl :: Text
librarySchemaUrl = Text
""
    , libraryAttributes :: Attributes
libraryAttributes = Attributes
emptyAttributes
    }


{- | Set the schema URL on an 'InstrumentationLibrary'.

@since 0.4.0.0
-}
withSchemaUrl :: Text -> InstrumentationLibrary -> InstrumentationLibrary
withSchemaUrl :: Text -> InstrumentationLibrary -> InstrumentationLibrary
withSchemaUrl Text
url InstrumentationLibrary
lib = InstrumentationLibrary
lib {librarySchemaUrl = url}


{- | Set attributes on an 'InstrumentationLibrary'.

@since 0.4.0.0
-}
withLibraryAttributes :: Attributes -> InstrumentationLibrary -> InstrumentationLibrary
withLibraryAttributes :: Attributes -> InstrumentationLibrary -> InstrumentationLibrary
withLibraryAttributes Attributes
attrs InstrumentationLibrary
lib = InstrumentationLibrary
lib {libraryAttributes = attrs}


{- | An attribute represents user-provided metadata about a span, link, or event.

 'Any' values are used in place of 'Standard Attributes' in logs because third-party
 logs may not conform to the 'Standard Attribute' format.

 Telemetry tools may use this data to support high-cardinality querying, visualization
 in waterfall diagrams, trace sampling decisions, and more.

@since 0.0.1.0
-}
data AnyValue
  = TextValue Text
  | BoolValue Bool
  | DoubleValue Double
  | IntValue Int64
  | ByteStringValue ByteString
  | ArrayValue [AnyValue]
  | HashMapValue (H.HashMap Text AnyValue)
  | NullValue
  deriving stock (ReadPrec [AnyValue]
ReadPrec AnyValue
Int -> ReadS AnyValue
ReadS [AnyValue]
(Int -> ReadS AnyValue)
-> ReadS [AnyValue]
-> ReadPrec AnyValue
-> ReadPrec [AnyValue]
-> Read AnyValue
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS AnyValue
readsPrec :: Int -> ReadS AnyValue
$creadList :: ReadS [AnyValue]
readList :: ReadS [AnyValue]
$creadPrec :: ReadPrec AnyValue
readPrec :: ReadPrec AnyValue
$creadListPrec :: ReadPrec [AnyValue]
readListPrec :: ReadPrec [AnyValue]
Read, Int -> AnyValue -> ShowS
[AnyValue] -> ShowS
AnyValue -> String
(Int -> AnyValue -> ShowS)
-> (AnyValue -> String) -> ([AnyValue] -> ShowS) -> Show AnyValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AnyValue -> ShowS
showsPrec :: Int -> AnyValue -> ShowS
$cshow :: AnyValue -> String
show :: AnyValue -> String
$cshowList :: [AnyValue] -> ShowS
showList :: [AnyValue] -> ShowS
Show, AnyValue -> AnyValue -> Bool
(AnyValue -> AnyValue -> Bool)
-> (AnyValue -> AnyValue -> Bool) -> Eq AnyValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AnyValue -> AnyValue -> Bool
== :: AnyValue -> AnyValue -> Bool
$c/= :: AnyValue -> AnyValue -> Bool
/= :: AnyValue -> AnyValue -> Bool
Eq, Eq AnyValue
Eq AnyValue =>
(AnyValue -> AnyValue -> Ordering)
-> (AnyValue -> AnyValue -> Bool)
-> (AnyValue -> AnyValue -> Bool)
-> (AnyValue -> AnyValue -> Bool)
-> (AnyValue -> AnyValue -> Bool)
-> (AnyValue -> AnyValue -> AnyValue)
-> (AnyValue -> AnyValue -> AnyValue)
-> Ord AnyValue
AnyValue -> AnyValue -> Bool
AnyValue -> AnyValue -> Ordering
AnyValue -> AnyValue -> AnyValue
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AnyValue -> AnyValue -> Ordering
compare :: AnyValue -> AnyValue -> Ordering
$c< :: AnyValue -> AnyValue -> Bool
< :: AnyValue -> AnyValue -> Bool
$c<= :: AnyValue -> AnyValue -> Bool
<= :: AnyValue -> AnyValue -> Bool
$c> :: AnyValue -> AnyValue -> Bool
> :: AnyValue -> AnyValue -> Bool
$c>= :: AnyValue -> AnyValue -> Bool
>= :: AnyValue -> AnyValue -> Bool
$cmax :: AnyValue -> AnyValue -> AnyValue
max :: AnyValue -> AnyValue -> AnyValue
$cmin :: AnyValue -> AnyValue -> AnyValue
min :: AnyValue -> AnyValue -> AnyValue
Ord, Typeable AnyValue
Typeable AnyValue =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> AnyValue -> c AnyValue)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c AnyValue)
-> (AnyValue -> Constr)
-> (AnyValue -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c AnyValue))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AnyValue))
-> ((forall b. Data b => b -> b) -> AnyValue -> AnyValue)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> AnyValue -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> AnyValue -> r)
-> (forall u. (forall d. Data d => d -> u) -> AnyValue -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> AnyValue -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> AnyValue -> m AnyValue)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AnyValue -> m AnyValue)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AnyValue -> m AnyValue)
-> Data AnyValue
AnyValue -> Constr
AnyValue -> DataType
(forall b. Data b => b -> b) -> AnyValue -> AnyValue
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> AnyValue -> u
forall u. (forall d. Data d => d -> u) -> AnyValue -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AnyValue -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AnyValue -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AnyValue
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AnyValue -> c AnyValue
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c AnyValue)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AnyValue)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AnyValue -> c AnyValue
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AnyValue -> c AnyValue
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AnyValue
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AnyValue
$ctoConstr :: AnyValue -> Constr
toConstr :: AnyValue -> Constr
$cdataTypeOf :: AnyValue -> DataType
dataTypeOf :: AnyValue -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c AnyValue)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c AnyValue)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AnyValue)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AnyValue)
$cgmapT :: (forall b. Data b => b -> b) -> AnyValue -> AnyValue
gmapT :: (forall b. Data b => b -> b) -> AnyValue -> AnyValue
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AnyValue -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AnyValue -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AnyValue -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AnyValue -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> AnyValue -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> AnyValue -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> AnyValue -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> AnyValue -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AnyValue -> m AnyValue
Data, (forall x. AnyValue -> Rep AnyValue x)
-> (forall x. Rep AnyValue x -> AnyValue) -> Generic AnyValue
forall x. Rep AnyValue x -> AnyValue
forall x. AnyValue -> Rep AnyValue x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AnyValue -> Rep AnyValue x
from :: forall x. AnyValue -> Rep AnyValue x
$cto :: forall x. Rep AnyValue x -> AnyValue
to :: forall x. Rep AnyValue x -> AnyValue
Generic)
  deriving anyclass (Eq AnyValue
Eq AnyValue =>
(Int -> AnyValue -> Int) -> (AnyValue -> Int) -> Hashable AnyValue
Int -> AnyValue -> Int
AnyValue -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> AnyValue -> Int
hashWithSalt :: Int -> AnyValue -> Int
$chash :: AnyValue -> Int
hash :: AnyValue -> Int
Hashable)


-- | Create a `TextAttribute` from the string value.
instance IsString AnyValue where
  fromString :: String -> AnyValue
  fromString :: String -> AnyValue
fromString = Text -> AnyValue
TextValue (Text -> AnyValue) -> (String -> Text) -> String -> AnyValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. IsString a => String -> a
fromString


{- | Convert a Haskell value to an 'Any' value.

 @

 data Foo = Foo

 instance ToValue Foo where
   toValue Foo = TextValue "Foo"

 @

@since 0.0.1.0
-}
class ToValue a where
  toValue :: a -> AnyValue


instance ToValue Text where
  toValue :: Text -> AnyValue
  toValue :: Text -> AnyValue
toValue = Text -> AnyValue
TextValue


instance ToValue Bool where
  toValue :: Bool -> AnyValue
  toValue :: Bool -> AnyValue
toValue = Bool -> AnyValue
BoolValue


instance ToValue Double where
  toValue :: Double -> AnyValue
  toValue :: Double -> AnyValue
toValue = Double -> AnyValue
DoubleValue


instance ToValue Int64 where
  toValue :: Int64 -> AnyValue
  toValue :: Int64 -> AnyValue
toValue = Int64 -> AnyValue
IntValue


instance ToValue ByteString where
  toValue :: ByteString -> AnyValue
  toValue :: ByteString -> AnyValue
toValue = ByteString -> AnyValue
ByteStringValue


instance (ToValue a) => ToValue [a] where
  toValue :: (ToValue a) => [a] -> AnyValue
  toValue :: ToValue a => [a] -> AnyValue
toValue = [AnyValue] -> AnyValue
ArrayValue ([AnyValue] -> AnyValue) -> ([a] -> [AnyValue]) -> [a] -> AnyValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> AnyValue) -> [a] -> [AnyValue]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> AnyValue
forall a. ToValue a => a -> AnyValue
toValue


instance (ToValue a) => ToValue (H.HashMap Text a) where
  toValue :: (ToValue a) => H.HashMap Text a -> AnyValue
  toValue :: ToValue a => HashMap Text a -> AnyValue
toValue = HashMap Text AnyValue -> AnyValue
HashMapValue (HashMap Text AnyValue -> AnyValue)
-> (HashMap Text a -> HashMap Text AnyValue)
-> HashMap Text a
-> AnyValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> AnyValue) -> HashMap Text a -> HashMap Text AnyValue
forall a b. (a -> b) -> HashMap Text a -> HashMap Text b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> AnyValue
forall a. ToValue a => a -> AnyValue
toValue


instance ToValue AnyValue where
  toValue :: AnyValue -> AnyValue
  toValue :: AnyValue -> AnyValue
toValue = AnyValue -> AnyValue
forall a. a -> a
id


-- | @since 0.0.1.0
data ShutdownResult = ShutdownSuccess | ShutdownFailure | ShutdownTimeout
  deriving stock (ShutdownResult -> ShutdownResult -> Bool
(ShutdownResult -> ShutdownResult -> Bool)
-> (ShutdownResult -> ShutdownResult -> Bool) -> Eq ShutdownResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ShutdownResult -> ShutdownResult -> Bool
== :: ShutdownResult -> ShutdownResult -> Bool
$c/= :: ShutdownResult -> ShutdownResult -> Bool
/= :: ShutdownResult -> ShutdownResult -> Bool
Eq, Int -> ShutdownResult -> ShowS
[ShutdownResult] -> ShowS
ShutdownResult -> String
(Int -> ShutdownResult -> ShowS)
-> (ShutdownResult -> String)
-> ([ShutdownResult] -> ShowS)
-> Show ShutdownResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ShutdownResult -> ShowS
showsPrec :: Int -> ShutdownResult -> ShowS
$cshow :: ShutdownResult -> String
show :: ShutdownResult -> String
$cshowList :: [ShutdownResult] -> ShowS
showList :: [ShutdownResult] -> ShowS
Show)


{- | Combine two shutdown results, preferring the "worst" outcome.
Failure > Timeout > Success.

@since 0.4.0.0
-}
worstShutdown :: ShutdownResult -> ShutdownResult -> ShutdownResult
worstShutdown :: ShutdownResult -> ShutdownResult -> ShutdownResult
worstShutdown ShutdownResult
ShutdownFailure ShutdownResult
_ = ShutdownResult
ShutdownFailure
worstShutdown ShutdownResult
_ ShutdownResult
ShutdownFailure = ShutdownResult
ShutdownFailure
worstShutdown ShutdownResult
ShutdownTimeout ShutdownResult
_ = ShutdownResult
ShutdownTimeout
worstShutdown ShutdownResult
_ ShutdownResult
ShutdownTimeout = ShutdownResult
ShutdownTimeout
worstShutdown ShutdownResult
ShutdownSuccess ShutdownResult
ShutdownSuccess = ShutdownResult
ShutdownSuccess


{- | The outcome of a call to @OpenTelemetry.Trace.forceFlush@ or @OpenTelemetry.Log.forceFlush@

@since 0.0.1.0
-}
data FlushResult
  = {- | One or more spans or @LogRecord@s did not export from all associated exporters
    within the alotted timeframe.
    -}
    FlushTimeout
  | -- | Flushing spans or @LogRecord@s to all associated exporters succeeded.
    FlushSuccess
  | {- | One or more exporters failed to successfully export one or more
    unexported spans or @LogRecord@s.
    -}
    FlushError
  deriving stock (FlushResult -> FlushResult -> Bool
(FlushResult -> FlushResult -> Bool)
-> (FlushResult -> FlushResult -> Bool) -> Eq FlushResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FlushResult -> FlushResult -> Bool
== :: FlushResult -> FlushResult -> Bool
$c/= :: FlushResult -> FlushResult -> Bool
/= :: FlushResult -> FlushResult -> Bool
Eq, Int -> FlushResult -> ShowS
[FlushResult] -> ShowS
FlushResult -> String
(Int -> FlushResult -> ShowS)
-> (FlushResult -> String)
-> ([FlushResult] -> ShowS)
-> Show FlushResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FlushResult -> ShowS
showsPrec :: Int -> FlushResult -> ShowS
$cshow :: FlushResult -> String
show :: FlushResult -> String
$cshowList :: [FlushResult] -> ShowS
showList :: [FlushResult] -> ShowS
Show)


{- | Combine two flush results, preferring the "worst" outcome.
Error > Timeout > Success.

@since 0.0.1.0
-}
worstFlush :: FlushResult -> FlushResult -> FlushResult
worstFlush :: FlushResult -> FlushResult -> FlushResult
worstFlush FlushResult
FlushError FlushResult
_ = FlushResult
FlushError
worstFlush FlushResult
_ FlushResult
FlushError = FlushResult
FlushError
worstFlush FlushResult
FlushTimeout FlushResult
_ = FlushResult
FlushTimeout
worstFlush FlushResult
_ FlushResult
FlushTimeout = FlushResult
FlushTimeout
worstFlush FlushResult
FlushSuccess FlushResult
FlushSuccess = FlushResult
FlushSuccess


-- | @since 0.0.1.0
data ExportResult
  = Success
  | Failure (Maybe SomeException)


{- | Parses a package-version string like @\"my-lib-1.2.3\"@ into an
 'InstrumentationLibrary'. Tries to split off a trailing version (digits and
 dots after the rightmost @-@). Falls back to treating the whole string as a
 package name with no version.

@since 0.0.1.0
-}
parseInstrumentationLibrary :: (MonadFail m) => String -> m InstrumentationLibrary
parseInstrumentationLibrary :: forall (m :: * -> *).
MonadFail m =>
String -> m InstrumentationLibrary
parseInstrumentationLibrary String
packageString =
  case String -> Maybe (String, String)
splitPackageVersion String
packageString of
    Just (String
name, String
version) ->
      InstrumentationLibrary -> m InstrumentationLibrary
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InstrumentationLibrary -> m InstrumentationLibrary)
-> InstrumentationLibrary -> m InstrumentationLibrary
forall a b. (a -> b) -> a -> b
$
        InstrumentationLibrary
          { libraryName :: Text
libraryName = String -> Text
T.pack String
name
          , libraryVersion :: Text
libraryVersion = String -> Text
T.pack String
version
          , librarySchemaUrl :: Text
librarySchemaUrl = Text
""
          , libraryAttributes :: Attributes
libraryAttributes = Attributes
emptyAttributes
          }
    Maybe (String, String)
Nothing
      | String -> Bool
isValidPackageName String
packageString ->
          InstrumentationLibrary -> m InstrumentationLibrary
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InstrumentationLibrary -> m InstrumentationLibrary)
-> InstrumentationLibrary -> m InstrumentationLibrary
forall a b. (a -> b) -> a -> b
$
            InstrumentationLibrary
              { libraryName :: Text
libraryName = String -> Text
T.pack String
packageString
              , libraryVersion :: Text
libraryVersion = Text
""
              , librarySchemaUrl :: Text
librarySchemaUrl = Text
""
              , libraryAttributes :: Attributes
libraryAttributes = Attributes
emptyAttributes
              }
      | Bool
otherwise ->
          String -> m InstrumentationLibrary
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m InstrumentationLibrary)
-> String -> m InstrumentationLibrary
forall a b. (a -> b) -> a -> b
$ String
"could not parse package string: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
packageString


{- | Try splitting @\"name-1.2.3\"@ or @\"name-1.2.3-hash\"@ at the rightmost
@-@ that precedes a version. The version is the maximal leading sequence of
digits and dots; any trailing content (e.g. @-inplace@, @-hash@) is
discarded, matching GHC's package-id format.
-}
splitPackageVersion :: String -> Maybe (String, String)
splitPackageVersion :: String -> Maybe (String, String)
splitPackageVersion String
s =
  (Int -> Maybe (String, String) -> Maybe (String, String))
-> Maybe (String, String) -> [Int] -> Maybe (String, String)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
    (\Int
i Maybe (String, String)
acc -> Int -> Maybe (String, String) -> Maybe (String, String)
tryAt Int
i Maybe (String, String)
acc)
    Maybe (String, String)
forall a. Maybe a
Nothing
    ([Int] -> [Int]
forall a. [a] -> [a]
reverse [Int]
dashPositions)
  where
    dashPositions :: [Int]
dashPositions = ((Int, Char) -> Int) -> [(Int, Char)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, Char) -> Int
forall a b. (a, b) -> a
fst ([(Int, Char)] -> [Int]) -> [(Int, Char)] -> [Int]
forall a b. (a -> b) -> a -> b
$ ((Int, Char) -> Bool) -> [(Int, Char)] -> [(Int, Char)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Int
_, Char
c) -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-') ([(Int, Char)] -> [(Int, Char)]) -> [(Int, Char)] -> [(Int, Char)]
forall a b. (a -> b) -> a -> b
$ [Int] -> String -> [(Int, Char)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] String
s
    tryAt :: Int -> Maybe (String, String) -> Maybe (String, String)
tryAt Int
i Maybe (String, String)
fallback =
      let name :: String
name = Int -> ShowS
forall a. Int -> [a] -> [a]
take Int
i String
s
          rest :: String
rest = Int -> ShowS
forall a. Int -> [a] -> [a]
drop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) String
s
          version :: String
version = (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
takeWhile Char -> Bool
isVersionChar String
rest
      in if Bool -> Bool
not (String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
version)
           Bool -> Bool -> Bool
&& Char -> Bool
isDigit (String -> Char
forall a. HasCallStack => [a] -> a
head String
version)
           Bool -> Bool -> Bool
&& String -> Bool
isValidPackageName String
name
           then (String, String) -> Maybe (String, String)
forall a. a -> Maybe a
Just (String
name, String
version)
           else Maybe (String, String)
fallback
    isVersionChar :: Char -> Bool
isVersionChar Char
c = Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.'


isValidPackageName :: String -> Bool
isValidPackageName :: String -> Bool
isValidPackageName [] = Bool
False
isValidPackageName [Char
_] = Bool
False
isValidPackageName String
s = (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isNameChar String
s Bool -> Bool -> Bool
&& Char -> Bool
isAlphaNum (String -> Char
forall a. HasCallStack => [a] -> a
last String
s)
  where
    isNameChar :: Char -> Bool
isNameChar Char
c = Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-'


{- | Works out the instrumentation library for your package.

@since 0.0.1.0
-}
detectInstrumentationLibrary :: forall m. (TH.Quasi m, TH.Quote m) => m TH.Exp
detectInstrumentationLibrary :: forall (m :: * -> *). (Quasi m, Quote m) => m Exp
detectInstrumentationLibrary = do
  TH.Loc {loc_package} <- m Loc
forall (m :: * -> *). Quasi m => m Loc
TH.qLocation
  lib <- parseInstrumentationLibrary loc_package
  TH.lift lib