Skip to content

Commit 4c5f21d

Browse files
committed
Use open_telemetry_decorator
1 parent a362ae1 commit 4c5f21d

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

lib/nerves_hub/deployments/orchestrator.ex

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ defmodule NervesHub.Deployments.Orchestrator do
99
"""
1010

1111
use GenServer
12+
use OpenTelemetryDecorator
1213

1314
require Logger
1415

15-
require OpenTelemetry.Tracer, as: Tracer
16-
1716
alias NervesHub.Devices
1817
alias NervesHub.Devices.Device
1918
alias NervesHub.Repo
@@ -50,60 +49,59 @@ defmodule NervesHub.Deployments.Orchestrator do
5049
As devices update and reconnect, the new orchestrator is told that the update
5150
was successful, and the process is repeated.
5251
"""
52+
@decorate with_span("Deployments.Orchestrator.trigger_update")
5353
def trigger_update(deployment) do
54-
Tracer.with_span "NervesHub.Deployments.Orchestrator.trigger_update" do
55-
:telemetry.execute([:nerves_hub, :deployment, :trigger_update], %{count: 1})
56-
57-
match_conditions = [
58-
{:and, {:==, {:map_get, :deployment_id, :"$1"}, deployment.id},
59-
{:==, {:map_get, :updating, :"$1"}, false},
60-
{:==, {:map_get, :updates_enabled, :"$1"}, true},
61-
{:"/=", {:map_get, :firmware_uuid, :"$1"}, deployment.firmware.uuid}}
62-
]
63-
64-
match_return = %{
65-
device_id: {:element, 1, :"$_"},
66-
pid: {:element, 1, {:element, 2, :"$_"}},
67-
firmware_uuid: {:map_get, :firmware_uuid, {:element, 2, {:element, 2, :"$_"}}}
68-
}
69-
70-
devices =
71-
Registry.select(NervesHub.Devices.Registry, [
72-
{{:_, :_, :"$1"}, match_conditions, [match_return]}
73-
])
74-
75-
# Get a rough count of devices to update
76-
count = deployment.concurrent_updates - Devices.count_inflight_updates_for(deployment)
77-
# Just in case inflight goes higher than concurrent, limit it to 0
78-
count = max(count, 0)
79-
80-
# use a reduce to bounce out early?
81-
# limit the number of devices to 5 minutes / 500ms?
82-
83-
devices
84-
|> Enum.take(count)
85-
|> Enum.each(fn %{device_id: device_id, pid: pid} ->
86-
:telemetry.execute([:nerves_hub, :deployment, :trigger_update, :device], %{count: 1})
87-
88-
device = %Device{id: device_id}
89-
90-
# Check again because other nodes are processing at the same time
91-
if Devices.count_inflight_updates_for(deployment) < deployment.concurrent_updates do
92-
case Devices.told_to_update(device, deployment) do
93-
{:ok, inflight_update} ->
94-
send(pid, {"deployments/update", inflight_update})
95-
96-
:error ->
97-
Logger.error(
98-
"An inflight update could not be created or found for the device #{device.identifier} (#{device.id})"
99-
)
100-
end
54+
:telemetry.execute([:nerves_hub, :deployment, :trigger_update], %{count: 1})
55+
56+
match_conditions = [
57+
{:and, {:==, {:map_get, :deployment_id, :"$1"}, deployment.id},
58+
{:==, {:map_get, :updating, :"$1"}, false},
59+
{:==, {:map_get, :updates_enabled, :"$1"}, true},
60+
{:"/=", {:map_get, :firmware_uuid, :"$1"}, deployment.firmware.uuid}}
61+
]
62+
63+
match_return = %{
64+
device_id: {:element, 1, :"$_"},
65+
pid: {:element, 1, {:element, 2, :"$_"}},
66+
firmware_uuid: {:map_get, :firmware_uuid, {:element, 2, {:element, 2, :"$_"}}}
67+
}
68+
69+
devices =
70+
Registry.select(NervesHub.Devices.Registry, [
71+
{{:_, :_, :"$1"}, match_conditions, [match_return]}
72+
])
73+
74+
# Get a rough count of devices to update
75+
count = deployment.concurrent_updates - Devices.count_inflight_updates_for(deployment)
76+
# Just in case inflight goes higher than concurrent, limit it to 0
77+
count = max(count, 0)
78+
79+
# use a reduce to bounce out early?
80+
# limit the number of devices to 5 minutes / 500ms?
81+
82+
devices
83+
|> Enum.take(count)
84+
|> Enum.each(fn %{device_id: device_id, pid: pid} ->
85+
:telemetry.execute([:nerves_hub, :deployment, :trigger_update, :device], %{count: 1})
86+
87+
device = %Device{id: device_id}
88+
89+
# Check again because other nodes are processing at the same time
90+
if Devices.count_inflight_updates_for(deployment) < deployment.concurrent_updates do
91+
case Devices.told_to_update(device, deployment) do
92+
{:ok, inflight_update} ->
93+
send(pid, {"deployments/update", inflight_update})
94+
95+
:error ->
96+
Logger.error(
97+
"An inflight update could not be created or found for the device #{device.identifier} (#{device.id})"
98+
)
10199
end
100+
end
102101

103-
# Slow the update a bit to allow for concurrent nodes
104-
Process.sleep(500)
105-
end)
106-
end
102+
# Slow the update a bit to allow for concurrent nodes
103+
Process.sleep(500)
104+
end)
107105
end
108106

109107
def init(deployment) do

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ defmodule NervesHub.MixProject do
107107
branch: "update-obans-semantic-conventions",
108108
subdir: "instrumentation/opentelemetry_oban"},
109109
{:opentelemetry_bandit, "~> 0.2.0-rc.1"},
110+
{:open_telemetry_decorator, "~> 1.5"},
110111
{:phoenix, "~> 1.7.0"},
111112
{:phoenix_ecto, "~> 4.0"},
112113
{:phoenix_html, "~> 3.3.1", override: true},

mix.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
"nimble_ownership": {:hex, :nimble_ownership, "1.0.0", "3f87744d42c21b2042a0aa1d48c83c77e6dd9dd357e425a038dd4b49ba8b79a1", [:mix], [], "hexpm", "7c16cc74f4e952464220a73055b557a273e8b1b7ace8489ec9d86e9ad56cb2cc"},
5353
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
5454
"nimble_strftime": {:hex, :nimble_strftime, "0.1.1", "b988184d1bd945bc139b2c27dd00a6c0774ec94f6b0b580083abd62d5d07818b", [:mix], [], "hexpm", "89e599c9b8b4d1203b7bb5c79eb51ef7c6a28fbc6228230b312f8b796310d755"},
55+
"o11y": {:hex, :o11y, "0.2.6", "9386b36f9cc65ea838733b4838dea02334c4fb8721e53b904d02d50396df5cdc", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:opentelemetry_api, "~> 1.3", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "ea631c5ba03b155c1bc5849804da5d375538659446e4bb9388a0427959d122ef"},
5556
"oban": {:hex, :oban, "2.18.3", "1608c04f8856c108555c379f2f56bc0759149d35fa9d3b825cb8a6769f8ae926", [:mix], [{:ecto_sql, "~> 3.10", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ecto_sqlite3, "~> 0.9", [hex: :ecto_sqlite3, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "36ca6ca84ef6518f9c2c759ea88efd438a3c81d667ba23b02b062a0aa785475e"},
57+
"open_telemetry_decorator": {:hex, :open_telemetry_decorator, "1.5.7", "d0bb1942a3ea33e8fbf92ac44e084771a7dea076a559d974bf5b8ac11f76beaa", [:mix], [{:decorator, "~> 1.4", [hex: :decorator, repo: "hexpm", optional: false]}, {:o11y, "~> 0.2", [hex: :o11y, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.4", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "80566f68159c727c77986c617478a63c4e51256061baf52503d58d9e2316c6c4"},
5658
"opentelemetry": {:hex, :opentelemetry, "1.5.0", "7dda6551edfc3050ea4b0b40c0d2570423d6372b97e9c60793263ef62c53c3c2", [:rebar3], [{:opentelemetry_api, "~> 1.4", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "cdf4f51d17b592fc592b9a75f86a6f808c23044ba7cf7b9534debbcc5c23b0ee"},
5759
"opentelemetry_api": {:hex, :opentelemetry_api, "1.4.0", "63ca1742f92f00059298f478048dfb826f4b20d49534493d6919a0db39b6db04", [:mix, :rebar3], [], "hexpm", "3dfbbfaa2c2ed3121c5c483162836c4f9027def469c41578af5ef32589fcfc58"},
5860
"opentelemetry_bandit": {:hex, :opentelemetry_bandit, "0.2.0-rc.1", "3b78e4a472c20102af2d6852a04a890a8780de26feda560a63283045bc9088f5", [:mix], [{:nimble_options, "~> 1.1", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.3", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_semantic_conventions, "~> 1.27", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}, {:otel_http, "~> 0.2", [hex: :otel_http, repo: "hexpm", optional: false]}, {:plug, ">= 1.15.0", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9755c38e8b952204299f568cc30b5462851340c8b4b50702614ca403275b573a"},

0 commit comments

Comments
 (0)