{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}

{- |
Module      :  OpenTelemetry.Attributes.Key
Copyright   :  (c) Ian Duncan, 2021
License     :  BSD-3
Description :  Names for key-value pair metadata
Maintainer  :  Ian Duncan
Stability   :  experimental
Portability :  non-portable (GHC extensions)
-}
module OpenTelemetry.Attributes.Key (
  AttributeKey (..),
  forget,
) where

import Data.String (IsString (..))
import Data.Text (Text)
import qualified Data.Text as T
import GHC.Generics (Generic)
import OpenTelemetry.Attributes.Attribute (Attribute)


{- | A 'AttributeKey' is a name for an attribute. The type parameter sets the type of the
attribute the key should be associated with. This is useful for standardising
attribute keys, since we can define both the key and the type of value it is
intended to record.

For example, we might define:

@
-- See https://opentelemetry.io/docs/specs/semconv/attributes-registry/server/
serverPortKey :: AttributeKey Int
serverPortKey = "server.port"
@
-}
newtype AttributeKey a = AttributeKey {forall a. AttributeKey a -> Text
unkey :: Text} deriving stock (Int -> AttributeKey a -> ShowS
[AttributeKey a] -> ShowS
AttributeKey a -> String
(Int -> AttributeKey a -> ShowS)
-> (AttributeKey a -> String)
-> ([AttributeKey a] -> ShowS)
-> Show (AttributeKey a)
forall a. Int -> AttributeKey a -> ShowS
forall a. [AttributeKey a] -> ShowS
forall a. AttributeKey a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> AttributeKey a -> ShowS
showsPrec :: Int -> AttributeKey a -> ShowS
$cshow :: forall a. AttributeKey a -> String
show :: AttributeKey a -> String
$cshowList :: forall a. [AttributeKey a] -> ShowS
showList :: [AttributeKey a] -> ShowS
Show, AttributeKey a -> AttributeKey a -> Bool
(AttributeKey a -> AttributeKey a -> Bool)
-> (AttributeKey a -> AttributeKey a -> Bool)
-> Eq (AttributeKey a)
forall a. AttributeKey a -> AttributeKey a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. AttributeKey a -> AttributeKey a -> Bool
== :: AttributeKey a -> AttributeKey a -> Bool
$c/= :: forall a. AttributeKey a -> AttributeKey a -> Bool
/= :: AttributeKey a -> AttributeKey a -> Bool
Eq, Eq (AttributeKey a)
Eq (AttributeKey a) =>
(AttributeKey a -> AttributeKey a -> Ordering)
-> (AttributeKey a -> AttributeKey a -> Bool)
-> (AttributeKey a -> AttributeKey a -> Bool)
-> (AttributeKey a -> AttributeKey a -> Bool)
-> (AttributeKey a -> AttributeKey a -> Bool)
-> (AttributeKey a -> AttributeKey a -> AttributeKey a)
-> (AttributeKey a -> AttributeKey a -> AttributeKey a)
-> Ord (AttributeKey a)
AttributeKey a -> AttributeKey a -> Bool
AttributeKey a -> AttributeKey a -> Ordering
AttributeKey a -> AttributeKey a -> AttributeKey a
forall a. Eq (AttributeKey a)
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
forall a. AttributeKey a -> AttributeKey a -> Bool
forall a. AttributeKey a -> AttributeKey a -> Ordering
forall a. AttributeKey a -> AttributeKey a -> AttributeKey a
$ccompare :: forall a. AttributeKey a -> AttributeKey a -> Ordering
compare :: AttributeKey a -> AttributeKey a -> Ordering
$c< :: forall a. AttributeKey a -> AttributeKey a -> Bool
< :: AttributeKey a -> AttributeKey a -> Bool
$c<= :: forall a. AttributeKey a -> AttributeKey a -> Bool
<= :: AttributeKey a -> AttributeKey a -> Bool
$c> :: forall a. AttributeKey a -> AttributeKey a -> Bool
> :: AttributeKey a -> AttributeKey a -> Bool
$c>= :: forall a. AttributeKey a -> AttributeKey a -> Bool
>= :: AttributeKey a -> AttributeKey a -> Bool
$cmax :: forall a. AttributeKey a -> AttributeKey a -> AttributeKey a
max :: AttributeKey a -> AttributeKey a -> AttributeKey a
$cmin :: forall a. AttributeKey a -> AttributeKey a -> AttributeKey a
min :: AttributeKey a -> AttributeKey a -> AttributeKey a
Ord, (forall x. AttributeKey a -> Rep (AttributeKey a) x)
-> (forall x. Rep (AttributeKey a) x -> AttributeKey a)
-> Generic (AttributeKey a)
forall x. Rep (AttributeKey a) x -> AttributeKey a
forall x. AttributeKey a -> Rep (AttributeKey a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (AttributeKey a) x -> AttributeKey a
forall a x. AttributeKey a -> Rep (AttributeKey a) x
$cfrom :: forall a x. AttributeKey a -> Rep (AttributeKey a) x
from :: forall x. AttributeKey a -> Rep (AttributeKey a) x
$cto :: forall a x. Rep (AttributeKey a) x -> AttributeKey a
to :: forall x. Rep (AttributeKey a) x -> AttributeKey a
Generic)


-- | Raise an error if the string is empty.
instance IsString (AttributeKey a) where
  fromString :: String -> AttributeKey a
fromString String
"" = String -> AttributeKey a
forall a. HasCallStack => String -> a
error String
"AttributeKey cannot be empty"
  fromString String
s = Text -> AttributeKey a
forall a. Text -> AttributeKey a
AttributeKey (Text -> AttributeKey a) -> Text -> AttributeKey a
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
s


forget :: AttributeKey a -> AttributeKey Attribute
forget :: forall a. AttributeKey a -> AttributeKey Attribute
forget = Text -> AttributeKey Attribute
forall a. Text -> AttributeKey a
AttributeKey (Text -> AttributeKey Attribute)
-> (AttributeKey a -> Text)
-> AttributeKey a
-> AttributeKey Attribute
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AttributeKey a -> Text
forall a. AttributeKey a -> Text
unkey