Skip to content

Commit

Permalink
2891 fix job editor metrics (#2893)
Browse files Browse the repository at this point in the history
* Allow Snapshot.Job when logging JobEditor metrics

* Refactor log regex generators

* Formatting

* Update CHANGELOG
  • Loading branch information
rorymckinley authored Feb 4, 2025
1 parent 21d36d1 commit bb5d716
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ and this project adheres to
- Standardizes sort order arrows (all now show the direction of sort, rather
than the direction they would sort if toggled)
[#2423](https://github.com/OpenFn/lightning/issues/2423)
- Allow JobEditor metrics to be tracked when the version of the workflow being
viewed is not the latest version.
[#2891](https://github.com/OpenFn/lightning/issues/2891)

## [v2.10.13] - 2025-01-29

Expand Down
21 changes: 20 additions & 1 deletion lib/lightning_web/ui_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ defmodule LightningWeb.UiMetrics do
A temporary measure to allow WorkflowEditor and JobEditor UI components to
report selected metrics and have these logged.
"""

alias Lightning.Repo
alias Lightning.Workflows.Job

require Logger

def log_job_editor_metrics(job, metrics) do
Expand All @@ -17,7 +21,22 @@ defmodule LightningWeb.UiMetrics do
end

defp enrich_job_editor_metric(metric, job) do
%{id: job_id, workflow_id: workflow_id} = job
{job_id, workflow_id} =
job
|> case do
%{id: job_id, workflow_id: workflow_id} ->
{job_id, workflow_id}

%{id: job_id} ->
Repo.get(Job, job_id)
|> case do
%{workflow_id: workflow_id} ->
{job_id, workflow_id}

nil ->
{job_id, "unknown"}
end
end

metric
|> common_enrichment()
Expand Down
83 changes: 77 additions & 6 deletions test/lightning_web/ui_metrics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ defmodule LightningWeb.UiMetricsTest do

job = insert(:job)

%{job: job, metrics: metrics}
snapshot_job = build(:snapshot_job, id: job.id)

%{job: job, metrics: metrics, snapshot_job: snapshot_job}
end

test "logs the job editor metrics provided", %{
job: job,
test "logs the job editor metrics when a job is provided", %{
job: %{id: job_id, workflow_id: workflow_id} = job,
metrics: metrics
} do
Mox.stub(Lightning.MockConfig, :ui_metrics_tracking_enabled?, fn ->
Expand All @@ -44,15 +46,17 @@ defmodule LightningWeb.UiMetricsTest do

expected_entry_1 =
job_editor_log_regex(
job: job,
job_id: job_id,
workflow_id: workflow_id,
event: "mount to 1st render",
start: 1_737_635_739_914,
end: 1_737_635_808_890
)

expected_entry_2 =
job_editor_log_regex(
job: job,
job_id: job_id,
workflow_id: workflow_id,
event: "render before fetching metadata",
start: 1_737_637_606_066,
end: 1_737_637_623_051
Expand All @@ -64,6 +68,72 @@ defmodule LightningWeb.UiMetricsTest do
assert capture_log(fun) =~ expected_entry_2
end

test "logs the job editor metrics when a snapshot job is provided", %{
job: %{id: job_id, workflow_id: workflow_id},
metrics: metrics,
snapshot_job: snapshot_job
} do
Mox.stub(Lightning.MockConfig, :ui_metrics_tracking_enabled?, fn ->
true
end)

expected_entry_1 =
job_editor_log_regex(
job_id: job_id,
workflow_id: workflow_id,
event: "mount to 1st render",
start: 1_737_635_739_914,
end: 1_737_635_808_890
)

expected_entry_2 =
job_editor_log_regex(
job_id: job_id,
workflow_id: workflow_id,
event: "render before fetching metadata",
start: 1_737_637_606_066,
end: 1_737_637_623_051
)

fun = fn -> UiMetrics.log_job_editor_metrics(snapshot_job, metrics) end

assert capture_log(fun) =~ expected_entry_1
assert capture_log(fun) =~ expected_entry_2
end

test "logs the metrics when the workflow for the snapshot job is missing", %{
metrics: metrics
} do
snapshot_job = build(:snapshot_job)

Mox.stub(Lightning.MockConfig, :ui_metrics_tracking_enabled?, fn ->
true
end)

expected_entry_1 =
job_editor_log_regex(
job_id: snapshot_job.id,
workflow_id: "unknown",
event: "mount to 1st render",
start: 1_737_635_739_914,
end: 1_737_635_808_890
)

expected_entry_2 =
job_editor_log_regex(
job_id: snapshot_job.id,
workflow_id: "unknown",
event: "render before fetching metadata",
start: 1_737_637_606_066,
end: 1_737_637_623_051
)

fun = fn -> UiMetrics.log_job_editor_metrics(snapshot_job, metrics) end

assert capture_log(fun) =~ expected_entry_1
assert capture_log(fun) =~ expected_entry_2
end

test "does not write metrics to the log if logging is disabled", %{
job: job,
metrics: metrics
Expand All @@ -78,7 +148,8 @@ defmodule LightningWeb.UiMetricsTest do
end

def job_editor_log_regex(opts \\ []) do
%{id: job_id, workflow_id: workflow_id} = Keyword.fetch!(opts, :job)
job_id = Keyword.fetch!(opts, :job_id)
workflow_id = Keyword.fetch!(opts, :workflow_id)

event = Keyword.fetch!(opts, :event)

Expand Down

0 comments on commit bb5d716

Please sign in to comment.