You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2021/03/19 16:25:34 UTC

[GitHub] [couchdb] rnewson commented on a change in pull request #3416: Prometheus endpoint

rnewson commented on a change in pull request #3416:
URL: https://github.com/apache/couchdb/pull/3416#discussion_r597809665



##########
File path: src/chttpd/src/chttpd_node.erl
##########
@@ -117,6 +117,12 @@ handle_node_req(#httpd{method='GET', path_parts=[_, Node, <<"_stats">> | Path]}=
     chttpd:send_json(Req, EJSON1);
 handle_node_req(#httpd{path_parts=[_, _Node, <<"_stats">>]}=Req) ->
     send_method_not_allowed(Req, "GET");
+handle_node_req(#httpd{method='GET', path_parts=[_, Node, <<"_prometheus">>]}=Req) ->
+    Metrics = call_node(Node, couch_prometheus_server, scrape, []),
+    Header = [{<<"Content-Type">>, <<"text/plain">>}],

Review comment:
       I think there's supposed to be a version parameter on this?

##########
File path: src/couch_prometheus/src/couch_prometheus_http.erl
##########
@@ -0,0 +1,79 @@
+-module(couch_prometheus_http).
+
+-compile(tuple_calls).
+
+-export([
+    start_link/0,
+    handle_request/1
+]).
+
+-include("couch_prometheus.hrl").
+
+start_link() ->
+    IP = case config:get("prometheus", "bind_address", "any") of
+        "any" -> any;
+        Else -> Else
+    end,
+    Port = config:get("prometheus", "port"),
+    ok = couch_httpd:validate_bind_address(IP),
+
+    Options = [
+        {name, ?MODULE},
+        {loop, fun ?MODULE:handle_request/1},
+        {ip, IP},
+        {port, Port}
+    ],
+    case mochiweb_http:start(Options) of
+        {ok, Pid} ->
+            {ok, Pid};
+        {error, Reason} ->
+            io:format("Failure to start Mochiweb: ~s~n", [Reason]),
+            {error, Reason}
+    end.
+
+handle_request(MochiReq) ->
+    RawUri = MochiReq:get(raw_path),
+    {"/" ++ Path, _, _} = mochiweb_util:urlsplit_path(RawUri),
+    PathParts =  string:tokens(Path, "/"),    try
+        case PathParts of
+            ["_node", Node, "_prometheus"] ->
+                send_prometheus(MochiReq, Node);
+            _ ->
+                send_resp(MochiReq, 404, [], <<>>)
+        end
+    catch T:R ->
+        Body = io_lib:format("~p:~p", [T, R]),
+        send_resp(MochiReq, 500, [], Body)

Review comment:
       we work hard elsewhere not to send ugly errors to the user, can we follow the chttpd:send_error pattern here?

##########
File path: src/couch_prometheus/src/couch_prometheus_sup.erl
##########
@@ -0,0 +1,39 @@
+% Licensed 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(couch_prometheus_sup).
+
+-behaviour(supervisor).
+
+-export([
+    start_link/0,
+    init/1
+]).
+
+-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
+
+start_link() ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+init([]) ->
+    {ok, {
+        {one_for_one, 5, 10}, [

Review comment:
       how were these numbers chosen? 

##########
File path: src/couch_prometheus/src/couch_prometheus_server.erl
##########
@@ -0,0 +1,167 @@
+% Licensed 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(couch_prometheus_server).
+
+-behaviour(gen_server).
+
+-import(couch_prometheus_util, [
+    couch_to_prom/3,
+    to_prom/3,
+    to_prom_summary/2
+]).
+
+-export([
+    start_link/0,
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    code_change/3,
+    terminate/2,
+
+    scrape/0

Review comment:
       can we keep the gen_server callbacks in a separate export block to public or private functions pls? we do that ~everywhere else.

##########
File path: src/couch_prometheus/src/couch_prometheus_http.erl
##########
@@ -0,0 +1,79 @@
+-module(couch_prometheus_http).
+
+-compile(tuple_calls).
+
+-export([
+    start_link/0,
+    handle_request/1
+]).
+
+-include("couch_prometheus.hrl").
+
+start_link() ->
+    IP = case config:get("prometheus", "bind_address", "any") of
+        "any" -> any;
+        Else -> Else
+    end,
+    Port = config:get("prometheus", "port"),
+    ok = couch_httpd:validate_bind_address(IP),
+
+    Options = [
+        {name, ?MODULE},
+        {loop, fun ?MODULE:handle_request/1},
+        {ip, IP},
+        {port, Port}
+    ],
+    case mochiweb_http:start(Options) of
+        {ok, Pid} ->
+            {ok, Pid};
+        {error, Reason} ->
+            io:format("Failure to start Mochiweb: ~s~n", [Reason]),
+            {error, Reason}
+    end.
+
+handle_request(MochiReq) ->
+    RawUri = MochiReq:get(raw_path),
+    {"/" ++ Path, _, _} = mochiweb_util:urlsplit_path(RawUri),
+    PathParts =  string:tokens(Path, "/"),    try
+        case PathParts of
+            ["_node", Node, "_prometheus"] ->
+                send_prometheus(MochiReq, Node);
+            _ ->
+                send_resp(MochiReq, 404, [], <<>>)
+        end
+    catch T:R ->
+        Body = io_lib:format("~p:~p", [T, R]),
+        send_resp(MochiReq, 500, [], Body)
+    end.
+
+send_prometheus(MochiReq, Node) ->
+    Headers = couch_httpd:server_header() ++ [
+        {<<"Content-Type">>, <<"text/plain">>}

Review comment:
       version param?

##########
File path: src/couch_prometheus/src/couch_prometheus_server.erl
##########
@@ -0,0 +1,167 @@
+% Licensed 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(couch_prometheus_server).
+
+-behaviour(gen_server).
+
+-import(couch_prometheus_util, [
+    couch_to_prom/3,
+    to_prom/3,
+    to_prom_summary/2
+]).
+
+-export([
+    start_link/0,
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    code_change/3,
+    terminate/2,
+
+    scrape/0
+]).
+
+-include("couch_prometheus.hrl").
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+-record(st, {
+    metrics,
+    refresh
+}).
+
+init([]) ->
+    Metrics = refresh_metrics(),
+    RT = update_refresh_timer(),
+    {ok, #st{metrics=Metrics, refresh=RT}}.
+
+scrape() ->
+    {ok, Metrics} = gen_server:call(?MODULE, scrape),

Review comment:
       will this ever take >5s?

##########
File path: src/couch_prometheus/src/couch_prometheus_sup.erl
##########
@@ -0,0 +1,39 @@
+% Licensed 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(couch_prometheus_sup).
+
+-behaviour(supervisor).
+
+-export([
+    start_link/0,
+    init/1
+]).
+
+-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
+
+start_link() ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+init([]) ->
+    {ok, {
+        {one_for_one, 5, 10}, [

Review comment:
       if the couch_prometheus_server process crashing should the couch_prometheus_http be restarted? (i.e, rest_for_one).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org