[CODE HEALTH] Fix clang-tidy bugprone-exception-escape in ostream exporters#4137
[CODE HEALTH] Fix clang-tidy bugprone-exception-escape in ostream exporters#4137jensecrest wants to merge 13 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4137 +/- ##
==========================================
- Coverage 82.83% 82.71% -0.12%
==========================================
Files 406 406
Lines 16913 16937 +24
==========================================
- Hits 14009 14007 -2
- Misses 2904 2930 +26
🚀 New features to boost your workflow:
|
|
Could you please update warning_limit in |
…/jensecrest/opentelemetry-cpp into clangtidy-bugproneexcesc-ostream
Didn't see exception paths for exporters in other tests, so I didn't think it would be necessary to artificially throw and create a test around that here. Please let me know if you think this is necessary. |
Updated. |
To fix CI formatting error - remove whitespace from line 66
|
@owent This PR should be ready for a re-review if you're able to take another look! |
All checks were passing up until I updated the warning_limits in .github/workflows/clang-tidy.yaml to be exactly the number of warnings that were remaining after this change. Just rebased yet again and now those checks are failing, so a different PR must have increased the clang-tidy warnings. Do I need to re-update to increase the expected number of warnings then? |
|
LGTM once the remaining clang-tidy checks pass. |
Fixes #2053
Changes
Fixes 3
bugprone-exception-escapeclang-tidy warnings in the ostream exporters by wrapping the throwing portions ofExportintry/catch, guarded by#if OPENTELEMETRY_HAVE_EXCEPTIONS.OStreamLogRecordExporter::Exportinlog_record_exporter.ccOStreamSpanExporter::Exportinspan_exporter.ccOStreamMetricExporter::Exportinmetric_exporter.ccWhat the problem is
All three
Exportfunctions are markednoexcept(required by their base class interfaces), but contain operations that can throw:sout_ << ...—std::ostream::operator<<can throwstd::ios_base::failureif exceptions are enabled on the streamstd::string(char*, len)construction — can throwstd::bad_allocprintAttributes,printEvents,printInstrumentationInfoMetricData— all use ostream internallyIf any of these throw inside a
noexceptfunction, the program callsstd::terminate()— a crash.Why try/catch is the minimal fix
noexcept— it's on the pure virtual base class interfaces (LogRecordExporter,SpanExporter,PushMetricExporter). Changing it would be an API/ABI break.operator<<orstd::stringconstruction. The entire purpose of these functions is to write to an ostream.try/catchis the established pattern in this codebase — seePeriodicExportingMetricReader::CollectAndExportOnce()inperiodic_exporting_metric_reader.cc, which uses the same#if OPENTELEMETRY_HAVE_EXCEPTIONS/try/catchstructure.What the change does
Wraps the throwing portion of each
Exportfunction intry/catch, guarded by#if OPENTELEMETRY_HAVE_EXCEPTIONS(the project's standard guard for builds with exceptions disabled via-fno-exceptions). On exception, the error is logged viaOTEL_INTERNAL_LOG_ERRORandkFailureis returned.What the change does NOT do
noexceptspecifierkSuccesskFailurebefore the try block)std::terminate()(crash) to logging an error and returningkFailure(graceful degradation)For significant contributions please make sure you have completed the following items:
CHANGELOG.mdupdated for non-trivial changesAI
Assisted-by: This contribution was prepared with the assistance of an AI development tool.