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

Add options: query_only? and resource_fun #34

Open
wants to merge 2 commits 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
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +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 |
| 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 @@ -108,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.
50 changes: 27 additions & 23 deletions lib/spandex_ecto/ecto_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule SpandexEcto.EctoLogger do
tracer = config[:tracer] || raise "tracer is a required option for #{inspect(__MODULE__)}"
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 @@ -31,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 All @@ -58,34 +60,36 @@ defmodule SpandexEcto.EctoLogger do

report_error(tracer, log_entry)

if queue_time != 0 do
tracer.start_span("queue")
tracer.update_span(service: service, start: start, completion_time: start + queue_time)
tracer.finish_span()
end
unless query_only? do
if queue_time != 0 do
tracer.start_span("queue")
tracer.update_span(service: service, start: start, completion_time: start + queue_time)
tracer.finish_span()
end

if query_time != 0 do
tracer.start_span("run_query")
if query_time != 0 do
tracer.start_span("run_query")

tracer.update_span(
service: service,
start: start + queue_time,
completion_time: start + queue_time + query_time
)
tracer.update_span(
service: service,
start: start + queue_time,
completion_time: start + queue_time + query_time
)

tracer.finish_span()
end
tracer.finish_span()
end

if decoding_time != 0 do
tracer.start_span("decode")
if decoding_time != 0 do
tracer.start_span("decode")

tracer.update_span(
service: service,
start: start + queue_time + query_time,
completion_time: now
)
tracer.update_span(
service: service,
start: start + queue_time + query_time,
completion_time: now
)

tracer.finish_span()
tracer.finish_span()
end
end

tracer.finish_span()
Expand Down