Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only start os_mon when agent enabled #473

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/new_relic/enabled_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ defmodule NewRelic.EnabledSupervisor do
NewRelic.Aggregate.Supervisor
]

NewRelic.OsMon.start()

Supervisor.init(children, strategy: :one_for_one)
end
end
29 changes: 29 additions & 0 deletions lib/new_relic/os_mon.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule NewRelic.OsMon do
def start() do
Application.ensure_all_started(:os_mon)
:persistent_term.put(__MODULE__, true)
end

@mb 1024 * 1024
def get_system_memory() do
when_enabled(fn ->
case :memsup.get_system_memory_data()[:system_total_memory] do
nil -> nil
bytes -> trunc(bytes / @mb)
end
end)
end

def util() do
when_enabled(fn ->
:cpu_sup.util()
end)
end

defp when_enabled(fun, default \\ nil) do
case :persistent_term.get(__MODULE__, false) do
true -> fun.()
false -> default
end
end
end
4 changes: 2 additions & 2 deletions lib/new_relic/sampler/beam.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
:erlang.system_flag(:scheduler_wall_time, true)

# throw away first value
:cpu_sup.util()
NewRelic.OsMon.util()
:erlang.statistics(:scheduler_wall_time)

NewRelic.sample_process()
Expand Down Expand Up @@ -80,7 +80,7 @@
schedulers: :erlang.system_info(:schedulers),
scheduler_utilization: :erlang.statistics(:scheduler_wall_time) |> Enum.sort(),
cpu_count: :erlang.system_info(:logical_processors),
cpu_utilization: :cpu_sup.util()
cpu_utilization: NewRelic.OsMon.util()
}
end

Expand All @@ -107,6 +107,6 @@
end

defp safe_div(_, +0.0), do: 0.0
defp safe_div(_, -0.0), do: 0.0

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Unit Tests - Elixir 1.15 / OTP 26

this clause cannot match because a previous clause at line 109 always matches

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Unit Tests - Elixir 1.15 / OTP 26

this clause cannot match because a previous clause at line 109 always matches

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Unit Tests - Elixir 1.15 / OTP 26

this clause cannot match because a previous clause at line 109 always matches

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Unit Tests - Elixir 1.14 / OTP 25

this clause cannot match because a previous clause at line 109 always matches

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Integration Tests - Elixir 1.15 / OTP 26

this clause cannot match because a previous clause at line 109 always matches

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Integration Tests - Elixir 1.15 / OTP 26

this clause cannot match because a previous clause at line 109 always matches

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Integration Tests - Elixir 1.15 / OTP 26

this clause cannot match because a previous clause at line 109 always matches

Check warning on line 110 in lib/new_relic/sampler/beam.ex

View workflow job for this annotation

GitHub Actions / Integration Tests - Elixir 1.15 / OTP 26

this clause cannot match because a previous clause at line 109 always matches
defp safe_div(a, b), do: a / b
end
10 changes: 1 addition & 9 deletions lib/new_relic/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ defmodule NewRelic.Util do
%{
metadata_version: 5,
logical_processors: :erlang.system_info(:logical_processors),
total_ram_mib: get_system_memory(),
total_ram_mib: NewRelic.OsMon.get_system_memory(),
hostname: hostname()
}
|> maybe_add_ip_addresses
Expand Down Expand Up @@ -167,14 +167,6 @@ defmodule NewRelic.Util do
end
end

@mb 1024 * 1024
defp get_system_memory() do
case :memsup.get_system_memory_data()[:system_total_memory] do
nil -> nil
bytes -> trunc(bytes / @mb)
end
end

defp get_hostname do
with {:ok, name} <- :inet.gethostname(), do: to_string(name)
end
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule NewRelic.Mixfile do

def application do
[
extra_applications: [:logger, :inets, :ssl, :os_mon],
extra_applications: [:logger, :inets, :ssl],
included_applications: [:os_mon],
mod: {NewRelic.Application, []}
]
end
Expand Down
Loading