Sentry elixir无法对元组进行编码



我知道,从纯粹的意义上讲,JSON不考虑元组,但我认为从JSON编码的角度来看,将元组视为列表并不是不合理的。\有其他人面对并解决了这个问题吗?我不想对错误数据进行预处理,用列表替换元组。

也许我需要指定一种不同的序列化方法?

编辑:这里有一个实际的例子:

这是一些玩具代码。

the_data = {:ok, %{...}}
Sentry.capture_message(message, the_data)

它所做的只是尝试向Sentry发送数据中包含元组的消息。

如果你不熟悉Sentry,Sentry elixir库提供了两个函数(当然还有许多其他函数(,用于显式地向Sentry发送异常或消息。功能包括:

Sentry.capture_exception/2
Sentry.capture_message/2

此外,错误在气泡上升到"0"时被发送到Sentry;顶部";。这些不能被拦截,所以我必须指定(并实现(CCD_;处理程序";在Sentry的配置中。

这就是我工作环境的配置:

config :sentry,
dsn: "https://my_neato_sentry_key@sentry.io/33333343",
environment_name: :staging,
enable_source_code_context: true,
root_source_code_path: File.cwd!(),
tags: %{
env: "staging"
},
before_send_event: {SomeApplication.Utils.SentryLogger, :before_send},
included_environments: [:staging],
filter: SomeApplication.SentryEventFilter

我的before_send函数基本上尝试对数据进行健全性检查,并用列表替换所有元组。不过,我还没有完全实现这一点,我暂时使用Kernel.inspect/2将其转换为字符串,而不是替换所有元组。当然,这并不理想,因为他们我无法操纵哨兵视图中的数据:

def before_send(sentry_event) do
IO.puts "------- BEFORE SEND TWO ---------------------------"
sentry_event
|> inspect(limit: :infinity)
end

这将产生以下输出:

{:invalid, {:ok, the_data}}

capture_message失败。

默认情况下,sentry使用jason对其JSON进行编码,而默认情况下jason不编码元组。您可以通过为Tuple:实现Jason.Encoder来改变这一点

defimpl Jason.Encoder, for: Tuple do
def encode(tuple, opts) do
Jason.Encode.list(Tuple.to_list(tuple), opts)
end
end

请注意,这将对应用程序中元组转换为JSON的方式产生全局影响。

最新更新