Skip to content

Commit 3204eda

Browse files
authored
Merge pull request #742 from aeroyorch/non-collectable-resources-in-exposer
Allow injection of external CivetServer via Exposer constructor
2 parents 3ad985a + f6d0065 commit 3204eda

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

pull/include/prometheus/exposer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Endpoint;
2121

2222
class PROMETHEUS_CPP_PULL_EXPORT Exposer {
2323
public:
24+
explicit Exposer(std::shared_ptr<CivetServer> server);
2425
explicit Exposer(const std::string& bind_address, std::size_t num_threads = 2,
2526
const CivetCallbacks* callbacks = nullptr);
2627
explicit Exposer(std::vector<std::string> options,
@@ -48,7 +49,7 @@ class PROMETHEUS_CPP_PULL_EXPORT Exposer {
4849
private:
4950
detail::Endpoint& GetEndpointForUri(const std::string& uri);
5051

51-
std::unique_ptr<CivetServer> server_;
52+
std::shared_ptr<CivetServer> server_;
5253
std::vector<std::unique_ptr<detail::Endpoint>> endpoints_;
5354
std::mutex mutex_;
5455
};

pull/src/exposer.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
namespace prometheus {
1313

14+
Exposer::Exposer(std::shared_ptr<CivetServer> server)
15+
: server_(std::move(server)) {
16+
if (!server_) {
17+
throw std::invalid_argument("Invalid CivetServer: cannot be null");
18+
}
19+
}
20+
1421
Exposer::Exposer(const std::string& bind_address, const std::size_t num_threads,
1522
const CivetCallbacks* callbacks)
1623
: Exposer(
@@ -20,7 +27,7 @@ Exposer::Exposer(const std::string& bind_address, const std::size_t num_threads,
2027

2128
Exposer::Exposer(std::vector<std::string> options,
2229
const CivetCallbacks* callbacks)
23-
: server_(detail::make_unique<CivetServer>(std::move(options), callbacks)) {
30+
: Exposer(std::make_shared<CivetServer>(std::move(options), callbacks)) {
2431
}
2532

2633
Exposer::~Exposer() = default;

pull/tests/unit/exposer_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <gtest/gtest.h>
44

5+
#include "CivetServer.h"
6+
57
namespace prometheus {
68
namespace {
79

@@ -21,5 +23,9 @@ TEST(ExposerTest, listenOnDistinctPorts) {
2123
EXPECT_NE(firstExposerPorts, secondExposerPorts);
2224
}
2325

26+
TEST(ExposerTest, invalidExternalServer) {
27+
EXPECT_THROW(Exposer(std::shared_ptr<CivetServer>(nullptr)), std::invalid_argument);
28+
}
29+
2430
} // namespace
2531
} // namespace prometheus

0 commit comments

Comments
 (0)