Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/upgrade phx graphql telemetry #401

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/ret/scene.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ defmodule Ret.Scene do

# Dataloader
def data(), do: Dataloader.Ecto.new(Repo, query: &query/2)
# Guard against loading removed or delested scenes/scene listings
def query(Scene, _), do: from s in Scene, where: s.state != ^:removed
def query(SceneListing, _), do: from sl in SceneListing, where: sl.state != ^:delisted
# Guard against loading removed scenes or delisted scene listings
def query(Scene, _), do: from(s in Scene, where: s.state != ^:removed)
def query(SceneListing, _), do: from(sl in SceneListing, where: sl.state != ^:delisted)

def scene_or_scene_listing_by_sid(sid) do
Scene |> Repo.get_by(scene_sid: sid) ||
Expand Down
7 changes: 1 addition & 6 deletions lib/ret_web/context.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
defmodule RetWeb.Context do
@behaviour Plug

import Plug.Conn
import Ecto.Query, only: [where: 2]

alias RetWeb.{Repo, User}

def init(opts), do: opts

def call(conn, _) do
Expand All @@ -22,4 +17,4 @@ defmodule RetWeb.Context do
%{}
end
end
end
end
2 changes: 2 additions & 0 deletions lib/ret_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ defmodule RetWeb.Endpoint do
plug(Phoenix.CodeReloader)
end

plug Phoenix.LiveDashboard.RequestLogger,
param_key: "request_logger"
plug(Plug.RequestId)
plug(Plug.Logger)

Expand Down
11 changes: 5 additions & 6 deletions lib/ret_web/middlewares/handle_changeset_errors.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
defmodule RetWeb.Middlewares.HandleChangesetErrors do
@behaviour Absinthe.Middleware
def call(resolution, _) do
%{resolution |
errors: Enum.flat_map(resolution.errors, &handle_error/1)
}
%{resolution | errors: Enum.flat_map(resolution.errors, &handle_error/1)}
end

defp handle_error(%Ecto.Changeset{} = changeset) do
changeset
|> Ecto.Changeset.traverse_errors(fn {err, _opts} -> err end)
|> Enum.map(fn({k,v}) -> "#{k}: #{v}" end)
|> Ecto.Changeset.traverse_errors(fn {err, _opts} -> err end)
|> Enum.map(fn {k, v} -> "#{k}: #{v}" end)
end

defp handle_error(error), do: [error]
end
end
25 changes: 18 additions & 7 deletions lib/ret_web/resolvers/room_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,46 @@ defmodule RetWeb.Resolvers.RoomResolver do
alias Ret.Hub
import Canada, only: [can?: 2]

def my_rooms(_parent, args, %{context: %{account: account} }) do
def my_rooms(_parent, args, %{context: %{account: account}}) do
{:ok, Hub.get_my_rooms(account, args)}
end

def my_rooms(_parent, args, _resolutions) do
def my_rooms(_parent, _args, _resolutions) do
{:error, "Not authorized"}
end

def favorite_rooms(_parent, args, %{context: %{account: account} }) do
def favorite_rooms(_parent, args, %{context: %{account: account}}) do
{:ok, Hub.get_favorite_rooms(account, args)}
end

def favorite_rooms(_parent, args, _resolutions) do
def favorite_rooms(_parent, _args, _resolutions) do
{:error, "Not authorized"}
end

def public_rooms(_parent, args, _resolutions) do
{:ok, Hub.get_public_rooms(args)}
end

def create_room(_parent, args, %{context: %{account: account}}) do
{:ok, hub} = Hub.create(args)

hub
|> Ret.Repo.preload(Hub.hub_preloads())
|> Hub.changeset_for_creator_assignment(account, hub.creator_assignment_token)
|> Ret.Repo.update!()

{:ok, hub}
end

def create_room(_parent, args, _resolutions) do
Hub.create(args)
end

def embed_token(hub, _args, %{context: %{account: account} }) do
def embed_token(hub, _args, %{context: %{account: account}}) do
if account |> can?(embed_hub(hub)) do
{:ok, hub.embed_token}
else
{:ok, nil}
{:ok, nil}
end
end

Expand All @@ -44,7 +55,7 @@ defmodule RetWeb.Resolvers.RoomResolver do

def turn(_hub, _args, _resolutions) do
{:ok, Hub.generate_turn_info()}
end
end

def member_permissions(hub, _args, _resolutions) do
{:ok, Hub.member_permissions_for_hub_as_atoms(hub)}
Expand Down
4 changes: 2 additions & 2 deletions lib/ret_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ defmodule RetWeb.Router do

scope "/api/v2", as: :api_v2 do
pipe_through([:parsed_body, :api, :auth_optional, :graphql] ++ if(Mix.env() == :prod, do: [:ssl_only], else: []))
forward "/graphiql", Absinthe.Plug.GraphiQL, schema: RetWeb.Schema
forward "/", Absinthe.Plug, schema: RetWeb.Schema
forward "/graphiql", Absinthe.Plug.GraphiQL, json_codec: Jason, schema: RetWeb.Schema
forward "/", Absinthe.Plug, json_codec: Jason, schema: RetWeb.Schema
end

# Directly accessible APIs.
Expand Down
8 changes: 4 additions & 4 deletions lib/ret_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule RetWeb.Schema do
def middleware(middleware, _field, %{identifier: :mutation}) do
middleware ++ [RetWeb.Middlewares.HandleChangesetErrors]
end

def middleware(middleware, _field, _object), do: middleware

import_types(Absinthe.Type.Custom)
Expand All @@ -21,14 +22,13 @@ defmodule RetWeb.Schema do

def context(ctx) do
loader =
Dataloader.new
Dataloader.new()
|> Dataloader.add_source(Scene, Scene.data())

Map.put(ctx, :loader, loader)
end

def plugins do
[Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
end

end
9 changes: 9 additions & 0 deletions lib/ret_web/schema/room_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,39 @@ defmodule RetWeb.Schema.RoomTypes do
field(:entry_code, :string)
field(:entry_mode, :string)
field(:host, :string)

field(:port, :integer) do
resolve(&Resolvers.RoomResolver.port/3)
end

field(:turn, :turn_info) do
resolve(&Resolvers.RoomResolver.turn/3)
end

field(:embed_token, :string) do
resolve(&Resolvers.RoomResolver.embed_token/3)
end

field(:member_permissions, :member_permissions) do
resolve(&Resolvers.RoomResolver.member_permissions/3)
end

field(:room_size, :integer) do
resolve(&Resolvers.RoomResolver.room_size/3)
end

field(:member_count, :integer) do
resolve(&Resolvers.RoomResolver.member_count/3)
end

field(:lobby_count, :integer) do
resolve(&Resolvers.RoomResolver.lobby_count/3)
end

field(:scene, :scene_or_scene_listing) do
resolve(dataloader(Scene))
end

# TODO: Figure out user_data
end

Expand Down
8 changes: 4 additions & 4 deletions lib/ret_web/schema/scene_types.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule RetWeb.Schema.SceneTypes do
use Absinthe.Schema.Notation
alias RetWeb.Resolvers
alias Ret.{Scene, SceneListing}

union :scene_or_scene_listing do
types [:scene, :scene_listing]
resolve_type fn
types([:scene, :scene_listing])

resolve_type(fn
%Scene{}, _ -> :scene
%SceneListing{}, _ -> :scene_listing
end
end)
end

object :scene do
Expand Down
5 changes: 5 additions & 0 deletions lib/ret_web/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ defmodule RetWeb.Telemetry do
tags: [:route],
unit: {:native, :millisecond}
),
#Absinthe Metrics
summary("absinthe.execute.operation.stop.duration"),
summary("absinthe.resolve.field.stop.duration"),
summary("absinthe.middleware.batch.stop.duration"),

# VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
Expand Down
18 changes: 9 additions & 9 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ defmodule Ret.Mixfile do
defp deps do
[
{:ecto_boot_migration, "~> 0.2.0"},
{:phoenix, "~> 1.4.0"},
{:phoenix_pubsub, "~> 1.1"},
{:phoenix, "~> 1.5.0"},
{:phoenix_pubsub, "~> 2.0"},
{:phoenix_ecto, "~> 4.0"},
{:plug, "~> 1.7"},
# Avoid 3.4.0 for now bc https://github.com/elixir-ecto/ecto/issues/3246
{:ecto, "~> 3.3.0"},
{:ecto_sql, "~> 3.3.0"},
{:absinthe, "~> 1.4"},
{:absinthe, "~> 1.5"},
{:dataloader, "~> 1.0.0"},
{:absinthe_plug, "~> 1.4"},
{:absinthe_phoenix, "~> 1.4.0"},
{:absinthe_plug, "~> 1.5"},
{:absinthe_phoenix, "~> 2.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.13"},
{:phoenix_live_view, "~> 0.12"},
{:phoenix_live_view, "~> 0.14.4"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:telemetry_metrics, "~> 0.4"},
{:telemetry_poller, "~> 0.4"},
{:phoenix_live_dashboard, "~> 0.1"},
{:phoenix_live_dashboard, "~> 0.2.7"},
{:gettext, "~> 0.17"},
{:cowboy, "~> 2.6.3"},
{:plug_cowboy, "~> 2.0"},
{:cowboy, "~> 2.8"},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: If we upgrade cowboy to 2.8, then we need to make sure we are running erlang 22+

See phoenixframework/phoenix#3950

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we are running 22.0 in habitat deploys:
https://github.com/mozilla/hubs-ops/blob/master/plans/erlang/habitat/plan.sh#L3
Need to confirm this and check terraform / hmc

{:plug_cowboy, "~> 2.1"},
{:distillery, "~> 2.0"},
{:peerage, "~> 1.0"},
{:httpoison, "~> 1.5"},
Expand Down
Loading