{-# LANGUAGE DefaultSignatures #-}
module OpenTelemetry.Resource (
mkResource,
mkResourceWithSchema,
semConvSchemaUrl,
Resource,
(.=),
(.=?),
mergeResources,
ToResource (..),
MaterializedResources,
materializeResources,
emptyMaterializedResources,
getMaterializedResourcesSchema,
getMaterializedResourcesAttributes,
materializeResourcesWithSchema,
setMaterializedResourcesSchema,
getResourceAttributes,
getResourceSchemaUrl,
) where
import Data.Maybe (catMaybes)
import Data.Text (Text)
import qualified Data.Text as T
import OpenTelemetry.Attributes
import OpenTelemetry.Internal.Logging (otelLogWarning)
import System.IO.Unsafe (unsafePerformIO)
semConvSchemaUrl :: Text
semConvSchemaUrl :: Text
semConvSchemaUrl = Text
"https://opentelemetry.io/schemas/1.40.0"
data Resource = Resource
{ Resource -> Maybe Text
resourceSchemaUrl :: !(Maybe Text)
, Resource -> Attributes
resourceAttributes :: !Attributes
}
instance Show Resource where
showsPrec :: Int -> Resource -> ShowS
showsPrec Int
d (Resource Maybe Text
s Attributes
a) =
Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"Resource "
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Maybe Text -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Maybe Text
s
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Attributes -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Attributes
a
instance Eq Resource where
Resource Maybe Text
s1 Attributes
a1 == :: Resource -> Resource -> Bool
== Resource Maybe Text
s2 Attributes
a2 = Maybe Text
s1 Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Text
s2 Bool -> Bool -> Bool
&& Attributes
a1 Attributes -> Attributes -> Bool
forall a. Eq a => a -> a -> Bool
== Attributes
a2
mkResource :: [Maybe (Text, Attribute)] -> Resource
mkResource :: [Maybe (Text, Attribute)] -> Resource
mkResource = Maybe Text -> Attributes -> Resource
Resource Maybe Text
forall a. Maybe a
Nothing (Attributes -> Resource)
-> ([Maybe (Text, Attribute)] -> Attributes)
-> [Maybe (Text, Attribute)]
-> Resource
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, Attribute)] -> Attributes
unsafeAttributesFromListIgnoringLimits ([(Text, Attribute)] -> Attributes)
-> ([Maybe (Text, Attribute)] -> [(Text, Attribute)])
-> [Maybe (Text, Attribute)]
-> Attributes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (Text, Attribute)] -> [(Text, Attribute)]
forall a. [Maybe a] -> [a]
catMaybes
mkResourceWithSchema :: Maybe Text -> [Maybe (Text, Attribute)] -> Resource
mkResourceWithSchema :: Maybe Text -> [Maybe (Text, Attribute)] -> Resource
mkResourceWithSchema Maybe Text
schema = Maybe Text -> Attributes -> Resource
Resource Maybe Text
schema (Attributes -> Resource)
-> ([Maybe (Text, Attribute)] -> Attributes)
-> [Maybe (Text, Attribute)]
-> Resource
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, Attribute)] -> Attributes
unsafeAttributesFromListIgnoringLimits ([(Text, Attribute)] -> Attributes)
-> ([Maybe (Text, Attribute)] -> [(Text, Attribute)])
-> [Maybe (Text, Attribute)]
-> Attributes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (Text, Attribute)] -> [(Text, Attribute)]
forall a. [Maybe a] -> [a]
catMaybes
(.=) :: (ToAttribute a) => Text -> a -> Maybe (Text, Attribute)
Text
k .= :: forall a. ToAttribute a => Text -> a -> Maybe (Text, Attribute)
.= a
v = (Text, Attribute) -> Maybe (Text, Attribute)
forall a. a -> Maybe a
Just (Text
k, a -> Attribute
forall a. ToAttribute a => a -> Attribute
toAttribute a
v)
(.=?) :: (ToAttribute a) => Text -> Maybe a -> Maybe (Text, Attribute)
Text
k .=? :: forall a.
ToAttribute a =>
Text -> Maybe a -> Maybe (Text, Attribute)
.=? Maybe a
mv = (\Text
k' a
v -> (Text
k', a -> Attribute
forall a. ToAttribute a => a -> Attribute
toAttribute a
v)) Text
k (a -> (Text, Attribute)) -> Maybe a -> Maybe (Text, Attribute)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe a
mv
instance Semigroup Resource where
<> :: Resource -> Resource -> Resource
(<>) = Resource -> Resource -> Resource
mergeResources
instance Monoid Resource where
mempty :: Resource
mempty = Maybe Text -> Attributes -> Resource
Resource Maybe Text
forall a. Maybe a
Nothing Attributes
emptyAttributes
mergeResources
:: Resource
-> Resource
-> Resource
mergeResources :: Resource -> Resource -> Resource
mergeResources (Resource Maybe Text
newSchema Attributes
newAttrs) (Resource Maybe Text
oldSchema Attributes
oldAttrs) =
Maybe Text -> Attributes -> Resource
Resource (Maybe Text -> Maybe Text -> Maybe Text
mergeSchemaUrls Maybe Text
newSchema Maybe Text
oldSchema) (Attributes -> Attributes -> Attributes
unsafeMergeAttributesIgnoringLimits Attributes
newAttrs Attributes
oldAttrs)
mergeSchemaUrls :: Maybe Text -> Maybe Text -> Maybe Text
mergeSchemaUrls :: Maybe Text -> Maybe Text -> Maybe Text
mergeSchemaUrls Maybe Text
Nothing Maybe Text
b = Maybe Text
b
mergeSchemaUrls Maybe Text
a Maybe Text
Nothing = Maybe Text
a
mergeSchemaUrls a :: Maybe Text
a@(Just Text
s1) (Just Text
s2)
| Text
s1 Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
s2 = Maybe Text
a
| Bool
otherwise =
IO (Maybe Text) -> Maybe Text
forall a. IO a -> a
unsafePerformIO (IO (Maybe Text) -> Maybe Text) -> IO (Maybe Text) -> Maybe Text
forall a b. (a -> b) -> a -> b
$ do
String -> IO ()
otelLogWarning (String
"Resource schema URL conflict: '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
s1 String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"' vs '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
s2 String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"'")
Maybe Text -> IO (Maybe Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Text
a
class ToResource a where
toResource :: a -> Resource
getResourceAttributes :: Resource -> Attributes
getResourceAttributes :: Resource -> Attributes
getResourceAttributes = Resource -> Attributes
resourceAttributes
getResourceSchemaUrl :: Resource -> Maybe Text
getResourceSchemaUrl :: Resource -> Maybe Text
getResourceSchemaUrl = Resource -> Maybe Text
resourceSchemaUrl
data MaterializedResources = MaterializedResources
{ MaterializedResources -> Maybe String
materializedResourcesSchema :: Maybe String
, MaterializedResources -> Attributes
materializedResourcesAttributes :: Attributes
}
deriving (Int -> MaterializedResources -> ShowS
[MaterializedResources] -> ShowS
MaterializedResources -> String
(Int -> MaterializedResources -> ShowS)
-> (MaterializedResources -> String)
-> ([MaterializedResources] -> ShowS)
-> Show MaterializedResources
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MaterializedResources -> ShowS
showsPrec :: Int -> MaterializedResources -> ShowS
$cshow :: MaterializedResources -> String
show :: MaterializedResources -> String
$cshowList :: [MaterializedResources] -> ShowS
showList :: [MaterializedResources] -> ShowS
Show, MaterializedResources -> MaterializedResources -> Bool
(MaterializedResources -> MaterializedResources -> Bool)
-> (MaterializedResources -> MaterializedResources -> Bool)
-> Eq MaterializedResources
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MaterializedResources -> MaterializedResources -> Bool
== :: MaterializedResources -> MaterializedResources -> Bool
$c/= :: MaterializedResources -> MaterializedResources -> Bool
/= :: MaterializedResources -> MaterializedResources -> Bool
Eq)
emptyMaterializedResources :: MaterializedResources
emptyMaterializedResources :: MaterializedResources
emptyMaterializedResources = Maybe String -> Attributes -> MaterializedResources
MaterializedResources Maybe String
forall a. Maybe a
Nothing Attributes
emptyAttributes
getMaterializedResourcesSchema :: MaterializedResources -> Maybe String
getMaterializedResourcesSchema :: MaterializedResources -> Maybe String
getMaterializedResourcesSchema = MaterializedResources -> Maybe String
materializedResourcesSchema
getMaterializedResourcesAttributes :: MaterializedResources -> Attributes
getMaterializedResourcesAttributes :: MaterializedResources -> Attributes
getMaterializedResourcesAttributes = MaterializedResources -> Attributes
materializedResourcesAttributes
materializeResources :: Resource -> MaterializedResources
materializeResources :: Resource -> MaterializedResources
materializeResources (Resource Maybe Text
mSchema Attributes
attrs) =
Maybe String -> Attributes -> MaterializedResources
MaterializedResources (Text -> String
T.unpack (Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
mSchema) Attributes
attrs
materializeResourcesWithSchema :: Maybe String -> Resource -> MaterializedResources
materializeResourcesWithSchema :: Maybe String -> Resource -> MaterializedResources
materializeResourcesWithSchema Maybe String
schema (Resource Maybe Text
_ Attributes
attrs) = Maybe String -> Attributes -> MaterializedResources
MaterializedResources Maybe String
schema Attributes
attrs
setMaterializedResourcesSchema :: Maybe String -> MaterializedResources -> MaterializedResources
setMaterializedResourcesSchema :: Maybe String -> MaterializedResources -> MaterializedResources
setMaterializedResourcesSchema Maybe String
schema MaterializedResources
mr = MaterializedResources
mr {materializedResourcesSchema = schema}