Permalink
Browse files

Move core read-only stats from riak_kv to riak_core

  • Loading branch information...
russelldb committed Apr 30, 2012
1 parent 519c80b commit 53e9b15aa54060de2cf45e73a869c51c3c56f17f
View
@@ -40,10 +40,15 @@
riak_core_metric,
riak_core_metric_counter,
riak_core_metric_duration,
+ riak_core_metric_erl,
+ riak_core_metric_env,
riak_core_metric_histogram,
+ riak_core_metric_host,
riak_core_metric_meter,
riak_core_metric_proc,
+ riak_core_metric_ring,
riak_core_metric_sup,
+ riak_core_metric_vnodeq,
riak_core_nodeid,
riak_core_node_watcher,
riak_core_node_watcher_events,
View
@@ -29,19 +29,6 @@
-include_lib("eunit/include/eunit.hrl").
-endif.
--export_type([stat_specs/0]).
-
--type stat_specs() :: [stat()].
-
--type stat() :: {Name :: atom(),
- Args :: [type() | group() | presentation()]
- }.
-
--type type() :: {type, counter | meter | histogram | duration}.
--type group() :: {group, GroupName::atom()}.
--type presentation() :: {presentation, [{Name :: atom(),
- Spec :: riak_core_metric_duration:display_spec() |
- riak_core_metric_histogram:display_spec()}]}.
behaviour_info(callbacks) ->
[{new, 0},
{value, 3},
@@ -0,0 +1,47 @@
+%%
+%% Copyright (c) 2007-2012 Basho Technologies, Inc. All Rights Reserved.
+%%
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+%% -------------------------------------------------------------------
+
+%% TODO provide access to app envs by app/name
+-module(riak_core_metric_env).
+
+-behaviour(riak_core_metric).
+
+-export([new/0, value/3, value/4, update/2, options/1]).
+
+new() ->
+ env.
+
+options(env) ->
+ [{App, [K || {K, _V} <- app_helper:get_env(App)]} || {App, _Mod} <- [{riak_core, vnode}|riak_core:vnode_modules()]].
+
+update(_Arg, env) ->
+ env.
+
+value(Level, _Name, env) when Level < 3 ->
+ [];
+value(_Level, _Name, env) ->
+ [{App, [E || E <- app_helper:get_env(App)]} || {App, _Mod} <- [{riak_core, vnode}|riak_core:vnode_modules()]].
+
+value(_Level, App, _Name, env) when is_atom(App)->
+ app_helper:get_env(App);
+value(_Level, [App, Var], _Name, env) ->
+ {riak_core_metric:join_as_atom([App, '_', Var]), app_helper:get_env(App, Var)}.
+
+
+
@@ -0,0 +1,105 @@
+%%
+%% Copyright (c) 2007-2012 Basho Technologies, Inc. All Rights Reserved.
+%%
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+%% -------------------------------------------------------------------
+
+%% @doc Read only stat for cpu
+-module(riak_core_metric_erl).
+
+-behaviour(riak_core_metric).
+
+-export([new/0, value/3, value/4, update/2, options/1]).
+
+new() ->
+ erl.
+
+options(erl) ->
+ [{memory, [K || {K, _} <- erlang:memory()]},
+ {app, [A || {A,_, _} <- application:which_applications()]},
+ {system, system(1)}].
+
+update(_Arg, erl) ->
+ erl.
+
+value(Level, _Name, erl) ->
+ lists:flatten([value(Field, Level) || Field <- [memory, app, system]]).
+
+value(Level, Field, _Name, erl) when is_atom(Field) ->
+ value(Field, Level);
+value(_Level, [app, Elem], _Name, erl) ->
+ Stat = value(app, 5),
+ ElemName = riak_core_metric:join_as_atom([Elem, '_', version]),
+ proplists:lookup(ElemName, Stat);
+value(_Level, [system, Elem], _Name, erl) ->
+ system_info(Elem);
+value(_Level, [Field, Elem], _Name, erl) ->
+ Stat = value(Field, 5),
+ ElemName = riak_core_metric:join_as_atom([Field, '_', Elem]),
+ proplists:lookup(ElemName, Stat).
+
+value(memory, 0) ->
+ [{list_to_atom("memory_" ++ atom_to_list(K)), V} || {K,V} <- erlang:memory(),
+ lists:member(K, memory(0))];
+value(memory, _Level) ->
+ [{list_to_atom("memory_" ++ atom_to_list(K)), V} || {K,V} <- erlang:memory()];
+value(app, Level) when Level < 2 ->
+ [];
+value(app, Level) when Level > 1 ->
+ [{list_to_atom(atom_to_list(A) ++ "_version"), list_to_binary(V)}
+ || {A,_,V} <- application:which_applications()];
+value(system, Level) ->
+ [system_info(Field) || Field <- system(Level)].
+
+system_info(nodename) ->
+ {nodename, node()};
+system_info(connected_nodes) ->
+ {connected_nodes, nodes()};
+system_info(system_version) ->
+ {sys_system_version, list_to_binary(string:strip(erlang:system_info(system_version), right, $\n))};
+system_info(threads_enabled) ->
+ {sys_threads_enabled, erlang:system_info(threads)};
+system_info(Field) ->
+ Key = riak_core_metric:join_as_atom([sys, '_', Field]),
+ Val = maybe_to_binary(erlang:system_info(Field)),
+ {Key, Val}.
+
+maybe_to_binary(SysInfo) when is_list(SysInfo) ->
+ list_to_binary(SysInfo);
+maybe_to_binary(SysInfo) ->
+ SysInfo.
+
+memory(0) ->
+ [total, processes].
+
+system(0) ->
+ [nodename, connected_nodes];
+system(_Level) ->
+ system(0) ++ system_info().
+
+system_info() ->
+ [driver_version,
+ global_heaps_size,
+ heap_type,
+ logical_processors,
+ otp_release,
+ process_count,
+ smp_support,
+ system_version,
+ system_architecture,
+ threads_enabled,
+ thread_pool_size,
+ wordsize].
@@ -0,0 +1,61 @@
+%%
+%% Copyright (c) 2007-2012 Basho Technologies, Inc. All Rights Reserved.
+%%
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+%% -------------------------------------------------------------------
+
+%% @doc Read only stat for cpu
+-module(riak_core_metric_host).
+
+-behaviour(riak_core_metric).
+
+-export([new/0, value/3, value/4, update/2, options/1]).
+
+new() ->
+ host.
+
+options(host) ->
+ [{cpu, [nprocs, avg1, avg5, avg15]},
+ {mem, [total, allocated]},
+ {disk, [Id || {Id, _, _} <- disksup:get_disk_data()]}].
+
+update(_Arg, host) ->
+ host.
+
+value(_Level, _Name, host) ->
+ lists:flatten([value(Field) || Field <- [cpu, mem, disk]]).
+
+value(_Level, Field, _Name, host) when is_atom(Field) ->
+ value(Field);
+value(_Level, [disk, Disk], _Name, host) ->
+ {disk, Disks} = value(disk),
+ proplists:lookup(Disk, Disks);
+value(_Level, [Field, Elem], _Name, host) ->
+ Stat = value(Field),
+ ElemName = riak_core_metric:join_as_atom([Field, '_', Elem]),
+ proplists:lookup(ElemName, Stat).
+
+value(cpu) ->
+ [{cpu_nprocs, cpu_sup:nprocs()},
+ {cpu_avg1, cpu_sup:avg1()},
+ {cpu_avg5, cpu_sup:avg5()},
+ {cpu_avg15, cpu_sup:avg15()}];
+value(mem) ->
+ {Total, Alloc, _} = memsup:get_memory_data(),
+ [{mem_total, Total},
+ {mem_allocated, Alloc}];
+value(disk) ->
+ {disk, disksup:get_disk_data()}.
@@ -30,7 +30,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
--record(state, {name, mod, mod_state, description}).
+-record(state, {name, mod, mod_state, description, level}).
%%%===================================================================
%%% API
@@ -62,6 +62,7 @@ init(Args) ->
{type, Type} = proplists:lookup(type, Args), %% Does mod need init args?
Mod = mod_from_type(Type),
Description = proplists:get_value(description, Args),
+ Level = proplists:get_value(level, Args, 0),
ModState = Mod:new(),
DoTicks = do_ticks(Mod),
if DoTicks == true ->
@@ -70,10 +71,13 @@ init(Args) ->
ok
end,
{ok, #state{name=Name, mod=Mod, mod_state=ModState,
- description=Description}}.
+ description=Description, level=Level}}.
-handle_call({level, Level}, _From, #state{mod=Mod, mod_state=ModState, name=Name}=State) ->
- Stat = Mod:value(Level, Name, ModState),
+handle_call({level, Level}, _From, #state{mod=Mod, mod_state=ModState, name=Name, level=ActiveLevel}=State) ->
+ Stat = if Level >= ActiveLevel ->
+ Mod:value(Level, Name, ModState);
+ true -> []
+ end,
{reply, {ok, Stat}, State};
handle_call({spec, Level, Spec}, _From, #state{mod=Mod, mod_state=ModState, name=Name}=State) ->
Stat = Mod:value(Level, Spec, Name, ModState),
@@ -0,0 +1,64 @@
+%%
+%% Copyright (c) 2007-2012 Basho Technologies, Inc. All Rights Reserved.
+%%
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+%% -------------------------------------------------------------------
+
+%% TODO add the rings_reonciled stuff here?
+-module(riak_core_metric_ring).
+
+-behaviour(riak_core_metric).
+
+-export([new/0, value/3, value/4, update/2, options/1]).
+
+new() ->
+ ring.
+
+options(ring) ->
+ fields(5).
+
+update(_Arg, ring) ->
+ ring.
+
+value(Level, _Name, ring) ->
+ {ok, R} = riak_core_ring_manager:get_my_ring(),
+ [value(R, F) || F <- fields(Level)].
+
+value(_Level, Field, _Name, ring) ->
+ {ok, R} = riak_core_ring_manager:get_my_ring(),
+ value(R, Field).
+
+value(Ring, members) ->
+ {ring_members, riak_core_ring:all_members(Ring)};
+value(Ring, num_partitions) ->
+ {ring_num_partitions, riak_core_ring:num_partitions(Ring)};
+value(_Ring, creation_size) ->
+ {ring_creation_size, app_helper:get_env(riak_core, ring_creation_size)};
+value(Ring, ownership) ->
+ {ring_ownership, list_to_binary(lists:flatten(io_lib:format("~p", [dict:to_list(
+ lists:foldl(fun({_P, N}, Acc) ->
+ case dict:find(N, Acc) of
+ {ok, V} ->
+ dict:store(N, V+1, Acc);
+ error ->
+ dict:store(N, 1, Acc)
+ end
+ end, dict:new(), riak_core_ring:all_owners(Ring)))])))}.
+
+fields(0) ->
+ [members, num_partitions, creation_size];
+fields(_Level) ->
+ fields(0) ++ [ownership].
@@ -68,7 +68,8 @@ mod_refs(App, Mod) ->
[stat_ref(App, Stat, Args) || {Stat, Args} <- Stats].
stat_ref(App, Stat, Args) ->
- {{App, Stat}, {riak_core_metric_proc, start_link, [Stat, Args]},
+ Group = proplists:get_value(group, Args),
+ {{App, Stat, Group}, {riak_core_metric_proc, start_link, [Stat, Args]},
permanent, 5000, worker, [riak_core_metric_proc]}.
remove_slide_private_dirs() ->
Oops, something went wrong.

0 comments on commit 53e9b15

Please sign in to comment.