blob: cc6b39b7532ff81c27dafc4dfff329be7ba1c0b0 [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>
38
Craig Tiller1b4e3302015-12-17 16:35:00 -080039#include "src/proto/grpc/testing/metrics.grpc.pb.h"
40#include "src/proto/grpc/testing/metrics.pb.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070041
42namespace grpc {
43namespace testing {
44
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070045QpsGauge::QpsGauge()
46 : start_time_(gpr_now(GPR_CLOCK_REALTIME)), num_queries_(0) {}
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070047
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070048void QpsGauge::Reset() {
49 std::lock_guard<std::mutex> lock(num_queries_mu_);
50 num_queries_ = 0;
51 start_time_ = gpr_now(GPR_CLOCK_REALTIME);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070052}
53
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070054void QpsGauge::Incr() {
55 std::lock_guard<std::mutex> lock(num_queries_mu_);
56 num_queries_++;
57}
58
59long QpsGauge::Get() {
60 std::lock_guard<std::mutex> lock(num_queries_mu_);
61 gpr_timespec time_diff =
62 gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start_time_);
63 long duration_secs = time_diff.tv_sec > 0 ? time_diff.tv_sec : 1;
64 return num_queries_ / duration_secs;
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -080065}
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070066
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080067grpc::Status MetricsServiceImpl::GetAllGauges(
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070068 ServerContext* context, const EmptyMessage* request,
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080069 ServerWriter<GaugeResponse>* writer) {
Sree Kuchibhotla559e45b2016-02-19 03:02:16 -080070 gpr_log(GPR_DEBUG, "GetAllGauges called");
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070071
72 std::lock_guard<std::mutex> lock(mu_);
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070073 for (auto it = qps_gauges_.begin(); it != qps_gauges_.end(); it++) {
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080074 GaugeResponse resp;
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080075 resp.set_name(it->first); // Gauge name
76 resp.set_long_value(it->second->Get()); // Gauge value
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070077 writer->Write(resp);
78 }
79
80 return Status::OK;
81}
82
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080083grpc::Status MetricsServiceImpl::GetGauge(ServerContext* context,
84 const GaugeRequest* request,
85 GaugeResponse* response) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070086 std::lock_guard<std::mutex> lock(mu_);
87
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070088 const auto it = qps_gauges_.find(request->name());
89 if (it != qps_gauges_.end()) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070090 response->set_name(it->first);
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080091 response->set_long_value(it->second->Get());
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070092 }
93
94 return Status::OK;
95}
96
Sree Kuchibhotla3714e302016-04-22 09:50:46 -070097std::shared_ptr<QpsGauge> MetricsServiceImpl::CreateQpsGauge(
98 const grpc::string& name, bool* already_present) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070099 std::lock_guard<std::mutex> lock(mu_);
100
Sree Kuchibhotla3714e302016-04-22 09:50:46 -0700101 std::shared_ptr<QpsGauge> qps_gauge(new QpsGauge());
102 const auto p = qps_gauges_.emplace(name, qps_gauge);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700103
Sree Kuchibhotla3714e302016-04-22 09:50:46 -0700104 // p.first is an iterator pointing to <name, shared_ptr<QpsGauge>> pair.
105 // p.second is a boolean which is set to 'true' if the QpsGauge is
106 // successfully inserted in the guages_ map and 'false' if it is already
107 // present in the map
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800108 *already_present = !p.second;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700109 return p.first->second;
110}
111
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -0800112// Starts the metrics server and returns the grpc::Server instance. Call Wait()
113// on the returned server instance.
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700114std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) {
115 gpr_log(GPR_INFO, "Building metrics server..");
116
Sree Kuchibhotlabc3127d2015-11-19 10:32:59 -0800117 const grpc::string address = "0.0.0.0:" + std::to_string(port);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700118
119 ServerBuilder builder;
120 builder.AddListeningPort(address, grpc::InsecureServerCredentials());
121 builder.RegisterService(this);
122
123 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
124 gpr_log(GPR_INFO, "Metrics server %s started. Ready to receive requests..",
125 address.c_str());
126
127 return server;
128}
129
130} // namespace testing
131} // namespace grpc