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