Skip to content

Commit

Permalink
Fix console debug logs for otlp exporters. (#1848)
Browse files Browse the repository at this point in the history
Fixes #1847
  • Loading branch information
marcalff authored Dec 7, 2022
1 parent 57bf8c2 commit 41821d6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 17 deletions.
18 changes: 18 additions & 0 deletions examples/otlp/http_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h"
# include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h"
# include "opentelemetry/logs/provider.h"
# include "opentelemetry/sdk/common/global_log_handler.h"
# include "opentelemetry/sdk/logs/logger_provider_factory.h"
# include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
Expand All @@ -27,6 +28,8 @@ namespace logs_sdk = opentelemetry::sdk::logs;
namespace logs = opentelemetry::logs;
namespace trace_sdk = opentelemetry::sdk::trace;

namespace internal_log = opentelemetry::sdk::common::internal_log;

namespace
{

Expand Down Expand Up @@ -56,6 +59,15 @@ void InitLogger()
}
} // namespace

/*
Usage:
- example_otlp_http_log
- example_otlp_http_log <URL>
- example_otlp_http_log <URL> <DEBUG>
- example_otlp_http_log <URL> <DEBUG> <BIN>
<DEBUG> = yes|no, to turn console debug on or off
<BIN> = bin, to export in binary format
*/
int main(int argc, char *argv[])
{
if (argc > 1)
Expand All @@ -78,6 +90,12 @@ int main(int argc, char *argv[])
}
}
}

if (opts.console_debug)
{
internal_log::GlobalLogHandler::SetLogLevel(internal_log::LogLevel::Debug);
}

InitLogger();
InitTracer();
foo_library();
Expand Down
18 changes: 18 additions & 0 deletions examples/otlp/http_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/provider.h"
Expand All @@ -20,6 +21,8 @@ namespace nostd = opentelemetry::nostd;
namespace trace_sdk = opentelemetry::sdk::trace;
namespace otlp = opentelemetry::exporter::otlp;

namespace internal_log = opentelemetry::sdk::common::internal_log;

namespace
{
opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts;
Expand All @@ -35,6 +38,15 @@ void InitTracer()
}
} // namespace

/*
Usage:
- example_otlp_http
- example_otlp_http <URL>
- example_otlp_http <URL> <DEBUG>
- example_otlp_http <URL> <DEBUG> <BIN>
<DEBUG> = yes|no, to turn console debug on or off
<BIN> = bin, to export in binary format
*/
int main(int argc, char *argv[])
{
if (argc > 1)
Expand All @@ -55,6 +67,12 @@ int main(int argc, char *argv[])
}
}
}

if (opts.console_debug)
{
internal_log::GlobalLogHandler::SetLogLevel(internal_log::LogLevel::Debug);
}

// Removing this line will leave the default noop TracerProvider in place.
InitTracer();

Expand Down
4 changes: 2 additions & 2 deletions exporters/elasticsearch/src/es_log_record_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ sdk::common::ExportResult ElasticsearchLogRecordExporter::Export(
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] DEBUG: Export " << span_count
<< " trace span(s) success");
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Export " << span_count
<< " trace span(s) success");
}
return true;
},
Expand Down
6 changes: 3 additions & 3 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ResponseHandler : public http_client::EventHandler
{
log_message = BuildResponseLogMessage(response, body_);

OTEL_INTERNAL_LOG_ERROR("OTLP HTTP Client] Export failed, " << log_message);
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Export failed, " << log_message);
result = sdk::common::ExportResult::kFailure;
}
else if (console_debug_)
Expand Down Expand Up @@ -289,7 +289,7 @@ class ResponseHandler : public http_client::EventHandler
case http_client::SessionState::WriteError:
if (console_debug_)
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG:Session state: error writing request");
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Session state: error writing request");
}
break;

Expand Down Expand Up @@ -760,7 +760,7 @@ sdk::common::ExportResult OtlpHttpClient::Export(
if (options_.console_debug)
{
OTEL_INTERNAL_LOG_DEBUG(
"[OTLP HTTP Client] DEBUG: Waiting for response from "
"[OTLP HTTP Client] Waiting for response from "
<< options_.url << " (timeout = "
<< std::chrono::duration_cast<std::chrono::milliseconds>(options_.timeout).count()
<< " milliseconds)");
Expand Down
7 changes: 3 additions & 4 deletions exporters/otlp/src/otlp_http_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export(
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << span_count
<< " trace span(s) success");
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << span_count
<< " trace span(s) success");
}
return true;
});
Expand All @@ -109,8 +109,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export(
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << span_count
<< " trace span(s) success");
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << span_count << " trace span(s) success");
}
return opentelemetry::sdk::common::ExportResult::kSuccess;
#endif
Expand Down
5 changes: 2 additions & 3 deletions exporters/otlp/src/otlp_http_log_record_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpLogRecordExporter::Export(
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << log_count
<< " log(s) success");
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << log_count << " log(s) success");
}
return true;
});
Expand All @@ -114,7 +113,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpLogRecordExporter::Export(
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << log_count << " log(s) success");
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << log_count << " log(s) success");
}
return opentelemetry::sdk::common::ExportResult::kSuccess;
# endif
Expand Down
6 changes: 2 additions & 4 deletions exporters/otlp/src/otlp_http_metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpMetricExporter::Export(
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << metric_count
<< " metric(s) success");
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << metric_count << " metric(s) success");
}
return true;
});
Expand All @@ -114,8 +113,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpMetricExporter::Export(
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << metric_count
<< " metric(s) success");
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << metric_count << " metric(s) success");
}
return opentelemetry::sdk::common::ExportResult::kSuccess;
#endif
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/common/global_log_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void DefaultLogHandler::Handle(LogLevel level,
output_s << "[" << LevelToString(level) << "] ";
if (file != nullptr)
{
output_s << "File: " << file << ":" << line;
output_s << "File: " << file << ":" << line << " ";
}
if (msg != nullptr)
{
Expand Down

0 comments on commit 41821d6

Please sign in to comment.