Skip to content

Commit

Permalink
fix too many unnamed operation name by improving the regex (#45)
Browse files Browse the repository at this point in the history
* fix too many unnamed operation name by improving the regex

---------

Co-authored-by: emmanuel.pinault <[email protected]>
  • Loading branch information
epinault and epinault authored Nov 8, 2023
1 parent ee527c5 commit 89b1bd3
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 21 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-22.04, ubuntu-20.04]
elixir_version: [1.12.3, 1.13.3, 1.14.1]
otp_version: [24, 25]
elixir_version: [ 1.13, 1.14, 1.15]
otp_version: [24, 25, 26]
exclude:
- otp_version: 26
elixir_version: 1.14
- otp_version: 26
elixir_version: 1.13
- otp_version: 25
elixir_version: 1.12.3
elixir_version: 1.13
steps:
- uses: actions/checkout@v3

Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.14.3
elixir 1.15.6
erlang 25.2
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## v0.12.0 (2023-11-07)
### Changed
* Upgrade dependencies
* fix bad regex to capture better operation name

## v0.11.0 (2022-11-28)
### Changed
* Upgrade dependencies
Expand Down
6 changes: 3 additions & 3 deletions lib/uinta/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ if Code.ensure_loaded?(Plug) do
@default_filter ~w(password passwordConfirmation idToken refreshToken)
@default_sampling_ratio 1.0

@query_name_regex ~r/^(?:(?:query|mutation)\s+(\w+)(?:\(\s*\$\w+:\s+\[?\w+\]?!?(?:,?\s+\$\w+:\s+\[?\w+\]?!?)*\s*\))?\s*)?{/
@query_name_regex ~r/^(?:query|mutation)\s+(\w+)|{\W+(\w+)\W+?{/m

@type format :: :json | :string
@type graphql_info :: %{type: String.t(), operation: String.t(), variables: String.t() | nil}
Expand Down Expand Up @@ -307,7 +307,7 @@ if Code.ensure_loaded?(Plug) do
end
end

@spec formatted_diff(integer()) :: list(String.t())
@spec formatted_diff(integer()) :: String.t()
defp formatted_diff(diff) when diff > 1000 do
"#{diff |> div(1000) |> Integer.to_string()}ms"
end
Expand All @@ -318,7 +318,7 @@ if Code.ensure_loaded?(Plug) do
defp connection_type(%{state: :set_chunked}), do: "Chunked"
defp connection_type(_), do: "Sent"

@spec operation_name(String.t()) :: String.t() | nil
@spec operation_name(map()) :: String.t() | nil
defp operation_name(%{"operationName" => name}), do: name

defp operation_name(%{"query" => query}) do
Expand Down
10 changes: 5 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Uinta.MixProject do
name: "Uinta",
description: "Simpler structured logs and lower log volume for Elixir apps",
version: @version,
elixir: "~> 1.11",
elixir: "~> 1.13",
source_url: @project_url,
homepage_url: @project_url,
start_permanent: Mix.env() == :prod,
Expand All @@ -29,10 +29,10 @@ defmodule Uinta.MixProject do

defp deps do
[
{:credo, "~> 1.6", only: [:dev, :test]},
{:ex_doc, "~> 0.21", only: :dev, runtime: false},
{:jason, "~> 1.1"},
{:plug, ">= 0.0.0", optional: true}
{:credo, "~> 1.7", only: [:dev, :test]},
{:ex_doc, "~> 0.30", only: :dev, runtime: false},
{:jason, "~> 1.4"},
{:plug, "~> 1.10", optional: true}
]
end

Expand Down
12 changes: 6 additions & 6 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
%{
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"credo": {:hex, :credo, "1.7.0", "6119bee47272e85995598ee04f2ebbed3e947678dee048d10b5feca139435f75", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6839fcf63d1f0d1c0f450abc8564a57c43d644077ab96f2934563e68b8a769d7"},
"earmark_parser": {:hex, :earmark_parser, "1.4.33", "3c3fd9673bb5dcc9edc28dd90f50c87ce506d1f71b70e3de69aa8154bc695d44", [:mix], [], "hexpm", "2d526833729b59b9fdb85785078697c72ac5e5066350663e5be6a1182da61b8f"},
"ex_doc": {:hex, :ex_doc, "0.30.5", "aa6da96a5c23389d7dc7c381eba862710e108cee9cfdc629b7ec021313900e9e", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "88a1e115dcb91cefeef7e22df4a6ebbe4634fbf98b38adcbc25c9607d6d9d8e6"},
"credo": {:hex, :credo, "1.7.1", "6e26bbcc9e22eefbff7e43188e69924e78818e2fe6282487d0703652bc20fd62", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e9871c6095a4c0381c89b6aa98bc6260a8ba6addccf7f6a53da8849c748a58a2"},
"earmark_parser": {:hex, :earmark_parser, "1.4.37", "2ad73550e27c8946648b06905a57e4d454e4d7229c2dafa72a0348c99d8be5f7", [:mix], [], "hexpm", "6b19783f2802f039806f375610faa22da130b8edc21209d0bff47918bb48360e"},
"ex_doc": {:hex, :ex_doc, "0.30.9", "d691453495c47434c0f2052b08dd91cc32bc4e1a218f86884563448ee2502dd2", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d7aaaf21e95dc5cddabf89063327e96867d00013963eadf2c6ad135506a8bc10"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"plug": {:hex, :plug, "1.14.2", "cff7d4ec45b4ae176a227acd94a7ab536d9b37b942c8e8fa6dfc0fff98ff4d80", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "842fc50187e13cf4ac3b253d47d9474ed6c296a8732752835ce4a86acdf68d13"},
"plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"},
"plug": {:hex, :plug, "1.15.1", "b7efd81c1a1286f13efb3f769de343236bd8b7d23b4a9f40d3002fc39ad8f74c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "459497bd94d041d98d948054ec6c0b76feacd28eec38b219ca04c0de13c79d30"},
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
}
2 changes: 1 addition & 1 deletion test/uinta/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Uinta.FormatterTest do
end

test "formats metadata charlist" do
result = Formatter.format(:info, "Testing", {{1980, 1, 1}, {0, 0, 0, 0}}, prop: 'abc')
result = Formatter.format(:info, "Testing", {{1980, 1, 1}, {0, 0, 0, 0}}, prop: ~c"abc")

%{"metadata" => %{"prop" => metadata_value}} = Jason.decode!(result)
assert metadata_value == "abc"
Expand Down
54 changes: 52 additions & 2 deletions test/uinta/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,32 @@ defmodule Uinta.PlugTest do
assert message =~ ~r/http.status_code\":200/u
end

test "logs graphql json to console" do
test "logs graphql json to console, use operationName" do
variables = %{"user_uid" => "b1641ddf-b7b0-445e-bcbb-96ef359eae81"}
params = %{"operationName" => "getUser", "query" => "query getUser", "variables" => variables}

params = %{
"operationName" => "getUser",
"query" => "query totoQuery",
"variables" => variables
}

message =
capture_log(fn ->
JsonPlug.call(conn(:post, "/graphql", params), [])
end)

assert message =~ ~r/client_ip\":\"127.0.0.1\"/u
assert message =~ ~r/duration_ms\":[0-9]+\.?[0-9]+/u
assert message =~ ~r"path\":\"/graphql\""u
assert message =~ ~r/status\":\"200\"/u
assert message =~ ~r/timing\":\"[0-9]+[µm]s\"/u
assert message =~ ~r/method\":\"QUERY\"/u
assert message =~ ~r/operation_name\":\"getUser\"/u
end

test "logs graphql json to console use Query for operationName" do
variables = %{"user_uid" => "b1641ddf-b7b0-445e-bcbb-96ef359eae81"}
params = %{"query" => "query getUser", "variables" => variables}

message =
capture_log(fn ->
Expand All @@ -214,6 +237,33 @@ defmodule Uinta.PlugTest do
assert message =~ ~r/operation_name\":\"getUser\"/u
end

test "logs graphql json to console use mutation for operationName" do
variables = %{"user_uid" => "b1641ddf-b7b0-445e-bcbb-96ef359eae81"}

query = """
mutation track($userId: String!, $event: String!, $properties: [String]) {
track(userId: $userId, event: $event, properties: $properties) {
status
}
}
"""

params = %{"query" => query, "variables" => variables}

message =
capture_log(fn ->
JsonPlug.call(conn(:post, "/graphql", params), [])
end)

assert message =~ ~r/client_ip\":\"127.0.0.1\"/u
assert message =~ ~r/duration_ms\":[0-9]+\.?[0-9]+/u
assert message =~ ~r"path\":\"/graphql\""u
assert message =~ ~r/status\":\"200\"/u
assert message =~ ~r/timing\":\"[0-9]+[µm]s\"/u
assert message =~ ~r/method\":\"MUTATION\"/u
assert message =~ ~r/operation_name\":\"track\"/u
end

test "logs graphql json to console with extra headers" do
variables = %{"user_uid" => "b1641ddf-b7b0-445e-bcbb-96ef359eae81"}
params = %{"operationName" => "getUser", "query" => "query getUser", "variables" => variables}
Expand Down

0 comments on commit 89b1bd3

Please sign in to comment.