Skip to content

Commit 4591282

Browse files
Add flush_threshold/flush_interval logic to OpenTsdbWriter
1 parent 9610e4a commit 4591282

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
@@ -1870,6 +1870,8 @@ Configuration Attributes:
18701870
--------------------------|-----------------------|----------------------------------
18711871
host | String | **Optional.** OpenTSDB host address. Defaults to `127.0.0.1`.
18721872
port | Number | **Optional.** OpenTSDB port. Defaults to `4242`.
1873+
flush\_interval | Duration | **Optional.** How long to buffer data points before sending. Defaults to `15s`.
1874+
flush\_threshold | Number | **Optional.** How many bytes to buffer before forcing a flush to the backend. Defaults to `2MiB`.
18731875
diconnect\_timeout | Duration | **Optional.** Timeout to wait for any outstanding data to be flushed to OpenTSDB before disconnecting. Defaults to `10s`.
18741876
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`.
18751877
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"
@@ -89,6 +90,13 @@ void OpenTsdbWriter::Resume()
8990
<< "Exception during OpenTsdb operation: " << DiagnosticInformation(exp);
9091
});
9192

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

94102
m_Connection = new PerfdataWriterConnection{this, GetHost(), GetPort()};
@@ -106,6 +114,8 @@ void OpenTsdbWriter::Pause()
106114
{
107115
m_HandleCheckResults.disconnect();
108116

117+
m_FlushTimer->Stop(true);
118+
109119
std::promise<void> queueDonePromise;
110120

111121
m_WorkQueue.Enqueue([&]() {
@@ -284,7 +294,9 @@ void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
284294
AddMetric(checkable, metric + ".latency", tags, cr->CalculateLatency(), ts);
285295
AddMetric(checkable, metric + ".execution_time", tags, cr->CalculateExecutionTime(), ts);
286296

287-
SendMsgBuffer();
297+
if (GetFlushThreshold() <= m_MsgBuf.GetLength()) {
298+
SendMsgBuffer();
299+
}
288300
}
289301
);
290302
}
@@ -388,7 +400,22 @@ void OpenTsdbWriter::AddMetric(const Checkable::Ptr& checkable, const String& me
388400

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

394421
void OpenTsdbWriter::SendMsgBuffer()
@@ -399,7 +426,7 @@ void OpenTsdbWriter::SendMsgBuffer()
399426
<< "Flushing data buffer to OpenTsdb.";
400427

401428
try {
402-
m_Connection->Send(boost::asio::buffer(std::exchange(m_MsgBuf, std::string{})));
429+
m_Connection->Send(boost::asio::buffer(std::exchange(m_MsgBuf.GetData(), {})));
403430
} catch (const PerfdataWriterConnection::Stopped& ex) {
404431
Log(LogDebug, "OpenTsdbWriter") << ex.what();
405432
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
Locked<PerfdataWriterConnection::Ptr> m_LockedConnection;
4143

@@ -47,6 +49,7 @@ class OpenTsdbWriter final : public ObjectImpl<OpenTsdbWriter>
4749
void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
4850
void AddMetric(const Checkable::Ptr& checkable, const String& metric,
4951
const std::map<String, String>& tags, double value, double ts);
52+
void FlushTimeout();
5053
void SendMsgBuffer();
5154
void AddPerfdata(const Checkable::Ptr& checkable, const String& metric,
5255
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)