Skip to content

Commit 8a7efaa

Browse files
committed
Setup opentelemetry_exporter
1 parent a787475 commit 8a7efaa

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

lib/sentry/application.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,9 @@ defmodule Sentry.Application do
5959
if config[:quantum][:cron][:enabled] do
6060
Sentry.Integrations.Quantum.Cron.attach_telemetry_handler()
6161
end
62+
63+
if config[:opentelemetry] do
64+
Sentry.Integrations.Opentelemetry.setup()
65+
end
6266
end
6367
end

lib/sentry/config.ex

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ defmodule Sentry.Config do
6262
]
6363
]
6464
]
65+
],
66+
opentelemetry: [
67+
type: :boolean,
68+
doc: """
69+
A boolean switch that sets up [OpenTelemetry](https://opentelemetry.io/docs/languages/erlang/)
70+
automatically to instrument your application and export the spans to Sentry.
71+
*Available since vTODO*.
72+
"""
6573
]
6674
]
6775

@@ -464,37 +472,43 @@ defmodule Sentry.Config do
464472
@spec dsn() :: nil | {String.t(), String.t(), String.t()}
465473
def dsn, do: get(:dsn)
466474

467-
@spec envelope_endpoint() :: String.t()
475+
@spec envelope_endpoint() :: nil | String.t()
468476
def envelope_endpoint do
469-
{base_uri, _, _} = dsn()
470-
base_uri <> "envelope/"
477+
case dsn() do
478+
{base_uri, _, _} -> base_uri <> "envelope/"
479+
_ -> nil
480+
end
471481
end
472482

473-
@spec spans_endpoint() :: String.t()
483+
@spec spans_endpoint() :: nil | String.t()
474484
def spans_endpoint do
475-
{base_uri, _, _} = dsn()
476-
base_uri <> "spans/"
485+
case dsn() do
486+
{base_uri, _, _} -> base_uri <> "spans/"
487+
_ -> nil
488+
end
477489
end
478490

479-
@spec auth_headers() :: [{String.t(), String.t()}]
491+
@spec auth_headers() :: nil | [{String.t(), String.t()}]
480492
def auth_headers do
481-
{_, public_key, secret_key} = dsn()
482-
483-
auth_query =
484-
[
485-
sentry_version: @sentry_version,
486-
sentry_client: @sentry_client,
487-
sentry_timestamp: System.system_time(:second),
488-
sentry_key: public_key,
489-
sentry_secret: secret_key
490-
]
491-
|> Enum.reject(fn {_, value} -> is_nil(value) end)
492-
|> Enum.map_join(", ", fn {name, value} -> "#{name}=#{value}" end)
493+
case dsn() do
494+
{_, public_key, secret_key} ->
495+
auth_query =
496+
[
497+
sentry_version: @sentry_version,
498+
sentry_client: @sentry_client,
499+
sentry_timestamp: System.system_time(:second),
500+
sentry_key: public_key,
501+
sentry_secret: secret_key
502+
]
503+
|> Enum.reject(fn {_, value} -> is_nil(value) end)
504+
|> Enum.map_join(", ", fn {name, value} -> "#{name}=#{value}" end)
493505

494-
[
495-
{"User-Agent", @sentry_client},
496-
{"X-Sentry-Auth", "Sentry " <> auth_query}
497-
]
506+
[
507+
{"User-Agent", @sentry_client},
508+
{"X-Sentry-Auth", "Sentry " <> auth_query}
509+
]
510+
_ -> nil
511+
end
498512
end
499513

500514
# TODO: remove me on v11.0.0, :included_environments has been deprecated
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule Sentry.Integrations.Opentelemetry do
2+
@moduledoc false
3+
4+
alias Sentry.Config
5+
6+
def setup do
7+
if Config.dsn() do
8+
Application.put_env(:opentelemetry, :traces_exporter, :otlp)
9+
Application.put_env(:opentelemetry_exporter, :otlp_protocol, :http_protobuf)
10+
Application.put_env(:opentelemetry_exporter, :otlp_traces_endpoint, Config.spans_endpoint())
11+
Application.put_env(:opentelemetry_exporter, :otlp_headers, Config.auth_headers())
12+
end
13+
14+
:ok
15+
end
16+
end

lib/sentry/transport.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ defmodule Sentry.Transport do
2020
when is_atom(client) and is_list(retries) do
2121
case Envelope.to_binary(envelope) do
2222
{:ok, body} ->
23-
{endpoint, headers} = {Config.envelope_endpoint(), Config.auth_headers()}
24-
post_envelope_with_retries(client, endpoint, headers, body, retries)
23+
post_envelope_with_retries(client, Config.envelope_endpoint(), Config.auth_headers(), body, retries)
2524

2625
{:error, reason} ->
2726
{:error, {:invalid_json, reason}}

0 commit comments

Comments
 (0)