Skip to content

Commit 41821d6

Browse files
authored
Fix console debug logs for otlp exporters. (#1848)
Fixes #1847
1 parent 57bf8c2 commit 41821d6

File tree

8 files changed

+49
-17
lines changed

8 files changed

+49
-17
lines changed

examples/otlp/http_log_main.cc

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h"
77
# include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h"
88
# include "opentelemetry/logs/provider.h"
9+
# include "opentelemetry/sdk/common/global_log_handler.h"
910
# include "opentelemetry/sdk/logs/logger_provider_factory.h"
1011
# include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h"
1112
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
@@ -27,6 +28,8 @@ namespace logs_sdk = opentelemetry::sdk::logs;
2728
namespace logs = opentelemetry::logs;
2829
namespace trace_sdk = opentelemetry::sdk::trace;
2930

31+
namespace internal_log = opentelemetry::sdk::common::internal_log;
32+
3033
namespace
3134
{
3235

@@ -56,6 +59,15 @@ void InitLogger()
5659
}
5760
} // namespace
5861

62+
/*
63+
Usage:
64+
- example_otlp_http_log
65+
- example_otlp_http_log <URL>
66+
- example_otlp_http_log <URL> <DEBUG>
67+
- example_otlp_http_log <URL> <DEBUG> <BIN>
68+
<DEBUG> = yes|no, to turn console debug on or off
69+
<BIN> = bin, to export in binary format
70+
*/
5971
int main(int argc, char *argv[])
6072
{
6173
if (argc > 1)
@@ -78,6 +90,12 @@ int main(int argc, char *argv[])
7890
}
7991
}
8092
}
93+
94+
if (opts.console_debug)
95+
{
96+
internal_log::GlobalLogHandler::SetLogLevel(internal_log::LogLevel::Debug);
97+
}
98+
8199
InitLogger();
82100
InitTracer();
83101
foo_library();

examples/otlp/http_main.cc

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
55
#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
6+
#include "opentelemetry/sdk/common/global_log_handler.h"
67
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
78
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
89
#include "opentelemetry/trace/provider.h"
@@ -20,6 +21,8 @@ namespace nostd = opentelemetry::nostd;
2021
namespace trace_sdk = opentelemetry::sdk::trace;
2122
namespace otlp = opentelemetry::exporter::otlp;
2223

24+
namespace internal_log = opentelemetry::sdk::common::internal_log;
25+
2326
namespace
2427
{
2528
opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts;
@@ -35,6 +38,15 @@ void InitTracer()
3538
}
3639
} // namespace
3740

41+
/*
42+
Usage:
43+
- example_otlp_http
44+
- example_otlp_http <URL>
45+
- example_otlp_http <URL> <DEBUG>
46+
- example_otlp_http <URL> <DEBUG> <BIN>
47+
<DEBUG> = yes|no, to turn console debug on or off
48+
<BIN> = bin, to export in binary format
49+
*/
3850
int main(int argc, char *argv[])
3951
{
4052
if (argc > 1)
@@ -55,6 +67,12 @@ int main(int argc, char *argv[])
5567
}
5668
}
5769
}
70+
71+
if (opts.console_debug)
72+
{
73+
internal_log::GlobalLogHandler::SetLogLevel(internal_log::LogLevel::Debug);
74+
}
75+
5876
// Removing this line will leave the default noop TracerProvider in place.
5977
InitTracer();
6078

exporters/elasticsearch/src/es_log_record_exporter.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ sdk::common::ExportResult ElasticsearchLogRecordExporter::Export(
355355
}
356356
else
357357
{
358-
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] DEBUG: Export " << span_count
359-
<< " trace span(s) success");
358+
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Export " << span_count
359+
<< " trace span(s) success");
360360
}
361361
return true;
362362
},

exporters/otlp/src/otlp_http_client.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ResponseHandler : public http_client::EventHandler
113113
{
114114
log_message = BuildResponseLogMessage(response, body_);
115115

116-
OTEL_INTERNAL_LOG_ERROR("OTLP HTTP Client] Export failed, " << log_message);
116+
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] Export failed, " << log_message);
117117
result = sdk::common::ExportResult::kFailure;
118118
}
119119
else if (console_debug_)
@@ -289,7 +289,7 @@ class ResponseHandler : public http_client::EventHandler
289289
case http_client::SessionState::WriteError:
290290
if (console_debug_)
291291
{
292-
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG:Session state: error writing request");
292+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Session state: error writing request");
293293
}
294294
break;
295295

@@ -760,7 +760,7 @@ sdk::common::ExportResult OtlpHttpClient::Export(
760760
if (options_.console_debug)
761761
{
762762
OTEL_INTERNAL_LOG_DEBUG(
763-
"[OTLP HTTP Client] DEBUG: Waiting for response from "
763+
"[OTLP HTTP Client] Waiting for response from "
764764
<< options_.url << " (timeout = "
765765
<< std::chrono::duration_cast<std::chrono::milliseconds>(options_.timeout).count()
766766
<< " milliseconds)");

exporters/otlp/src/otlp_http_exporter.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export(
9494
}
9595
else
9696
{
97-
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << span_count
98-
<< " trace span(s) success");
97+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << span_count
98+
<< " trace span(s) success");
9999
}
100100
return true;
101101
});
@@ -109,8 +109,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export(
109109
}
110110
else
111111
{
112-
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << span_count
113-
<< " trace span(s) success");
112+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << span_count << " trace span(s) success");
114113
}
115114
return opentelemetry::sdk::common::ExportResult::kSuccess;
116115
#endif

exporters/otlp/src/otlp_http_log_record_exporter.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpLogRecordExporter::Export(
9999
}
100100
else
101101
{
102-
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << log_count
103-
<< " log(s) success");
102+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << log_count << " log(s) success");
104103
}
105104
return true;
106105
});
@@ -114,7 +113,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpLogRecordExporter::Export(
114113
}
115114
else
116115
{
117-
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << log_count << " log(s) success");
116+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << log_count << " log(s) success");
118117
}
119118
return opentelemetry::sdk::common::ExportResult::kSuccess;
120119
# endif

exporters/otlp/src/otlp_http_metric_exporter.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpMetricExporter::Export(
9999
}
100100
else
101101
{
102-
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << metric_count
103-
<< " metric(s) success");
102+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << metric_count << " metric(s) success");
104103
}
105104
return true;
106105
});
@@ -114,8 +113,7 @@ opentelemetry::sdk::common::ExportResult OtlpHttpMetricExporter::Export(
114113
}
115114
else
116115
{
117-
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] DEBUG: Export " << metric_count
118-
<< " metric(s) success");
116+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export " << metric_count << " metric(s) success");
119117
}
120118
return opentelemetry::sdk::common::ExportResult::kSuccess;
121119
#endif

sdk/src/common/global_log_handler.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void DefaultLogHandler::Handle(LogLevel level,
2626
output_s << "[" << LevelToString(level) << "] ";
2727
if (file != nullptr)
2828
{
29-
output_s << "File: " << file << ":" << line;
29+
output_s << "File: " << file << ":" << line << " ";
3030
}
3131
if (msg != nullptr)
3232
{

0 commit comments

Comments
 (0)