| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*is % allowed in string |
| 17 |
*/ |
| 18 |
#ifndef GRPC_TEST_CPP_METRICS_SERVER_H |
| 19 |
#define GRPC_TEST_CPP_METRICS_SERVER_H |
| 20 |
|
| 21 |
#include <map> |
| 22 |
#include <mutex> |
| 23 |
|
| 24 |
#include <grpcpp/server.h> |
| 25 |
|
| 26 |
#include "src/proto/grpc/testing/metrics.grpc.pb.h" |
| 27 |
#include "src/proto/grpc/testing/metrics.pb.h" |
| 28 |
|
| 29 |
/* |
| 30 |
* This implements a Metrics server defined in |
| 31 |
* src/proto/grpc/testing/metrics.proto. Any |
| 32 |
* test service can use this to export Metrics (TODO (sreek): Only Gauges for |
| 33 |
* now). |
| 34 |
* |
| 35 |
* Example: |
| 36 |
* MetricsServiceImpl metricsImpl; |
| 37 |
* .. |
| 38 |
* // Create QpsGauge(s). Note: QpsGauges can be created even after calling |
| 39 |
* // 'StartServer'. |
| 40 |
* QpsGauge qps_gauge1 = metricsImpl.CreateQpsGauge("foo", is_present); |
| 41 |
* // qps_gauge1 can now be used anywhere in the program by first making a |
| 42 |
* // one-time call qps_gauge1.Reset() and then calling qps_gauge1.Incr() |
| 43 |
* // every time to increment a query counter |
| 44 |
* |
| 45 |
* ... |
| 46 |
* // Create the metrics server |
| 47 |
* std::unique_ptr<grpc::Server> server = metricsImpl.StartServer(port); |
| 48 |
* server->Wait(); // Note: This is blocking. |
| 49 |
*/ |
| 50 |
namespace grpc { |
| 51 |
namespace testing { |
| 52 |
|
| 53 |
class QpsGauge { |
| 54 |
public: |
| 55 |
QpsGauge(); |
| 56 |
|
| 57 |
// Initialize the internal timer and reset the query count to 0 |
| 58 |
void Reset(); |
| 59 |
|
| 60 |
// Increment the query count by 1 |
| 61 |
void Incr(); |
| 62 |
|
| 63 |
// Return the current qps (i.e query count divided by the time since this |
| 64 |
// QpsGauge object created (or Reset() was called)) |
| 65 |
long Get(); |
| 66 |
|
| 67 |
private: |
| 68 |
gpr_timespec start_time_; |
| 69 |
long num_queries_; |
| 70 |
std::mutex num_queries_mu_; |
| 71 |
}; |
| 72 |
|
| 73 |
class MetricsServiceImpl final : public MetricsService::Service { |
| 74 |
public: |
| 75 |
grpc::Status GetAllGauges(ServerContext* context, const EmptyMessage* request, |
| 76 |
ServerWriter<GaugeResponse>* writer) override; |
| 77 |
|
| 78 |
grpc::Status GetGauge(ServerContext* context, const GaugeRequest* request, |
| 79 |
GaugeResponse* response) override; |
| 80 |
|
| 81 |
// Create a QpsGauge with name 'name'. is_present is set to true if the Gauge |
| 82 |
// is already present in the map. |
| 83 |
// NOTE: CreateQpsGauge can be called anytime (i.e before or after calling |
| 84 |
// StartServer). |
| 85 |
std::shared_ptr<QpsGauge> CreateQpsGauge(const std::string& name, |
| 86 |
bool* already_present); |
| 87 |
|
| 88 |
std::unique_ptr<grpc::Server> StartServer(int port); |
| 89 |
|
| 90 |
private: |
| 91 |
std::map<string, std::shared_ptr<QpsGauge>> qps_gauges_; |
| 92 |
std::mutex mu_; |
| 93 |
}; |
| 94 |
|
| 95 |
} // namespace testing |
| 96 |
} // namespace grpc |
| 97 |
|
| 98 |
#endif // GRPC_TEST_CPP_METRICS_SERVER_H |