-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add connection scraper and tracker metrics (#412)
- Loading branch information
1 parent
23cb23f
commit 314f64b
Showing
10 changed files
with
273 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "CollectorStats.h" | ||
|
||
namespace collector { | ||
|
||
#define X(n) #n, | ||
std::array<std::string, CollectorStats::timer_type_max> CollectorStats::timer_type_to_name = { | ||
TIMER_NAMES | ||
}; | ||
#undef X | ||
|
||
#define X(n) #n, | ||
std::array<std::string, CollectorStats::counter_type_max> CollectorStats::counter_type_to_name = { | ||
COUNTER_NAMES | ||
}; | ||
#undef X | ||
|
||
} // namespace collector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#ifndef COLLECTOR_COLLECTORSTATS_H | ||
#define COLLECTOR_COLLECTORSTATS_H | ||
|
||
#include <array> | ||
#include <atomic> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "TimeUtil.h" | ||
|
||
#define TIMER_NAMES \ | ||
X(net_scrape_read) \ | ||
X(net_scrape_update) \ | ||
X(net_fetch_state) \ | ||
X(net_create_message) \ | ||
X(net_write_message) | ||
|
||
#define COUNTER_NAMES \ | ||
X(net_conn_updates) \ | ||
X(net_conn_deltas) \ | ||
X(net_conn_inactive) \ | ||
X(net_cep_updates) \ | ||
X(net_cep_deltas) \ | ||
X(net_cep_inactive) \ | ||
X(net_known_ip_networks) \ | ||
X(net_known_public_ips) | ||
|
||
namespace collector { | ||
|
||
class CollectorStats { | ||
public: | ||
# define X(n) n, | ||
enum TimerType { | ||
TIMER_NAMES | ||
X(timer_type_max) | ||
}; | ||
# undef X | ||
static std::array<std::string, timer_type_max> timer_type_to_name; | ||
|
||
inline int64_t GetTimerCount(size_t index) const { return timer_count_[index]; } | ||
inline int64_t GetTimerDurationMicros(size_t index) const { return timer_total_us_[index]; } | ||
inline void EndTimerAt(size_t index, int64_t duration_us) { | ||
++timer_count_[index]; | ||
timer_total_us_[index] += duration_us; | ||
} | ||
|
||
# define X(n) n, | ||
enum CounterType { | ||
COUNTER_NAMES | ||
X(counter_type_max) | ||
}; | ||
# undef X | ||
static std::array<std::string, counter_type_max> counter_type_to_name; | ||
|
||
inline int64_t GetCounter(size_t index) const { return counter_[index]; } | ||
inline void CounterSet(size_t index, int64_t val) { | ||
counter_[index] = val; | ||
} | ||
inline void CounterAdd(size_t index, int64_t val) { | ||
counter_[index] += val; | ||
} | ||
|
||
private: | ||
std::array<std::atomic<int64_t>, timer_type_max> timer_count_ = {{}}; | ||
std::array<std::atomic<int64_t>, timer_type_max> timer_total_us_ = {{}}; | ||
|
||
std::array<std::atomic<int64_t>, counter_type_max> counter_ = {{}}; | ||
}; | ||
|
||
namespace internal { | ||
|
||
template<typename T> | ||
class ScopedTimer { | ||
public: | ||
ScopedTimer(T* timer_array, size_t index) | ||
: timer_array_(timer_array), index_(index), start_time_(NowMicros()) {} | ||
~ScopedTimer() { | ||
if (timer_array_) { | ||
timer_array_->EndTimerAt(index_, NowMicros() - start_time_); | ||
} | ||
} | ||
constexpr operator bool() const { return true; } | ||
|
||
private: | ||
T* timer_array_; | ||
size_t index_; | ||
int64_t start_time_; | ||
}; | ||
|
||
template<typename T> | ||
ScopedTimer<T> scoped_timer(T* timer_array, size_t index) { | ||
return ScopedTimer<T>(timer_array, index); | ||
} | ||
|
||
} // namespace internal | ||
|
||
#define SCOPED_TIMER(s,i) auto __scoped_timer_ ## __LINE__ = internal::scoped_timer(s,i) | ||
#define WITH_TIMER(s,i) if (SCOPED_TIMER(s,i)) | ||
|
||
#define COUNTER_SET(s,i,v) do { if (s != nullptr) { s->CounterSet(i, static_cast<int64_t>(v)); } } while(0) | ||
#define COUNTER_ADD(s,i,v) do { if (s != nullptr) { s->CounterAdd(i, static_cast<int64_t>(v)); } } while(0) | ||
#define COUNTER_INC(s,i) COUNTER_ADD(s,i,1) | ||
#define COUNTER_ZERO(s,i) COUNTER_SET(s,i,0) | ||
|
||
} // namespace collector | ||
|
||
#endif //COLLECTOR_COLLECTORSTATS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.