-
Notifications
You must be signed in to change notification settings - Fork 233
/
riak_kv_wm_stats.erl
89 lines (76 loc) · 3.1 KB
/
riak_kv_wm_stats.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
%% -------------------------------------------------------------------
%%
%% riak_kv_wm_stats: publishing Riak runtime stats via HTTP
%%
%% Copyright (c) 2007-2013 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.
%%
%% -------------------------------------------------------------------
-module(riak_kv_wm_stats).
%% webmachine resource exports
-export([
init/1,
encodings_provided/2,
content_types_provided/2,
service_available/2,
forbidden/2,
produce_body/2,
pretty_print/2
]).
-include_lib("webmachine/include/webmachine.hrl").
-record(ctx, {}).
init(_) ->
{ok, #ctx{}}.
%% @spec encodings_provided(webmachine:wrq(), context()) ->
%% {[encoding()], webmachine:wrq(), context()}
%% @doc Get the list of encodings this resource provides.
%% "identity" is provided for all methods, and "gzip" is
%% provided for GET as well
encodings_provided(ReqData, Context) ->
case wrq:method(ReqData) of
'GET' ->
{[{"identity", fun(X) -> X end},
{"gzip", fun(X) -> zlib:gzip(X) end}], ReqData, Context};
_ ->
{[{"identity", fun(X) -> X end}], ReqData, Context}
end.
%% @spec content_types_provided(webmachine:wrq(), context()) ->
%% {[ctype()], webmachine:wrq(), context()}
%% @doc Get the list of content types this resource provides.
%% "application/json" and "text/plain" are both provided
%% for all requests. "text/plain" is a "pretty-printed"
%% version of the "application/json" content.
content_types_provided(ReqData, Context) ->
{[{"application/json", produce_body},
{"text/plain", pretty_print}],
ReqData, Context}.
service_available(ReqData, Ctx) ->
{true, ReqData, Ctx}.
forbidden(RD, Ctx) ->
{riak_kv_wm_utils:is_forbidden(RD), RD, Ctx}.
produce_body(ReqData, Ctx) ->
Body = mochijson2:encode({struct, get_stats()}),
{Body, ReqData, Ctx}.
%% @spec pretty_print(webmachine:wrq(), context()) ->
%% {string(), webmachine:wrq(), context()}
%% @doc Format the respons JSON object is a "pretty-printed" style.
pretty_print(RD1, C1=#ctx{}) ->
{Json, RD2, C2} = produce_body(RD1, C1),
{json_pp:print(binary_to_list(list_to_binary(Json))), RD2, C2}.
get_stats() ->
{value, {disk, Disk}, Stats} = lists:keytake(disk, 1, riak_kv_stat:get_stats()),
DiskFlat = [{struct, [{id, list_to_binary(Id)}, {size, Size}, {used, Used}]} || {Id, Size, Used} <- Disk],
lists:append([Stats, [{disk, DiskFlat}], riak_core_stat:get_stats(), yz_stat:search_stats()]).