Skip to content

Commit ad4a1da

Browse files
committed
Support for liveview traces/spans
1 parent b9dae92 commit ad4a1da

File tree

25 files changed

+588
-33
lines changed

25 files changed

+588
-33
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
6767
defp build_transaction(
6868
%SpanRecord{attributes: attributes, origin: "opentelemetry_phoenix"} = root_span,
6969
child_spans
70-
) do
70+
)
71+
when map_size(attributes) > 0 do
7172
Transaction.new(%{
7273
transaction: "#{attributes["phoenix.plug"]}##{attributes["phoenix.action"]}",
7374
start_timestamp: root_span.start_time,
@@ -103,6 +104,25 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
103104
})
104105
end
105106

107+
defp build_transaction(
108+
%SpanRecord{attributes: attributes, origin: "opentelemetry_phoenix"} = root_span,
109+
child_spans
110+
)
111+
when map_size(attributes) == 0 do
112+
Transaction.new(%{
113+
transaction: root_span.name,
114+
start_timestamp: root_span.start_time,
115+
timestamp: root_span.end_time,
116+
transaction_info: %{
117+
source: "view"
118+
},
119+
contexts: %{
120+
trace: build_trace_context(root_span)
121+
},
122+
spans: Enum.map(child_spans, &build_span(&1))
123+
})
124+
end
125+
106126
defp build_transaction(
107127
%SpanRecord{attributes: attributes, origin: "opentelemetry_bandit"} = root_span,
108128
child_spans
@@ -135,7 +155,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
135155

136156
defp build_trace_context(
137157
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = root_span
138-
) do
158+
)
159+
when map_size(attributes) > 0 do
139160
%{
140161
trace_id: root_span.trace_id,
141162
span_id: root_span.span_id,
@@ -149,6 +170,20 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
149170
}
150171
end
151172

173+
defp build_trace_context(
174+
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = root_span
175+
)
176+
when map_size(attributes) == 0 do
177+
%{
178+
trace_id: root_span.trace_id,
179+
span_id: root_span.span_id,
180+
parent_span_id: nil,
181+
op: "http.server.live",
182+
description: root_span.name,
183+
origin: root_span.origin
184+
}
185+
end
186+
152187
defp build_trace_context(
153188
%SpanRecord{origin: "opentelemetry_ecto", attributes: attributes} = root_span
154189
) do
@@ -174,6 +209,22 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
174209
}
175210
end
176211

212+
defp build_span(
213+
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = span_record
214+
)
215+
when map_size(attributes) == 0 do
216+
%Span{
217+
op: "http.server.live",
218+
description: span_record.name,
219+
start_timestamp: span_record.start_time,
220+
timestamp: span_record.end_time,
221+
trace_id: span_record.trace_id,
222+
span_id: span_record.span_id,
223+
parent_span_id: span_record.parent_span_id,
224+
origin: span_record.origin
225+
}
226+
end
227+
177228
defp build_span(
178229
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = span_record
179230
) do

test_integrations/phoenix_app/config/test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Config
33
# Configure your database
44
config :phoenix_app, PhoenixApp.Repo,
55
adapter: Ecto.Adapters.SQLite3,
6+
pool: Ecto.Adapters.SQL.Sandbox,
67
database: "db/test.sqlite3"
78

89
# We don't run a server during test. If one is required,
-16 KB
Binary file not shown.
-32 KB
Binary file not shown.

test_integrations/phoenix_app/db/dev.sqlite3-wal

Whitespace-only changes.
-4 KB
Binary file not shown.
-32 KB
Binary file not shown.
-24.2 KB
Binary file not shown.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
defmodule PhoenixApp.Accounts do
2+
@moduledoc """
3+
The Accounts context.
4+
"""
5+
6+
import Ecto.Query, warn: false
7+
alias PhoenixApp.Repo
8+
9+
alias PhoenixApp.Accounts.User
10+
11+
@doc """
12+
Returns the list of users.
13+
14+
## Examples
15+
16+
iex> list_users()
17+
[%User{}, ...]
18+
19+
"""
20+
def list_users do
21+
Repo.all(User)
22+
end
23+
24+
@doc """
25+
Gets a single user.
26+
27+
Raises `Ecto.NoResultsError` if the User does not exist.
28+
29+
## Examples
30+
31+
iex> get_user!(123)
32+
%User{}
33+
34+
iex> get_user!(456)
35+
** (Ecto.NoResultsError)
36+
37+
"""
38+
def get_user!(id), do: Repo.get!(User, id)
39+
40+
@doc """
41+
Creates a user.
42+
43+
## Examples
44+
45+
iex> create_user(%{field: value})
46+
{:ok, %User{}}
47+
48+
iex> create_user(%{field: bad_value})
49+
{:error, %Ecto.Changeset{}}
50+
51+
"""
52+
def create_user(attrs \\ %{}) do
53+
%User{}
54+
|> User.changeset(attrs)
55+
|> Repo.insert()
56+
end
57+
58+
@doc """
59+
Updates a user.
60+
61+
## Examples
62+
63+
iex> update_user(user, %{field: new_value})
64+
{:ok, %User{}}
65+
66+
iex> update_user(user, %{field: bad_value})
67+
{:error, %Ecto.Changeset{}}
68+
69+
"""
70+
def update_user(%User{} = user, attrs) do
71+
user
72+
|> User.changeset(attrs)
73+
|> Repo.update()
74+
end
75+
76+
@doc """
77+
Deletes a user.
78+
79+
## Examples
80+
81+
iex> delete_user(user)
82+
{:ok, %User{}}
83+
84+
iex> delete_user(user)
85+
{:error, %Ecto.Changeset{}}
86+
87+
"""
88+
def delete_user(%User{} = user) do
89+
Repo.delete(user)
90+
end
91+
92+
@doc """
93+
Returns an `%Ecto.Changeset{}` for tracking user changes.
94+
95+
## Examples
96+
97+
iex> change_user(user)
98+
%Ecto.Changeset{data: %User{}}
99+
100+
"""
101+
def change_user(%User{} = user, attrs \\ %{}) do
102+
User.changeset(user, attrs)
103+
end
104+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule PhoenixApp.Accounts.User do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
schema "users" do
6+
field :name, :string
7+
field :age, :integer
8+
9+
timestamps(type: :utc_datetime)
10+
end
11+
12+
@doc false
13+
def changeset(user, attrs) do
14+
user
15+
|> cast(attrs, [:name, :age])
16+
|> validate_required([:name, :age])
17+
end
18+
end

0 commit comments

Comments
 (0)