Skip to content

Commit e52320f

Browse files
Add flush_threshold/flush_interval logic to OpenTsdbWriter
1 parent 2c0033d commit e52320f

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

doc/09-object-types.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,8 @@ Configuration Attributes:
18691869
--------------------------|-----------------------|----------------------------------
18701870
host | String | **Optional.** OpenTSDB host address. Defaults to `127.0.0.1`.
18711871
port | Number | **Optional.** OpenTSDB port. Defaults to `4242`.
1872+
flush\_interval | Duration | **Optional.** How long to buffer data points before sending. Defaults to `15s`.
1873+
flush\_threshold | Number | **Optional.** How many bytes to buffer before forcing a flush to the backend. Defaults to `2MiB`.
18721874
diconnect\_timeout | Duration | **Optional.** Timeout to wait for any outstanding data to be flushed to OpenTSDB before disconnecting. Defaults to `10s`.
18731875
enable\_ha | Boolean | **Optional.** Enable the high availability functionality. Only valid in a [cluster setup](06-distributed-monitoring.md#distributed-monitoring-high-availability-features). Defaults to `false`.
18741876
enable_generic_metrics | Boolean | **Optional.** Re-use metric names to store different perfdata values for a particular check. Use tags to distinguish perfdata instead of metric name. Defaults to `false`.

lib/perfdata/opentsdbwriter.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: GPL-2.0-or-later
33

44
#include "perfdata/opentsdbwriter.hpp"
5+
#include "base/defer.hpp"
56
#include "perfdata/opentsdbwriter-ti.cpp"
67
#include "icinga/service.hpp"
78
#include "icinga/checkcommand.hpp"
@@ -88,6 +89,13 @@ void OpenTsdbWriter::Resume()
8889
<< "Exception during OpenTsdb operation: " << DiagnosticInformation(exp);
8990
});
9091

92+
/* Setup timer for periodically flushing m_DataBuffer */
93+
m_FlushTimer = Timer::Create();
94+
m_FlushTimer->SetInterval(GetFlushInterval());
95+
m_FlushTimer->OnTimerExpired.connect([this](const Timer * const&) { FlushTimeout(); });
96+
m_FlushTimer->Start();
97+
m_FlushTimer->Reschedule(0);
98+
9199
ReadConfigTemplate();
92100

93101
m_Connection = new PerfdataWriterConnection{this, GetHost(), GetPort()};
@@ -104,6 +112,8 @@ void OpenTsdbWriter::Pause()
104112
{
105113
m_HandleCheckResults.disconnect();
106114

115+
m_FlushTimer->Stop(true);
116+
107117
std::promise<void> queueDonePromise;
108118

109119
m_WorkQueue.Enqueue([&]() {
@@ -282,7 +292,9 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
282292
AddMetric(checkable, metric + ".latency", tags, cr->CalculateLatency(), ts);
283293
AddMetric(checkable, metric + ".execution_time", tags, cr->CalculateExecutionTime(), ts);
284294

285-
SendMsgBuffer();
295+
if (GetFlushThreshold() <= m_MsgBuf.GetLength()) {
296+
SendMsgBuffer();
297+
}
286298
}
287299
);
288300
}
@@ -387,7 +399,22 @@ void OpenTsdbWriter::AddMetric(const Checkable::Ptr& checkable, const String& me
387399

388400
/* do not send \n to debug log */
389401
msgbuf << "\n";
390-
m_MsgBuf.append(msgbuf.str());
402+
m_MsgBuf += msgbuf.str();
403+
}
404+
405+
/**
406+
* Queues a Flush on the work-queue if none is queued yet.
407+
*/
408+
void OpenTsdbWriter::FlushTimeout()
409+
{
410+
if (m_FlushTimerInQueue.exchange(true, std::memory_order_relaxed)) {
411+
return;
412+
}
413+
414+
m_WorkQueue.Enqueue([&]() {
415+
Defer resetFlushTimer{[&]() { m_FlushTimerInQueue.store(false, std::memory_order_relaxed); }};
416+
SendMsgBuffer();
417+
});
391418
}
392419

393420
void OpenTsdbWriter::SendMsgBuffer()
@@ -398,7 +425,7 @@ void OpenTsdbWriter::SendMsgBuffer()
398425
<< "Flushing data buffer to OpenTsdb.";
399426

400427
try {
401-
m_Connection->Send(boost::asio::buffer(std::exchange(m_MsgBuf, std::string{})));
428+
m_Connection->Send(boost::asio::buffer(std::exchange(m_MsgBuf.GetData(), {})));
402429
} catch (const PerfdataWriterConnection::Stopped& ex) {
403430
Log(LogDebug, "OpenTsdbWriter") << ex.what();
404431
return;

lib/perfdata/opentsdbwriter.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class OpenTsdbWriter final : public ObjectImpl<OpenTsdbWriter>
3535

3636
private:
3737
WorkQueue m_WorkQueue{10000000, 1};
38-
std::string m_MsgBuf;
38+
Timer::Ptr m_FlushTimer;
39+
std::atomic_bool m_FlushTimerInQueue{false};
40+
String m_MsgBuf;
3941
PerfdataWriterConnection::Ptr m_Connection;
4042

4143
boost::signals2::connection m_HandleCheckResults;
@@ -46,6 +48,7 @@ class OpenTsdbWriter final : public ObjectImpl<OpenTsdbWriter>
4648
void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
4749
void AddMetric(const Checkable::Ptr& checkable, const String& metric,
4850
const std::map<String, String>& tags, double value, double ts);
51+
void FlushTimeout();
4952
void SendMsgBuffer();
5053
void AddPerfdata(const Checkable::Ptr& checkable, const String& metric,
5154
const std::map<String, String>& tags, const CheckResult::Ptr& cr, double ts);

lib/perfdata/opentsdbwriter.ti

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class OpenTsdbWriter : ConfigObject
3131
[config] bool enable_generic_metrics {
3232
default {{{ return false; }}}
3333
};
34+
[config] int flush_interval {
35+
default {{{ return 15; }}}
36+
};
37+
[config] std::size_t flush_threshold {
38+
default {{{ return 2 * 1024 * 1024; }}}
39+
};
3440
[config] double disconnect_timeout {
3541
default {{{ return 10; }}}
3642
};

0 commit comments

Comments
 (0)