Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 4 | * 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 Quintas | 1aeabd7 | 2016-01-19 10:44:05 -0800 | [diff] [blame] | 36 | #include <grpc++/server.h> |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 37 | #include <grpc++/server_builder.h> |
| 38 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 39 | #include "src/proto/grpc/testing/metrics.grpc.pb.h" |
| 40 | #include "src/proto/grpc/testing/metrics.pb.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 41 | |
| 42 | namespace grpc { |
| 43 | namespace testing { |
| 44 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 45 | QpsGauge::QpsGauge() |
| 46 | : start_time_(gpr_now(GPR_CLOCK_REALTIME)), num_queries_(0) {} |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 47 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 48 | void 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 Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 54 | void QpsGauge::Incr() { |
| 55 | std::lock_guard<std::mutex> lock(num_queries_mu_); |
| 56 | num_queries_++; |
| 57 | } |
| 58 | |
| 59 | long 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 Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 65 | } |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 66 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 67 | grpc::Status MetricsServiceImpl::GetAllGauges( |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 68 | ServerContext* context, const EmptyMessage* request, |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 69 | ServerWriter<GaugeResponse>* writer) { |
Sree Kuchibhotla | 559e45b | 2016-02-19 03:02:16 -0800 | [diff] [blame] | 70 | gpr_log(GPR_DEBUG, "GetAllGauges called"); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 71 | |
| 72 | std::lock_guard<std::mutex> lock(mu_); |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 73 | for (auto it = qps_gauges_.begin(); it != qps_gauges_.end(); it++) { |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 74 | GaugeResponse resp; |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 75 | resp.set_name(it->first); // Gauge name |
| 76 | resp.set_long_value(it->second->Get()); // Gauge value |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 77 | writer->Write(resp); |
| 78 | } |
| 79 | |
| 80 | return Status::OK; |
| 81 | } |
| 82 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 83 | grpc::Status MetricsServiceImpl::GetGauge(ServerContext* context, |
| 84 | const GaugeRequest* request, |
| 85 | GaugeResponse* response) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 86 | std::lock_guard<std::mutex> lock(mu_); |
| 87 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 88 | const auto it = qps_gauges_.find(request->name()); |
| 89 | if (it != qps_gauges_.end()) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 90 | response->set_name(it->first); |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 91 | response->set_long_value(it->second->Get()); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return Status::OK; |
| 95 | } |
| 96 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 97 | std::shared_ptr<QpsGauge> MetricsServiceImpl::CreateQpsGauge( |
| 98 | const grpc::string& name, bool* already_present) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 99 | std::lock_guard<std::mutex> lock(mu_); |
| 100 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 101 | std::shared_ptr<QpsGauge> qps_gauge(new QpsGauge()); |
| 102 | const auto p = qps_gauges_.emplace(name, qps_gauge); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 103 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame^] | 104 | // 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 Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 108 | *already_present = !p.second; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 109 | return p.first->second; |
| 110 | } |
| 111 | |
Sree Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 112 | // Starts the metrics server and returns the grpc::Server instance. Call Wait() |
| 113 | // on the returned server instance. |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 114 | std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) { |
| 115 | gpr_log(GPR_INFO, "Building metrics server.."); |
| 116 | |
Sree Kuchibhotla | bc3127d | 2015-11-19 10:32:59 -0800 | [diff] [blame] | 117 | const grpc::string address = "0.0.0.0:" + std::to_string(port); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 118 | |
| 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 |