Skip to content

Commit

Permalink
feat: better telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher2K committed Nov 10, 2024
1 parent 1a2f4f6 commit 00dbcca
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion swapify_api/lib/swapify_api/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule SwapifyApi.Application do

@impl true
def start(_type, _args) do
OpentelemetryPhoenix.setup()
OpentelemetryBandit.setup()
OpentelemetryPhoenix.setup(adapter: :bandit)
OpentelemetryEcto.setup([:swapify_api, :repo], db_statement: :enabled)
OpentelemetryOban.setup(trace: [:jobs])

Expand Down
6 changes: 6 additions & 0 deletions swapify_api/lib/swapify_api/plugs/authenticated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ defmodule SwapifyApi.Plugs.Authenticated do
@moduledoc """
Plug that protects authenticated endpoints
"""
use OpenTelemetryDecorator

alias SwapifyApi.Accounts.Token

import Plug.Conn

def init(default), do: default

@decorate with_span("plug.authenticated")
def call(%Plug.Conn{} = conn, _) do
case get_session(conn, :access_token) do
nil ->
O11y.set_attribute("user.id", nil)
halt_request(conn)

access_token ->
case Token.verify_and_validate(access_token) do
{:ok, %{"user_id" => user_id, "user_email" => user_email}} ->
O11y.set_attribute("user.id", user_id)

conn
|> assign(:user_id, user_id)
|> assign(:user_email, user_email)
Expand Down
1 change: 1 addition & 0 deletions swapify_api/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmodule SwapifyApi.MixProject do
{:open_telemetry_decorator, "~> 1.5"},
{:opentelemetry, "~> 1.5.0"},
{:opentelemetry_api, "~> 1.4.0"},
{:opentelemetry_bandit, "~> 0.1.4"},
{:opentelemetry_ecto, "~> 1.2.0"},
{:opentelemetry_exporter, "~> 1.8.0"},
{:opentelemetry_oban, "~> 1.1.1"},
Expand Down
1 change: 1 addition & 0 deletions swapify_api/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"open_telemetry_decorator": {:hex, :open_telemetry_decorator, "1.5.7", "d0bb1942a3ea33e8fbf92ac44e084771a7dea076a559d974bf5b8ac11f76beaa", [:mix], [{:decorator, "~> 1.4", [hex: :decorator, repo: "hexpm", optional: false]}, {:o11y, "~> 0.2", [hex: :o11y, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.4", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "80566f68159c727c77986c617478a63c4e51256061baf52503d58d9e2316c6c4"},
"opentelemetry": {:hex, :opentelemetry, "1.5.0", "7dda6551edfc3050ea4b0b40c0d2570423d6372b97e9c60793263ef62c53c3c2", [:rebar3], [{:opentelemetry_api, "~> 1.4", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "cdf4f51d17b592fc592b9a75f86a6f808c23044ba7cf7b9534debbcc5c23b0ee"},
"opentelemetry_api": {:hex, :opentelemetry_api, "1.4.0", "63ca1742f92f00059298f478048dfb826f4b20d49534493d6919a0db39b6db04", [:mix, :rebar3], [], "hexpm", "3dfbbfaa2c2ed3121c5c483162836c4f9027def469c41578af5ef32589fcfc58"},
"opentelemetry_bandit": {:hex, :opentelemetry_bandit, "0.1.4", "9ef3c36572fa779877ec0146b6c97854ced439650b8a7c2c6bbeb31dc54a69ce", [:mix], [{:opentelemetry_api, "~> 1.2", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_semantic_conventions, "~> 0.2", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}, {:opentelemetry_telemetry, "~> 1.0", [hex: :opentelemetry_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "17b620e3bce73ee206cd6417e699d243da0fd4fbf3cbcdae406e91436634294c"},
"opentelemetry_ecto": {:hex, :opentelemetry_ecto, "1.2.0", "2382cb47ddc231f953d3b8263ed029d87fbf217915a1da82f49159d122b64865", [:mix], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_process_propagator, "~> 0.2", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "70dfa2e79932e86f209df00e36c980b17a32f82d175f0068bf7ef9a96cf080cf"},
"opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.8.0", "5d546123230771ef4174e37bedfd77e3374913304cd6ea3ca82a2add49cd5d56", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.5.0", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.4.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.18", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "a1f9f271f8d3b02b81462a6bfef7075fd8457fdb06adff5d2537df5e2264d9af"},
"opentelemetry_oban": {:hex, :opentelemetry_oban, "1.1.1", "519e9ba60d3dc3483ad2df3fade131d47056e0dae74f0724c8a40b9718f089d1", [:mix], [{:oban, "~> 2.0", [hex: :oban, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.2", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_semantic_conventions, "~> 0.2", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}, {:opentelemetry_telemetry, "~> 1.1", [hex: :opentelemetry_telemetry, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ae6aed431626a94a4bb6bf5b268247ced687ec8f99eced6887e3754f9d3a2089"},
Expand Down

0 comments on commit 00dbcca

Please sign in to comment.