diff --git a/CHANGELOG.md b/CHANGELOG.md index 34afb39b98..b84b7a0d7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,3 +69,4 @@ significant modifications will be credited to OpenTelemetry Authors. ([#290](https://github.com/open-telemetry/opentelemetry-demo/pull/290)) * Added Frontend [Cypress](https://www.cypress.io/) E2E tests ([#298](https://github.com/open-telemetry/opentelemetry-demo/pull/298)) +* Added baggage support in CurrencyService [#281](https://github.com/open-telemetry/opentelemetry-demo/pull/281) diff --git a/docker-compose.yml b/docker-compose.yml index 333c21f75f..fb478a6d6c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -120,7 +120,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}" environment: diff --git a/docs/trace_service_features.md b/docs/trace_service_features.md index 75ceb3b2b4..96eccc162c 100644 --- a/docs/trace_service_features.md +++ b/docs/trace_service_features.md @@ -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: | diff --git a/src/currencyservice/CMakeLists.txt b/src/currencyservice/CMakeLists.txt index d24357a4f8..90fc3b3bd9 100644 --- a/src/currencyservice/CMakeLists.txt +++ b/src/currencyservice/CMakeLists.txt @@ -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) diff --git a/src/currencyservice/src/client.cpp b/src/currencyservice/src/client.cpp index a4b3959e19..a648fcca7a 100644 --- a/src/currencyservice/src/client.cpp +++ b/src/currencyservice/src/client.cpp @@ -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); @@ -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()) { diff --git a/src/currencyservice/src/server.cpp b/src/currencyservice/src/server.cpp index b601147736..38420e45b4 100644 --- a/src/currencyservice/src/server.cpp +++ b/src/currencyservice/src/server.cpp @@ -7,14 +7,17 @@ #include #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 #include #include #include +#include using namespace std; @@ -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 @@ -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 baggageLists; + for (auto i = range.first; i != range.second; ++i) + { + baggageLists.emplace_back(std::string(i->second.data())); + } + std::vector> 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); @@ -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"); @@ -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 baggageLists; + for (auto i = range.first; i != range.second; ++i) + { + baggageLists.emplace_back(std::string(i->second.data())); + } + std::vector> 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); @@ -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"); diff --git a/src/currencyservice/src/tracer_common.h b/src/currencyservice/src/tracer_common.h index 09db16796b..bfea3210f7 100644 --- a/src/currencyservice/src/tracer_common.h +++ b/src/currencyservice/src/tracer_common.h @@ -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 #include @@ -72,16 +71,15 @@ class GrpcServerCarrier : public opentelemetry::context::propagation::TextMapCar void initTracer() { - auto exporter = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpGrpcExporter()); - auto processor = std::unique_ptr( - 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> processors; processors.push_back(std::move(processor)); - - auto context = std::make_shared(std::move(processors)); - auto provider = opentelemetry::nostd::shared_ptr( - new opentelemetry::sdk::trace::TracerProvider(context)); + std::shared_ptr context = + opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors)); + std::shared_ptr provider = + opentelemetry::sdk::trace::TracerProviderFactory::Create(context); // Set the global trace provider opentelemetry::trace::Provider::SetTracerProvider(provider);