-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea066c8
commit 072436a
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
apps/lenra/lib/monitor/application_deployment_measurement.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Lenra.Monitor.ApplicationDeploymentMeasurement do | ||
@moduledoc """ | ||
Lenra.Monitor.ApplicationDeploymentMeasurement is a ecto schema to store measurements of applications deployment. | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias Lenra.Apps.App | ||
|
||
schema "session_measurement" do | ||
belongs_to(:application, App) | ||
|
||
field(:start_time, :utc_datetime) | ||
field(:end_time, :utc_datetime) | ||
|
||
field(:duration, :integer) | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(application_deployment_measurement, params \\ %{}) do | ||
application_deployment_measurement | ||
|> cast(params, [:start_time, :end_time, :duration]) | ||
|> validate_required([:start_time, :application_id]) | ||
|> foreign_key_constraint(:application_id) | ||
end | ||
|
||
def new(application_id, params \\ %{}) do | ||
%__MODULE__{application_id: application_id} | ||
|> __MODULE__.changeset(params) | ||
end | ||
|
||
def update(application_deployment_measurement, params) do | ||
application_deployment_measurement | ||
|> changeset(params) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Lenra.Monitor.ApplicationDeploymentMonitor do | ||
@moduledoc """ | ||
The application deployment monitor which monitors the time spent deploying an app. | ||
""" | ||
|
||
use GenServer | ||
use SwarmNamed | ||
|
||
alias Lenra.Telemetry | ||
|
||
require Logger | ||
|
||
def monitor(pid, metadata) do | ||
GenServer.call(__MODULE__, {:monitor, pid, metadata}) | ||
rescue | ||
e -> | ||
Logger.error("#{__MODULE__} fail in monitor with metadata #{inspect(metadata)} and error: #{inspect(e)}") | ||
end | ||
|
||
def start_link(_opts) do | ||
Logger.debug("Start #{__MODULE__}") | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
def init(_) do | ||
{:ok, %{}} | ||
end | ||
|
||
def handle_call({:monitor, pid, metadata}, _from, state) do | ||
Logger.debug("#{__MODULE__} monitor #{inspect(pid)} with metadata #{inspect(metadata)}") | ||
|
||
Process.monitor(pid) | ||
|
||
start_time = Map.get(metadata, :start_time) | ||
|
||
{:reply, :ok, Map.put(state, pid, {start_time, metadata})} | ||
end | ||
|
||
def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do | ||
{{start_time, metadata}, new_state} = Map.pop(state, pid) | ||
|
||
Logger.debug("#{__MODULE__} handle down #{inspect(pid)} with metadata #{inspect(metadata)}") | ||
|
||
Telemetry.stop(:app_deployment, start_time, metadata) | ||
|
||
{:noreply, new_state} | ||
end | ||
end |