首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Instrumentation中的Elixir Absinthe

Instrumentation中的Elixir Absinthe
EN

Stack Overflow用户
提问于 2021-04-13 11:28:01
回答 1查看 75关注 0票数 0

我在找我的苦艾酒/菲尼克斯服务器。我想知道使用Jason将数据编码为json需要多长时间。

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

代码语言:javascript
复制
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如下所示:

代码语言:javascript
复制
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需要多长时间?也许用遥测?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-05-03 22:59:24

如果只是对JSON进行编码,您可以为其添加一个自定义模块。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67068334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档