Skip to content

Commit

Permalink
Improve Ecto.Multi instrumentation
Browse files Browse the repository at this point in the history
Instead of discarding `begin` and `commit` queries, use them as the
start and end point of a parent span that groups the transaction.

Handle `rollback` queries as well by adding them to this transaction
group, quickly showing at a glance whether the transaction succeeded
or failed.
  • Loading branch information
unflxw committed Nov 15, 2023
1 parent 4957df9 commit 33f0875
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 27 deletions.
10 changes: 10 additions & 0 deletions .changesets/improve-ecto-transaction-instrumentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
bump: "patch"
type: "change"
---

Improve Ecto transaction instrumentation. Queries performed as part of an
`Ecto.Multi` or an `Ecto.Repo.transaction` were already individually
instrumented, but now they are displayed in the event timeline as child events
of a broader transaction event. An additional event is added at the end of the
transaction, to denote whether the transaction was committed or rolled back.
60 changes: 56 additions & 4 deletions lib/appsignal/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,74 @@ defmodule Appsignal.Ecto do
end

@doc false
def handle_event(_event, _measurements, %{query: "begin"}, _config), do: :ok
def handle_event(_event, _measurements, %{query: "commit"}, _config), do: :ok
def handle_event(_event, %{total_time: total_time}, %{repo: repo, query: "begin"}, _config) do
handle_begin(@tracer.current_span(), total_time, repo)
end

def handle_event(_event, %{total_time: total_time}, %{repo: repo, query: "commit"}, _config) do
handle_commit(@tracer.current_span(), total_time, repo)
end

def handle_event(_event, %{total_time: total_time}, %{repo: repo, query: "rollback"}, _config) do
handle_rollback(@tracer.current_span(), total_time, repo)
end

def handle_event(_event, %{total_time: total_time}, %{repo: repo, query: query}, _config) do
handle_query(@tracer.current_span(), total_time, repo, query)
end

def handle_event(_event, _measurements, _metadata, _config), do: :ok

defp handle_begin(nil, _total_time, _repo), do: nil

defp handle_begin(current_span, total_time, repo) do
time = :os.system_time()

# Intentionally leave span open to be closed
# by `handle_commit/3` or `handle_rollback/3`
"http_request"
|> @tracer.create_span(current_span, start_time: time - total_time)
|> @span.set_name("Transaction #{module_name(repo)}")
|> @span.set_attribute("appsignal:category", "transaction.ecto")
end

defp handle_commit(nil, _total_time, _repo), do: nil

defp handle_commit(current_span, total_time, repo) do
time = :os.system_time()

"http_request"
|> @tracer.create_span(current_span, start_time: time - total_time)
|> @span.set_name("Commit #{module_name(repo)}")
|> @span.set_attribute("appsignal:category", "commit.ecto")
|> @tracer.close_span(end_time: time)

# Close span created by `handle_begin/3`
@tracer.close_span(current_span, end_time: time)
end

defp handle_rollback(nil, _total_time, _repo), do: nil

defp handle_rollback(current_span, total_time, repo) do
time = :os.system_time()

"http_request"
|> @tracer.create_span(current_span, start_time: time - total_time)
|> @span.set_name("Rollback #{module_name(repo)}")
|> @span.set_attribute("appsignal:category", "rollback.ecto")
|> @tracer.close_span(end_time: time)

# Close span created by `handle_begin/3`
@tracer.close_span(current_span, end_time: time)
end

defp handle_query(nil, _total_time, _repo, _query), do: nil

defp handle_query(_current, total_time, repo, query) do
defp handle_query(current_span, total_time, repo, query) do
time = :os.system_time()

"http_request"
|> @tracer.create_span(@tracer.current_span(), start_time: time - total_time)
|> @tracer.create_span(current_span, start_time: time - total_time)
|> @span.set_name("Query #{module_name(repo)}")
|> @span.set_attribute("appsignal:category", "query.ecto")
|> @span.set_sql(query)
Expand Down
Loading

0 comments on commit 33f0875

Please sign in to comment.