blob: 9296d6515e822b131594eaa80f494f80992e0b6e [file] [log] [blame]
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *is % allowed in string
32 */
33
34#include "test/cpp/util/metrics_server.h"
35
David Garcia Quintas1aeabd72016-01-19 10:44:05 -080036#include <grpc++/server.h>
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070037#include <grpc++/server_builder.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070038#include <grpc/support/log.h>
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070039
Craig Tiller1b4e3302015-12-17 16:35:00 -080040#include "src/proto/grpc/testing/metrics.grpc.pb.h"
41#include "src/proto/grpc/testing/metrics.pb.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070042
43namespace grpc {
44namespace testing {
45
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070046QpsGauge::QpsGauge()
47 : start_time_(gpr_now(GPR_CLOCK_REALTIME)), num_queries_(0) {}
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070048
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070049void QpsGauge::Reset() {
50 std::lock_guard<std::mutex> lock(num_queries_mu_);
51 num_queries_ = 0;
52 start_time_ = gpr_now(GPR_CLOCK_REALTIME);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070053}
54
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070055void QpsGauge::Incr() {
56 std::lock_guard<std::mutex> lock(num_queries_mu_);
57 num_queries_++;
58}
59
60long QpsGauge::Get() {
61 std::lock_guard<std::mutex> lock(num_queries_mu_);
62 gpr_timespec time_diff =
63 gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start_time_);
64 long duration_secs = time_diff.tv_sec > 0 ? time_diff.tv_sec : 1;
65 return num_queries_ / duration_secs;
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -080066}
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070067
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080068grpc::Status MetricsServiceImpl::GetAllGauges(
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070069 ServerContext* context, const EmptyMessage* request,
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080070 ServerWriter<GaugeResponse>* writer) {
Sree Kuchibhotla559e45b2016-02-19 03:02:16 -080071 gpr_log(GPR_DEBUG, "GetAllGauges called");
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070072
73 std::lock_guard<std::mutex> lock(mu_);
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070074 for (auto it = qps_gauges_.begin(); it != qps_gauges_.end(); it++) {
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080075 GaugeResponse resp;
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080076 resp.set_name(it->first); // Gauge name
77 resp.set_long_value(it->second->Get()); // Gauge value
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070078 writer->Write(resp);
79 }
80
81 return Status::OK;
82}
83
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080084grpc::Status MetricsServiceImpl::GetGauge(ServerContext* context,
85 const GaugeRequest* request,
86 GaugeResponse* response) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070087 std::lock_guard<std::mutex> lock(mu_);
88
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070089 const auto it = qps_gauges_.find(request->name());
90 if (it != qps_gauges_.end()) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070091 response->set_name(it->first);
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080092 response->set_long_value(it->second->Get());
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070093 }
94
95 return Status::OK;
96}
97
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070098std::shared_ptr<QpsGauge> MetricsServiceImpl::CreateQpsGauge(
99 const grpc::string& name, bool* already_present) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700100 std::lock_guard<std::mutex> lock(mu_);
101
Sree Kuchibhotla3714e302016-04-22 09:50:46 -0700102 std::shared_ptr<QpsGauge> qps_gauge(new QpsGauge());
Vijay Paia63271c2016-06-15 12:56:38 -0700103 const auto p = qps_gauges_.insert(std::make_pair(name, qps_gauge));
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700104
Sree Kuchibhotla3714e302016-04-22 09:50:46 -0700105 // p.first is an iterator pointing to <name, shared_ptr<QpsGauge>> pair.
106 // p.second is a boolean which is set to 'true' if the QpsGauge is
107 // successfully inserted in the guages_ map and 'false' if it is already
108 // present in the map
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800109 *already_present = !p.second;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700110 return p.first->second;
111}
112
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -0800113// Starts the metrics server and returns the grpc::Server instance. Call Wait()
114// on the returned server instance.
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700115std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) {
116 gpr_log(GPR_INFO, "Building metrics server..");
117
Vijay Paia63271c2016-06-15 12:56:38 -0700118 const grpc::string address = "0.0.0.0:" + grpc::to_string(port);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700119
120 ServerBuilder builder;
121 builder.AddListeningPort(address, grpc::InsecureServerCredentials());
122 builder.RegisterService(this);
123
124 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
125 gpr_log(GPR_INFO, "Metrics server %s started. Ready to receive requests..",
126 address.c_str());
127
128 return server;
129}
130
131} // namespace testing
132} // namespace grpc