Elixir 苦艾酒仪器

Posted

技术标签:

【中文标题】Elixir 苦艾酒仪器【英文标题】:Elixir Absinthe Instrumentation 【发布时间】:2021-07-08 02:55:11 【问题描述】:

我正在寻找我的苦艾酒/凤凰服务器。我想知道使用 Jason 将数据编码为 json 需要多长时间。

我的endpoint.ex 文件如下所示:

defmodule AssessmentApi.Web.Endpoint do
  use Phoenix.Endpoint, otp_app: :assessment_api

  socket "/socket", AssessmentApi.Web.UserSocket

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phoenix.digest
  # when deploying your static files in production.
  plug Plug.Static,
    at: "/", from: :assessment_api, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

  if code_reloading? do
    socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
    plug Phoenix.LiveReloader
    plug Phoenix.CodeReloader
  end

  plug Plug.RequestId
  plug Plug.Logger

  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
    pass: ["*/*"],
    json_decoder: Jason

  plug Plug.MethodOverride
  plug Plug.Head

  # The session will be stored in the cookie and signed,
  # this means its contents can be read but not tampered with.
  # Set :encryption_salt if you would also like to encrypt it.
  plug Plug.Session,
    store: :cookie,
    key: "adfadfasdfasdfadsf",
    signing_salt: "asdsfasdfasdfasdf"

  plug CORSPlug
  plug AssessmentApi.Web.Logger
  plug AssessmentApi.Web.Router
end

我的router.ex 如下所示:

defmodule AssessmentApi.Web.Router do
  use AssessmentApi.Web, :router

  pipeline :api do
    plug AssessmentApi.Guardian.AuthPipeline

    plug :accepts, ["json"]
  end

  scope "/" do
    pipe_through :api

    forward "/api", Absinthe.Plug,
      schema: AssessmentApi.Web.Schema,
      json_codec: Jason

    forward "/graphiql", Absinthe.Plug.GraphiQL,
      schema: AssessmentApi.Web.Schema,
      json_codec: Jason
  end
end

如何检测应用程序的 json_decoder 部分,以了解从响应数据中生成 json 需要多长时间?也许使用遥测?

谢谢

【问题讨论】:

是的,现在社区主要使用 telemetry。另外,这个问题显然是题外话,你最好在这里问这个:elixirforum.com 【参考方案1】:

如果只是 JSON 的编码,您可以为其添加自定义模块。

defmodule InstrumentedJason do
  def encode!(body, opts) do
    :telemetry.span(
      [:your_app, :encode_absinthe_result],
      %,
      fn ->
        result = Jason.encode!(body, opts)

        result, %
      end
    )
    
  end
end

# router.ex
  forward "/api", Absinthe.Plug,
      schema: AssessmentApi.Web.Schema,
      json_codec: InstrumentedJason

【讨论】:

以上是关于Elixir 苦艾酒仪器的主要内容,如果未能解决你的问题,请参考以下文章

我怎样才能让苦艾酒和 Dataloader 一起工作?

Elixir / JInterface - 从 Java 将字符串列表发送回 elixir

简单Elixir游戏服务器-安装Elixir

Elixir快速介绍

Erlang - Elixir:啥是监督树?

Elixir东游记/上:intro/1