Elixir Absinthe Instrumentation

295 views Asked by At

I'm looking to instrument my absinthe/phoenix server. I would like to find out how long it takes to encode data as json using Jason.

My endpoint.ex file looks like following:

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

And my router.ex looks like the following:

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

How do I instrument the json_decoder portion of the application to find out how long it takes to make json out of the response data? Maybe using telemetry?

Thanks

1

There are 1 answers

0
Maarten van Vliet On

If it is just the encoding of JSON you could add a custom module for it.

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