opentelemetry_ecto - add possibility to setup additional attributes. (#94)

* Make possible to include additional attributes to the span through setup

* Adjust code review changes
This commit is contained in:
cevado 2022-09-02 19:47:34 +00:00 committed by GitHub
parent d5bae4d624
commit a9e6fa0c84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -36,6 +36,9 @@ defmodule OpentelemetryEcto do
defaults to the concatenation of the event name with periods, e.g. defaults to the concatenation of the event name with periods, e.g.
`"blog.repo.query"`. This will always be followed with a colon and the `"blog.repo.query"`. This will always be followed with a colon and the
source (the table name for SQL adapters). source (the table name for SQL adapters).
* `:additional_attributes` - additional attributes to include in the span. If there
are conflits with default provided attributes, the ones provided with
this config will have precedence.
""" """
def setup(event_prefix, config \\ []) do def setup(event_prefix, config \\ []) do
event = event_prefix ++ [:query] event = event_prefix ++ [:query]
@ -74,6 +77,7 @@ defmodule OpentelemetryEcto do
end <> if source != nil, do: ":#{source}", else: "" end <> if source != nil, do: ":#{source}", else: ""
time_unit = Keyword.get(config, :time_unit, :microsecond) time_unit = Keyword.get(config, :time_unit, :microsecond)
additional_attributes = Keyword.get(config, :additional_attributes, %{})
db_type = db_type =
case type do case type do
@ -101,6 +105,8 @@ defmodule OpentelemetryEcto do
_, acc -> _, acc ->
acc acc
end) end)
|> Map.merge(base_attributes)
|> Map.merge(additional_attributes)
parent_context = OpentelemetryProcessPropagator.fetch_parent_ctx(1, :"$callers") parent_context = OpentelemetryProcessPropagator.fetch_parent_ctx(1, :"$callers")
@ -111,7 +117,7 @@ defmodule OpentelemetryEcto do
s = s =
OpenTelemetry.Tracer.start_span(span_name, %{ OpenTelemetry.Tracer.start_span(span_name, %{
start_time: start_time, start_time: start_time,
attributes: Map.merge(attributes, base_attributes), attributes: attributes,
kind: :client kind: :client
}) })

View File

@ -61,6 +61,14 @@ defmodule OpentelemetryEctoTest do
} = :otel_attributes.map(attributes) } = :otel_attributes.map(attributes)
end end
test "include additionaL_attributes" do
attach_handler(additional_attributes: %{"config.attribute": "special value", "db.instance": "my_instance"})
Repo.all(User)
assert_receive {:span, span(attributes: attributes)}
assert %{"config.attribute": "special value", "db.instance": "my_instance"} = :otel_attributes.map(attributes)
end
test "changes the time unit" do test "changes the time unit" do
attach_handler(time_unit: :millisecond) attach_handler(time_unit: :millisecond)