Skip to content

Commit b00958b

Browse files
authored
[BUILD] Cleanup cppcheck warnings (open-telemetry#3619)
1 parent cbfbb02 commit b00958b

File tree

12 files changed

+142
-68
lines changed

12 files changed

+142
-68
lines changed

.github/workflows/cppcheck.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
21
name: cppcheck
32

43
on:
54
push:
6-
branches: [ main ]
5+
branches: [main]
76
pull_request:
8-
branches: [ main ]
7+
branches: [main]
98

109
permissions:
1110
contents: read
@@ -66,12 +65,7 @@ jobs:
6665
set +e
6766
readonly WARNING_COUNT=`grep -c -E "\[.+\]" cppcheck.log`
6867
echo "cppcheck reported ${WARNING_COUNT} warning(s)"
69-
# Acceptable limit, to decrease over time down to 0
70-
readonly WARNING_LIMIT=10
71-
# FAIL the build if WARNING_COUNT > WARNING_LIMIT
72-
if [ $WARNING_COUNT -gt $WARNING_LIMIT ] ; then
73-
exit 1
74-
# WARN in annotations if WARNING_COUNT > 0
75-
elif [ $WARNING_COUNT -gt 0 ] ; then
68+
if [ $WARNING_COUNT -gt 0 ] ; then
7669
echo "::warning::cppcheck reported ${WARNING_COUNT} warning(s)"
70+
exit 1
7771
fi

ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient
323323

324324
void SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept override;
325325

326+
bool InternalCancelAllSessions() noexcept;
327+
326328
inline uint64_t GetMaxSessionsPerConnection() const noexcept
327329
{
328330
return max_sessions_per_connection_;

ext/src/http/client/curl/http_client_curl.cc

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ HttpClient::~HttpClient()
302302
}
303303

304304
// Force to abort all sessions
305-
CancelAllSessions();
305+
InternalCancelAllSessions();
306306

307307
if (!background_thread)
308308
{
@@ -342,27 +342,7 @@ std::shared_ptr<opentelemetry::ext::http::client::Session> HttpClient::CreateSes
342342

343343
bool HttpClient::CancelAllSessions() noexcept
344344
{
345-
// CancelSession may change sessions_, we can not change a container while iterating it.
346-
while (true)
347-
{
348-
std::unordered_map<uint64_t, std::shared_ptr<Session>> sessions;
349-
{
350-
// We can only cleanup session and curl handles in the IO thread.
351-
std::lock_guard<std::mutex> lock_guard{sessions_m_};
352-
sessions = sessions_;
353-
}
354-
355-
if (sessions.empty())
356-
{
357-
break;
358-
}
359-
360-
for (auto &session : sessions)
361-
{
362-
session.second->CancelSession();
363-
}
364-
}
365-
return true;
345+
return InternalCancelAllSessions();
366346
}
367347

368348
bool HttpClient::FinishAllSessions() noexcept
@@ -435,6 +415,31 @@ void HttpClient::CleanupSession(uint64_t session_id)
435415
}
436416
}
437417

418+
bool HttpClient::InternalCancelAllSessions() noexcept
419+
{
420+
// CancelSession may change sessions_, we can not change a container while iterating it.
421+
while (true)
422+
{
423+
std::unordered_map<uint64_t, std::shared_ptr<Session>> sessions;
424+
{
425+
// We can only cleanup session and curl handles in the IO thread.
426+
std::lock_guard<std::mutex> lock_guard{sessions_m_};
427+
sessions = sessions_;
428+
}
429+
430+
if (sessions.empty())
431+
{
432+
break;
433+
}
434+
435+
for (auto &session : sessions)
436+
{
437+
session.second->CancelSession();
438+
}
439+
}
440+
return true;
441+
}
442+
438443
bool HttpClient::MaybeSpawnBackgroundThread()
439444
{
440445
std::lock_guard<std::mutex> lock_guard{background_thread_m_};

sdk/include/opentelemetry/sdk/common/circular_buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class CircularBuffer
181181
{
182182
return {};
183183
}
184-
auto data = data_.get();
184+
AtomicUniquePtr<T> *data = data_.get();
185185
if (tail_index < head_index)
186186
{
187187
return CircularBufferRange<AtomicUniquePtr<T>>{nostd::span<AtomicUniquePtr<T>>{

sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ class BatchLogRecordProcessor : public LogRecordProcessor
9393

9494
/**
9595
* Shuts down the processor and does any cleanup required. Completely drains the buffer/queue of
96-
* all its logs and passes them to the exporter. Any subsequent calls to
97-
* ForceFlush or Shutdown will return immediately without doing anything.
96+
* all its logs and passes them to the exporter.
9897
*
99-
* NOTE: Timeout functionality not supported yet.
98+
* @param timeout minimum amount of microseconds to wait for shutdown before giving up and
99+
* returning failure.
100+
* @return true if the shutdown succeeded, false otherwise
100101
*/
101102
bool Shutdown(
102103
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
@@ -107,6 +108,13 @@ class BatchLogRecordProcessor : public LogRecordProcessor
107108
~BatchLogRecordProcessor() override;
108109

109110
protected:
111+
/**
112+
* Shuts down the processor and does any cleanup required. Completely drains the buffer/queue of
113+
* all its logs and passes them to the exporter.
114+
*/
115+
bool InternalShutdown(
116+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
117+
110118
/**
111119
* The background routine performed by the worker thread.
112120
*/

sdk/include/opentelemetry/sdk/logs/multi_log_record_processor.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,32 @@ class MultiLogRecordProcessor : public LogRecordProcessor
4949
/**
5050
* Shuts down the processor and does any cleanup required.
5151
* ShutDown should only be called once for each processor.
52-
* @param timeout minimum amount of microseconds to wait for
53-
* shutdown before giving up and returning failure.
52+
* @param timeout minimum amount of microseconds to wait for shutdown before giving up and
53+
* returning failure.
5454
* @return true if the shutdown succeeded, false otherwise
5555
*/
5656
bool Shutdown(
5757
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
5858

59+
protected:
60+
/**
61+
* Exports all log records that have not yet been exported to the configured Exporter.
62+
* @param timeout that the forceflush is required to finish within.
63+
* @return a result code indicating whether it succeeded, failed or timed out
64+
*/
65+
bool InternalForceFlush(
66+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
67+
68+
/**
69+
* Shuts down the processor and does any cleanup required.
70+
* ShutDown should only be called once for each processor.
71+
* @param timeout minimum amount of microseconds to wait for shutdown before giving up and
72+
* returning failure.
73+
* @return true if the shutdown succeeded, false otherwise
74+
*/
75+
bool InternalShutdown(
76+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
77+
5978
private:
6079
std::vector<std::unique_ptr<LogRecordProcessor>> processors_;
6180
};

sdk/include/opentelemetry/sdk/trace/batch_span_processor.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ class BatchSpanProcessor : public SpanProcessor
9191

9292
/**
9393
* Shuts down the processor and does any cleanup required. Completely drains the buffer/queue of
94-
* all its ended spans and passes them to the exporter. Any subsequent calls to OnStart, OnEnd,
95-
* ForceFlush or Shutdown will return immediately without doing anything.
94+
* all its ended spans and passes them to the exporter.
9695
*
97-
* NOTE: Timeout functionality not supported yet.
96+
* @param timeout minimum amount of microseconds to wait for shutdown before giving up and
97+
* returning failure.
98+
* @return true if the shutdown succeeded, false otherwise
9899
*/
99100
bool Shutdown(
100101
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
@@ -108,6 +109,17 @@ class BatchSpanProcessor : public SpanProcessor
108109
~BatchSpanProcessor() override;
109110

110111
protected:
112+
/**
113+
* Shuts down the processor and does any cleanup required. Completely drains the buffer/queue of
114+
* all its ended spans and passes them to the exporter.
115+
*
116+
* @param timeout minimum amount of microseconds to wait for shutdown before giving up and
117+
* returning failure.
118+
* @return true if the shutdown succeeded, false otherwise
119+
*/
120+
bool InternalShutdown(
121+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
122+
111123
/**
112124
* The background routine performed by the worker thread.
113125
*/

sdk/include/opentelemetry/sdk/trace/multi_span_processor.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ class MultiSpanProcessor : public SpanProcessor
123123

124124
bool Shutdown(
125125
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override
126+
{
127+
return InternalShutdown(timeout);
128+
}
129+
130+
~MultiSpanProcessor() override
131+
{
132+
InternalShutdown();
133+
Cleanup();
134+
}
135+
136+
protected:
137+
bool InternalShutdown(
138+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept
126139
{
127140
bool result = true;
128141
ProcessorNode *node = head_;
@@ -135,12 +148,6 @@ class MultiSpanProcessor : public SpanProcessor
135148
return result;
136149
}
137150

138-
~MultiSpanProcessor() override
139-
{
140-
Shutdown();
141-
Cleanup();
142-
}
143-
144151
private:
145152
struct ProcessorNode
146153
{

sdk/include/opentelemetry/sdk/trace/simple_processor.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ class SimpleSpanProcessor : public SpanProcessor
7676

7777
bool Shutdown(
7878
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override
79+
{
80+
return InternalShutdown(timeout);
81+
}
82+
83+
~SimpleSpanProcessor() override { InternalShutdown(); }
84+
85+
protected:
86+
bool InternalShutdown(
87+
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept
7988
{
8089
// We only call shutdown ONCE.
8190
if (exporter_ != nullptr && !shutdown_latch_.test_and_set(std::memory_order_acquire))
@@ -85,8 +94,6 @@ class SimpleSpanProcessor : public SpanProcessor
8594
return true;
8695
}
8796

88-
~SimpleSpanProcessor() override { Shutdown(); }
89-
9097
private:
9198
std::unique_ptr<SpanExporter> exporter_;
9299
opentelemetry::common::SpinLockMutex lock_;

sdk/src/logs/batch_log_record_processor.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,19 @@ void BatchLogRecordProcessor::GetWaitAdjustedTime(
368368
}
369369

370370
bool BatchLogRecordProcessor::Shutdown(std::chrono::microseconds timeout) noexcept
371+
{
372+
return InternalShutdown(timeout);
373+
}
374+
375+
BatchLogRecordProcessor::~BatchLogRecordProcessor()
376+
{
377+
if (synchronization_data_->is_shutdown.load() == false)
378+
{
379+
InternalShutdown();
380+
}
381+
}
382+
383+
bool BatchLogRecordProcessor::InternalShutdown(std::chrono::microseconds timeout) noexcept
371384
{
372385
auto start_time = std::chrono::system_clock::now();
373386

@@ -391,14 +404,6 @@ bool BatchLogRecordProcessor::Shutdown(std::chrono::microseconds timeout) noexce
391404
return true;
392405
}
393406

394-
BatchLogRecordProcessor::~BatchLogRecordProcessor()
395-
{
396-
if (synchronization_data_->is_shutdown.load() == false)
397-
{
398-
Shutdown();
399-
}
400-
}
401-
402407
} // namespace logs
403408
} // namespace sdk
404409
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)