diff --git a/instrumentation/opentelemetry_oban/config/config.exs b/instrumentation/opentelemetry_oban/config/config.exs new file mode 100644 index 00000000..bf8698cc --- /dev/null +++ b/instrumentation/opentelemetry_oban/config/config.exs @@ -0,0 +1,9 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +import Config + +try do + import_config "#{Mix.env()}.exs" +rescue + _ -> :ok +end diff --git a/instrumentation/opentelemetry_oban/config/test.exs b/instrumentation/opentelemetry_oban/config/test.exs new file mode 100644 index 00000000..a1eecdab --- /dev/null +++ b/instrumentation/opentelemetry_oban/config/test.exs @@ -0,0 +1,11 @@ +import Config + +config :opentelemetry_oban, + ecto_repos: [TestRepo] + +config :opentelemetry_oban, TestRepo, + hostname: "localhost", + username: "postgres", + password: "postgres", + database: "opentelemetry_oban_test", + pool: Ecto.Adapters.SQL.Sandbox diff --git a/instrumentation/opentelemetry_oban/docker-compose.yml b/instrumentation/opentelemetry_oban/docker-compose.yml deleted file mode 100644 index 10ef2418..00000000 --- a/instrumentation/opentelemetry_oban/docker-compose.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: "3.7" - -services: - postgres: - image: postgres:17.2 - environment: - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres - - POSTGRES_DB=opentelemetry_oban_test - ports: - - 5432:5432 diff --git a/instrumentation/opentelemetry_oban/mix.exs b/instrumentation/opentelemetry_oban/mix.exs index f043740b..b642e62c 100644 --- a/instrumentation/opentelemetry_oban/mix.exs +++ b/instrumentation/opentelemetry_oban/mix.exs @@ -10,13 +10,14 @@ defmodule OpentelemetryOban.MixProject do elixir: "~> 1.11", start_permanent: Mix.env() == :prod, deps: deps(), + aliases: aliases(), + elixirc_paths: elixirc_paths(Mix.env()), docs: [ source_url_pattern: "https://github.com/open-telemetry/opentelemetry-erlang-contrib/blob/main/instrumentation/opentelemetry_oban/%{path}#L%{line}", main: "OpentelemetryOban", extras: ["README.md"] ], - elixirc_paths: elixirc_paths(Mix.env()), package: [ name: "opentelemetry_oban", description: "OpenTelemetry tracing for Oban", @@ -41,7 +42,13 @@ defmodule OpentelemetryOban.MixProject do ] end - # Run "mix help deps" to learn about dependencies. + defp elixirc_paths(:test), do: ["lib", "test/support"] + defp elixirc_paths(_), do: ["lib"] + + defp aliases() do + [test: ["ecto.drop -q", "ecto.create -q", "ecto.migrate --quiet", "test"]] + end + defp deps do [ {:oban, "~> 2.0"}, @@ -55,7 +62,4 @@ defmodule OpentelemetryOban.MixProject do {:postgrex, ">= 0.0.0", only: [:dev, :test]} ] end - - defp elixirc_paths(:test), do: ["lib", "test/support"] - defp elixirc_paths(_), do: ["lib"] end diff --git a/instrumentation/opentelemetry_oban/priv/test_repo/migrations/1_setup_tables.exs b/instrumentation/opentelemetry_oban/priv/test_repo/migrations/1_setup_tables.exs new file mode 100644 index 00000000..2dee6050 --- /dev/null +++ b/instrumentation/opentelemetry_oban/priv/test_repo/migrations/1_setup_tables.exs @@ -0,0 +1,5 @@ +defmodule OpentelemetryOban.TestRepo.Migrations.SetupTables do + use Ecto.Migration + + def up, do: Oban.Migrations.up() +end diff --git a/instrumentation/opentelemetry_oban/test/support/jobs/test_job.ex b/instrumentation/opentelemetry_oban/test/support/jobs/test_job.ex new file mode 100644 index 00000000..3844dee7 --- /dev/null +++ b/instrumentation/opentelemetry_oban/test/support/jobs/test_job.ex @@ -0,0 +1,8 @@ +defmodule TestJob do + use Oban.Worker, queue: :events, max_attempts: 1 + + @impl Oban.Worker + def perform(_job) do + :ok + end +end diff --git a/instrumentation/opentelemetry_oban/test/support/jobs/test_job_that_returns_error.ex b/instrumentation/opentelemetry_oban/test/support/jobs/test_job_that_returns_error.ex new file mode 100644 index 00000000..a9e2074e --- /dev/null +++ b/instrumentation/opentelemetry_oban/test/support/jobs/test_job_that_returns_error.ex @@ -0,0 +1,8 @@ +defmodule TestJobThatReturnsError do + use Oban.Worker, queue: :events, max_attempts: 1 + + @impl Oban.Worker + def perform(_job) do + {:error, :something} + end +end diff --git a/instrumentation/opentelemetry_oban/test/support/jobs/test_job_that_throws_exception.ex b/instrumentation/opentelemetry_oban/test/support/jobs/test_job_that_throws_exception.ex new file mode 100644 index 00000000..d4eead15 --- /dev/null +++ b/instrumentation/opentelemetry_oban/test/support/jobs/test_job_that_throws_exception.ex @@ -0,0 +1,10 @@ +defmodule TestJobThatThrowsException do + use Oban.Worker, queue: :events, max_attempts: 1 + + @impl Oban.Worker + def perform(_job) do + raise %UndefinedFunctionError{ + message: "function Some.error/0 is undefined (module Some is not available)" + } + end +end diff --git a/instrumentation/opentelemetry_oban/test/support/jobs/test_job_with_inner_span.ex b/instrumentation/opentelemetry_oban/test/support/jobs/test_job_with_inner_span.ex new file mode 100644 index 00000000..2f4ab619 --- /dev/null +++ b/instrumentation/opentelemetry_oban/test/support/jobs/test_job_with_inner_span.ex @@ -0,0 +1,11 @@ +defmodule TestJobWithInnerSpan do + use Oban.Worker, queue: :events, max_attempts: 1 + require OpenTelemetry.Tracer + + @impl Oban.Worker + def perform(_job) do + OpenTelemetry.Tracer.with_span "span inside the job" do + :ok + end + end +end diff --git a/instrumentation/opentelemetry_oban/test/support/test_helpers.ex b/instrumentation/opentelemetry_oban/test/support/test_helpers.ex new file mode 100644 index 00000000..9d196f0f --- /dev/null +++ b/instrumentation/opentelemetry_oban/test/support/test_helpers.ex @@ -0,0 +1,7 @@ +defmodule TestHelpers do + def remove_oban_handlers() do + Enum.each(:telemetry.list_handlers([:oban]), fn handler -> + :telemetry.detach(handler[:id]) + end) + end +end diff --git a/instrumentation/opentelemetry_oban/test/test_helper.exs b/instrumentation/opentelemetry_oban/test/test_helper.exs index 3270d9cd..f695813b 100644 --- a/instrumentation/opentelemetry_oban/test/test_helper.exs +++ b/instrumentation/opentelemetry_oban/test/test_helper.exs @@ -1,75 +1,12 @@ -ExUnit.start(capture_log: true) +TestRepo.start_link() -TestRepo.start_link( - database: "opentelemetry_oban_test", - hostname: "localhost", - username: "postgres", - password: "postgres", - pool: Ecto.Adapters.SQL.Sandbox -) +ExUnit.start(capture_log: true) Ecto.Adapters.SQL.Sandbox.mode(TestRepo, {:shared, self()}) -defmodule PrepareOban do - use Ecto.Migration - def up, do: Oban.Migrations.up() -end - -Ecto.Migrator.run(TestRepo, [{0, PrepareOban}], :up, all: true) -TestRepo.query("TRUNCATE oban_jobs", []) - Oban.start_link( repo: TestRepo, plugins: [Oban.Plugins.Pruner], notifier: Oban.Notifiers.PG, testing: :manual ) - -defmodule TestJob do - use Oban.Worker, queue: :events, max_attempts: 1 - - @impl Oban.Worker - def perform(_job) do - :ok - end -end - -defmodule TestJobWithInnerSpan do - use Oban.Worker, queue: :events, max_attempts: 1 - require OpenTelemetry.Tracer - - @impl Oban.Worker - def perform(_job) do - OpenTelemetry.Tracer.with_span "span inside the job" do - :ok - end - end -end - -defmodule TestJobThatReturnsError do - use Oban.Worker, queue: :events, max_attempts: 1 - - @impl Oban.Worker - def perform(_job) do - {:error, :something} - end -end - -defmodule TestJobThatThrowsException do - use Oban.Worker, queue: :events, max_attempts: 1 - - @impl Oban.Worker - def perform(_job) do - raise %UndefinedFunctionError{ - message: "function Some.error/0 is undefined (module Some is not available)" - } - end -end - -defmodule TestHelpers do - def remove_oban_handlers() do - Enum.each(:telemetry.list_handlers([:oban]), fn handler -> - :telemetry.detach(handler[:id]) - end) - end -end