This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
hello_client.cc
162 lines (137 loc) · 5.57 KB
/
hello_client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <grpcpp/grpcpp.h>
#include <grpcpp/opencensus.h>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include "absl/strings/str_cat.h"
#include "absl/time/clock.h"
#include "examples/grpc/exporters.h"
#include "examples/grpc/hello.grpc.pb.h"
#include "examples/grpc/hello.pb.h"
#include "opencensus/stats/aggregation.h"
#include "opencensus/stats/bucket_boundaries.h"
#include "opencensus/stats/view_descriptor.h"
#include "opencensus/tags/context_util.h"
#include "opencensus/tags/tag_key.h"
#include "opencensus/tags/tag_map.h"
#include "opencensus/tags/with_tag_map.h"
#include "opencensus/trace/context_util.h"
#include "opencensus/trace/sampler.h"
#include "opencensus/trace/trace_config.h"
#include "opencensus/trace/with_span.h"
namespace {
using examples::HelloReply;
using examples::HelloRequest;
using examples::HelloService;
opencensus::tags::TagKey MyKey() {
static const auto key = opencensus::tags::TagKey::Register("my_key");
return key;
}
opencensus::stats::Aggregation MillisDistributionAggregation() {
return opencensus::stats::Aggregation::Distribution(
opencensus::stats::BucketBoundaries::Explicit(
{0, 0.1, 1, 10, 100, 1000}));
}
ABSL_CONST_INIT const absl::string_view kRpcClientRoundtripLatencyMeasureName =
"grpc.io/client/roundtrip_latency";
::opencensus::tags::TagKey ClientMethodTagKey() {
static const auto method_tag_key =
::opencensus::tags::TagKey::Register("grpc_client_method");
return method_tag_key;
}
::opencensus::tags::TagKey ClientStatusTagKey() {
static const auto status_tag_key =
::opencensus::tags::TagKey::Register("grpc_client_status");
return status_tag_key;
}
} // namespace
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " host:port [name]\n";
return 1;
}
const std::string hostport = argv[1];
std::string name = "world";
if (argc >= 3) {
name = argv[2];
}
// Register the OpenCensus gRPC plugin to enable stats and tracing in gRPC.
grpc::RegisterOpenCensusPlugin();
// Add a view of client RPC latency broken down by our custom key.
opencensus::stats::ViewDescriptor()
.set_name("example/client/roundtrip_latency/cumulative")
.set_measure(kRpcClientRoundtripLatencyMeasureName)
.set_aggregation(MillisDistributionAggregation())
.add_column(ClientMethodTagKey())
.add_column(ClientStatusTagKey())
.add_column(MyKey())
.RegisterForExport();
RegisterExporters();
// Create a Channel to send RPCs over.
std::shared_ptr<grpc::Channel> channel =
grpc::CreateChannel(hostport, grpc::InsecureChannelCredentials());
std::unique_ptr<HelloService::Stub> stub = HelloService::NewStub(channel);
// Create a span, this will be the parent of the RPC span.
static opencensus::trace::AlwaysSampler sampler;
auto span = opencensus::trace::Span::StartSpan(
"HelloClient", /*parent=*/nullptr, {&sampler});
std::cout << "HelloClient span context is " << span.context().ToString()
<< "\n";
// Extend the current tag map.
// (in this example, there are no tags currently present, but in real code we
// wouldn't want to drop tags)
static const auto key = opencensus::tags::TagKey::Register("my_key");
std::vector<std::pair<opencensus::tags::TagKey, std::string>> tags(
opencensus::tags::GetCurrentTagMap().tags());
tags.emplace_back(key, "my_value");
opencensus::tags::TagMap tag_map(std::move(tags));
{
opencensus::trace::WithSpan ws(span);
opencensus::tags::WithTagMap wt(tag_map);
// The client Span ends when ctx falls out of scope.
grpc::ClientContext ctx;
ctx.AddMetadata("key1", "value1");
HelloRequest request;
HelloReply reply;
request.set_name(name);
std::cout << "Sending request: \"" << request.ShortDebugString() << "\"\n";
opencensus::trace::GetCurrentSpan().AddAnnotation("Sending request.");
// Send the RPC.
grpc::Status status = stub->SayHello(&ctx, request, &reply);
std::cout << "Got status: " << status.error_code() << ": \""
<< status.error_message() << "\"\n";
std::cout << "Got reply: \"" << reply.ShortDebugString() << "\"\n";
opencensus::trace::GetCurrentSpan().AddAnnotation(
"Got reply.", {{"status", absl::StrCat(status.error_code(), ": ",
status.error_message())}});
if (!status.ok()) {
// TODO: Map grpc::StatusCode to trace::StatusCode.
opencensus::trace::GetCurrentSpan().SetStatus(
opencensus::trace::StatusCode::UNKNOWN, status.error_message());
}
// Don't forget to explicitly end the span!
opencensus::trace::GetCurrentSpan().End();
}
// Disable tracing any further RPCs (which will be sent by exporters).
opencensus::trace::TraceConfig::SetCurrentTraceParams(
{128, 128, 128, 128, opencensus::trace::ProbabilitySampler(0.0)});
// Sleep while exporters run in the background.
std::cout << "Client sleeping, ^C to exit.\n";
while (true) {
absl::SleepFor(absl::Seconds(10));
}
}