Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for dataloader kv sources #424

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,30 @@ defmodule OpentelemetryDataloader do
OpenTelemetry.Ctx.attach(parent_ctx)
end

{batch_name, _batch_args} = metadata.batch

attributes = %{
"dataloader.batch_key" => inspect(batch_name)
}
attributes =
case metadata.batch do
# Ecto source
{batch_name, _batch_args} ->
%{
"dataloader.batch_key" => inspect(batch_name)
}

# KV source with atom key
batch_name when is_atom(batch_name) ->
%{
"dataloader.batch_key" => to_string(batch_name)
}

# KV source with binary key
batch_name when is_binary(batch_name) ->
%{
"dataloader.batch_key" => batch_name
}

# Unknown source type
_ ->
%{}
end

OpentelemetryTelemetry.start_telemetry_span(
config.tracer_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,98 @@ defmodule OpentelemetryDataloaderTest do
assert %{"dataloader.batch_key" => key} = :otel_attributes.map(attributes)
assert key =~ ~r/OpentelemetryDataloader.TestModels.Post/
end

test "captures dataloader kv source events with atom keys" do
OpentelemetryDataloader.setup()

loader =
Dataloader.new()
|> Dataloader.add_source(:kv, Dataloader.KV.new(&kv_query/2))
|> Dataloader.load(:kv, :test_maps, 1)
|> Dataloader.load_many(:kv, :test_maps, [4, 9])

Dataloader.run(loader)

assert_receive {:span,
span(
name: "dataloader.run",
attributes: attributes,
kind: :client
)}

assert %{} = :otel_attributes.map(attributes)

assert_receive {:span,
span(
name: "dataloader.batch",
attributes: attributes,
kind: :client
)}

assert %{"dataloader.batch_key" => "test_maps"} = :otel_attributes.map(attributes)
end

test "captures dataloader kv source events with modules" do
OpentelemetryDataloader.setup()

loader =
Dataloader.new()
|> Dataloader.add_source(:kv, Dataloader.KV.new(&kv_query/2))
|> Dataloader.load(:kv, TestModule, 1)
|> Dataloader.load_many(:kv, TestModule, [1, 3])

Dataloader.run(loader)

assert_receive {:span,
span(
name: "dataloader.run",
attributes: attributes,
kind: :client
)}

assert %{} = :otel_attributes.map(attributes)

assert_receive {:span,
span(
name: "dataloader.batch",
attributes: attributes,
kind: :client
)}

assert %{"dataloader.batch_key" => "Elixir.TestModule"} = :otel_attributes.map(attributes)
end

test "captures dataloader kv source events with binary names" do
OpentelemetryDataloader.setup()

loader =
Dataloader.new()
|> Dataloader.add_source(:kv, Dataloader.KV.new(&kv_query/2))
|> Dataloader.load(:kv, "test_keys", 1)
|> Dataloader.load_many(:kv, "test_keys", [1, 3])

Dataloader.run(loader)

assert_receive {:span,
span(
name: "dataloader.run",
attributes: attributes,
kind: :client
)}

assert %{} = :otel_attributes.map(attributes)

assert_receive {:span,
span(
name: "dataloader.batch",
attributes: attributes,
kind: :client
)}

assert %{"dataloader.batch_key" => "test_keys"} = :otel_attributes.map(attributes)
end

defp kv_query(:test_maps, ids), do: Enum.map(ids, &%{id: &1})
defp kv_query(TestModule, ids), do: Enum.map(ids, &%{id: &1})
defp kv_query("test_keys", ids), do: Enum.map(ids, & &1)
end
Loading