blob: eac29f30932a793d8503e0a0912462954538e82d [file] [log] [blame]
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -07001/*
2 *
3 * Copyright 2015, Google Inc.
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
36#include <vector>
37
38#include <grpc++/server_builder.h>
39
40#include "test/proto/metrics.grpc.pb.h"
41#include "test/proto/metrics.pb.h"
42
43namespace grpc {
44namespace testing {
45
46using std::vector;
47
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080048Gauge::Gauge(long initial_val) : val_(initial_val) {}
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070049
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080050void Gauge::Set(long new_val) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070051 val_.store(new_val, std::memory_order_relaxed);
52}
53
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080054long Gauge::Get() { return val_.load(std::memory_order_relaxed); }
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070055
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080056grpc::Status MetricsServiceImpl::GetAllGauges(
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070057 ServerContext* context, const EmptyMessage* request,
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080058 ServerWriter<GaugeResponse>* writer) {
59 gpr_log(GPR_INFO, "GetAllGauges called");
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070060
61 std::lock_guard<std::mutex> lock(mu_);
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080062 for (auto it = gauges_.begin(); it != gauges_.end(); it++) {
63 GaugeResponse resp;
64 resp.set_name(it->first); // Gauge name
65 resp.set_value(it->second->Get()); // Gauge value
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070066 writer->Write(resp);
67 }
68
69 return Status::OK;
70}
71
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080072grpc::Status MetricsServiceImpl::GetGauge(ServerContext* context,
73 const GaugeRequest* request,
74 GaugeResponse* response) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070075 std::lock_guard<std::mutex> lock(mu_);
76
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080077 auto it = gauges_.find(request->name());
78 if (it != gauges_.end()) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070079 response->set_name(it->first);
80 response->set_value(it->second->Get());
81 }
82
83 return Status::OK;
84}
85
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080086std::shared_ptr<Gauge> MetricsServiceImpl::CreateGauge(string name,
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070087 bool& already_present) {
88 std::lock_guard<std::mutex> lock(mu_);
89
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080090 std::shared_ptr<Gauge> gauge(new Gauge(0));
91 auto p = gauges_.emplace(name, gauge);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070092
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080093 // p.first is an iterator pointing to <name, shared_ptr<Gauge>> pair. p.second
94 // is a boolean indicating if the Gauge is already present in the map
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070095 already_present = !p.second;
96 return p.first->second;
97}
98
99// Starts the metrics server and returns the grpc::Server instance. Call
100// wait() on the returned server instance.
101std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) {
102 gpr_log(GPR_INFO, "Building metrics server..");
103
104 grpc::string address = "0.0.0.0:" + std::to_string(port);
105
106 ServerBuilder builder;
107 builder.AddListeningPort(address, grpc::InsecureServerCredentials());
108 builder.RegisterService(this);
109
110 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
111 gpr_log(GPR_INFO, "Metrics server %s started. Ready to receive requests..",
112 address.c_str());
113
114 return server;
115}
116
117} // namespace testing
118} // namespace grpc