From 1129fe974ad11e56c87511ab881fe5bb1cea5b52 Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Wed, 14 Feb 2024 15:08:04 +0100 Subject: [PATCH 1/7] Add OTel logs to currencyservice --- .env | 1 + docker-compose.minimal.yml | 4 +-- docker-compose.yml | 4 +-- src/currencyservice/Dockerfile | 2 +- src/currencyservice/src/logger_common.h | 35 +++++++++++++++++++++++++ src/currencyservice/src/meter_common.h | 10 +------ src/currencyservice/src/server.cpp | 27 +++++++++++++------ 7 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 src/currencyservice/src/logger_common.h diff --git a/.env b/.env index 29b9118758..c7875a1987 100644 --- a/.env +++ b/.env @@ -47,6 +47,7 @@ CHECKOUT_SERVICE_PORT=5050 CHECKOUT_SERVICE_ADDR=checkoutservice:${CHECKOUT_SERVICE_PORT} # Currency Service +OPENTELEMETRY_CPP_VERSION=1.13.0 CURRENCY_SERVICE_PORT=7001 CURRENCY_SERVICE_ADDR=currencyservice:${CURRENCY_SERVICE_PORT} diff --git a/docker-compose.minimal.yml b/docker-compose.minimal.yml index d7cdabb43b..e352450955 100644 --- a/docker-compose.minimal.yml +++ b/docker-compose.minimal.yml @@ -130,8 +130,7 @@ services: cache_from: - ${IMAGE_NAME}:${IMAGE_VERSION}-currencyservice args: - - GRPC_VERSION=1.46.0 - - OPENTELEMETRY_VERSION=1.5.0 + - OPENTELEMETRY_CPP_VERSION deploy: resources: limits: @@ -141,6 +140,7 @@ services: - "${CURRENCY_SERVICE_PORT}" environment: - CURRENCY_SERVICE_PORT + - VERSION=${IMAGE_VERSION} - OTEL_EXPORTER_OTLP_ENDPOINT - OTEL_RESOURCE_ATTRIBUTES=${OTEL_RESOURCE_ATTRIBUTES},service.name=currencyservice # The C++ SDK does not support OTEL_SERVICE_NAME depends_on: diff --git a/docker-compose.yml b/docker-compose.yml index 4eaabc52bd..10b7a357de 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -161,8 +161,7 @@ services: cache_from: - ${IMAGE_NAME}:${IMAGE_VERSION}-currencyservice args: - - GRPC_VERSION=1.46.0 - - OPENTELEMETRY_VERSION=1.5.0 + - OPENTELEMETRY_CPP_VERSION deploy: resources: limits: @@ -172,6 +171,7 @@ services: - "${CURRENCY_SERVICE_PORT}" environment: - CURRENCY_SERVICE_PORT + - VERSION=${IMAGE_VERSION} - OTEL_EXPORTER_OTLP_ENDPOINT - OTEL_RESOURCE_ATTRIBUTES=${OTEL_RESOURCE_ATTRIBUTES},service.name=currencyservice # The C++ SDK does not support OTEL_SERVICE_NAME depends_on: diff --git a/src/currencyservice/Dockerfile b/src/currencyservice/Dockerfile index 38cb7f2dce..6aa591c180 100644 --- a/src/currencyservice/Dockerfile +++ b/src/currencyservice/Dockerfile @@ -18,7 +18,7 @@ FROM alpine:3.18 as builder RUN apk update && apk add git cmake make g++ grpc-dev protobuf-dev linux-headers -ARG OPENTELEMETRY_CPP_VERSION=1.13.0 +ARG OPENTELEMETRY_CPP_VERSION RUN git clone https://github.com/open-telemetry/opentelemetry-cpp \ && cd opentelemetry-cpp/ \ diff --git a/src/currencyservice/src/logger_common.h b/src/currencyservice/src/logger_common.h new file mode 100644 index 0000000000..5c931a16d4 --- /dev/null +++ b/src/currencyservice/src/logger_common.h @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h" +#include "opentelemetry/logs/provider.h" +#include "opentelemetry/sdk/logs/logger.h" +#include "opentelemetry/sdk/logs/logger_provider_factory.h" +#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" +#include "opentelemetry/sdk/logs/logger_context_factory.h" +#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h" + +using namespace std; +namespace nostd = opentelemetry::nostd; +namespace otlp = opentelemetry::exporter::otlp; +namespace logs = opentelemetry::logs; +namespace logs_sdk = opentelemetry::sdk::logs; + +namespace +{ + void initLogger() { + otlp::OtlpGrpcLogRecordExporterOptions loggerOptions; + auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(loggerOptions); + auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); + std::vector> processors; + processors.push_back(std::move(processor)); + auto context = logs_sdk::LoggerContextFactory::Create(std::move(processors)); + std::shared_ptr provider = logs_sdk::LoggerProviderFactory::Create(std::move(context)); + opentelemetry::logs::Provider::SetLoggerProvider(provider); + } + + nostd::shared_ptr getLogger(std::string name){ + auto provider = logs::Provider::GetLoggerProvider(); + return provider->GetLogger(name + "_logger", name, OPENTELEMETRY_SDK_VERSION); + } +} \ No newline at end of file diff --git a/src/currencyservice/src/meter_common.h b/src/currencyservice/src/meter_common.h index 61ae118994..72f86f4695 100644 --- a/src/currencyservice/src/meter_common.h +++ b/src/currencyservice/src/meter_common.h @@ -16,22 +16,14 @@ namespace otlp_exporter = opentelemetry::exporter::otlp; namespace { - std::string version{ "1.3.0" }; - std::string name{ "app_currency" }; - std::string schema{ "https://opentelemetry.io/schemas/1.2.0" }; - void initMeter() { // Build MetricExporter otlp_exporter::OtlpGrpcMetricExporterOptions otlpOptions; - // Configuration via environment variable not supported yet - //otlpOptions.aggregation_temporality = otlp_exporter::PreferredAggregationTemporality::kCumulative; auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(otlpOptions); // Build MeterProvider and Reader metric_sdk::PeriodicExportingMetricReaderOptions options; - //options.export_interval_millis = std::chrono::milliseconds(60000); - //options.export_timeout_millis = std::chrono::milliseconds(30000); std::unique_ptr reader{ new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options) }; auto provider = std::shared_ptr(new metric_sdk::MeterProvider()); @@ -40,7 +32,7 @@ namespace metrics_api::Provider::SetMeterProvider(provider); } - nostd::unique_ptr> initIntCounter() + nostd::unique_ptr> initIntCounter(std::string name, std::string version) { std::string counter_name = name + "_counter"; auto provider = metrics_api::Provider::GetMeterProvider(); diff --git a/src/currencyservice/src/server.cpp b/src/currencyservice/src/server.cpp index 26b07a6524..76ebc3e0a1 100644 --- a/src/currencyservice/src/server.cpp +++ b/src/currencyservice/src/server.cpp @@ -1,6 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include #include #include #include @@ -11,6 +12,7 @@ #include "opentelemetry/trace/span_context_kv_iterable_view.h" #include "opentelemetry/baggage/baggage.h" #include "opentelemetry/nostd/string_view.h" +#include "logger_common.h" #include "meter_common.h" #include "tracer_common.h" @@ -80,7 +82,11 @@ namespace {"ZAR", 16.0583}, }; + std::string version = std::getenv("VERSION"); + std::string name{ "currencyservice" }; + nostd::unique_ptr> currency_counter; + nostd::shared_ptr logger; class HealthServer final : public grpc::health::v1::Health::Service { @@ -127,10 +133,11 @@ class CurrencyService final : public oteldemo::CurrencyService::Service span->AddEvent("Currencies fetched, response sent back"); span->SetStatus(StatusCode::kOk); + + logger->Info(std::string(__func__) + " successful"); + // Make sure to end your spans! span->End(); - - std::cout << __func__ << " successful" << std::endl; return Status::OK; } @@ -203,14 +210,18 @@ class CurrencyService final : public oteldemo::CurrencyService::Service // End the span span->AddEvent("Conversion successful, response sent back"); span->SetStatus(StatusCode::kOk); - std::cout << __func__ << " conversion successful" << std::endl; + + logger->Info(std::string(__func__) + " conversion successful"); + span->End(); return Status::OK; } catch(...) { span->AddEvent("Conversion failed"); span->SetStatus(StatusCode::kError); - std::cout << __func__ << " conversion failure" << std::endl; + + logger->Error(std::string(__func__) + " conversion failure"); + span->End(); return Status::CANCELLED; } @@ -237,7 +248,7 @@ void RunServer(uint16_t port) builder.AddListeningPort(address, grpc::InsecureServerCredentials()); std::unique_ptr server(builder.BuildAndStart()); - std::cout << "Currency Server listening on port: " << address << std::endl; + logger->Debug("Currency Server listening on port: " + address); server->Wait(); server->Shutdown(); } @@ -252,11 +263,11 @@ int main(int argc, char **argv) { uint16_t port = atoi(argv[1]); - std::cout << "Port: " << port << "\n"; - initTracer(); initMeter(); - currency_counter = initIntCounter(); + initLogger(); + currency_counter = initIntCounter(name, version); + logger = getLogger(name); RunServer(port); return 0; From 3bca94edf0cabac3faa4a8a4a79fe6740f2968a3 Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Wed, 14 Feb 2024 15:16:08 +0100 Subject: [PATCH 2/7] line break --- src/currencyservice/src/logger_common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/currencyservice/src/logger_common.h b/src/currencyservice/src/logger_common.h index 5c931a16d4..cd58165580 100644 --- a/src/currencyservice/src/logger_common.h +++ b/src/currencyservice/src/logger_common.h @@ -32,4 +32,4 @@ namespace auto provider = logs::Provider::GetLoggerProvider(); return provider->GetLogger(name + "_logger", name, OPENTELEMETRY_SDK_VERSION); } -} \ No newline at end of file +} From a09d5fa2af54b655fdd5e48c8bc7993b90d1d9da Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Wed, 14 Feb 2024 15:17:52 +0100 Subject: [PATCH 3/7] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 173f7221a3..a670f9bada 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,8 @@ the release. ([#1359](https://github.com/open-telemetry/opentelemetry-demo/pull/1359)) * [productcatalog] allow products to be extended ([#1363](https://github.com/open-telemetry/opentelemetry-demo/pull/1363)) +* [currencyservice] Add OTLP logs + ([#1378](https://github.com/open-telemetry/opentelemetry-demo/pull/1378)) ## 1.7.2 From 3d9292cb89c7e7e0f80be957307baf70f56d7e6f Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Wed, 14 Feb 2024 15:47:55 +0100 Subject: [PATCH 4/7] Move OPENTELEMETRY_CPP_VERSION back --- .env | 1 - docker-compose.minimal.yml | 2 -- docker-compose.yml | 2 -- src/currencyservice/Dockerfile | 2 +- 4 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.env b/.env index c7875a1987..29b9118758 100644 --- a/.env +++ b/.env @@ -47,7 +47,6 @@ CHECKOUT_SERVICE_PORT=5050 CHECKOUT_SERVICE_ADDR=checkoutservice:${CHECKOUT_SERVICE_PORT} # Currency Service -OPENTELEMETRY_CPP_VERSION=1.13.0 CURRENCY_SERVICE_PORT=7001 CURRENCY_SERVICE_ADDR=currencyservice:${CURRENCY_SERVICE_PORT} diff --git a/docker-compose.minimal.yml b/docker-compose.minimal.yml index e352450955..54c37e4764 100644 --- a/docker-compose.minimal.yml +++ b/docker-compose.minimal.yml @@ -129,8 +129,6 @@ services: context: ./src/currencyservice cache_from: - ${IMAGE_NAME}:${IMAGE_VERSION}-currencyservice - args: - - OPENTELEMETRY_CPP_VERSION deploy: resources: limits: diff --git a/docker-compose.yml b/docker-compose.yml index 10b7a357de..af86a0635d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -160,8 +160,6 @@ services: context: ./src/currencyservice cache_from: - ${IMAGE_NAME}:${IMAGE_VERSION}-currencyservice - args: - - OPENTELEMETRY_CPP_VERSION deploy: resources: limits: diff --git a/src/currencyservice/Dockerfile b/src/currencyservice/Dockerfile index 6aa591c180..38cb7f2dce 100644 --- a/src/currencyservice/Dockerfile +++ b/src/currencyservice/Dockerfile @@ -18,7 +18,7 @@ FROM alpine:3.18 as builder RUN apk update && apk add git cmake make g++ grpc-dev protobuf-dev linux-headers -ARG OPENTELEMETRY_CPP_VERSION +ARG OPENTELEMETRY_CPP_VERSION=1.13.0 RUN git clone https://github.com/open-telemetry/opentelemetry-cpp \ && cd opentelemetry-cpp/ \ From 3df5a8d78d32b32ca8506ad82a979e49a1696e49 Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Thu, 15 Feb 2024 22:48:50 -0500 Subject: [PATCH 5/7] set opensearch version and timestamp field Signed-off-by: Pierre Tessier --- src/grafana/provisioning/datasources/opensearch.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/grafana/provisioning/datasources/opensearch.yaml b/src/grafana/provisioning/datasources/opensearch.yaml index 1de083338b..a6c085b122 100644 --- a/src/grafana/provisioning/datasources/opensearch.yaml +++ b/src/grafana/provisioning/datasources/opensearch.yaml @@ -16,4 +16,5 @@ datasources: logLevelField: severity logMessageField: body pplEnabled: true - timeField: "@timestamp" + timeField: observedTimestamp + version: 2.11.1 From 14b303304535b70f71ba8e992eb7488ce881fed3 Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Thu, 15 Feb 2024 22:49:01 -0500 Subject: [PATCH 6/7] add comment for opensearch version update Signed-off-by: Pierre Tessier --- .env | 1 + 1 file changed, 1 insertion(+) diff --git a/.env b/.env index 29b9118758..dbddebf4fa 100644 --- a/.env +++ b/.env @@ -8,6 +8,7 @@ IMAGE_NAME=ghcr.io/open-telemetry/demo COLLECTOR_CONTRIB_IMAGE=otel/opentelemetry-collector-contrib:0.93.0 GRAFANA_IMAGE=grafana/grafana:10.3.1 JAEGERTRACING_IMAGE=jaegertracing/all-in-one:1.53 +# must also update version field in /src/grafana/provisioning/datasources/opensearch.yml OPENSEARCH_IMAGE=opensearchproject/opensearch:2.11.1 POSTGRES_IMAGE=postgres:16.1 PROMETHEUS_IMAGE=quay.io/prometheus/prometheus:v2.49.1 From cb799dae1711d739347689cd0c35af45b971b8d3 Mon Sep 17 00:00:00 2001 From: Juliano Costa Date: Fri, 16 Feb 2024 13:31:48 +0100 Subject: [PATCH 7/7] Change log to INFO --- src/currencyservice/src/server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/currencyservice/src/server.cpp b/src/currencyservice/src/server.cpp index 76ebc3e0a1..e824e9ff39 100644 --- a/src/currencyservice/src/server.cpp +++ b/src/currencyservice/src/server.cpp @@ -248,7 +248,7 @@ void RunServer(uint16_t port) builder.AddListeningPort(address, grpc::InsecureServerCredentials()); std::unique_ptr server(builder.BuildAndStart()); - logger->Debug("Currency Server listening on port: " + address); + logger->Info("Currency Server listening on port: " + address); server->Wait(); server->Shutdown(); }