Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame^] | 1 | /* |
| 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> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include "test/proto/metrics.grpc.pb.h" |
| 42 | #include "test/proto/metrics.pb.h" |
| 43 | |
| 44 | /* |
| 45 | * This implements a Metrics server defined in test/proto/metrics.proto. Any |
| 46 | * test service can use this to export Metrics (TODO (sreek): Only Guages for |
| 47 | * now). |
| 48 | * |
| 49 | * Example: |
| 50 | * MetricsServiceImpl metricsImpl; |
| 51 | * .. |
| 52 | * // Create Guage(s). Note: Guages can be created even after calling |
| 53 | * // 'StartServer'. |
| 54 | * Guage guage1 = metricsImpl.CreateGuage("foo",is_present); |
| 55 | * // guage1 can now be used anywhere in the program to set values. |
| 56 | * ... |
| 57 | * // Create the metrics server |
| 58 | * std::unique_ptr<grpc::Server> server = metricsImpl.StartServer(port); |
| 59 | * server->Wait(); // Note: This is blocking. |
| 60 | */ |
| 61 | namespace grpc { |
| 62 | namespace testing { |
| 63 | |
| 64 | using std::map; |
| 65 | using std::vector; |
| 66 | |
| 67 | class Guage { |
| 68 | public: |
| 69 | Guage(long initial_val); |
| 70 | void Set(long new_val); |
| 71 | long Get(); |
| 72 | |
| 73 | private: |
| 74 | std::atomic_long val_; |
| 75 | }; |
| 76 | |
| 77 | class MetricsServiceImpl GRPC_FINAL : public MetricsService::Service { |
| 78 | public: |
| 79 | grpc::Status GetAllGuages(ServerContext* context, const EmptyMessage* request, |
| 80 | ServerWriter<GuageResponse>* writer) GRPC_OVERRIDE; |
| 81 | |
| 82 | grpc::Status GetGuage(ServerContext* context, const GuageRequest* request, |
| 83 | GuageResponse* response) GRPC_OVERRIDE; |
| 84 | |
| 85 | // Create a Guage with name 'name'. is_present is set to true if the Guage |
| 86 | // is already present in the map. |
| 87 | // NOTE: CreateGuage can be called anytime (i.e before or after calling |
| 88 | // StartServer). |
| 89 | std::shared_ptr<Guage> CreateGuage(string name, bool& is_present); |
| 90 | |
| 91 | std::unique_ptr<grpc::Server> StartServer(int port); |
| 92 | |
| 93 | private: |
| 94 | std::map<string, std::shared_ptr<Guage>> guages_; |
| 95 | std::mutex mu_; |
| 96 | }; |
| 97 | |
| 98 | } // namespace testing |
| 99 | } // namespace grpc |
| 100 | |
| 101 | #endif // GRPC_TEST_CPP_METRICS_SERVER_H |