Anonymous avatar Anonymous committed 49752c8

/stats provides riak_stat information over HTTP

Comments (0)

Files changed (3)

apps/riak/ebin/riak.app

              riakserver_pb,
              slide,
              spiraltime,
+             stats_http_resource,
              vclock
             ]},
   {applications, [
          %% Number of VNodes allowed to do handoff concurrently.
          {handoff_concurrency, 4},
 
+         %% Endpoint for system stats HTTP provider
+         {stats_urlpath, "stats"},
+
          %% Secondary code paths
          {add_paths, []}
         ]}

apps/riak/src/riak_web.erl

 %% "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.    
+%% under the License.
 
 %% @doc Convenience functions for setting up the Jiak HTTP interface
 %%      of Riak.  This module loads parameters from the application
 dispatch_table() ->
     JiakProps = jiak_props(),
     RawProps = raw_props(),
+    MapredProps = mapred_props(),
+    StatsProps = stats_props(),
+
     [{[proplists:get_value(jiak_name, JiakProps),bucket],
       jiak_resource,
       [{key_type, container}|JiakProps]},
      {[proplists:get_value(prefix, RawProps),bucket,key,'*'],
       raw_link_walker_resource, RawProps},
 
+     {[proplists:get_value(prefix, MapredProps)],
+      mapred_resource, MapredProps},
+     
+     {[proplists:get_value(prefix, StatsProps)],
+      stats_http_resource, StatsProps},
+
      {["ping"], ping_http_resource, []}].
 
 jiak_props() ->
 raw_props() ->
     [{prefix, riak:get_app_env(raw_name, "raw")},
      {riak, local}].
+
+mapred_props() ->
+    [{prefix, riak:get_app_env(mapred_name, "mapred")}].
+
+stats_props() ->
+    [{prefix, riak:get_app_env(stats_urlpath, "stats")}].

apps/riak/src/stats_http_resource.erl

+%% 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(stats_http_resource).
+-author('Andy Gross <andy@basho.com>').
+
+%% webmachine resource exports
+-export([
+         init/1,
+         encodings_provided/2,
+         content_types_provided/2,
+         service_available/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) ->
+    case riak:get_app_env(riak_stat, false) of
+        false ->
+            {false, wrq:append_to_response_body("riak_stat is disabled on this node.\n", ReqData),
+             Ctx};
+        true ->
+            {true, ReqData, Ctx}
+    end.
+
+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() ->
+    proplists:delete(disk, [{vm_processes, length(processes())}|riak_stat:get_stats()]).
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.