Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenTelemetry integration #843

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ if config_env() == :test do
config :logger, backends: []
end

config :opentelemetry, span_processor: {Sentry.OpenTelemetry.SpanProcessor, []}

config :opentelemetry,
sampler: {Sentry.OpenTelemetry.Sampler, [drop: ["Elixir.Oban.Stager process"]]}

config :phoenix, :json_library, Jason
22 changes: 22 additions & 0 deletions lib/sentry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,28 @@ defmodule Sentry do
end
end

def send_transaction(transaction, options \\ []) do
# TODO: remove on v11.0.0, :included_environments was deprecated in 10.0.0.
included_envs = Config.included_environments()

cond do
Config.test_mode?() ->
Client.send_transaction(transaction, options)

!Config.dsn() ->
# We still validate options even if we're not sending the event. This aims at catching
# configuration issues during development instead of only when deploying to production.
_options = NimbleOptions.validate!(options, Options.send_event_schema())
:ignored

included_envs == :all or to_string(Config.environment_name()) in included_envs ->
Client.send_transaction(transaction, options)

true ->
:ignored
end
end

@doc """
Captures a check-in built with the given `options`.

Expand Down
1 change: 1 addition & 0 deletions lib/sentry/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Sentry.Application do
Sentry.Sources,
Sentry.Dedupe,
Sentry.ClientReport.Sender,
Sentry.OpenTelemetry.SpanStorage,
{Sentry.Integrations.CheckInIDMappings,
[
max_expected_check_in_time:
Expand Down
74 changes: 72 additions & 2 deletions lib/sentry/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ defmodule Sentry.Client do
Event,
Interfaces,
LoggerUtils,
Transport,
Options
Options,
Transaction,
Transport
}

require Logger
Expand Down Expand Up @@ -107,6 +108,26 @@ defmodule Sentry.Client do
|> Transport.encode_and_post_envelope(client, request_retries)
end

def send_transaction(%Transaction{} = transaction, opts \\ []) do
opts = NimbleOptions.validate!(opts, Options.send_transaction_schema())

result_type = Keyword.get_lazy(opts, :result, &Config.send_result/0)
client = Keyword.get_lazy(opts, :client, &Config.client/0)

request_retries =
Keyword.get_lazy(opts, :request_retries, fn ->
Application.get_env(:sentry, :request_retries, Transport.default_retries())
end)

case encode_and_send(transaction, result_type, client, request_retries) do
{:ok, id} ->
{:ok, id}

{:error, %ClientError{} = error} ->
{:error, error}
end
end

defp sample_event(sample_rate) do
cond do
sample_rate == 1 -> :ok
Expand Down Expand Up @@ -205,6 +226,42 @@ defmodule Sentry.Client do
end
end

defp encode_and_send(
%Transaction{} = transaction,
_result_type = :sync,
client,
request_retries
) do
case Sentry.Test.maybe_collect(transaction) do
:collected ->
{:ok, ""}

:not_collecting ->
send_result =
transaction
|> Envelope.from_transaction()
|> Transport.encode_and_post_envelope(client, request_retries)

send_result
end
end

defp encode_and_send(
%Transaction{} = transaction,
_result_type = :none,
client,
_request_retries
) do
case Sentry.Test.maybe_collect(transaction) do
:collected ->
{:ok, ""}

:not_collecting ->
:ok = Transport.Sender.send_async(client, transaction)
{:ok, ""}
end
end

@spec render_event(Event.t()) :: map()
def render_event(%Event{} = event) do
json_library = Config.json_library()
Expand All @@ -225,6 +282,19 @@ defmodule Sentry.Client do
|> update_if_present(:threads, fn list -> Enum.map(list, &render_thread/1) end)
end

@spec render_transaction(%Transaction{}) :: map()
def render_transaction(%Transaction{} = transaction) do
transaction
|> Transaction.to_map()
|> Map.merge(%{
platform: "elixir",
sdk: %{
name: "sentry.elixir",
version: Application.spec(:sentry, :vsn)
}
})
end

defp render_exception(%Interfaces.Exception{} = exception) do
exception
|> Map.from_struct()
Expand Down
45 changes: 42 additions & 3 deletions lib/sentry/envelope.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ defmodule Sentry.Envelope do
@moduledoc false
# https://develop.sentry.dev/sdk/envelopes/

alias Sentry.{Attachment, CheckIn, ClientReport, Config, Event, UUID}
alias Sentry.{
Attachment,
CheckIn,
ClientReport,
Config,
Event,
Transaction,
UUID
}

@type t() :: %__MODULE__{
event_id: UUID.t(),
items: [Event.t() | Attachment.t() | CheckIn.t() | ClientReport.t(), ...]
items: [
Attachment.t() | CheckIn.t() | ClientReport.t() | Event.t() | Transaction.t()
]
}

@enforce_keys [:event_id, :items]
Expand Down Expand Up @@ -46,13 +56,31 @@ defmodule Sentry.Envelope do
}
end

@doc """
Creates a new envelope containing a transaction with spans.
"""
@spec from_transaction(Sentry.Transaction.t()) :: t()
def from_transaction(%Transaction{} = transaction) do
%__MODULE__{
event_id: transaction.event_id,
items: [transaction]
}
end

@doc """
Returns the "data category" of the envelope's contents (to be used in client reports and more).
"""
@doc since: "10.8.0"
@spec get_data_category(Attachment.t() | CheckIn.t() | ClientReport.t() | Event.t()) ::
@spec get_data_category(
Attachment.t()
| CheckIn.t()
| ClientReport.t()
| Event.t()
| Transaction.t()
) ::
String.t()
def get_data_category(%Attachment{}), do: "attachment"
def get_data_category(%Transaction{}), do: "transaction"
def get_data_category(%CheckIn{}), do: "monitor"
def get_data_category(%ClientReport{}), do: "internal"
def get_data_category(%Event{}), do: "error"
Expand Down Expand Up @@ -126,4 +154,15 @@ defmodule Sentry.Envelope do
throw(error)
end
end

defp item_to_binary(json_library, %Transaction{} = transaction) do
case transaction |> Sentry.Client.render_transaction() |> json_library.encode() do
{:ok, encoded_transaction} ->
header = ~s({"type": "transaction", "length": #{byte_size(encoded_transaction)}})
[header, ?\n, encoded_transaction, ?\n]

{:error, _reason} = error ->
throw(error)
end
end
end
27 changes: 27 additions & 0 deletions lib/sentry/opentelemetry/sampler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Sentry.OpenTelemetry.Sampler do
@moduledoc false

def setup(config) do
config
end

def description(_) do
"SentrySampler"
end

def should_sample(
_ctx,
_trace_id,
_links,
span_name,
_span_kind,
_attributes,
config
) do
if span_name in config[:drop] do
{:drop, [], []}
else
{:record_and_sample, [], []}
end
end
end
Loading
Loading