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

Allow passing custom tags through telemetry_options #32

Open
wants to merge 1 commit into
base: master
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ config :my_app, MyApp.Repo,
]
```

## Customizing Span Resources
## Customizing Span Resources and Tags

By default, SpandexEcto uses the query as name for the span's resource. In
order get a better feeling for the context of your spans, you can label your
span's resources using the option [`:telemetry_options`](https://hexdocs.pm/ecto/Ecto.Repo.html#module-shared-options)
span's resources and tags using the option [`:telemetry_options`](https://hexdocs.pm/ecto/Ecto.Repo.html#module-shared-options)
of almost all of `Ecto.Repo`'s repository functions.

### Examples

```elixir
Repo.all(query, telemetry_options: [spandex_resource: "users-with-addresses"])
Repo.get!(User, id, telemetry_options: [spandex_resource: "get-user"])
Repo.get!(User, id, telemetry_options: [spandex_resource: "get-user", spandex_tags: [foo: "bar"]])
```
7 changes: 4 additions & 3 deletions lib/spandex_ecto/ecto_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,17 @@ defmodule SpandexEcto.EctoLogger do
defp to_nanoseconds(time) when is_integer(time), do: System.convert_time_unit(time, :native, :nanosecond)
defp to_nanoseconds(_time), do: 0

defp tags(%{params: params}) when is_list(params) do
defp tags(%{params: params, tags: tags}) when is_list(params) do
param_count =
params
|> Enum.count()
|> to_string()

[
Keyword.merge(tags,
param_count: param_count
]
)
end

defp tags(%{tags: tags}) when is_list(tags), do: tags
defp tags(_), do: []
end
3 changes: 2 additions & 1 deletion lib/spandex_ecto/telemetry_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule SpandexEcto.TelemetryAdapter do
decode_time: Map.get(measurements, :decode_time, 0),
queue_time: Map.get(measurements, :queue_time, 0),
result: wrap_result(metadata.result),
resource: get_in(metadata, [:options, :spandex_resource])
resource: get_in(metadata, [:options, :spandex_resource]),
tags: get_in(metadata, [:options, :spandex_tags]) || []
}

EctoLogger.trace(log_entry, "#{repo_name}_database", config)
Expand Down