From ce3229f71254fc33bde078154b0ea4dd7e6c61df Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Mon, 8 Aug 2022 16:26:52 +0530 Subject: [PATCH 1/8] Updating grpc version --- docker-compose.yml | 2 +- src/currencyservice/CMakeLists.txt | 2 +- src/currencyservice/src/server.cpp | 18 ++++++++-------- src/currencyservice/src/tracer_common.h | 28 ++++++++++++------------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index ed5bc87567..1520788a30 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: 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/server.cpp b/src/currencyservice/src/server.cpp index b601147736..dd7021ca2d 100644 --- a/src/currencyservice/src/server.cpp +++ b/src/currencyservice/src/server.cpp @@ -7,7 +7,7 @@ #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 "tracer_common.h" @@ -102,10 +102,10 @@ 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); // Fetch and parse whatever HTTP headers we can from the gRPC request. @@ -162,10 +162,10 @@ 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); // Fetch and parse whatever HTTP headers we can from the gRPC 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); From 5df39b6e69bcedde0e34495d39a1edbdd7e3d23b Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Tue, 9 Aug 2022 18:10:53 +0530 Subject: [PATCH 2/8] Baggage changes --- src/currencyservice/src/client.cpp | 1 + src/currencyservice/src/server.cpp | 33 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/currencyservice/src/client.cpp b/src/currencyservice/src/client.cpp index a4b3959e19..5fc5aeb69d 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", "k1=V1,K2=v2;metadata,k3=v3"); supported_currencies.clear(); Status status = stub_->GetSupportedCurrencies(&context, request, &response); diff --git a/src/currencyservice/src/server.cpp b/src/currencyservice/src/server.cpp index dd7021ca2d..cd88c2bf78 100644 --- a/src/currencyservice/src/server.cpp +++ b/src/currencyservice/src/server.cpp @@ -10,11 +10,14 @@ #include "opentelemetry/trace/semantic_conventions.h" #include "opentelemetry/trace/span_context_kv_iterable_view.h" #include "tracer_common.h" +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/nostd/string_view.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,16 @@ 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"); + const char* baggage_str = nullptr; + for (auto i = range.first; i != range.second; ++i) + { + baggage_str = i->second.data(); + } + auto baggage = Baggage::FromHeader(std::string(baggage_str)); + StartSpanOptions options; options.kind = SpanKind::kServer; GrpcServerCarrier carrier(context); @@ -108,6 +121,14 @@ class CurrencyService final : public hipstershop::CurrencyService::Service {SemanticConventions::RPC_GRPC_STATUS_CODE, 0}}, options); auto scope = get_tracer("currencyservice")->WithActiveSpan(span); + + // 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 +171,16 @@ 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"); + const char* baggage_str = nullptr; + for (auto i = range.first; i != range.second; ++i) + { + baggage_str = i->second.data(); + } + auto baggage = Baggage::FromHeader(std::string(baggage_str)); + StartSpanOptions options; options.kind = SpanKind::kServer; GrpcServerCarrier carrier(context); From 543a94fb7ef8d452c4578d4c929c477a267af189 Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Wed, 10 Aug 2022 12:28:07 +0530 Subject: [PATCH 3/8] Added Baggage support --- src/currencyservice/src/client.cpp | 6 ++++ src/currencyservice/src/server.cpp | 45 +++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/currencyservice/src/client.cpp b/src/currencyservice/src/client.cpp index 5fc5aeb69d..1f635286ee 100644 --- a/src/currencyservice/src/client.cpp +++ b/src/currencyservice/src/client.cpp @@ -47,6 +47,8 @@ class CurrencyClient GetSupportedCurrenciesResponse response; ClientContext context; context.AddMetadata("baggage", "k1=V1,K2=v2;metadata,k3=v3"); + context.AddMetadata("unknown", "k1=V1,K2=v2;metadata,k3=v3"); + context.AddMetadata("baggage", "k4=V4,K5=v5;metadata,k6=v6"); supported_currencies.clear(); Status status = stub_->GetSupportedCurrencies(&context, request, &response); @@ -82,6 +84,9 @@ class CurrencyClient Money response; ClientContext context; + context.AddMetadata("baggage", "k1=V1,K2=v2;metadata,k3=v3"); + context.AddMetadata("unknown", "k1=V1,K2=v2;metadata,k3=v3"); + context.AddMetadata("baggage", "k4=V4,K5=v5;metadata,k6=v6"); Status status = stub_->Convert(&context, request, &response); if (status.ok()) { @@ -102,6 +107,7 @@ class CurrencyClient request.set_service("CurrencyService"); HealthCheckResponse response; ClientContext context; + Status s = hc_stub_->Check(&context, request, &response); if (s.ok()) { if (response.status() == grpc::health::v1::HealthCheckResponse::SERVING) { diff --git a/src/currencyservice/src/server.cpp b/src/currencyservice/src/server.cpp index cd88c2bf78..38420e45b4 100644 --- a/src/currencyservice/src/server.cpp +++ b/src/currencyservice/src/server.cpp @@ -9,9 +9,9 @@ #include "opentelemetry/trace/context.h" #include "opentelemetry/trace/semantic_conventions.h" #include "opentelemetry/trace/span_context_kv_iterable_view.h" -#include "tracer_common.h" #include "opentelemetry/baggage/baggage.h" #include "opentelemetry/nostd/string_view.h" +#include "tracer_common.h" #include #include @@ -96,12 +96,16 @@ class CurrencyService final : public hipstershop::CurrencyService::Service // Read baggage from client context auto clientContext = context->client_metadata(); auto range = clientContext.equal_range("baggage"); - const char* baggage_str = nullptr; + std::vector baggageLists; for (auto i = range.first; i != range.second; ++i) { - baggage_str = i->second.data(); + 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; } - auto baggage = Baggage::FromHeader(std::string(baggage_str)); StartSpanOptions options; options.kind = SpanKind::kServer; @@ -122,12 +126,14 @@ class CurrencyService final : public hipstershop::CurrencyService::Service options); auto scope = get_tracer("currencyservice")->WithActiveSpan(span); - // 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; - }); + 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"); @@ -174,12 +180,16 @@ class CurrencyService final : public hipstershop::CurrencyService::Service // Read baggage from client context auto clientContext = context->client_metadata(); auto range = clientContext.equal_range("baggage"); - const char* baggage_str = nullptr; + std::vector baggageLists; for (auto i = range.first; i != range.second; ++i) { - baggage_str = i->second.data(); + 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; } - auto baggage = Baggage::FromHeader(std::string(baggage_str)); StartSpanOptions options; options.kind = SpanKind::kServer; @@ -199,6 +209,15 @@ class CurrencyService final : public hipstershop::CurrencyService::Service {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"); From 1772fc27aee71076ef26713abc328777fe703c86 Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Wed, 10 Aug 2022 15:56:39 +0530 Subject: [PATCH 4/8] Updated docs --- docs/trace_service_features.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: | From 862af7e24c45eb1cc4ddfbe84a901aa2ac9579a4 Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Wed, 10 Aug 2022 16:13:24 +0530 Subject: [PATCH 5/8] Udpated client --- src/currencyservice/src/client.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/currencyservice/src/client.cpp b/src/currencyservice/src/client.cpp index 1f635286ee..94e56e55de 100644 --- a/src/currencyservice/src/client.cpp +++ b/src/currencyservice/src/client.cpp @@ -46,9 +46,7 @@ class CurrencyClient Empty request; GetSupportedCurrenciesResponse response; ClientContext context; - context.AddMetadata("baggage", "k1=V1,K2=v2;metadata,k3=v3"); - context.AddMetadata("unknown", "k1=V1,K2=v2;metadata,k3=v3"); - context.AddMetadata("baggage", "k4=V4,K5=v5;metadata,k6=v6"); + context.AddMetadata("baggage", "ServiceName=CurrencyService,Method=GetSupportedCurrencies"); supported_currencies.clear(); Status status = stub_->GetSupportedCurrencies(&context, request, &response); @@ -84,9 +82,8 @@ class CurrencyClient Money response; ClientContext context; - context.AddMetadata("baggage", "k1=V1,K2=v2;metadata,k3=v3"); - context.AddMetadata("unknown", "k1=V1,K2=v2;metadata,k3=v3"); - context.AddMetadata("baggage", "k4=V4,K5=v5;metadata,k6=v6"); + context.AddMetadata("baggage", "ServiceName=CurrencyService,Method=Convert"); + Status status = stub_->Convert(&context, request, &response); if (status.ok()) { From 1890e7731b58c0902ab7735def640bb1a175dc24 Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Wed, 10 Aug 2022 16:16:55 +0530 Subject: [PATCH 6/8] update --- src/currencyservice/src/client.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/currencyservice/src/client.cpp b/src/currencyservice/src/client.cpp index 94e56e55de..a648fcca7a 100644 --- a/src/currencyservice/src/client.cpp +++ b/src/currencyservice/src/client.cpp @@ -104,7 +104,6 @@ class CurrencyClient request.set_service("CurrencyService"); HealthCheckResponse response; ClientContext context; - Status s = hc_stub_->Check(&context, request, &response); if (s.ok()) { if (response.status() == grpc::health::v1::HealthCheckResponse::SERVING) { From d4595feeca50aecff9f86aeb16e4774b0c0fc65c Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Tue, 16 Aug 2022 10:07:32 +0530 Subject: [PATCH 7/8] update Readme --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34afb39b98..3a0e5ceb54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,3 +69,5 @@ 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) + From f624b8eb6504133cc8774bcd1faf9db7708b3534 Mon Sep 17 00:00:00 2001 From: DebajitDas Date: Tue, 16 Aug 2022 10:13:43 +0530 Subject: [PATCH 8/8] Updated Changelog --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a0e5ceb54..b84b7a0d7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,4 +70,3 @@ significant modifications will be credited to OpenTelemetry Authors. * 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) -