Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/grpc/test/cpp/util/metrics_server.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, 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 <grpc++/server.h>
37 #include <grpc++/server_builder.h>
38
39 #include "src/proto/grpc/testing/metrics.grpc.pb.h"
40 #include "src/proto/grpc/testing/metrics.pb.h"
41
42 namespace grpc {
43 namespace testing {
44
45 Gauge::Gauge(long initial_val) : val_(initial_val) {}
46
47 void Gauge::Set(long new_val) {
48 std::lock_guard<std::mutex> lock(val_mu_);
49 val_ = new_val;
50 }
51
52 long Gauge::Get() {
53 std::lock_guard<std::mutex> lock(val_mu_);
54 return val_;
55 }
56
57 grpc::Status MetricsServiceImpl::GetAllGauges(
58 ServerContext* context, const EmptyMessage* request,
59 ServerWriter<GaugeResponse>* writer) {
60 gpr_log(GPR_DEBUG, "GetAllGauges called");
61
62 std::lock_guard<std::mutex> lock(mu_);
63 for (auto it = gauges_.begin(); it != gauges_.end(); it++) {
64 GaugeResponse resp;
65 resp.set_name(it->first); // Gauge name
66 resp.set_long_value(it->second->Get()); // Gauge value
67 writer->Write(resp);
68 }
69
70 return Status::OK;
71 }
72
73 grpc::Status MetricsServiceImpl::GetGauge(ServerContext* context,
74 const GaugeRequest* request,
75 GaugeResponse* response) {
76 std::lock_guard<std::mutex> lock(mu_);
77
78 const auto it = gauges_.find(request->name());
79 if (it != gauges_.end()) {
80 response->set_name(it->first);
81 response->set_long_value(it->second->Get());
82 }
83
84 return Status::OK;
85 }
86
87 std::shared_ptr<Gauge> MetricsServiceImpl::CreateGauge(const grpc::string& name,
88 bool* already_present) {
89 std::lock_guard<std::mutex> lock(mu_);
90
91 std::shared_ptr<Gauge> gauge(new Gauge(0));
92 const auto p = gauges_.emplace(name, gauge);
93
94 // p.first is an iterator pointing to <name, shared_ptr<Gauge>> pair. p.second
95 // 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
97 *already_present = !p.second;
98 return p.first->second;
99 }
100
101 // Starts the metrics server and returns the grpc::Server instance. Call Wait()
102 // on the returned server instance.
103 std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) {
104 gpr_log(GPR_INFO, "Building metrics server..");
105
106 const grpc::string address = "0.0.0.0:" + std::to_string(port);
107
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
OLDNEW
« no previous file with comments | « third_party/grpc/test/cpp/util/metrics_server.h ('k') | third_party/grpc/test/cpp/util/slice_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698