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

Commit 4ffcd16

Browse files
authored
gRPC example: export stats from client as well as server. (#415)
Add a custom view with custom breakdown.
1 parent 93d26cf commit 4ffcd16

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

examples/grpc/hello_client.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "examples/grpc/exporters.h"
2626
#include "examples/grpc/hello.grpc.pb.h"
2727
#include "examples/grpc/hello.pb.h"
28+
#include "opencensus/stats/aggregation.h"
29+
#include "opencensus/stats/bucket_boundaries.h"
30+
#include "opencensus/stats/view_descriptor.h"
2831
#include "opencensus/tags/context_util.h"
2932
#include "opencensus/tags/tag_key.h"
3033
#include "opencensus/tags/tag_map.h"
@@ -40,6 +43,32 @@ using examples::HelloReply;
4043
using examples::HelloRequest;
4144
using examples::HelloService;
4245

46+
opencensus::tags::TagKey MyKey() {
47+
static const auto key = opencensus::tags::TagKey::Register("my_key");
48+
return key;
49+
}
50+
51+
opencensus::stats::Aggregation MillisDistributionAggregation() {
52+
return opencensus::stats::Aggregation::Distribution(
53+
opencensus::stats::BucketBoundaries::Explicit(
54+
{0, 0.1, 1, 10, 100, 1000}));
55+
}
56+
57+
ABSL_CONST_INIT const absl::string_view kRpcClientRoundtripLatencyMeasureName =
58+
"grpc.io/client/roundtrip_latency";
59+
60+
::opencensus::tags::TagKey ClientMethodTagKey() {
61+
static const auto method_tag_key =
62+
::opencensus::tags::TagKey::Register("grpc_client_method");
63+
return method_tag_key;
64+
}
65+
66+
::opencensus::tags::TagKey ClientStatusTagKey() {
67+
static const auto status_tag_key =
68+
::opencensus::tags::TagKey::Register("grpc_client_status");
69+
return status_tag_key;
70+
}
71+
4372
} // namespace
4473

4574
int main(int argc, char **argv) {
@@ -56,6 +85,16 @@ int main(int argc, char **argv) {
5685
// Register the OpenCensus gRPC plugin to enable stats and tracing in gRPC.
5786
grpc::RegisterOpenCensusPlugin();
5887

88+
// Add a view of client RPC latency broken down by our custom key.
89+
opencensus::stats::ViewDescriptor()
90+
.set_name("example/client/roundtrip_latency/cumulative")
91+
.set_measure(kRpcClientRoundtripLatencyMeasureName)
92+
.set_aggregation(MillisDistributionAggregation())
93+
.add_column(ClientMethodTagKey())
94+
.add_column(ClientStatusTagKey())
95+
.add_column(MyKey())
96+
.RegisterForExport();
97+
5998
RegisterExporters();
6099

61100
// Create a Channel to send RPCs over.

0 commit comments

Comments
 (0)