Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit b14a5c0

Browse files
authored
Make stats export interval configurable at runtime. (#422)
Fixes #412.
1 parent eabb06a commit b14a5c0

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

opencensus/stats/internal/stats_exporter.cc

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ StatsExporterImpl* StatsExporterImpl::Get() {
3737
return global_stats_exporter_impl;
3838
}
3939

40+
void StatsExporterImpl::SetInterval(absl::Duration interval) {
41+
absl::MutexLock l(&mu_);
42+
export_interval_ = interval;
43+
}
44+
45+
absl::Time StatsExporterImpl::GetNextExportTime() const {
46+
absl::MutexLock l(&mu_);
47+
return absl::Now() + export_interval_;
48+
}
49+
4050
void StatsExporterImpl::AddView(const ViewDescriptor& view) {
4151
absl::MutexLock l(&mu_);
4252
views_[view.name()] = absl::make_unique<opencensus::stats::View>(view);
@@ -90,31 +100,44 @@ void StatsExporterImpl::StartExportThread() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
90100
}
91101

92102
void StatsExporterImpl::RunWorkerLoop() {
93-
absl::Time next_export_time = absl::Now() + export_interval_;
103+
absl::Time next_export_time = GetNextExportTime();
94104
while (true) {
95105
// SleepFor() returns immediately when given a negative duration.
96106
absl::SleepFor(next_export_time - absl::Now());
97107
// In case the last export took longer than the export interval, we
98108
// calculate the next time from now.
99-
next_export_time = absl::Now() + export_interval_;
109+
next_export_time = GetNextExportTime();
100110
Export();
101111
}
102112
}
103113

114+
// StatsExporter
115+
// -------------
116+
117+
// static
118+
void StatsExporter::SetInterval(absl::Duration interval) {
119+
StatsExporterImpl::Get()->SetInterval(interval);
120+
}
121+
122+
// static
104123
void StatsExporter::RemoveView(absl::string_view name) {
105124
StatsExporterImpl::Get()->RemoveView(name);
106125
}
107126

127+
// static
108128
void StatsExporter::RegisterPushHandler(std::unique_ptr<Handler> handler) {
109129
StatsExporterImpl::Get()->RegisterPushHandler(std::move(handler));
110130
}
111131

132+
// static
112133
std::vector<std::pair<ViewDescriptor, ViewData>> StatsExporter::GetViewData() {
113134
return StatsExporterImpl::Get()->GetViewData();
114135
}
115136

137+
// static
116138
void StatsExporter::ExportForTesting() { StatsExporterImpl::Get()->Export(); }
117139

140+
// static
118141
void StatsExporter::ClearHandlersForTesting() {
119142
StatsExporterImpl::Get()->ClearHandlersForTesting();
120143
}

opencensus/stats/internal/stats_exporter_impl.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ namespace stats {
3333
class StatsExporterImpl {
3434
public:
3535
static StatsExporterImpl* Get();
36-
36+
void SetInterval(absl::Duration interval);
37+
absl::Time GetNextExportTime() const;
3738
void AddView(const ViewDescriptor& view);
38-
3939
void RemoveView(absl::string_view name);
4040

4141
// Adds a handler, which cannot be subsequently removed (except by
@@ -44,27 +44,22 @@ class StatsExporterImpl {
4444
void RegisterPushHandler(std::unique_ptr<StatsExporter::Handler> handler);
4545

4646
std::vector<std::pair<ViewDescriptor, ViewData>> GetViewData();
47-
4847
void Export();
49-
5048
void ClearHandlersForTesting();
5149

5250
private:
53-
StatsExporterImpl() {}
51+
StatsExporterImpl() = default;
5452

5553
void StartExportThread() EXCLUSIVE_LOCKS_REQUIRED(mu_);
5654

5755
// Loops forever, calling Export() every export_interval_.
5856
void RunWorkerLoop();
5957

60-
const absl::Duration export_interval_ = absl::Seconds(10);
61-
6258
mutable absl::Mutex mu_;
63-
59+
absl::Duration export_interval_ GUARDED_BY(mu_) = absl::Seconds(10);
6460
std::vector<std::unique_ptr<StatsExporter::Handler>> handlers_
6561
GUARDED_BY(mu_);
6662
std::unordered_map<std::string, std::unique_ptr<View>> views_ GUARDED_BY(mu_);
67-
6863
bool thread_started_ GUARDED_BY(mu_) = false;
6964
std::thread t_ GUARDED_BY(mu_);
7065
};

opencensus/stats/internal/stats_exporter_test.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ MeasureDouble TestMeasure() {
6565

6666
class StatsExporterTest : public ::testing::Test {
6767
protected:
68+
static void SetUpTestSuite() { StatsExporter::SetInterval(absl::Seconds(5)); }
69+
6870
void SetUp() {
6971
// Access the measure to ensure it has been registered.
7072
TestMeasure();
@@ -165,7 +167,7 @@ TEST_F(StatsExporterTest, TimedExport) {
165167
std::vector<std::pair<ViewDescriptor, ViewData>> exported_data;
166168
MockExporter::Register(&exported_data);
167169
descriptor1_.RegisterForExport();
168-
absl::SleepFor(absl::Seconds(11));
170+
absl::SleepFor(absl::Seconds(6));
169171
EXPECT_THAT(exported_data,
170172
::testing::UnorderedElementsAre(::testing::Key(descriptor1_)));
171173
}

opencensus/stats/stats_exporter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ namespace stats {
3535
// StatsExporter is thread-safe.
3636
class StatsExporter final {
3737
public:
38+
// Sets the interval between exports. Takes effect after the current export
39+
// finishes.
40+
//
41+
// Warning: this API may be removed in future, in favor of configuring this
42+
// per-exporter.
43+
static void SetInterval(absl::Duration interval);
44+
3845
// Removes the view with 'name' from the registry, if one is registered.
3946
static void RemoveView(absl::string_view name);
4047

@@ -59,10 +66,12 @@ class StatsExporter final {
5966
static std::vector<std::pair<ViewDescriptor, ViewData>> GetViewData();
6067

6168
private:
69+
StatsExporter() = delete;
6270
friend class StatsExporterTest;
6371

6472
// Forces immediate export of data.
6573
static void ExportForTesting();
74+
6675
static void ClearHandlersForTesting();
6776
};
6877

0 commit comments

Comments
 (0)