Skip to content

Commit

Permalink
Add replication lag trace in milliseconds for every TX
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-dp committed Nov 26, 2024
1 parent f087dcb commit a133523
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/sync-service/lib/electric/shapes/consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ defmodule Electric.Shapes.Consumer do

notify_listeners(registry, :new_changes, shape_handle, last_log_offset)

report_replication_lag(txn)

{:cont, notify(txn, %{state | log_state: new_log_state})}

true ->
Expand Down Expand Up @@ -423,4 +425,21 @@ defmodule Electric.Shapes.Consumer do
"shape.where": shape.where
]
end

defp report_replication_lag(%Transaction{commit_timestamp: commit_timestamp}) do
# Compute time elapsed since commit
# since we are comparing PG's clock with our own
# there may be a slight skew so we make sure not to report negative lag.
# Since the lag is only useful when it becomes significant, a slight skew doesn't matter.
now = DateTime.utc_now()
lag = Kernel.max(0, DateTime.diff(now, commit_timestamp, :millisecond))

OpenTelemetry.with_span(
"shape_write.consumer.do_handle_txn.report_replication_lag",
[lag: lag],
fn ->
lag
end
)
end
end

0 comments on commit a133523

Please sign in to comment.