Skip to content

Commit

Permalink
Resolved somemore warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
msiddhu committed Jul 7, 2024
1 parent 3202581 commit db9c19d
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 131 deletions.
44 changes: 28 additions & 16 deletions examples/otlp/file_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"

#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
Expand Down Expand Up @@ -109,29 +110,40 @@ void CleanupLogger()

int main(int argc, char *argv[])
{
if (argc > 1)
try
{
opentelemetry::exporter::otlp::OtlpFileClientFileSystemOptions fs_backend;
fs_backend.file_pattern = argv[1];
opts.backend_options = fs_backend;
if (argc > 2)
if (argc > 1)
{
opentelemetry::exporter::otlp::OtlpFileClientFileSystemOptions logs_fs_backend;
logs_fs_backend.file_pattern = argv[2];
log_opts.backend_options = logs_fs_backend;
opentelemetry::exporter::otlp::OtlpFileClientFileSystemOptions fs_backend;
fs_backend.file_pattern = argv[1];
opts.backend_options = fs_backend;
if (argc > 2)
{
opentelemetry::exporter::otlp::OtlpFileClientFileSystemOptions logs_fs_backend;
logs_fs_backend.file_pattern = argv[2];
log_opts.backend_options = logs_fs_backend;
}
else
{
log_opts.backend_options = std::ref(std::cout);
}
}
else
{
log_opts.backend_options = std::ref(std::cout);
opts.backend_options = std::ref(std::cout);
}
InitLogger();
InitTracer();
foo_library();
CleanupTracer();
CleanupLogger();
}
else
catch (const std::exception &e)
{
opts.backend_options = std::ref(std::cout);
std::cerr << " [FileLogMain]: Error in main due to " << e.what() << "Exiting the program"
<< std::endl;
return EXIT_FAILURE;
}
InitLogger();
InitTracer();
foo_library();
CleanupTracer();
CleanupLogger();

return EXIT_SUCCESS;
}
18 changes: 9 additions & 9 deletions exporters/ostream/test/ostream_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ TEST(OStreamLogRecordExporter, SimpleLogToCout)
" severity_text : TRACE\n"
" body : Message\n",
" resource : \n",
" telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n",
" telemetry.sdk.version: ", OPENTELEMETRY_VERSION, "\n",
" telemetry.sdk.name: opentelemetry\n",
" telemetry.sdk.language: cpp\n",
" attributes : \n",
Expand Down Expand Up @@ -248,8 +248,8 @@ TEST(OStreamLogRecordExporter, LogWithStringAttributesToCerr)
std::vector<std::string> expected_output{
"{\n",
" timestamp : 0\n",
" severity_num : 0\n"
" severity_text : INVALID\n"
" severity_num : 0\n",
" severity_text : INVALID\n",
" body : \n",
" resource : \n",
" telemetry.sdk.version: ",
Expand Down Expand Up @@ -333,8 +333,8 @@ TEST(OStreamLogRecordExporter, LogWithVariantTypesToClog)
std::vector<std::string> expected_output{
"{\n",
" timestamp : 0\n",
" severity_num : 0\n"
" severity_text : INVALID\n"
" severity_num : 0\n",
" severity_text : INVALID\n",
" body : \n",
" resource : \n",
" service.name: unknown_service\n",
Expand Down Expand Up @@ -409,8 +409,8 @@ TEST(OStreamLogRecordExporter, IntegrationTest)
std::vector<std::string> expected_output{
"{\n",
" timestamp : " + std::to_string(now.time_since_epoch().count()) + "\n",
" severity_num : 5\n"
" severity_text : DEBUG\n"
" severity_num : 5\n",
" severity_text : DEBUG\n",
" body : Hello\n",
" resource : \n",
" telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n",
Expand Down Expand Up @@ -482,8 +482,8 @@ TEST(OStreamLogRecordExporter, IntegrationTestWithEventId)

// Compare actual vs expected outputs
std::vector<std::string> expected_output{
" severity_num : 5\n"
" severity_text : DEBUG\n"
" severity_num : 5\n",
" severity_text : DEBUG\n",
" body : Hello {key1} {key2}\n",
" resource : \n",
" telemetry.sdk.version: " OPENTELEMETRY_VERSION "\n",
Expand Down
3 changes: 3 additions & 0 deletions exporters/otlp/src/otlp_file_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ static void ConvertListFieldToJson(nlohmann::json &value,
const google::protobuf::Message &message,
const google::protobuf::FieldDescriptor *field_descriptor);

// NOLINTBEGIN(misc-no-recursion)
static void ConvertGenericMessageToJson(nlohmann::json &value,
const google::protobuf::Message &message)
{
Expand Down Expand Up @@ -953,6 +954,8 @@ void ConvertListFieldToJson(nlohmann::json &value,
}
}

// NOLINTEND(misc-no-recursion) suppressing for performance as if implemented with stack needs
// Dynamic memory allocation
} // namespace

class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender
Expand Down
60 changes: 38 additions & 22 deletions exporters/zipkin/src/zipkin_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,54 @@ sdk::common::ExportResult ZipkinExporter::Export(
exporter::zipkin::ZipkinSpan json_spans = {};
for (auto &recordable : spans)
{
auto rec = std::unique_ptr<Recordable>(static_cast<Recordable *>(recordable.release()));
if (rec != nullptr)
try
{
auto json_span = rec->span();
// add localEndPoint
json_span["localEndpoint"] = local_end_point_;
// check service.name
auto service_name = rec->GetServiceName();
if (service_name.size())
auto rec = std::unique_ptr<Recordable>(static_cast<Recordable *>(recordable.release()));
if (rec != nullptr)
{
json_span["localEndpoint"]["serviceName"] = service_name;
auto json_span = rec->span();
// add localEndPoint
json_span["localEndpoint"] = local_end_point_;
// check service.name
auto service_name = rec->GetServiceName();
if (service_name.size())
{
json_span["localEndpoint"]["serviceName"] = service_name;
}
json_spans.push_back(json_span);
}
json_spans.push_back(json_span);
}
catch (const std::exception &e)
{
OTEL_INTERNAL_LOG_ERROR(
"[Zipkin Trace Exporter] Exception while processing spans: " << e.what());
}
}
auto body_s = json_spans.dump();
http_client::Body body_v(body_s.begin(), body_s.end());
auto result = http_client_->PostNoSsl(url_parser_.url_, body_v, options_.headers);
if (result &&
(result.GetResponse().GetStatusCode() >= 200 && result.GetResponse().GetStatusCode() <= 299))
{
return sdk::common::ExportResult::kSuccess;
}
else

try
{
if (result.GetSessionState() == http_client::SessionState::ConnectFailed)
auto body_s = json_spans.dump();
http_client::Body body_v(body_s.begin(), body_s.end());
auto result = http_client_->PostNoSsl(url_parser_.url_, body_v, options_.headers);
if (result && (result.GetResponse().GetStatusCode() >= 200 &&
result.GetResponse().GetStatusCode() <= 299))
{
OTEL_INTERNAL_LOG_ERROR("ZIPKIN EXPORTER] Zipkin Exporter: Connection failed");
return sdk::common::ExportResult::kSuccess;
}
else
{
if (result.GetSessionState() == http_client::SessionState::ConnectFailed)
{
OTEL_INTERNAL_LOG_ERROR("ZIPKIN EXPORTER] Zipkin Exporter: Connection failed");
}
return sdk::common::ExportResult::kFailure;
}
}
catch (const std::exception &e)
{
OTEL_INTERNAL_LOG_ERROR("[Zipkin Trace Exporter] Exception during HTTP request: " << e.what());
return sdk::common::ExportResult::kFailure;
}
return sdk::common::ExportResult::kSuccess;
}

void ZipkinExporter::InitializeLocalEndpoint()
Expand Down
2 changes: 1 addition & 1 deletion functional/otlp/func_http_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const int TEST_FAILED = 1;
Command line parameters.
*/

enum test_mode
enum test_mode : std::uint8_t
{
MODE_NONE,
MODE_HTTP,
Expand Down
73 changes: 47 additions & 26 deletions sdk/src/logs/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/logs/logger.h"
#include "opentelemetry/sdk/logs/logger_context.h"
Expand Down Expand Up @@ -49,44 +50,64 @@ const opentelemetry::nostd::string_view Logger::GetName() noexcept

opentelemetry::nostd::unique_ptr<opentelemetry::logs::LogRecord> Logger::CreateLogRecord() noexcept
{
auto recordable = context_->GetProcessor().MakeRecordable();
std::unique_ptr<Recordable> recordable;
try
{
recordable = context_->GetProcessor().MakeRecordable();
}
catch (const std::exception &e)
{
OTEL_INTERNAL_LOG_ERROR("[Logger::CreateLogRecord]"
<< "Error while creating recordable: " << e.what()
<< " Unable to create new Log Record");
return nullptr;
}

recordable->SetObservedTimestamp(std::chrono::system_clock::now());

if (opentelemetry::context::RuntimeContext::GetCurrent().HasKey(opentelemetry::trace::kSpanKey))
try
{
opentelemetry::context::ContextValue context_value =
opentelemetry::context::RuntimeContext::GetCurrent().GetValue(
opentelemetry::trace::kSpanKey);
if (opentelemetry::nostd::holds_alternative<
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>>(context_value))
if (opentelemetry::context::RuntimeContext::GetCurrent().HasKey(opentelemetry::trace::kSpanKey))
{
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> &data =
opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>>(
context_value);
if (data)
opentelemetry::context::ContextValue context_value =
opentelemetry::context::RuntimeContext::GetCurrent().GetValue(
opentelemetry::trace::kSpanKey);
if (opentelemetry::nostd::holds_alternative<
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>>(context_value))
{
recordable->SetTraceId(data->GetContext().trace_id());
recordable->SetTraceFlags(data->GetContext().trace_flags());
recordable->SetSpanId(data->GetContext().span_id());
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> &data =
opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>>(
context_value);
if (data)
{
recordable->SetTraceId(data->GetContext().trace_id());
recordable->SetTraceFlags(data->GetContext().trace_flags());
recordable->SetSpanId(data->GetContext().span_id());
}
}
}
else if (opentelemetry::nostd::holds_alternative<
opentelemetry::nostd::shared_ptr<trace::SpanContext>>(context_value))
{
opentelemetry::nostd::shared_ptr<trace::SpanContext> &data =
opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<trace::SpanContext>>(
context_value);
if (data)
else if (opentelemetry::nostd::holds_alternative<
opentelemetry::nostd::shared_ptr<trace::SpanContext>>(context_value))
{
recordable->SetTraceId(data->trace_id());
recordable->SetTraceFlags(data->trace_flags());
recordable->SetSpanId(data->span_id());
opentelemetry::nostd::shared_ptr<trace::SpanContext> &data =
opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<trace::SpanContext>>(
context_value);
if (data)
{
recordable->SetTraceId(data->trace_id());
recordable->SetTraceFlags(data->trace_flags());
recordable->SetSpanId(data->span_id());
}
}
}
}

return opentelemetry::nostd::unique_ptr<opentelemetry::logs::LogRecord>(recordable.release());
}
catch (const std::exception &e)
{
OTEL_INTERNAL_LOG_ERROR("[Logger::CreateLogRecord]: " << e.what()
<< "Unable to create a new LogRecord");
return nullptr;
}
}

void Logger::EmitLogRecord(
Expand Down
Loading

0 comments on commit db9c19d

Please sign in to comment.