7
7
#include < grpc/health/v1/health.grpc.pb.h>
8
8
9
9
#include " opentelemetry/trace/context.h"
10
- #include " opentelemetry/trace/experimental_semantic_conventions .h"
10
+ #include " opentelemetry/trace/semantic_conventions .h"
11
11
#include " opentelemetry/trace/span_context_kv_iterable_view.h"
12
+ #include " opentelemetry/baggage/baggage.h"
13
+ #include " opentelemetry/nostd/string_view.h"
12
14
#include " tracer_common.h"
13
15
14
16
#include < grpcpp/grpcpp.h>
15
17
#include < grpcpp/server.h>
16
18
#include < grpcpp/server_builder.h>
17
19
#include < grpcpp/server_context.h>
20
+ #include < grpcpp/impl/codegen/string_ref.h>
18
21
19
22
using namespace std ;
20
23
@@ -31,7 +34,7 @@ using grpc::Server;
31
34
using Span = opentelemetry::trace::Span;
32
35
using SpanContext = opentelemetry::trace::SpanContext;
33
36
using namespace opentelemetry ::trace;
34
-
37
+ using namespace opentelemetry ::baggage ;
35
38
namespace context = opentelemetry::context;
36
39
37
40
namespace
@@ -90,6 +93,20 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
90
93
const Empty* request,
91
94
GetSupportedCurrenciesResponse* response) override
92
95
{
96
+ // Read baggage from client context
97
+ auto clientContext = context->client_metadata ();
98
+ auto range = clientContext.equal_range (" baggage" );
99
+ std::vector<std::string> baggageLists;
100
+ for (auto i = range.first ; i != range.second ; ++i)
101
+ {
102
+ baggageLists.emplace_back (std::string (i->second .data ()));
103
+ }
104
+ std::vector<opentelemetry::nostd::shared_ptr<Baggage>> baggages (baggageLists.size ());
105
+ for (int i = 0 ; i < baggageLists.size (); i++) {
106
+ auto baggage = Baggage::FromHeader (baggageLists[i]);
107
+ baggages[i] = baggage;
108
+ }
109
+
93
110
StartSpanOptions options;
94
111
options.kind = SpanKind::kServer ;
95
112
GrpcServerCarrier carrier (context);
@@ -102,12 +119,22 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
102
119
std::string span_name = " CurrencyService/GetSupportedCurrencies" ;
103
120
auto span =
104
121
get_tracer (" currencyservice" )->StartSpan (span_name,
105
- {{OTEL_GET_TRACE_ATTR (AttrRpcSystem) , " grpc" },
106
- {OTEL_GET_TRACE_ATTR (AttrRpcService) , " CurrencyService" },
107
- {OTEL_GET_TRACE_ATTR (AttrRpcMethod) , " GetSupportedCurrencies" },
108
- {OTEL_GET_TRACE_ATTR (AttrRpcGrpcStatusCode) , 0 }},
122
+ {{SemanticConventions::RPC_SYSTEM , " grpc" },
123
+ {SemanticConventions::RPC_SERVICE , " CurrencyService" },
124
+ {SemanticConventions::RPC_METHOD , " GetSupportedCurrencies" },
125
+ {SemanticConventions::RPC_GRPC_STATUS_CODE , 0 }},
109
126
options);
110
127
auto scope = get_tracer (" currencyservice" )->WithActiveSpan (span);
128
+
129
+ for (auto & baggage : baggages) {
130
+ // Set the key value pairs from baggage to Span Attributes
131
+ baggage->GetAllEntries ([&span](opentelemetry::nostd::string_view key,
132
+ opentelemetry::nostd::string_view value) {
133
+ span->SetAttribute (key, value);
134
+ return true ;
135
+ });
136
+ }
137
+
111
138
// Fetch and parse whatever HTTP headers we can from the gRPC request.
112
139
span->AddEvent (" Processing supported currencies request" );
113
140
@@ -150,6 +177,20 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
150
177
const CurrencyConversionRequest* request,
151
178
Money* response) override
152
179
{
180
+ // Read baggage from client context
181
+ auto clientContext = context->client_metadata ();
182
+ auto range = clientContext.equal_range (" baggage" );
183
+ std::vector<std::string> baggageLists;
184
+ for (auto i = range.first ; i != range.second ; ++i)
185
+ {
186
+ baggageLists.emplace_back (std::string (i->second .data ()));
187
+ }
188
+ std::vector<opentelemetry::nostd::shared_ptr<Baggage>> baggages (baggageLists.size ());
189
+ for (int i = 0 ; i < baggageLists.size (); i++) {
190
+ auto baggage = Baggage::FromHeader (baggageLists[i]);
191
+ baggages[i] = baggage;
192
+ }
193
+
153
194
StartSpanOptions options;
154
195
options.kind = SpanKind::kServer ;
155
196
GrpcServerCarrier carrier (context);
@@ -162,12 +203,21 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
162
203
std::string span_name = " CurrencyService/Convert" ;
163
204
auto span =
164
205
get_tracer (" currencyservice" )->StartSpan (span_name,
165
- {{OTEL_GET_TRACE_ATTR (AttrRpcSystem) , " grpc" },
166
- {OTEL_GET_TRACE_ATTR (AttrRpcService) , " CurrencyService" },
167
- {OTEL_GET_TRACE_ATTR (AttrRpcMethod) , " Convert" },
168
- {OTEL_GET_TRACE_ATTR (AttrRpcGrpcStatusCode) , 0 }},
206
+ {{SemanticConventions::RPC_SYSTEM , " grpc" },
207
+ {SemanticConventions::RPC_SERVICE , " CurrencyService" },
208
+ {SemanticConventions::RPC_METHOD , " Convert" },
209
+ {SemanticConventions::RPC_GRPC_STATUS_CODE , 0 }},
169
210
options);
170
211
auto scope = get_tracer (" currencyservice" )->WithActiveSpan (span);
212
+
213
+ for (auto & baggage : baggages) {
214
+ // Set the key value pairs from baggage to Span Attributes
215
+ baggage->GetAllEntries ([&span](opentelemetry::nostd::string_view key,
216
+ opentelemetry::nostd::string_view value) {
217
+ span->SetAttribute (key, value);
218
+ return true ;
219
+ });
220
+ }
171
221
// Fetch and parse whatever HTTP headers we can from the gRPC request.
172
222
span->AddEvent (" Processing currency conversion request" );
173
223
0 commit comments