feat(users/Profpatsch/whatcd-resolver): add ToOtelJsonAttr

Small helper class for putting a json otel attribute from random
types, via Enc.

Used for the redacted requests for now.

Change-Id: I29c31de01f1f5eb3f63ce5639e5b4df4f9b0dc40
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12953
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
This commit is contained in:
Profpatsch 2025-01-04 22:35:07 +01:00
parent 722499d8a9
commit 428f574b75
4 changed files with 76 additions and 6 deletions

View file

@ -127,6 +127,12 @@ nullOr inner = \case
list :: (a -> Enc) -> [a] -> Enc
list f = Enc . AesonEnc.list (\a -> (f a).unEnc)
tuple2 :: (a -> Enc) -> (b -> Enc) -> (a, b) -> Enc
tuple2 f g (a, b) = list id [f a, g b]
tuple3 :: (a -> Enc) -> (b -> Enc) -> (c -> Enc) -> (a, b, c) -> Enc
tuple3 f g h (a, b, c) = list id [f a, g b, h c]
-- | Encode a 'NonEmpty' as a json list.
nonEmpty :: (a -> Enc) -> NonEmpty a -> Enc
nonEmpty f = list f . toList

View file

@ -1,3 +1,4 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MagicHash #-}
@ -161,6 +162,7 @@ module MyPrelude
-- * Error handling
HasCallStack,
module Data.Error,
symbolText,
)
where
@ -223,9 +225,9 @@ import Data.Word (Word8)
import GHC.Exception (errorCallWithCallStackException)
import GHC.Exts (Any, RuntimeRep, TYPE, raise#)
import GHC.Generics (Generic)
import GHC.Natural (Natural)
import GHC.Records (HasField)
import GHC.Stack (HasCallStack)
import GHC.TypeLits
import GHC.Utils.Encoding qualified as GHC
import Language.Haskell.TH.Syntax (Lift)
import PyF (fmt)
@ -774,3 +776,9 @@ ifTrue pred' m = if pred' then m else mempty
ifExists :: (Monoid (f b), Applicative f) => (a -> b) -> Maybe a -> f b
ifExists f m = m & foldMap @Maybe (pure . f)
-- | Get the text of a symbol via TypeApplications
symbolText :: forall sym. (KnownSymbol sym) => Text
symbolText = do
symbolVal (Proxy :: Proxy sym)
& stringToText