forked from census-instrumentation/opencensus-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhello_client.cc
More file actions
97 lines (80 loc) · 3.19 KB
/
Copy pathhello_client.cc
File metadata and controls
97 lines (80 loc) · 3.19 KB
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
// 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 <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include <grpcpp/opencensus.h>
#include "absl/time/clock.h"
#include "examples/grpc/hello.grpc.pb.h"
#include "examples/grpc/hello.pb.h"
#include "examples/grpc/stackdriver.h"
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
#include "opencensus/exporters/stats/stdout/stdout_exporter.h"
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
#include "opencensus/trace/sampler.h"
#include "opencensus/trace/trace_config.h"
namespace {
using examples::HelloReply;
using examples::HelloRequest;
using examples::HelloService;
} // 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();
// Register exporters for Stackdriver.
RegisterStackdriverExporters();
// Trace all outgoing RPCs.
opencensus::trace::TraceConfig::SetCurrentTraceParams(
{128, 128, 128, 128, opencensus::trace::ProbabilitySampler(1.0)});
// For debugging, register exporters that just write to stdout.
opencensus::exporters::stats::StdoutExporter::Register();
opencensus::exporters::trace::StdoutExporter::Register();
// 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);
// Send the RPC.
{
// The client Span ends when ctx falls out of scope.
grpc::ClientContext ctx;
HelloRequest request;
HelloReply reply;
request.set_name(name);
std::cout << "Sending request: \"" << request.ShortDebugString() << "\"\n";
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";
}
// 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));
}
}