Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 6648c2e

Browse files
authored
Add custom stats to gRPC example. (#132)
1 parent 87fc6a4 commit 6648c2e

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

examples/grpc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Usage:
1313
```shell
1414
bazel build :all
1515
../../bazel-bin/examples/grpc/hello_server 9001
16-
../../bazel-bin/examples/grpc/hello_client [::]:9001
16+
../../bazel-bin/examples/grpc/hello_client "[::]:9001"
1717
```
1818

1919
You can see the Prometheus stats on http://127.0.0.1:8080/metrics

examples/grpc/hello_client.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ using examples::HelloService;
3838
} // namespace
3939

4040
int main(int argc, char** argv) {
41-
if (argc != 2) {
42-
std::cerr << "Usage: " << argv[0] << " host:port\n";
41+
if (argc < 2) {
42+
std::cerr << "Usage: " << argv[0] << " host:port [name]\n";
4343
return 1;
4444
}
4545
const std::string hostport = argv[1];
46+
std::string name = "world";
47+
if (argc >= 3) {
48+
name = argv[2];
49+
}
4650

4751
// Register the OpenCensus gRPC plugin to enable stats and tracing in gRPC.
4852
opencensus::RegisterGrpcPlugin();
@@ -69,7 +73,7 @@ int main(int argc, char** argv) {
6973
grpc::ClientContext ctx;
7074
HelloRequest request;
7175
HelloReply reply;
72-
request.set_name("world");
76+
request.set_name(name);
7377
std::cout << "Sending request: \"" << request.ShortDebugString() << "\"\n";
7478

7579
grpc::Status status = stub->SayHello(&ctx, request, &reply);

examples/grpc/hello_server.cc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
3333
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
3434
#include "opencensus/plugins/grpc/grpc_plugin.h"
35+
#include "opencensus/stats/stats.h"
3536
#include "opencensus/trace/sampler.h"
3637
#include "opencensus/trace/span.h"
3738
#include "opencensus/trace/trace_config.h"
@@ -43,6 +44,22 @@ using examples::HelloReply;
4344
using examples::HelloRequest;
4445
using examples::HelloService;
4546

47+
ABSL_CONST_INIT const char kLettersMeasureName[] =
48+
"example.org/measure/letters";
49+
50+
opencensus::stats::MeasureInt64 LettersMeasure() {
51+
static const opencensus::stats::MeasureInt64 measure =
52+
opencensus::stats::MeasureInt64::Register(
53+
kLettersMeasureName, "Number of letters in processed names.", "By");
54+
return measure;
55+
}
56+
57+
opencensus::stats::TagKey CaseKey() {
58+
static const opencensus::stats::TagKey key =
59+
opencensus::stats::TagKey::Register("example_uppercased");
60+
return key;
61+
}
62+
4663
// A helper function that performs some work in its own Span.
4764
void PerformWork(opencensus::trace::Span* parent) {
4865
auto span = opencensus::trace::Span::StartSpan("internal_work", parent);
@@ -67,7 +84,10 @@ class HelloServiceImpl final : public HelloService::Service {
6784
PerformWork(&span);
6885
span.AddAnnotation("Sleeping.");
6986
absl::SleepFor(absl::Milliseconds(30));
70-
// TODO: Record() custom stats.
87+
// Record custom stats.
88+
opencensus::stats::Record(
89+
{{LettersMeasure(), request->name().size()}},
90+
{{CaseKey(), isupper(request->name()[0]) ? "upper" : "lower"}});
7191
std::cerr << "SayHello RPC handled.\n";
7292
return grpc::Status::OK;
7393
}
@@ -106,6 +126,21 @@ int main(int argc, char** argv) {
106126
prometheus::Exposer exposer("127.0.0.1:8080");
107127
exposer.RegisterCollectable(exporter);
108128

129+
// Init custom measure.
130+
LettersMeasure();
131+
132+
// Add a View for custom stats.
133+
const opencensus::stats::ViewDescriptor letters_view =
134+
opencensus::stats::ViewDescriptor()
135+
.set_name("example.org/view/letters_view")
136+
.set_description("number of letters in names greeted over time")
137+
.set_measure(kLettersMeasureName)
138+
.set_aggregation(opencensus::stats::Aggregation::Sum())
139+
.add_column(CaseKey());
140+
opencensus::stats::View view(letters_view);
141+
assert(view.IsValid());
142+
letters_view.RegisterForExport();
143+
109144
// Start the RPC server. You shouldn't see any output from gRPC before this.
110145
std::cerr << "gRPC starting.\n";
111146
HelloServiceImpl service;

0 commit comments

Comments
 (0)