Skip to content

Commit

Permalink
Relaxing Grafana client error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Jun 15, 2021
1 parent 2f9b154 commit 31f10fa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/prom_ex/grafana_client.ex
Expand Up @@ -12,6 +12,15 @@ defmodule PromEx.GrafanaClient do

alias PromEx.GrafanaClient.Connection

@status_codes %{
200 => :ok,
400 => :bad_request,
401 => :unauthorized,
403 => :forbidden,
404 => :not_found,
412 => :already_exists
}

@typep handler_response ::
{:ok, result :: map()} | {:error, reason :: atom()} | {:error, reason :: Mint.TransportError.t()}

Expand Down Expand Up @@ -143,26 +152,19 @@ defmodule PromEx.GrafanaClient do

defp handle_grafana_response(finch_response) do
case finch_response do
{:ok, %Finch.Response{status: 200, body: body}} ->
{:ok, %Finch.Response{status: status_code, body: body}} when status_code in [200, 201] ->
{:ok, Jason.decode!(body)}

{:ok, %Finch.Response{status: 400}} ->
{:error, :bad_request}

{:ok, %Finch.Response{status: 401}} ->
{:error, :unauthorized}

{:ok, %Finch.Response{status: 404}} ->
{:error, :not_found}

{:ok, %Finch.Response{status: 412}} ->
{:error, :already_exists}
{:ok, %Finch.Response{status: status_code} = response} ->
Logger.warn("Recieved a #{status_code} from Grafana because: #{inspect(response)}")
{:error, lookup_status_code(status_code)}

{:error, %Mint.TransportError{} = mint_error_reason} ->
{:error, Exception.message(mint_error_reason)}

{:error, _unknown_reason} = error ->
error
unknown_response ->
Logger.warn("Recieved an unhandled response from Grafana because: #{inspect(unknown_response)}")
{:error, :unkown}
end
end

Expand All @@ -189,4 +191,8 @@ defmodule PromEx.GrafanaClient do
|> Map.put(:dashboard, dashboard)
|> Jason.encode!()
end

defp lookup_status_code(status_code) do
Map.get(@status_codes, status_code, :unknown)
end
end
40 changes: 40 additions & 0 deletions test/prom_ex/dashboard_uploader_test.exs
Expand Up @@ -39,6 +39,46 @@ defmodule PromEx.DashboardUploaderTest do
{:ok, bypass: bypass}
end

test "should handle HTTP errors", %{bypass: bypass} do
response_payload = """
{
"message": "Unauthorized request"
}
"""

Application.put_env(:prom_ex, DefaultPromExSetUp,
grafana: [
host: endpoint_url(bypass.port),
auth_token: "random_token",
folder_name: "Web App Dashboards",
annotate_app_lifecycle: false
]
)

folder_uid = DefaultPromExSetUp.__grafana_folder_uid__()

Bypass.expect_once(bypass, "GET", "/api/folders/#{folder_uid}", fn conn ->
Plug.Conn.resp(conn, 401, response_payload)
end)

assert capture_log(fn ->
{:ok, pid} =
start_supervised(
{
DashboardUploader,
[
name: DefaultPromExSetUp.__dashboard_uploader_name__(),
prom_ex_module: DefaultPromExSetUp,
default_dashboard_opts: []
]
},
restart: :temporary
)

wait_for_process(pid, 1_000)
end) =~ "Recieved a 401 from Grafana because"
end

test "should check for existing folders", %{bypass: bypass} do
response_payload = """
{
Expand Down

0 comments on commit 31f10fa

Please sign in to comment.