Skip to content

Commit

Permalink
Further improve coverage and handle OTEL encoding edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDryga committed May 14, 2024
1 parent e16d5b8 commit fce0792
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# LoggerJSON

[![Build Status](https://travis-ci.org/Nebo15/logger_json.svg?branch=master)](https://travis-ci.org/Nebo15/logger_json)
[![Coverage Status](https://coveralls.io/repos/github/Nebo15/logger_json/badge.svg?branch=master)](https://coveralls.io/github/Nebo15/logger_json?branch=master)
[![Module Version](https://img.shields.io/hexpm/v/logger_json.svg)](https://hex.pm/packages/logger_json)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/logger_json/)
[![Hex Download Total](https://img.shields.io/hexpm/dt/logger_json.svg)](https://hex.pm/packages/logger_json)
Expand Down
20 changes: 14 additions & 6 deletions lib/logger_json/formatters/google_cloud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
defp log_level(:notice), do: "NOTICE"
defp log_level(:info), do: "INFO"
defp log_level(:debug), do: "DEBUG"
defp log_level(_), do: "DEFAULT"

@doc false
def format_binary_message(binary) do
Expand Down Expand Up @@ -264,8 +263,11 @@ defmodule LoggerJSON.Formatters.GoogleCloud do

defp format_line(line) do
case Regex.run(~r/(.+)\:(\d+)\: (.*)/, line) do
[_, file, line, function] -> {:trace, "#{file}:#{line}:in `#{function}'"}
_ -> {:context, line}
[_, file, line, function] ->
{:trace, "#{file}:#{line}:in `#{function}'"}

_ ->
{:context, line}
end
end

Expand Down Expand Up @@ -299,7 +301,7 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
do: nil

defp format_span(%{otel_span_id: otel_span_id}, _project_id_or_nil),
do: IO.chardata_to_string(otel_span_id)
do: safe_chardata_to_string(otel_span_id)

defp format_span(%{span_id: span_id}, _project_id_or_nil),
do: span_id
Expand All @@ -308,14 +310,20 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
do: nil

defp format_trace(%{otel_trace_id: otel_trace_id}, nil),
do: IO.chardata_to_string(otel_trace_id)
do: safe_chardata_to_string(otel_trace_id)

defp format_trace(%{otel_trace_id: otel_trace_id}, project_id),
do: "projects/#{project_id}/traces/#{IO.chardata_to_string(otel_trace_id)}"
do: "projects/#{project_id}/traces/#{safe_chardata_to_string(otel_trace_id)}"

defp format_trace(%{trace_id: trace_id}, _project_id_or_nil),
do: trace_id

defp format_trace(_meta, _project_id_or_nil),
do: nil

def safe_chardata_to_string(chardata) when is_list(chardata) or is_binary(chardata) do
IO.chardata_to_string(chardata)
end

def safe_chardata_to_string(other), do: other
end
6 changes: 3 additions & 3 deletions test/logger_json/formatters/basic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule LoggerJSON.Formatters.BasicTest do
end
end

test "logs an LogEntry of a given level" do
test "logs message of a given level" do
for level <- Logger.levels() do
log =
capture_log(level, fn ->
Expand All @@ -56,7 +56,7 @@ defmodule LoggerJSON.Formatters.BasicTest do
end
end

test "logs an LogEntry with a map payload" do
test "logs message with a map payload" do
log =
capture_log(fn ->
Logger.debug(%{foo: :bar, fiz: [1, 2, 3, "buz"]})
Expand All @@ -69,7 +69,7 @@ defmodule LoggerJSON.Formatters.BasicTest do
}
end

test "logs an LogEntry with a keyword payload" do
test "logs message with a keyword payload" do
log =
capture_log(fn ->
Logger.debug(a: {0, false})
Expand Down
136 changes: 136 additions & 0 deletions test/logger_json/formatters/datadog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,62 @@ defmodule LoggerJSON.Formatters.DatadogTest do

assert log["dd.span_id"] == ""
assert log["dd.trace_id"] == ""

Logger.metadata(
otel_span_id: "ghijklmnopqrstuv",
otel_trace_id: "ghijklmnopqrstuv"
)

log =
capture_log(fn ->
Logger.debug("Hello")
end)
|> decode_or_print_error()

assert log["dd.span_id"] == ""
assert log["dd.trace_id"] == ""

Logger.metadata(
otel_span_id: "🚀🚀🚀🚀🚀🚀🚀🚀",
otel_trace_id: "🚀🚀🚀🚀🚀🚀🚀🚀"
)

log =
capture_log(fn ->
Logger.debug("Hello")
end)
|> decode_or_print_error()

assert log["dd.span_id"] == ""
assert log["dd.trace_id"] == ""

Logger.metadata(
otel_span_id: ~c"🚀🚀🚀🚀🚀🚀🚀🚀",
otel_trace_id: ~c"🚀🚀🚀🚀🚀🚀🚀🚀"
)

log =
capture_log(fn ->
Logger.debug("Hello")
end)
|> decode_or_print_error()

assert log["dd.span_id"] == ""
assert log["dd.trace_id"] == ""

Logger.metadata(
otel_span_id: ~c"🚀",
otel_trace_id: ~c"🚀"
)

log =
capture_log(fn ->
Logger.debug("Hello")
end)
|> decode_or_print_error()

assert log["dd.span_id"] == ""
assert log["dd.trace_id"] == ""
end

test "logs span and trace ids" do
Expand Down Expand Up @@ -277,4 +333,84 @@ defmodule LoggerJSON.Formatters.DatadogTest do
"useragent" => "Mozilla/5.0"
}
end

test "logs throws" do

Check failure on line 337 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (24.3, 1.16)

test logs throws (LoggerJSON.Formatters.DatadogTest)

Check failure on line 337 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (25, 1.16, true, true)

test logs throws (LoggerJSON.Formatters.DatadogTest)
Logger.metadata(crash_reason: {:throw, {:error, :whatever}})

log_entry =
capture_log(fn ->
Logger.debug("error here")
end)
|> decode_or_print_error()

assert %{
"error" => %{"message" => "error here"},
"logger" => %{
"file_name" => "/Users/andrew/Projects/os/logger_json/test/logger_json/formatters/datadog_test.exs",
"line" => _line,
"method_name" => "Elixir.LoggerJSON.Formatters.DatadogTest." <> _,
"thread_name" => _pid
}
} = log_entry
end

test "logs exits" do

Check failure on line 357 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (24.3, 1.16)

test logs exits (LoggerJSON.Formatters.DatadogTest)

Check failure on line 357 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (25, 1.16, true, true)

test logs exits (LoggerJSON.Formatters.DatadogTest)
Logger.metadata(crash_reason: {:exit, :sad_failure})

log_entry =
capture_log(fn ->
Logger.debug("error here")
end)
|> decode_or_print_error()

assert %{
"error" => %{"message" => "error here"},
"logger" => %{
"file_name" => "/Users/andrew/Projects/os/logger_json/test/logger_json/formatters/datadog_test.exs",
"line" => _line,
"method_name" => "Elixir.LoggerJSON.Formatters.DatadogTest." <> _,
"thread_name" => _pid
}
} = log_entry
end

test "logs process exits" do

Check failure on line 377 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (24.3, 1.16)

test logs process exits (LoggerJSON.Formatters.DatadogTest)

Check failure on line 377 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (25, 1.16, true, true)

test logs process exits (LoggerJSON.Formatters.DatadogTest)
Logger.metadata(crash_reason: {{:EXIT, self()}, :sad_failure})

log_entry =
capture_log(fn ->
Logger.debug("error here")
end)
|> decode_or_print_error()

assert %{
"error" => %{"message" => "error here"},
"logger" => %{
"file_name" => "/Users/andrew/Projects/os/logger_json/test/logger_json/formatters/datadog_test.exs",
"line" => _line,
"method_name" => "Elixir.LoggerJSON.Formatters.DatadogTest." <> _,
"thread_name" => _pid
}
} = log_entry
end

test "logs reasons in tuple" do

Check failure on line 397 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (24.3, 1.16)

test logs reasons in tuple (LoggerJSON.Formatters.DatadogTest)

Check failure on line 397 in test/logger_json/formatters/datadog_test.exs

View workflow job for this annotation

GitHub Actions / Build and Test (25, 1.16, true, true)

test logs reasons in tuple (LoggerJSON.Formatters.DatadogTest)
Logger.metadata(crash_reason: {:socket_closed_unexpectedly, []})

log_entry =
capture_log(fn ->
Logger.debug("error here")
end)
|> decode_or_print_error()

assert %{
"error" => %{"message" => "error here"},
"logger" => %{
"file_name" => "/Users/andrew/Projects/os/logger_json/test/logger_json/formatters/datadog_test.exs",
"line" => _line,
"method_name" => "Elixir.LoggerJSON.Formatters.DatadogTest." <> _,
"thread_name" => _pid
}
} = log_entry
end
end
3 changes: 3 additions & 0 deletions test/logger_json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ defmodule LoggerJSONTest do

assert LoggerJSON.configure_log_level!(:info) == :ok
assert Logger.level() == :info

assert LoggerJSON.configure_log_level!(nil) == :ok
assert Logger.level() == :info
end

test "raises on invalid log level" do
Expand Down

0 comments on commit fce0792

Please sign in to comment.