Skip to content

Commit 2eb243a

Browse files
authored
Merge pull request #215 from liquidvotingio/add-opentelemetry
Extend the span attributes & improve the span name.
2 parents c8125cd + a1dade1 commit 2eb243a

File tree

2 files changed

+62
-41
lines changed

2 files changed

+62
-41
lines changed

lib/liquid_voting/voting.ex

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ defmodule LiquidVoting.Voting do
22
@moduledoc """
33
The Voting context.
44
"""
5+
56
require OpenTelemetry.Tracer, as: Tracer
67

78
import Ecto.Query, warn: false
89

9-
alias __MODULE__.{Vote, Participant}
10-
alias LiquidVoting.{Repo, Delegations}
10+
alias __MODULE__.{Participant, Vote}
11+
alias LiquidVoting.{Delegations, Repo}
1112
alias LiquidVoting.Delegations.Delegation
1213

1314
@doc """
@@ -24,31 +25,42 @@ defmodule LiquidVoting.Voting do
2425
2526
"""
2627
def create_vote(attrs \\ %{}) do
27-
Repo.transaction(fn ->
28-
# TODO: refactor case statements into small functions.
28+
Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
29+
Tracer.set_attributes([
30+
{:request_id, Logger.metadata()[:request_id]},
31+
{:params,
32+
[
33+
{:organization_id, attrs[:organization_id]},
34+
{:participant_email, attrs[:participant_email]},
35+
{:proposal_url, attrs[:proposal_url]},
36+
{:yes, attrs[:yes]}
37+
]}
38+
])
2939

30-
%Vote{}
31-
|> Vote.changeset(attrs)
32-
|> Repo.insert()
33-
|> case do
34-
{:ok, vote} ->
35-
if delegation =
36-
Repo.get_by(Delegation,
37-
delegator_id: attrs[:participant_id],
38-
organization_id: attrs[:organization_id]
39-
) do
40-
case Delegations.delete_delegation(delegation) do
41-
{:ok, _delegation} -> vote
42-
{:error, changeset} -> Repo.rollback(changeset)
40+
Repo.transaction(fn ->
41+
%Vote{}
42+
|> Vote.changeset(attrs)
43+
|> Repo.insert()
44+
|> case do
45+
{:ok, vote} ->
46+
if delegation =
47+
Repo.get_by(Delegation,
48+
delegator_id: attrs[:participant_id],
49+
organization_id: attrs[:organization_id]
50+
) do
51+
case Delegations.delete_delegation(delegation) do
52+
{:ok, _delegation} -> vote
53+
{:error, changeset} -> Repo.rollback(changeset)
54+
end
55+
else
56+
vote
4357
end
44-
else
45-
vote
46-
end
47-
48-
{:error, changeset} ->
49-
Repo.rollback(changeset)
50-
end
51-
end)
58+
59+
{:error, changeset} ->
60+
Repo.rollback(changeset)
61+
end
62+
end)
63+
end
5264
end
5365

5466
@doc """
@@ -61,11 +73,10 @@ defmodule LiquidVoting.Voting do
6173
6274
"""
6375
def list_votes(organization_id) do
64-
Tracer.with_span "LV/voting" do
76+
Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
6577
Tracer.set_attributes([
66-
{:action, "list_votes"},
6778
{:request_id, Logger.metadata()[:request_id]},
68-
{:organization_id, organization_id}
79+
{:params, [{:organization_id, organization_id}]}
6980
])
7081

7182
Vote

lib/liquid_voting_web/resolvers/voting.ex

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ defmodule LiquidVotingWeb.Resolvers.Voting do
2828
do: {:ok, Voting.list_votes_by_proposal(proposal_url, organization_id)}
2929

3030
def votes(_, _, %{context: %{organization_id: organization_id}}) do
31-
Tracer.with_span "resolvers/voting" do
31+
Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
3232
Tracer.set_attributes([
33-
{:action, "votes"},
3433
{:request_id, Logger.metadata()[:request_id]},
35-
{:organization_id, organization_id}
34+
{:params, [{:organization_id, organization_id}]}
3635
])
3736

3837
{:ok, Voting.list_votes(organization_id)}
@@ -45,17 +44,28 @@ defmodule LiquidVotingWeb.Resolvers.Voting do
4544
def create_vote(_, %{participant_email: email, proposal_url: _, yes: _} = args, %{
4645
context: %{organization_id: organization_id}
4746
}) do
48-
case Voting.upsert_participant(%{email: email, organization_id: organization_id}) do
49-
{:error, changeset} ->
50-
{:error,
51-
message: "Could not create vote with given email",
52-
details: ChangesetErrors.error_details(changeset)}
47+
Tracer.with_span "#{__MODULE__} #{inspect(__ENV__.function)}" do
48+
Tracer.set_attributes([
49+
{:request_id, Logger.metadata()[:request_id]},
50+
{:params,
51+
[
52+
{:organization_id, organization_id},
53+
{:email, email}
54+
]}
55+
])
5356

54-
{:ok, participant} ->
55-
args
56-
|> Map.put(:organization_id, organization_id)
57-
|> Map.put(:participant_id, participant.id)
58-
|> create_vote_with_valid_arguments()
57+
case Voting.upsert_participant(%{email: email, organization_id: organization_id}) do
58+
{:error, changeset} ->
59+
{:error,
60+
message: "Could not create vote with given email",
61+
details: ChangesetErrors.error_details(changeset)}
62+
63+
{:ok, participant} ->
64+
args
65+
|> Map.put(:organization_id, organization_id)
66+
|> Map.put(:participant_id, participant.id)
67+
|> create_vote_with_valid_arguments()
68+
end
5969
end
6070
end
6171

0 commit comments

Comments
 (0)