Skip to content

Commit

Permalink
feat: Add measurements for app deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-martinez committed Jul 12, 2024
1 parent 1ee023d commit ea066c8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
13 changes: 8 additions & 5 deletions apps/lenra/lib/lenra/apps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 <-
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions apps/lenra/lib/monitor.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ea066c8

Please sign in to comment.