Skip to content

Commit

Permalink
Allow to specify a default function to generate span's resource from …
Browse files Browse the repository at this point in the history
…a query

Instead of adding `telemetry_options` to every query, this allows to provide
a custom function to convert a query to span's resource.
It is useful to group queries by target table names, for example.
  • Loading branch information
sekiyama58 committed Aug 17, 2022
1 parent 882d64c commit 8415b3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ You can override the global configuration by passing overrides to `:telemetry.at

The following configuration options are supported:

| Option | Description | Default |
| ------------- | -------------------------------------------------------- | ------- |
| `tracer` | Tracer instance to use for reporting traces (_required_) | |
| `service` | Service name for Ecto traces | `ecto` |
| `truncate` | Maximum length of a query (excess will be truncated) | 5000 |
| `query_only?` | Whether to omit queue/run_query/decode timings | false |
| Option | Description | Default |
| -------------- | -------------------------------------------------------- | ------- |
| `tracer` | Tracer instance to use for reporting traces (_required_) | |
| `service` | Service name for Ecto traces | `ecto` |
| `truncate` | Maximum length of a query (excess will be truncated) | 5000 |
| `query_only?` | Whether to omit queue/run_query/decode timings | false |
| `resource_fun` | The function to convert a query to span's resource | & &1 |

### Ecto 2

Expand Down Expand Up @@ -109,3 +110,6 @@ of almost all of `Ecto.Repo`'s repository functions.
Repo.all(query, telemetry_options: [spandex_resource: "users-with-addresses"])
Repo.get!(User, id, telemetry_options: [spandex_resource: "get-user"])
```

Or, you can provide a function to to convert a query to the span's resource to the `resource_fun` option.
This is applied only the query doesn't have the `spandex_resource` telemetry option.
3 changes: 2 additions & 1 deletion lib/spandex_ecto/ecto_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule SpandexEcto.EctoLogger do
service = config[:service] || :ecto
truncate = config[:truncate] || 5000
query_only? = config[:query_only?]
resource_fun = config[:resource_fun] || & &1

if tracer.current_trace_id() do
now = :os.system_time(:nano_seconds)
Expand All @@ -32,7 +33,7 @@ defmodule SpandexEcto.EctoLogger do
|> String.slice(0, truncate)

num_rows = num_rows(log_entry)
resource = log_entry[:resource] || query
resource = log_entry[:resource] || resource_fun.(query)

queue_time = get_time(log_entry, :queue_time)
query_time = get_time(log_entry, :query_time)
Expand Down

0 comments on commit 8415b3c

Please sign in to comment.