|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <cstdlib> |
| 16 | +#include <iostream> |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include <grpc++/grpc++.h> |
| 21 | + |
| 22 | +#include "examples/grpc/hello.grpc.pb.h" |
| 23 | +#include "examples/grpc/hello.pb.h" |
| 24 | +#include "examples/grpc/stackdriver.h" |
| 25 | +#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" |
| 26 | +#include "opencensus/exporters/stats/stdout/stdout_exporter.h" |
| 27 | +#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h" |
| 28 | +#include "opencensus/exporters/trace/stdout/stdout_exporter.h" |
| 29 | +#include "opencensus/plugins/grpc/grpc_plugin.h" |
| 30 | +#include "opencensus/trace/sampler.h" |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +using examples::HelloReply; |
| 35 | +using examples::HelloRequest; |
| 36 | +using examples::HelloService; |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +int main(int argc, char** argv) { |
| 41 | + if (argc != 2) { |
| 42 | + std::cerr << "Usage: " << argv[0] << " host:port\n"; |
| 43 | + return 1; |
| 44 | + } |
| 45 | + const std::string hostport = argv[1]; |
| 46 | + |
| 47 | + // Register the OpenCensus gRPC plugin to enable stats and tracing in gRPC. |
| 48 | + opencensus::RegisterGrpcPlugin(); |
| 49 | + |
| 50 | + // Register exporters for Stackdriver. |
| 51 | + RegisterStackdriverExporters(); |
| 52 | + |
| 53 | + // Trace all outgoing RPCs. |
| 54 | + opencensus::trace::TraceConfig::SetCurrentTraceParams( |
| 55 | + {128, 128, 128, 128, opencensus::trace::ProbabilitySampler(1.0)}); |
| 56 | + |
| 57 | + // For debugging, register exporters that just write to stdout. |
| 58 | + opencensus::exporters::stats::StdoutExporter::Register(); |
| 59 | + opencensus::exporters::trace::StdoutExporter::Register(); |
| 60 | + |
| 61 | + // Create a Channel to send RPCs over. |
| 62 | + std::shared_ptr<grpc::Channel> channel = |
| 63 | + grpc::CreateChannel(hostport, grpc::InsecureChannelCredentials()); |
| 64 | + std::unique_ptr<HelloService::Stub> stub = HelloService::NewStub(channel); |
| 65 | + |
| 66 | + // Send the RPC. |
| 67 | + { |
| 68 | + // The client Span ends when ctx falls out of scope. |
| 69 | + grpc::ClientContext ctx; |
| 70 | + HelloRequest request; |
| 71 | + HelloReply reply; |
| 72 | + request.set_name("world"); |
| 73 | + std::cout << "Sending request: \"" << request.ShortDebugString() << "\"\n"; |
| 74 | + |
| 75 | + grpc::Status status = stub->SayHello(&ctx, request, &reply); |
| 76 | + |
| 77 | + std::cout << "Got status: " << status.error_code() << ": \"" |
| 78 | + << status.error_message() << "\"\n"; |
| 79 | + std::cout << "Got reply: \"" << reply.ShortDebugString() << "\"\n"; |
| 80 | + } |
| 81 | + |
| 82 | + // Disable tracing any further RPCs (which will be sent by exporters). |
| 83 | + opencensus::trace::TraceConfig::SetCurrentTraceParams( |
| 84 | + {128, 128, 128, 128, opencensus::trace::ProbabilitySampler(0.0)}); |
| 85 | + |
| 86 | + // Sleep while exporters run in the background. |
| 87 | + std::cout << "Client sleeping, ^C to exit.\n"; |
| 88 | + while (true) { |
| 89 | + absl::SleepFor(absl::Seconds(10)); |
| 90 | + } |
| 91 | +} |
0 commit comments