Skip to content

Commit 4f220b1

Browse files
committed
Fix bug with shutdown SaveToFile thread and bug in simpleAPI
1 parent fda66c1 commit 4f220b1

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

core/include/prometheus/save_to_file.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
#include <chrono>
55
#include <string>
66
#include <fstream>
7+
#include <memory>
78

89
#include "registry.h"
910
#include "text_serializer.h"
1011

1112
namespace prometheus {
1213
class SaveToFile {
13-
std::chrono::seconds period { 1 };
14-
std::string filename;
15-
std::thread worker_thread { &SaveToFile::worker_function, this };
16-
Registry* registry_ptr { nullptr };
14+
std::chrono::seconds period { 1 };
15+
std::string filename;
16+
std::thread worker_thread { &SaveToFile::worker_function, this };
17+
std::shared_ptr<Registry> registry_ptr { nullptr };
18+
bool must_die { false };
1719

1820
void save_data() {
1921
if (registry_ptr) {
@@ -27,23 +29,33 @@ namespace prometheus {
2729
}
2830

2931
void worker_function() {
32+
// it need for fast shutdown this thread when SaveToFile destructor is called
33+
const uint64_t divider = 100;
34+
uint64_t fraction = divider;
3035
for (;;) {
31-
std::this_thread::sleep_for(period);
32-
save_data();
36+
std::chrono::milliseconds period_ms
37+
= std::chrono::duration_cast<std::chrono::milliseconds>(period);
38+
std::this_thread::sleep_for( period_ms / divider );
39+
if (must_die) {
40+
save_data();
41+
return;
42+
}
43+
if (--fraction == 0) {
44+
fraction = divider;
45+
save_data();
46+
}
3347
}
3448
}
3549

3650
public:
3751
SaveToFile() = default;
3852

3953
~SaveToFile() {
40-
if (worker_thread.joinable())
41-
worker_thread.detach();
42-
// save last data before finish
43-
save_data();
54+
must_die = true;
55+
worker_thread.join();
4456
}
4557

46-
SaveToFile(Registry& registry_, const std::chrono::seconds& period_, const std::string& filename_) {
58+
SaveToFile(std::shared_ptr<Registry>& registry_, const std::chrono::seconds& period_, const std::string& filename_) {
4759
set_registry(registry_);
4860
set_delay(period_);
4961
set_out_file(filename_);
@@ -63,8 +75,8 @@ namespace prometheus {
6375
return open_success;
6476
}
6577

66-
void set_registry (Registry& new_registry) {
67-
registry_ptr = &new_registry;
78+
void set_registry (std::shared_ptr<Registry>& new_registry_ptr) {
79+
registry_ptr = new_registry_ptr;
6880
}
6981

7082
};

simpleapi/include/prometheus/simpleapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace prometheus {
1919
namespace simpleapi {
2020

21-
extern Registry registry;
21+
extern Registry& registry;
2222
extern SaveToFile saver;
2323

2424

simpleapi/src/simpleapi.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
#include "prometheus/registry.h"
44
#include "prometheus/save_to_file.h"
55

6+
#include <memory>
7+
68
namespace prometheus {
79
namespace simpleapi {
810

9-
Registry registry;
10-
SaveToFile saver(registry, std::chrono::seconds(5), std::string("./metrics.prom"));
11+
std::shared_ptr<Registry> registry_ptr = std::make_shared<Registry>();
12+
Registry& registry = *registry_ptr;
13+
SaveToFile saver(registry_ptr, std::chrono::seconds(5), std::string("./metrics.prom"));
1114

1215
}
1316
}

0 commit comments

Comments
 (0)