Skip to content

Commit fda66c1

Browse files
committed
Fix some GCC bugs, upgrade simpleapi and expand its examples
1 parent 4ea3c4e commit fda66c1

15 files changed

+153
-101
lines changed

CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
project(prometheus-cpp-lite)
33
cmake_minimum_required(VERSION 2.6)
44

5-
if(WIN32)
5+
option(PROMETHEUS_BUILD_EXAMPLES "Build with examples" OFF)
6+
7+
if(WIN32)
68

79
# This properties specifies the directories into which targets files should be built
810
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) # Set old executable location variable
@@ -30,7 +32,8 @@ add_subdirectory("./core")
3032

3133
add_subdirectory("./simpleapi")
3234

33-
add_subdirectory("./examples")
34-
35+
if(PROMETHEUS_BUILD_EXAMPLES)
36+
add_subdirectory("./examples")
37+
endif()
3538

3639

core/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_
1010
add_custom_target (${PROJECT_NAME}-ide SOURCES ${PROMETHEUS_CPP_LITE_HEADERS})
1111

1212
set (${PROJECT_NAME}_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE)
13+
14+
if(NOT WIN32)
15+
find_package(Threads)
16+
target_link_libraries(${PROJECT_NAME} INTERFACE ${CMAKE_THREAD_LIBS_INIT})
17+
endif()

core/include/prometheus/builder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12

23
#include <string>
34
#include <map>
@@ -13,7 +14,7 @@ namespace prometheus {
1314
std::string help_;
1415

1516
public:
16-
Builder& Labels(const std::map<std::string, std::string>& labels) {
17+
Builder& Labels(const std::map<const std::string, const std::string>& labels) {
1718
labels_ = labels;
1819
return *this;
1920
}

core/include/prometheus/family.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ namespace prometheus {
239239

240240
ClientMetric collected = metric_pair.second->Collect();
241241
for (const Label& constant_label : constant_labels)
242-
collected.label.push_back(std::move(ClientMetric::Label(constant_label.first, constant_label.second)));
242+
collected.label.emplace_back(ClientMetric::Label(constant_label.first, constant_label.second));
243243

244244
const Labels& metric_labels = labels.at(metric_pair.first);
245245
for (const Label& metric_label : metric_labels)
246-
collected.label.push_back(std::move(ClientMetric::Label(metric_label.first, metric_label.second)));
246+
collected.label.emplace_back(ClientMetric::Label(metric_label.first, metric_label.second));
247247

248248
family.metric.push_back(std::move(collected));
249249

@@ -346,7 +346,7 @@ namespace prometheus {
346346
/// Register(Registry&).
347347
template <typename Registry>
348348
static CustomFamily& Build(Registry& registry, const std::string& name, const std::string& help, const Family::Labels& labels = Family::Labels()) {
349-
return registry.Add<CustomFamily>(name, help, labels);
349+
return registry.template Add<CustomFamily>(name, help, labels);
350350
}
351351

352352
};

core/include/prometheus/gauge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace prometheus {
5252
value = static_cast<Value>(time);
5353
}
5454
void Set(const Value& val) { value = val; } ///< \brief Set the gauge to the given value.
55-
const Value& Get() const { return value; } ///< \brief Get the current value of the gauge.
55+
const Value Get() const { return value; } ///< \brief Get the current value of the gauge.
5656

5757
virtual ClientMetric Collect() const { ///< \brief Get the current value of the gauge. Collect is called by the Registry when collecting metrics.
5858
ClientMetric metric;

core/include/prometheus/save_to_file.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ namespace prometheus {
1515
std::thread worker_thread { &SaveToFile::worker_function, this };
1616
Registry* registry_ptr { nullptr };
1717

18+
void save_data() {
19+
if (registry_ptr) {
20+
std::fstream out_file_stream;
21+
out_file_stream.open(filename, std::fstream::out | std::fstream::binary);
22+
if (out_file_stream.is_open()) {
23+
TextSerializer::Serialize(out_file_stream, registry_ptr->Collect());
24+
out_file_stream.close();
25+
}
26+
}
27+
}
28+
1829
void worker_function() {
1930
for (;;) {
2031
std::this_thread::sleep_for(period);
21-
if (registry_ptr) {
22-
std::fstream out_file_stream;
23-
out_file_stream.open(filename, std::fstream::out | std::fstream::binary);
24-
if (out_file_stream.is_open()) {
25-
TextSerializer::Serialize(out_file_stream, registry_ptr->Collect());
26-
out_file_stream.close();
27-
}
28-
}
32+
save_data();
2933
}
3034
}
3135

@@ -35,6 +39,8 @@ namespace prometheus {
3539
~SaveToFile() {
3640
if (worker_thread.joinable())
3741
worker_thread.detach();
42+
// save last data before finish
43+
save_data();
3844
}
3945

4046
SaveToFile(Registry& registry_, const std::chrono::seconds& period_, const std::string& filename_) {

examples/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ add_executable (save_to_file_example "save_to_file_example.cpp"
1818
target_link_libraries(save_to_file_example prometheus-cpp-lite-core)
1919

2020

21-
add_executable (simpleapi_example "simpleapi_use_in_class_example.cpp")
21+
add_executable (simpleapi_example "simpleapi_example.cpp")
2222
target_link_libraries(simpleapi_example prometheus-cpp-simpleapi)
23+
24+
add_executable (simpleapi_use_in_class_example "simpleapi_use_in_class_example.cpp")
25+
target_link_libraries(simpleapi_use_in_class_example prometheus-cpp-simpleapi)

examples/modern_example.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int main() {
5353
if (random_value & 8) udp_tx_counter += 0.7;
5454

5555
const std::array<std::string, 4> methods = { "GET", "PUT", "POST", "HEAD" };
56-
const std::string& method = methods.at(random_value % methods.size());
56+
const std::string& method = methods.at(static_cast<std::size_t>(random_value) % methods.size());
5757

5858
// dynamically calling Family<T>.Add() works but is slow and should be avoided
5959
http_requests_counter.Add({ {"method", method} }) += 10;
@@ -62,7 +62,4 @@ int main() {
6262
text_serializer.Serialize(std::cout, registry.Collect());
6363

6464
}
65-
66-
return 0;
67-
68-
}
65+
}

examples/original_example.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ int main() {
5656
if (random_value & 8) udp_tx_counter.Increment(10);
5757

5858
const std::array<std::string, 4> methods = { "GET", "PUT", "POST", "HEAD" };
59-
auto method = methods.at(random_value % methods.size());
59+
auto method = methods.at(static_cast<std::size_t>(random_value) % methods.size());
6060
// dynamically calling Family<T>.Add() works but is slow and should be avoided
6161
http_requests_counter.Add({ {"method", method} }).Increment();
6262

6363
TextSerializer text_serializer;
6464
text_serializer.Serialize(std::cout, registry->Collect());
6565

6666
}
67-
68-
return 0;
69-
}
67+
}

examples/save_to_file_example.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,4 @@ int main() {
4040
const int random_value = std::rand();
4141
metric += random_value % 10;
4242
}
43-
44-
return 0;
45-
46-
}
43+
}

0 commit comments

Comments
 (0)