Skip to content

Commit

Permalink
Refactor OpentelemetryOban test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Jan 9, 2025
1 parent b6c577b commit e43906e
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 81 deletions.
9 changes: 9 additions & 0 deletions instrumentation/opentelemetry_oban/config/config.exs
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions instrumentation/opentelemetry_oban/config/test.exs
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions instrumentation/opentelemetry_oban/docker-compose.yml

This file was deleted.

14 changes: 9 additions & 5 deletions instrumentation/opentelemetry_oban/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"},
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule OpentelemetryOban.TestRepo.Migrations.SetupTables do
use Ecto.Migration

def up, do: Oban.Migrations.up()
end
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
67 changes: 2 additions & 65 deletions instrumentation/opentelemetry_oban/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e43906e

Please sign in to comment.