Skip to content

Commit

Permalink
chore (sync service): replace span for decode_message by attribute …
Browse files Browse the repository at this point in the history
…on parent span (#2082)

This PR fixes #2078.
  • Loading branch information
kevin-dp authored Dec 3, 2024
1 parent 1f2a5ff commit bb27ee9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ defmodule Electric.Postgres.ReplicationClient do
end

defp process_x_log_data(data, wal_end, %State{} = state) do
data
|> decode_message()
OpenTelemetry.timed_fun("decode_message_duration", fn -> decode_message(data) end)
# # Useful for debugging:
# |> tap(fn %struct{} = msg ->
# message_type = struct |> to_string() |> String.split(".") |> List.last()
Expand Down Expand Up @@ -284,11 +283,7 @@ defmodule Electric.Postgres.ReplicationClient do
end

defp decode_message(data) do
OpenTelemetry.with_span(
"pg_txn.replication_client.decode_message",
[msg_size: byte_size(data)],
fn -> Decoder.decode(data) end
)
Decoder.decode(data)
end

defp encode_standby_status_update(state) do
Expand Down
12 changes: 12 additions & 0 deletions packages/sync-service/lib/electric/telemetry/open_telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule Electric.Telemetry.OpenTelemetry do
require OpenTelemetry.SemanticConventions.Trace

@typep span_name :: String.t()
@typep attr_name :: String.t()
@typep span_attrs :: :opentelemetry.attributes_map()
@typep span_ctx :: :opentelemetry.span_ctx()

Expand Down Expand Up @@ -69,6 +70,17 @@ defmodule Electric.Telemetry.OpenTelemetry do
end)
end

@doc """
Executes the provided function and records its duration in microseconds.
The duration is added to the current span as a span attribute named with the given `name`.
"""
@spec timed_fun(span_ctx() | nil, attr_name(), (-> term)) :: term
def timed_fun(span_ctx \\ nil, name, fun) when is_binary(name) do
{duration, result} = :timer.tc(fun)
add_span_attributes(span_ctx, %{name => duration})
result
end

@doc """
Wrap the given `fun` in an anonymous function, attaching the current process' span context to it.
Expand Down

0 comments on commit bb27ee9

Please sign in to comment.