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

Commit 8d44063

Browse files
authored
gRPC example: add custom tags in client. (#407)
Do this by extending the current tagmap.
1 parent d8ec569 commit 8d44063

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

examples/grpc/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ cc_binary(
5959
":exporters",
6060
":hello_cc_grpc",
6161
":hello_cc_proto",
62+
"//opencensus/tags",
63+
"//opencensus/tags:context_util",
64+
"//opencensus/tags:with_tag_map",
6265
"//opencensus/trace",
6366
"//opencensus/trace:context_util",
6467
"//opencensus/trace:with_span",

examples/grpc/hello_client.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include "examples/grpc/exporters.h"
2626
#include "examples/grpc/hello.grpc.pb.h"
2727
#include "examples/grpc/hello.pb.h"
28+
#include "opencensus/tags/context_util.h"
29+
#include "opencensus/tags/tag_key.h"
30+
#include "opencensus/tags/tag_map.h"
31+
#include "opencensus/tags/with_tag_map.h"
2832
#include "opencensus/trace/context_util.h"
2933
#include "opencensus/trace/sampler.h"
3034
#include "opencensus/trace/trace_config.h"
@@ -65,8 +69,19 @@ int main(int argc, char **argv) {
6569
"HelloClient", /*parent=*/nullptr, {&sampler});
6670
std::cout << "HelloClient span context is " << span.context().ToString()
6771
<< "\n";
72+
73+
// Extend the current tag map.
74+
// (in this example, there are no tags currently present, but in real code we
75+
// wouldn't want to drop tags)
76+
static const auto key = opencensus::tags::TagKey::Register("my_key");
77+
std::vector<std::pair<opencensus::tags::TagKey, std::string>> tags(
78+
opencensus::tags::GetCurrentTagMap().tags());
79+
tags.emplace_back(key, "my_value");
80+
opencensus::tags::TagMap tag_map(std::move(tags));
81+
6882
{
6983
opencensus::trace::WithSpan ws(span);
84+
opencensus::tags::WithTagMap wt(tag_map);
7085

7186
// The client Span ends when ctx falls out of scope.
7287
grpc::ClientContext ctx;

0 commit comments

Comments
 (0)