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

[currencyservice] Add OTLP logs #1378

Merged
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ the release.
([#1363](https://github.com/open-telemetry/opentelemetry-demo/pull/1363))
* [tests] update trace based tests for semantic conventions
([#1377](https://github.com/open-telemetry/opentelemetry-demo/pull/1377))
* [currencyservice] Add OTLP logs
([#1378](https://github.com/open-telemetry/opentelemetry-demo/pull/1378))
* [cartservice] update .NET to .NET 8.0.2
([#1380](https://github.com/open-telemetry/opentelemetry-demo/pull/1380))

Expand Down
4 changes: 1 addition & 3 deletions docker-compose.minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ services:
context: ./src/currencyservice
cache_from:
- ${IMAGE_NAME}:${IMAGE_VERSION}-currencyservice
args:
- GRPC_VERSION=1.46.0
- OPENTELEMETRY_VERSION=1.5.0
deploy:
resources:
limits:
Expand All @@ -141,6 +138,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:
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ services:
context: ./src/currencyservice
cache_from:
- ${IMAGE_NAME}:${IMAGE_VERSION}-currencyservice
args:
- GRPC_VERSION=1.46.0
- OPENTELEMETRY_VERSION=1.5.0
deploy:
resources:
limits:
Expand All @@ -172,6 +169,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:
Expand Down
35 changes: 35 additions & 0 deletions src/currencyservice/src/logger_common.h
Original file line number Diff line number Diff line change
@@ -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<std::unique_ptr<logs_sdk::LogRecordProcessor>> processors;
processors.push_back(std::move(processor));
auto context = logs_sdk::LoggerContextFactory::Create(std::move(processors));
std::shared_ptr<logs::LoggerProvider> provider = logs_sdk::LoggerProviderFactory::Create(std::move(context));
opentelemetry::logs::Provider::SetLoggerProvider(provider);
}

nostd::shared_ptr<opentelemetry::logs::Logger> getLogger(std::string name){
auto provider = logs::Provider::GetLoggerProvider();
return provider->GetLogger(name + "_logger", name, OPENTELEMETRY_SDK_VERSION);
}
}
10 changes: 1 addition & 9 deletions src/currencyservice/src/meter_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<metric_sdk::MetricReader> reader{
new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options) };
auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
Expand All @@ -40,7 +32,7 @@ namespace
metrics_api::Provider::SetMeterProvider(provider);
}

nostd::unique_ptr<metrics_api::Counter<uint64_t>> initIntCounter()
nostd::unique_ptr<metrics_api::Counter<uint64_t>> initIntCounter(std::string name, std::string version)
{
std::string counter_name = name + "_counter";
auto provider = metrics_api::Provider::GetMeterProvider();
Expand Down
27 changes: 19 additions & 8 deletions src/currencyservice/src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <demo.grpc.pb.h>
Expand All @@ -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"

Expand Down Expand Up @@ -80,7 +82,11 @@ namespace
{"ZAR", 16.0583},
};

std::string version = std::getenv("VERSION");
std::string name{ "currencyservice" };

nostd::unique_ptr<metrics_api::Counter<uint64_t>> currency_counter;
nostd::shared_ptr<opentelemetry::logs::Logger> logger;

class HealthServer final : public grpc::health::v1::Health::Service
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -237,7 +248,7 @@ void RunServer(uint16_t port)
builder.AddListeningPort(address, grpc::InsecureServerCredentials());

std::unique_ptr<Server> 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();
}
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/grafana/provisioning/datasources/opensearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ datasources:
logLevelField: severity
logMessageField: body
pplEnabled: true
timeField: "@timestamp"
timeField: observedTimestamp
version: 2.11.1
Loading