]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Make sure to include GRPC headers first because otherwise Abseil may create |
2 | // ambiguity with `nostd::variant` if compiled with Visual Studio 2015. Other | |
3 | // modern compilers are unaffected. | |
4 | #include <grpcpp/grpcpp.h> | |
5 | #ifdef BAZEL_BUILD | |
6 | # include "examples/grpc/protos/messages.grpc.pb.h" | |
7 | #else | |
8 | # include "messages.grpc.pb.h" | |
9 | #endif | |
10 | ||
11 | #include <iostream> | |
12 | #include <memory> | |
13 | #include <string> | |
14 | #include "opentelemetry/trace/experimental_semantic_conventions.h" | |
15 | #include "tracer_common.h" | |
16 | ||
17 | using grpc::Channel; | |
18 | using grpc::ClientContext; | |
19 | using grpc::ClientReader; | |
20 | using grpc::Status; | |
21 | ||
22 | using grpc_example::Greeter; | |
23 | using grpc_example::GreetRequest; | |
24 | using grpc_example::GreetResponse; | |
25 | ||
26 | namespace | |
27 | { | |
28 | namespace context = opentelemetry::context; | |
29 | using namespace opentelemetry::trace; | |
30 | class GreeterClient | |
31 | { | |
32 | public: | |
33 | GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {} | |
34 | ||
35 | std::string Greet(std::string ip, uint16_t port) | |
36 | { | |
37 | // Build gRPC Context objects and protobuf message containers | |
38 | GreetRequest request; | |
39 | GreetResponse response; | |
40 | ClientContext context; | |
41 | request.set_request("Nice to meet you!"); | |
42 | ||
43 | StartSpanOptions options; | |
44 | options.kind = SpanKind::kClient; | |
45 | ||
46 | std::string span_name = "GreeterClient/Greet"; | |
47 | auto span = get_tracer("grpc")->StartSpan( | |
48 | span_name, | |
49 | {{OTEL_GET_TRACE_ATTR(AttrRpcSystem), "grpc"}, | |
50 | {OTEL_GET_TRACE_ATTR(AttrRpcService), "grpc-example.GreetService"}, | |
51 | {OTEL_GET_TRACE_ATTR(AttrRpcMethod), "Greet"}, | |
52 | {OTEL_GET_TRACE_ATTR(AttrNetPeerIp), ip}, | |
53 | {OTEL_GET_TRACE_ATTR(AttrNetPeerPort), port}}, | |
54 | options); | |
55 | ||
56 | auto scope = get_tracer("grpc-client")->WithActiveSpan(span); | |
57 | ||
58 | // inject current context to grpc metadata | |
59 | auto current_ctx = context::RuntimeContext::GetCurrent(); | |
60 | GrpcClientCarrier carrier(&context); | |
61 | auto prop = context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); | |
62 | prop->Inject(carrier, current_ctx); | |
63 | ||
64 | // Send request to server | |
65 | Status status = stub_->Greet(&context, request, &response); | |
66 | if (status.ok()) | |
67 | { | |
68 | span->SetStatus(StatusCode::kOk); | |
69 | span->SetAttribute(OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), status.error_code()); | |
70 | // Make sure to end your spans! | |
71 | span->End(); | |
72 | return response.response(); | |
73 | } | |
74 | else | |
75 | { | |
76 | std::cout << status.error_code() << ": " << status.error_message() << std::endl; | |
77 | span->SetStatus(StatusCode::kError); | |
78 | span->SetAttribute(OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), status.error_code()); | |
79 | // Make sure to end your spans! | |
80 | span->End(); | |
81 | return "RPC failed"; | |
82 | } | |
83 | } | |
84 | ||
85 | private: | |
86 | std::unique_ptr<Greeter::Stub> stub_; | |
87 | }; // GreeterClient class | |
88 | ||
89 | void RunClient(uint16_t port) | |
90 | { | |
91 | GreeterClient greeter( | |
92 | grpc::CreateChannel("0.0.0.0:" + std::to_string(port), grpc::InsecureChannelCredentials())); | |
93 | std::string response = greeter.Greet("0.0.0.0", port); | |
94 | std::cout << "grpc_server says: " << response << std::endl; | |
95 | } | |
96 | } // namespace | |
97 | ||
98 | int main(int argc, char **argv) | |
99 | { | |
100 | initTracer(); | |
101 | // set global propagator | |
102 | context::propagation::GlobalTextMapPropagator::SetGlobalPropagator( | |
103 | opentelemetry::nostd::shared_ptr<context::propagation::TextMapPropagator>( | |
104 | new propagation::HttpTraceContext())); | |
105 | constexpr uint16_t default_port = 8800; | |
106 | uint16_t port; | |
107 | if (argc > 1) | |
108 | { | |
109 | port = atoi(argv[1]); | |
110 | } | |
111 | else | |
112 | { | |
113 | port = default_port; | |
114 | } | |
115 | RunClient(port); | |
116 | return 0; | |
117 | } |