Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for baggage read and Set as attributes, added sdk builders and Semantic changes #281

Merged
merged 13 commits into from
Aug 16, 2022
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ services:
context: ./src/currencyservice
args:
- GRPC_VERSION=1.46.0
- OPENTELEMETRY_VERSION=1.4.0
- OPENTELEMETRY_VERSION=1.5.0
ports:
- "${CURRENCY_SERVICE_PORT}:${CURRENCY_SERVICE_PORT}"
environment:
Expand Down
2 changes: 1 addition & 1 deletion docs/trace_service_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Emoji Legend
| Ad | Java | :100: | :100: | :100: | :construction: | :construction: | :construction: |
| Cart | .NET | :100: | :construction: | :100: | :construction: | :construction: | :construction: |
| Checkout | Go | :100: | :100: | :100: | :construction: | :construction: | :construction: |
| Currency | C++ | :no_bell: | :100: | :100: | :100: | :construction: | :construction: |
| Currency | C++ | :no_bell: | :100: | :100: | :100: | :construction: | :100: |
| Email | Ruby | :100: | :100: | :100: | :construction: | :construction: | :construction: |
| Feature Flag | Erlang / Elixir | :100: | :construction: | :construction: | :construction: | :construction: | :construction: |
| Frontend | JavaScript | :construction: | :construction: | :construction: | :construction: | :construction: | :construction: |
Expand Down
2 changes: 1 addition & 1 deletion src/currencyservice/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ add_executable(currencyservice src/server.cpp)
add_dependencies(currencyservice demo-proto)
target_link_libraries(
currencyservice demo-proto protobuf::libprotobuf
opentelemetry_resources opentelemetry_trace opentelemetry_common opentelemetry_exporter_otlp_grpc opentelemetry_proto opentelemetry_otlp_recordable gRPC::grpc++)
opentelemetry_trace opentelemetry_common opentelemetry_exporter_otlp_grpc opentelemetry_proto opentelemetry_otlp_recordable opentelemetry_resources gRPC::grpc++)

add_executable(currencyclient src/client.cpp)
add_dependencies(currencyclient demo-proto)
Expand Down
3 changes: 3 additions & 0 deletions src/currencyservice/src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CurrencyClient
Empty request;
GetSupportedCurrenciesResponse response;
ClientContext context;
context.AddMetadata("baggage", "ServiceName=CurrencyService,Method=GetSupportedCurrencies");

supported_currencies.clear();
Status status = stub_->GetSupportedCurrencies(&context, request, &response);
Expand Down Expand Up @@ -81,6 +82,8 @@ class CurrencyClient

Money response;
ClientContext context;
context.AddMetadata("baggage", "ServiceName=CurrencyService,Method=Convert");

Status status = stub_->Convert(&context, request, &response);

if (status.ok()) {
Expand Down
70 changes: 60 additions & 10 deletions src/currencyservice/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
#include <grpc/health/v1/health.grpc.pb.h>

#include "opentelemetry/trace/context.h"
#include "opentelemetry/trace/experimental_semantic_conventions.h"
#include "opentelemetry/trace/semantic_conventions.h"
#include "opentelemetry/trace/span_context_kv_iterable_view.h"
#include "opentelemetry/baggage/baggage.h"
#include "opentelemetry/nostd/string_view.h"
#include "tracer_common.h"

#include <grpcpp/grpcpp.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>
#include <grpcpp/impl/codegen/string_ref.h>

using namespace std;

Expand All @@ -31,7 +34,7 @@ using grpc::Server;
using Span = opentelemetry::trace::Span;
using SpanContext = opentelemetry::trace::SpanContext;
using namespace opentelemetry::trace;

using namespace opentelemetry::baggage;
namespace context = opentelemetry::context;

namespace
Expand Down Expand Up @@ -90,6 +93,20 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
const Empty* request,
GetSupportedCurrenciesResponse* response) override
{
// Read baggage from client context
auto clientContext = context->client_metadata();
auto range = clientContext.equal_range("baggage");
std::vector<std::string> baggageLists;
for (auto i = range.first; i != range.second; ++i)
{
baggageLists.emplace_back(std::string(i->second.data()));
}
std::vector<opentelemetry::nostd::shared_ptr<Baggage>> baggages(baggageLists.size());
for (int i = 0; i < baggageLists.size(); i++) {
auto baggage = Baggage::FromHeader(baggageLists[i]);
baggages[i] = baggage;
}

StartSpanOptions options;
options.kind = SpanKind::kServer;
GrpcServerCarrier carrier(context);
Expand All @@ -102,12 +119,22 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
std::string span_name = "CurrencyService/GetSupportedCurrencies";
auto span =
get_tracer("currencyservice")->StartSpan(span_name,
{{OTEL_GET_TRACE_ATTR(AttrRpcSystem), "grpc"},
{OTEL_GET_TRACE_ATTR(AttrRpcService), "CurrencyService"},
{OTEL_GET_TRACE_ATTR(AttrRpcMethod), "GetSupportedCurrencies"},
{OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), 0}},
{{SemanticConventions::RPC_SYSTEM, "grpc"},
{SemanticConventions::RPC_SERVICE, "CurrencyService"},
{SemanticConventions::RPC_METHOD, "GetSupportedCurrencies"},
{SemanticConventions::RPC_GRPC_STATUS_CODE, 0}},
options);
auto scope = get_tracer("currencyservice")->WithActiveSpan(span);

for (auto& baggage : baggages) {
// Set the key value pairs from baggage to Span Attributes
baggage->GetAllEntries([&span](opentelemetry::nostd::string_view key,
opentelemetry::nostd::string_view value) {
span->SetAttribute(key, value);
return true;
});
}

// Fetch and parse whatever HTTP headers we can from the gRPC request.
span->AddEvent("Processing supported currencies request");

Expand Down Expand Up @@ -150,6 +177,20 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
const CurrencyConversionRequest* request,
Money* response) override
{
// Read baggage from client context
auto clientContext = context->client_metadata();
auto range = clientContext.equal_range("baggage");
std::vector<std::string> baggageLists;
for (auto i = range.first; i != range.second; ++i)
{
baggageLists.emplace_back(std::string(i->second.data()));
}
std::vector<opentelemetry::nostd::shared_ptr<Baggage>> baggages(baggageLists.size());
for (int i = 0; i < baggageLists.size(); i++) {
auto baggage = Baggage::FromHeader(baggageLists[i]);
baggages[i] = baggage;
}

StartSpanOptions options;
options.kind = SpanKind::kServer;
GrpcServerCarrier carrier(context);
Expand All @@ -162,12 +203,21 @@ class CurrencyService final : public hipstershop::CurrencyService::Service
std::string span_name = "CurrencyService/Convert";
auto span =
get_tracer("currencyservice")->StartSpan(span_name,
{{OTEL_GET_TRACE_ATTR(AttrRpcSystem), "grpc"},
{OTEL_GET_TRACE_ATTR(AttrRpcService), "CurrencyService"},
{OTEL_GET_TRACE_ATTR(AttrRpcMethod), "Convert"},
{OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), 0}},
{{SemanticConventions::RPC_SYSTEM, "grpc"},
{SemanticConventions::RPC_SERVICE, "CurrencyService"},
{SemanticConventions::RPC_METHOD, "Convert"},
{SemanticConventions::RPC_GRPC_STATUS_CODE, 0}},
options);
auto scope = get_tracer("currencyservice")->WithActiveSpan(span);

for (auto& baggage : baggages) {
// Set the key value pairs from baggage to Span Attributes
baggage->GetAllEntries([&span](opentelemetry::nostd::string_view key,
opentelemetry::nostd::string_view value) {
span->SetAttribute(key, value);
return true;
});
}
// Fetch and parse whatever HTTP headers we can from the gRPC request.
span->AddEvent("Processing currency conversion request");

Expand Down
28 changes: 13 additions & 15 deletions src/currencyservice/src/tracer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once
#include "opentelemetry/exporters/ostream/span_exporter.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/trace/provider.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h"

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
#include "opentelemetry/context/propagation/global_propagator.h"
#include "opentelemetry/context/propagation/text_map_propagator.h"
#include "opentelemetry/exporters/ostream/span_exporter_factory.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_context_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/propagation/http_trace_context.h"
#include "opentelemetry/trace/provider.h"

#include <grpcpp/grpcpp.h>
#include <cstring>
Expand Down Expand Up @@ -72,16 +71,15 @@ class GrpcServerCarrier : public opentelemetry::context::propagation::TextMapCar

void initTracer()
{
auto exporter = std::unique_ptr<opentelemetry::sdk::trace::SpanExporter>(
new opentelemetry::exporter::otlp::OtlpGrpcExporter());
auto processor = std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>(
new opentelemetry::sdk::trace::SimpleSpanProcessor(std::move(exporter)));
auto exporter = opentelemetry::exporter::otlp::OtlpGrpcExporterFactory::Create();
auto processor =
opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
processors.push_back(std::move(processor));

auto context = std::make_shared<opentelemetry::sdk::trace::TracerContext>(std::move(processors));
auto provider = opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new opentelemetry::sdk::trace::TracerProvider(context));
std::shared_ptr<opentelemetry::sdk::trace::TracerContext> context =
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors));
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
opentelemetry::sdk::trace::TracerProviderFactory::Create(context);
// Set the global trace provider
opentelemetry::trace::Provider::SetTracerProvider(provider);

Expand Down