-
Notifications
You must be signed in to change notification settings - Fork 0
elixir: add ClientSignals.Plug for recording signals on request spans #3
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ node_modules/ | |
| __pycache__/ | ||
| *.py[cod] | ||
| _build/ | ||
| deps/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| if Code.ensure_loaded?(Plug.Conn) and Code.ensure_loaded?(OpenTelemetry.Tracer) do | ||
| defmodule ClientSignals.Plug do | ||
| @moduledoc """ | ||
| Reads the coarse, self-reported `Fly-Client-*` headers (as produced by | ||
| `ClientSignals.headers_for/2`) off an incoming request and attaches | ||
| them to the current request's OTel span as `fly.client.*` attributes. | ||
|
|
||
| This gives services a rough human-vs-agent traffic estimate for | ||
| capacity planning and product decisions. These values are self-reported | ||
| by the caller and must never be used for gating, enforcement, or any | ||
| per-request trust decision. | ||
|
|
||
| This module is only defined when both `:plug` and `:opentelemetry_api` | ||
| are loaded; both are optional dependencies of `:client_signals`. | ||
| """ | ||
|
|
||
| import Plug.Conn | ||
| require OpenTelemetry.Tracer, as: Tracer | ||
|
|
||
| @behaviour Plug | ||
|
|
||
| @interactive_header "fly-client-interactive" | ||
| @parent_header "fly-client-parent" | ||
| @agent_header "fly-client-agent" | ||
| @agent_source_header "fly-client-agent-source" | ||
| @ci_header "fly-client-ci" | ||
|
|
||
| # Headers are caller-controlled; bound how much of them we keep so a | ||
| # malicious or misbehaving client can't bloat span payloads. | ||
| @max_value_length 256 | ||
|
|
||
| @impl true | ||
| def init(opts), do: opts | ||
|
|
||
| @impl true | ||
| def call(conn, _opts) do | ||
| attrs = client_signal_attributes(conn) | ||
|
|
||
| if attrs != [] do | ||
| Tracer.set_attributes(attrs) | ||
| end | ||
|
|
||
| conn | ||
| end | ||
|
|
||
| @doc false | ||
| def client_signal_attributes(conn) do | ||
| [] | ||
| |> put_bool_attr(conn, @interactive_header, "fly.client.interactive") | ||
| |> put_string_attr(conn, @parent_header, "fly.client.parent") | ||
| |> put_string_attr(conn, @agent_header, "fly.client.agent") | ||
| |> put_string_attr(conn, @agent_source_header, "fly.client.agent_source") | ||
| |> put_bool_attr(conn, @ci_header, "fly.client.ci") | ||
| end | ||
|
|
||
| defp put_string_attr(attrs, conn, header, key) do | ||
| case get_req_header(conn, header) do | ||
| [value | _] -> | ||
| case String.trim(value) do | ||
| "" -> attrs | ||
| trimmed -> [{key, String.slice(trimmed, 0, @max_value_length)} | attrs] | ||
| end | ||
|
|
||
| _ -> | ||
| attrs | ||
| end | ||
| end | ||
|
|
||
| defp put_bool_attr(attrs, conn, header, key) do | ||
| case get_req_header(conn, header) do | ||
| [value | _] -> | ||
| case parse_bool(value) do | ||
| {:ok, bool} -> [{key, bool} | attrs] | ||
| :error -> attrs | ||
| end | ||
|
|
||
| _ -> | ||
| attrs | ||
| end | ||
| end | ||
|
|
||
| defp parse_bool(value) do | ||
| case value |> String.trim() |> String.downcase() do | ||
| v when v in ~w(1 t true) -> {:ok, true} | ||
| v when v in ~w(0 f false) -> {:ok, false} | ||
| _ -> :error | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| %{ | ||
| "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, | ||
| "opentelemetry_api": {:hex, :opentelemetry_api, "1.5.0", "1a676f3e3340cab81c763e939a42e11a70c22863f645aa06aafefc689b5550cf", [:mix, :rebar3], [], "hexpm", "f53ec8a1337ae4a487d43ac89da4bd3a3c99ddf576655d071deed8b56a2d5dda"}, | ||
| "plug": {:hex, :plug, "1.20.2", "adbee2441232412e37fbb357fd5e4cd533fdd253b29f2e1992262b0f1fb01462", [: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", "b16baf55877d60891002ffc1ce0b3ff7d6f30a38a23e02e4d4293c4ac266f136"}, | ||
| "plug_crypto": {:hex, :plug_crypto, "2.1.1", "19bda8184399cb24afa10be734f84a16ea0a2bc65054e23a62bb10f06bc89491", [:mix], [], "hexpm", "6470bce6ffe41c8bd497612ffde1a7e4af67f36a15eea5f921af71cf3e11247c"}, | ||
| "telemetry": {:hex, :telemetry, "1.4.2", "a0cb522801dffb1c49fe6e30561badffc7b6d0e180db1300df759faa22062855", [:rebar3], [], "hexpm", "928f6495066506077862c0d1646609eed891a4326bee3126ba54b60af61febb1"}, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| defmodule ClientSignals.PlugTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias ClientSignals.Plug, as: ClientSignalsPlug | ||
|
|
||
| defp build_conn(headers) do | ||
| conn = Plug.Test.conn(:get, "/") | ||
|
|
||
| Enum.reduce(headers, conn, fn {k, v}, conn -> | ||
| Plug.Conn.put_req_header(conn, k, v) | ||
| end) | ||
| end | ||
|
|
||
| describe "client_signal_attributes/1" do | ||
| test "collects all headers when present" do | ||
| conn = | ||
| build_conn([ | ||
| {"fly-client-interactive", "true"}, | ||
| {"fly-client-parent", "shell"}, | ||
| {"fly-client-agent", "claude-code"}, | ||
| {"fly-client-agent-source", "env:CLAUDECODE"}, | ||
| {"fly-client-ci", "true"} | ||
| ]) | ||
|
|
||
| attrs = ClientSignalsPlug.client_signal_attributes(conn) |> Map.new() | ||
|
|
||
| assert attrs == %{ | ||
| "fly.client.interactive" => true, | ||
| "fly.client.parent" => "shell", | ||
| "fly.client.agent" => "claude-code", | ||
| "fly.client.agent_source" => "env:CLAUDECODE", | ||
| "fly.client.ci" => true | ||
| } | ||
| end | ||
|
|
||
| test "returns nothing when no headers are present" do | ||
| assert ClientSignalsPlug.client_signal_attributes(build_conn([])) == [] | ||
| end | ||
|
|
||
| test "only includes attributes for headers actually present" do | ||
| conn = | ||
| build_conn([{"fly-client-interactive", "false"}, {"fly-client-parent", "python"}]) | ||
|
|
||
| attrs = ClientSignalsPlug.client_signal_attributes(conn) |> Map.new() | ||
|
|
||
| assert attrs == %{ | ||
| "fly.client.interactive" => false, | ||
| "fly.client.parent" => "python" | ||
| } | ||
| end | ||
|
|
||
| test "ignores unparseable boolean values" do | ||
| conn = build_conn([{"fly-client-interactive", "maybe"}, {"fly-client-ci", "nope"}]) | ||
|
|
||
| assert ClientSignalsPlug.client_signal_attributes(conn) == [] | ||
| end | ||
|
|
||
| test "ignores an empty or whitespace-only string value" do | ||
| conn = build_conn([{"fly-client-parent", " "}]) | ||
|
|
||
| assert ClientSignalsPlug.client_signal_attributes(conn) == [] | ||
| end | ||
|
|
||
| test "trims whitespace from string and boolean values" do | ||
| conn = | ||
| build_conn([{"fly-client-parent", " shell "}, {"fly-client-interactive", " true\n"}]) | ||
|
|
||
| attrs = ClientSignalsPlug.client_signal_attributes(conn) |> Map.new() | ||
|
|
||
| assert attrs == %{ | ||
| "fly.client.parent" => "shell", | ||
| "fly.client.interactive" => true | ||
| } | ||
| end | ||
|
|
||
| test "truncates string values longer than the max attribute length" do | ||
| long_value = String.duplicate("a", 500) | ||
| conn = build_conn([{"fly-client-parent", long_value}]) | ||
|
|
||
| attrs = ClientSignalsPlug.client_signal_attributes(conn) |> Map.new() | ||
|
|
||
| assert String.length(attrs["fly.client.parent"]) == 256 | ||
| end | ||
| end | ||
|
|
||
| describe "call/2" do | ||
| test "returns the conn unchanged" do | ||
| conn = build_conn([{"fly-client-interactive", "true"}]) | ||
|
|
||
| assert ClientSignalsPlug.call(conn, ClientSignalsPlug.init([])) == conn | ||
| end | ||
|
|
||
| test "does not raise when there are no client signal headers" do | ||
| conn = build_conn([]) | ||
|
|
||
| assert ClientSignalsPlug.call(conn, ClientSignalsPlug.init([])) == conn | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.