-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft: support ingesting gzipped data for logs
Close #1699
- Loading branch information
Showing
9 changed files
with
68 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
11 changes: 11 additions & 0 deletions
11
lib/logflare_web/controllers/plugs/compressed_body_reader.ex
This file contains 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,11 @@ | ||
defmodule Plug.Parsers.CompressedBodyReader do | ||
def read_body(conn, opts \\ []) do | ||
content_encoding = Plug.Conn.get_req_header(conn, "content-encoding") | ||
|
||
with {:ok, body, conn} <- Plug.Conn.read_body(conn, opts), | ||
do: {:ok, try_decompress(body, content_encoding), conn} | ||
end | ||
|
||
defp try_decompress(data, []), do: data | ||
defp try_decompress(data, ["gzip"]), do: :zlib.gunzip(data) | ||
end |
This file contains 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 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 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 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 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 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,33 @@ | ||
defmodule LogflareWeb.Plugs.CompressedBodyReaderTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
@subject Plug.Parsers.CompressedBodyReader | ||
|
||
doctest @subject | ||
|
||
def conn(body, headers \\ []) do | ||
conn = Plug.Test.conn("POST", "/example", body) | ||
|
||
Enum.reduce(headers, conn, fn {key, value}, conn -> | ||
Plug.Conn.put_req_header(conn, key, value) | ||
end) | ||
end | ||
|
||
property "with no `content-encoding` header data is passed through as is" do | ||
check all(data <- binary()) do | ||
assert {:ok, read, _} = @subject.read_body(conn(data)) | ||
assert read == data | ||
end | ||
end | ||
|
||
property "with `content-encoding: gzip` header data is passed through as is" do | ||
check all(data <- binary()) do | ||
compressed = :zlib.gzip(data) | ||
conn = conn(compressed, [{"content-encoding", "gzip"}]) | ||
|
||
assert {:ok, read, _} = @subject.read_body(conn) | ||
assert read == data | ||
end | ||
end | ||
end |
This file contains 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