blob: 36529095f962360adaffa72308a73f1da13bca34 [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#ifndef GRPC_TEST_CPP_METRICS_SERVER_H
34#define GRPC_TEST_CPP_METRICS_SERVER_H
35
36#include <atomic>
37#include <map>
38#include <mutex>
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070039
40#include "test/proto/metrics.grpc.pb.h"
41#include "test/proto/metrics.pb.h"
42
43/*
44 * This implements a Metrics server defined in test/proto/metrics.proto. Any
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080045 * test service can use this to export Metrics (TODO (sreek): Only Gauges for
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070046 * now).
47 *
48 * Example:
49 * MetricsServiceImpl metricsImpl;
50 * ..
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080051 * // Create Gauge(s). Note: Gauges can be created even after calling
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070052 * // 'StartServer'.
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080053 * Gauge gauge1 = metricsImpl.CreateGauge("foo",is_present);
54 * // gauge1 can now be used anywhere in the program to set values.
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070055 * ...
56 * // Create the metrics server
57 * std::unique_ptr<grpc::Server> server = metricsImpl.StartServer(port);
58 * server->Wait(); // Note: This is blocking.
59 */
60namespace grpc {
61namespace testing {
62
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080063// TODO(sreek): Add support for other types of Gauges like Double, String in
64// future
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080065class Gauge {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070066 public:
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080067 Gauge(long initial_val);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070068 void Set(long new_val);
69 long Get();
70
71 private:
72 std::atomic_long val_;
73};
74
75class MetricsServiceImpl GRPC_FINAL : public MetricsService::Service {
76 public:
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080077 grpc::Status GetAllGauges(ServerContext* context, const EmptyMessage* request,
78 ServerWriter<GaugeResponse>* writer) GRPC_OVERRIDE;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070079
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080080 grpc::Status GetGauge(ServerContext* context, const GaugeRequest* request,
81 GaugeResponse* response) GRPC_OVERRIDE;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070082
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080083 // Create a Gauge with name 'name'. is_present is set to true if the Gauge
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070084 // is already present in the map.
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080085 // NOTE: CreateGauge can be called anytime (i.e before or after calling
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070086 // StartServer).
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080087 std::shared_ptr<Gauge> CreateGauge(const grpc::string& name,
88 bool* already_present);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070089
90 std::unique_ptr<grpc::Server> StartServer(int port);
91
92 private:
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080093 std::map<string, std::shared_ptr<Gauge>> gauges_;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070094 std::mutex mu_;
95};
96
97} // namespace testing
98} // namespace grpc
99
100#endif // GRPC_TEST_CPP_METRICS_SERVER_H