blob: 34d51eb3169e4e5c2416dda753d33c1c9c94fc6b [file] [log] [blame]
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -07001/*
2 *
David Garcia Quintas61908542016-01-20 11:21:27 -08003 * Copyright 2015-2016, 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 Kuchibhotla4d0f2f92015-11-03 15:55:43 -080045Gauge::Gauge(long initial_val) : val_(initial_val) {}
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070046
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080047void Gauge::Set(long new_val) {
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -080048 std::lock_guard<std::mutex> lock(val_mu_);
49 val_ = new_val;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070050}
51
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -080052long Gauge::Get() {
53 std::lock_guard<std::mutex> lock(val_mu_);
54 return val_;
55}
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070056
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080057grpc::Status MetricsServiceImpl::GetAllGauges(
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070058 ServerContext* context, const EmptyMessage* request,
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080059 ServerWriter<GaugeResponse>* writer) {
Sree Kuchibhotla559e45b2016-02-19 03:02:16 -080060 gpr_log(GPR_DEBUG, "GetAllGauges called");
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070061
62 std::lock_guard<std::mutex> lock(mu_);
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080063 for (auto it = gauges_.begin(); it != gauges_.end(); it++) {
64 GaugeResponse resp;
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080065 resp.set_name(it->first); // Gauge name
66 resp.set_long_value(it->second->Get()); // Gauge value
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070067 writer->Write(resp);
68 }
69
70 return Status::OK;
71}
72
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080073grpc::Status MetricsServiceImpl::GetGauge(ServerContext* context,
74 const GaugeRequest* request,
75 GaugeResponse* response) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070076 std::lock_guard<std::mutex> lock(mu_);
77
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -080078 const auto it = gauges_.find(request->name());
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080079 if (it != gauges_.end()) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070080 response->set_name(it->first);
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080081 response->set_long_value(it->second->Get());
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070082 }
83
84 return Status::OK;
85}
86
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080087std::shared_ptr<Gauge> MetricsServiceImpl::CreateGauge(const grpc::string& name,
88 bool* already_present) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070089 std::lock_guard<std::mutex> lock(mu_);
90
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080091 std::shared_ptr<Gauge> gauge(new Gauge(0));
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -080092 const auto p = gauges_.emplace(name, gauge);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070093
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080094 // p.first is an iterator pointing to <name, shared_ptr<Gauge>> pair. p.second
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -080095 // is a boolean which is set to 'true' if the Gauge is inserted in the guages_
96 // map and 'false' if it is already present in the map
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080097 *already_present = !p.second;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070098 return p.first->second;
99}
100
Sree Kuchibhotla52a514a2015-11-19 10:28:47 -0800101// Starts the metrics server and returns the grpc::Server instance. Call Wait()
102// on the returned server instance.
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700103std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) {
104 gpr_log(GPR_INFO, "Building metrics server..");
105
Sree Kuchibhotlabc3127d2015-11-19 10:32:59 -0800106 const grpc::string address = "0.0.0.0:" + std::to_string(port);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700107
108 ServerBuilder builder;
109 builder.AddListeningPort(address, grpc::InsecureServerCredentials());
110 builder.RegisterService(this);
111
112 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
113 gpr_log(GPR_INFO, "Metrics server %s started. Ready to receive requests..",
114 address.c_str());
115
116 return server;
117}
118
119} // namespace testing
120} // namespace grpc