Skip to content

Commit

Permalink
chore: add sc to oban
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Dec 20, 2022
1 parent 7971f2f commit c733daf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions instrumentation/opentelemetry_ecto/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ config :opentelemetry_ecto, OpentelemetryEcto.TestRepo,
hostname: "localhost",
username: "postgres",
password: "postgres",
port: 5432,
database: "opentelemetry_ecto_test",
pool: Ecto.Adapters.SQL.Sandbox

Expand Down
37 changes: 27 additions & 10 deletions instrumentation/opentelemetry_ecto/lib/opentelemetry_ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ defmodule OpentelemetryEcto do
> represented as nested spans within a trace.
"""

alias OpenTelemetry.SemanticConventions.Trace

require Trace
require OpenTelemetry.Tracer

@doc """
Expand Down Expand Up @@ -49,7 +52,7 @@ defmodule OpentelemetryEcto do
def handle_event(
event,
measurements,
%{query: query, source: source, result: query_result, repo: repo, type: type},
%{query: query, source: source, result: query_result, repo: repo, type: type} = event_metadata,
config
) do
# Doing all this even if the span isn't sampled so the sampler
Expand All @@ -58,13 +61,22 @@ defmodule OpentelemetryEcto do
total_time = measurements.total_time
end_time = :opentelemetry.timestamp()
start_time = end_time - total_time
database = repo.config()[:database]
repo_config = repo.config()

dbg(%{
event_metadata: event_metadata,
config: config,
repo_config: repo_config
})

url =
case repo.config()[:url] do
nil ->
# TODO: add port
URI.to_string(%URI{scheme: "ecto", host: repo.config()[:hostname]})
URI.to_string(%URI{
scheme: "ecto",
host: repo_config[:hostname],
port: repo_config[:port]
})

url ->
url
Expand All @@ -88,12 +100,17 @@ defmodule OpentelemetryEcto do
# TODO: need connection information to complete the required attributes
# net.peer.name or net.peer.ip and net.peer.port
base_attributes = %{
"db.type": db_type,
"db.statement": query,
source: source,
"db.instance": database,
"db.url": url,
"total_time_#{time_unit}s": System.convert_time_unit(total_time, :native, time_unit)
Trace.db_user() => repo_config[:username],
Trace.db_system() => nil,
Trace.db_name() => nil,
Trace.db_operation() => nil,
Trace.db_sql_table() => nil,
Trace.db_statement() => query,
Trace.db_connection_string() => url,
:"db.type" => db_type,
:source => source,
:"db.instance" => repo_config[:database],
:"total_time_#{time_unit}s" => System.convert_time_unit(total_time, :native, time_unit)
}

attributes =
Expand Down
1 change: 1 addition & 0 deletions instrumentation/opentelemetry_ecto/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule OpentelemetryEcto.MixProject do
[
{:telemetry, "~> 0.4 or ~> 1.0"},
{:opentelemetry_api, "~> 1.0"},
{:opentelemetry_semantic_conventions, "~> 0.1.0"},
{:opentelemetry, "~> 1.0", only: [:dev, :test]},
{:opentelemetry_exporter, "~> 1.0", only: [:dev, :test]},
{:ex_doc, "~> 0.29", only: [:dev], runtime: false},
Expand Down
1 change: 1 addition & 0 deletions instrumentation/opentelemetry_ecto/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"opentelemetry_ecto": {:git, "https://github.com/bryannaegele/opentelemetry-erlang-contrib.git", "9730da9300cdd08138d3455deb270b06f3a440fa", [sparse: "propagators/opentelemetry_process_propagator", branch: "otel-process-walker"]},
"opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.2.2", "3966c56656627ef7db6c34c4ce28d44ac8629dcd065a310d7c33712fc2a1cfe3", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.1", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.1", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.11", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "5c11adeda19e0d203a04efe92cdd7a183da4b09ae8acaba7608e9303fa258e74"},
"opentelemetry_process_propagator": {:hex, :opentelemetry_process_propagator, "0.2.0", "d549f257fc7e20f0ff8674efb46f0dba1b3c5fe4ee6e4bd38d9873c7e18c6be1", [:mix, :rebar3], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "4062d3e982c5c7cd01b8b7189fc5c53dfacd666e47797483bc2310e5f0fa7f9c"},
"opentelemetry_semantic_conventions": {:hex, :opentelemetry_semantic_conventions, "0.1.0", "0a7463c0ec4541d9c8e09ee99bd7b5480fa6f90f40313a6d071fd7521b5a712f", [:mix, :rebar3], [], "hexpm", "eccf41bc7afec299484075aff570bf504a2f7e5c38fbcdaceb33a6d6b901448d"},
"postgrex": {:hex, :postgrex, "0.16.5", "fcc4035cc90e23933c5d69a9cd686e329469446ef7abba2cf70f08e2c4b69810", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "edead639dc6e882618c01d8fc891214c481ab9a3788dfe38dd5e37fd1d5fb2e8"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule OpentelemetryEctoTest do
"db.instance": "opentelemetry_ecto_test",
"db.statement": "SELECT u0.\"id\", u0.\"email\" FROM \"users\" AS u0",
"db.type": :sql,
"db.url": "ecto://localhost",
"db.connection_string": "ecto://localhost:5432",
decode_time_microseconds: _,
query_time_microseconds: _,
queue_time_microseconds: _,
Expand Down Expand Up @@ -84,7 +84,7 @@ defmodule OpentelemetryEctoTest do
"db.instance": "opentelemetry_ecto_test",
"db.statement": "SELECT p0.\"id\", p0.\"body\", p0.\"user_id\" FROM \"posts\" AS p0",
"db.type": :sql,
"db.url": "ecto://localhost",
"db.connection_string": "ecto://localhost:5432",
decode_time_milliseconds: _,
query_time_milliseconds: _,
queue_time_milliseconds: _,
Expand Down

0 comments on commit c733daf

Please sign in to comment.