From d62eff9a85828c6cca3c7d8ca5d307427f5b7b38 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Sat, 11 May 2024 10:03:43 -0400 Subject: [PATCH] Add error :reason to :execute_query Telemetry event Closes #366. --- lib/xandra/connection.ex | 5 ++++- test/integration/telemetry_test.exs | 35 ++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/xandra/connection.ex b/lib/xandra/connection.ex index 8e933833..87eb28e5 100644 --- a/lib/xandra/connection.ex +++ b/lib/xandra/connection.ex @@ -209,7 +209,10 @@ defmodule Xandra.Connection do end :telemetry.span([:xandra, :execute_query], telemetry_meta, fn -> - {fun.(), telemetry_meta} + case fun.() do + {:ok, response} -> {{:ok, response}, telemetry_meta} + {:error, error} -> {{:error, error}, Map.put(telemetry_meta, :reason, error)} + end end) {:error, error} -> diff --git a/test/integration/telemetry_test.exs b/test/integration/telemetry_test.exs index 47657064..a5f5e75d 100644 --- a/test/integration/telemetry_test.exs +++ b/test/integration/telemetry_test.exs @@ -107,7 +107,7 @@ defmodule TelemetryTest do assert is_integer(duration) end - test "execute_query", %{conn: conn} do + test "execute query", %{conn: conn} do ref = :telemetry_test.attach_event_handlers(self(), [ [:xandra, :execute_query, :start], @@ -138,4 +138,37 @@ defmodule TelemetryTest do assert metadata.extra_metadata == %{foo: :bar} assert is_integer(duration) end + + test "execute query with error", %{conn: conn} do + ref = + :telemetry_test.attach_event_handlers(self(), [ + [:xandra, :execute_query, :start], + [:xandra, :execute_query, :stop] + ]) + + statement = "invalid syntax" + + assert {:error, %Xandra.Error{reason: :invalid_syntax}} = + Xandra.execute(conn, statement, [], telemetry_metadata: %{foo: :bar}) + + assert_receive {[:xandra, :execute_query, :start], ^ref, %{system_time: system_time}, + metadata} + + assert metadata.query.statement == statement + assert metadata.connection_name == nil + assert metadata.address == "127.0.0.1" + assert metadata.port == XandraTest.IntegrationCase.port() + assert metadata.extra_metadata == %{foo: :bar} + assert is_integer(system_time) + + assert_receive {[:xandra, :execute_query, :stop], ^ref, %{duration: duration}, metadata} + + assert %Xandra.Error{reason: :invalid_syntax} = metadata.reason + assert metadata.query.statement == statement + assert metadata.connection_name == nil + assert metadata.address == "127.0.0.1" + assert metadata.port == XandraTest.IntegrationCase.port() + assert metadata.extra_metadata == %{foo: :bar} + assert is_integer(duration) + end end