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

Commit 93d26cf

Browse files
authored
gRPC example: show current span context and tags in server. (#413)
1 parent 8d44063 commit 93d26cf

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

examples/grpc/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ cc_binary(
8181
":hello_cc_grpc",
8282
":hello_cc_proto",
8383
"//opencensus/exporters/stats/prometheus:prometheus_exporter",
84+
"//opencensus/tags",
85+
"//opencensus/tags:context_util",
8486
"//opencensus/trace",
87+
"//opencensus/trace:context_util",
8588
"@com_github_grpc_grpc//:grpc++",
8689
"@com_github_grpc_grpc//:grpc_opencensus_plugin",
8790
"@com_github_jupp0r_prometheus_cpp//pull",

examples/grpc/hello_server.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
// Example RPC server using gRPC.
1616

17+
#include <grpcpp/grpcpp.h>
18+
#include <grpcpp/opencensus.h>
19+
1720
#include <cstdlib>
1821
#include <iostream>
1922
#include <memory>
2023
#include <string>
2124

22-
#include <grpcpp/grpcpp.h>
23-
#include <grpcpp/opencensus.h>
24-
2525
#include "absl/strings/escaping.h"
2626
#include "absl/strings/numbers.h"
2727
#include "absl/strings/str_cat.h"
@@ -30,6 +30,9 @@
3030
#include "examples/grpc/hello.pb.h"
3131
#include "opencensus/exporters/stats/prometheus/prometheus_exporter.h"
3232
#include "opencensus/stats/stats.h"
33+
#include "opencensus/tags/context_util.h"
34+
#include "opencensus/tags/tag_map.h"
35+
#include "opencensus/trace/context_util.h"
3336
#include "opencensus/trace/sampler.h"
3437
#include "opencensus/trace/span.h"
3538
#include "opencensus/trace/trace_config.h"
@@ -89,8 +92,13 @@ class HelloServiceImpl final : public HelloService::Service {
8992
{{LettersMeasure(), request->name().size()}},
9093
{{CaseKey(), isupper(request->name()[0]) ? "upper" : "lower"}});
9194
// Give feedback on stderr.
92-
std::cerr << "SayHello RPC handled.\n";
93-
std::cerr << " Metadata:\n";
95+
std::cerr << "SayHello RPC handler:\n";
96+
std::cerr << " Current context: "
97+
<< opencensus::trace::GetCurrentSpan().context().ToString()
98+
<< "\n";
99+
std::cerr << " Current tags: "
100+
<< opencensus::tags::GetCurrentTagMap().DebugString() << "\n";
101+
std::cerr << " gRPC metadata:\n";
94102
auto metadata = context->client_metadata();
95103
for (const auto &mdpair : metadata) {
96104
std::cerr << " \"" << absl::CEscape(ToStringView(mdpair.first))

0 commit comments

Comments
 (0)