From ea066c8742104351e279b731c13779851c2a8036 Mon Sep 17 00:00:00 2001 From: "jonas.martinez" Date: Fri, 12 Jul 2024 15:12:35 +0200 Subject: [PATCH] feat: Add measurements for app deployment --- apps/lenra/lib/lenra/apps.ex | 13 +++++++---- apps/lenra/lib/monitor.ex | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 apps/lenra/lib/monitor.ex diff --git a/apps/lenra/lib/lenra/apps.ex b/apps/lenra/lib/lenra/apps.ex index 70599a00..bfbcee49 100644 --- a/apps/lenra/lib/lenra/apps.ex +++ b/apps/lenra/lib/lenra/apps.ex @@ -334,6 +334,8 @@ defmodule Lenra.Apps do end def deploy_in_main_env(%Build{} = build) do + Lenra.Telemetry.start(:app_deployment, build.application_id) + with loaded_build <- Repo.preload(build, :application), loaded_app <- Repo.preload(loaded_build.application, main_env: [:environment]), %Deployment{} = deployment <- @@ -405,24 +407,25 @@ defmodule Lenra.Apps do |> Repo.transaction() ApplicationServices.stop_app("#{OpenfaasServices.get_function_name(service_name, build_number)}") + Lenra.Telemetry.stop(:app_deployment, deployment.application_id) transaction - # Function not found in openfaas, 2 retry (10s), - # To let openfaas deploy in case of overload, after 2 retry -> failure + # Function not found in openfaas, 30 retry (15s), + # To let openfaas deploy in case of overload, after 30 retry -> failure :error404 -> - if retry == 3 do + if retry == 30 do Logger.critical("Function #{service_name} not deploy on openfaas, this should not appens") update_deployement(deployment, status: :failure) else - Process.sleep(5000) + Process.sleep(500) update_deployement_after_deploy(deployment, env, service_name, build_number, retry + 1) end :error500 _any -> - Process.sleep(5000) + Process.sleep(500) update_deployement_after_deploy(deployment, env, service_name, build_number, retry + 1) end end diff --git a/apps/lenra/lib/monitor.ex b/apps/lenra/lib/monitor.ex new file mode 100644 index 00000000..c9e15d27 --- /dev/null +++ b/apps/lenra/lib/monitor.ex @@ -0,0 +1,45 @@ +defmodule Lenra.Monitor do + @moduledoc """ + This module is monitoring requests at different places + Lenra's monitor executes the following events: + * `[:lenra, :app_deployment, :start]` - Executed when the app's deployment is triggered. + #### Measurements + * start_time. + #### Metadata + * `:application_id` - The id of the application which is deploying. + * `[:lenra, :app_deployment, :stop]` - Executed when the `available_replicas` parameter is not 0 or null on OpenFaaS. + #### Measurements + * end_time. + * `:duration` - The time took by the openfaas function in `:native` unit of time. + """ + + alias Lenra.Repo + + def setup do + events = [ + [:lenra, :app_deployment, :start], + [:lenra, :app_deployment, :stop], + ] + + :telemetry.attach_many( + "lenra.monitor", + events, + &Lenra.Monitor.handle_event/4, + nil + ) + end + + def handle_event([:lenra, :app_deployment, :start], measurements, metadata, _config) do + application_id = Map.get(metadata, :application_id) + + Repo.insert(ApplicationDeploymentMeasurement.new(application_id, measurements)) + end + + def handle_event([:lenra, :app_deployment, :stop], measurements, metadata, _config) do + application_id = Map.get(metadata, :application_id) + + Repo.get_by!(ApplicationDeploymentMeasurement, uuid: application_id) + |> ApplicationDeploymentMeasurement.update(measurements) + |> Repo.update() + end +end