Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8ab1570
Expose API server with metrics in prometheus format.
StevenYCChou Nov 15, 2018
2427bce
Fix indentation and line wrapping.
StevenYCChou Nov 16, 2018
7b3f07e
Remove a redundant test.
StevenYCChou Nov 16, 2018
955a061
Make indentation consistent in Makefile.
StevenYCChou Nov 16, 2018
12b4a86
Fix typo.
StevenYCChou Nov 16, 2018
5c065da
Instrument first metric with opencensus-cpp.
StevenYCChou Nov 21, 2018
c667388
Makefile change
StevenYCChou Dec 6, 2018
cf5f833
remove unused variable.
StevenYCChou Dec 6, 2018
b788df1
expand comments on functions in measures.h
StevenYCChou Dec 6, 2018
e49c64d
remove declaration on internal variable
StevenYCChou Dec 6, 2018
ec5a859
make string declaration consistent
StevenYCChou Dec 6, 2018
bbbd851
add comments on why accessing measure in OAuth2 constructor helps
StevenYCChou Dec 6, 2018
acac889
Change the place to register view.
StevenYCChou Dec 6, 2018
ac7c9ea
Use the right header for prometheus compatible format.
StevenYCChou Dec 6, 2018
5ac289e
Address comments on measurement organization.
StevenYCChou Dec 7, 2018
d8b3e37
fix format in test file.
StevenYCChou Dec 7, 2018
93790ed
revert header/constructor because no need anymore.
StevenYCChou Dec 7, 2018
786245f
Wrap opencensus related code into a class.
StevenYCChou Dec 7, 2018
c22faac
Move util function into Metrics Class.
StevenYCChou Dec 7, 2018
cdfe9c1
remove explicit dependency on prometheus-cpp.
StevenYCChou Dec 7, 2018
7514b25
Clean up prometheus-cpp dependency on gitmodules as well.
StevenYCChou Dec 7, 2018
4f37882
Revert submodules on prometheus-cpp.
StevenYCChou Dec 7, 2018
96b268a
Revert chnage for prometheus-cpp.
StevenYCChou Dec 7, 2018
64929c8
Flush metrics only within one test, and add comments.
StevenYCChou Dec 7, 2018
88830d0
Avoid using string_view for constant string.
StevenYCChou Dec 7, 2018
6ee8ab8
Use constexpr consistently.
StevenYCChou Dec 10, 2018
bee8a32
Refactor code, fix tests which is not idempotent.
StevenYCChou Dec 10, 2018
2687728
initialize export related object every time to be safe - because we d…
StevenYCChou Dec 10, 2018
5c6526e
reorder variable definition.
StevenYCChou Dec 10, 2018
546958f
Use constexpr const.
StevenYCChou Dec 10, 2018
775737c
Add metrics tests on GCE Api Request Errors.
StevenYCChou Dec 11, 2018
fc935e6
fix format.
StevenYCChou Dec 11, 2018
1fe4649
Fix format on namespace.
StevenYCChou Dec 12, 2018
1099767
Fix typo.
StevenYCChou Dec 12, 2018
1e56d36
Address feedbacks.
StevenYCChou Dec 14, 2018
d6c6ac3
Add const.
StevenYCChou Dec 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
path = lib/googletest
url = https://github.com/google/googletest
ignore = dirty
[submodule "lib/prometheus-cpp"]
path = lib/prometheus-cpp
url = https://github.com/jupp0r/prometheus-cpp
1 change: 1 addition & 0 deletions lib/prometheus-cpp
Submodule prometheus-cpp added at e14ba4
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
init-submodules
build-cpp-netlib
build-yaml-cpp
build-prometheus-cpp
metadatad
41 changes: 33 additions & 8 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ NETWORK_URI_DIR=$(CPP_NETLIB_DIR)/deps/uri
NETWORK_URI_LIBDIR=$(NETWORK_URI_DIR)/src
YAML_CPP_DIR=$(LIBDIR)/yaml-cpp
YAML_CPP_LIBDIR=$(YAML_CPP_DIR)
SUBMODULE_DIRS=$(CPP_NETLIB_DIR) $(YAML_CPP_DIR)
PROMETHEUS_CPP_DIR=$(LIBDIR)/prometheus-cpp
PROMETHEUS_CPP_LIBDIR=$(PROMETHEUS_CPP_DIR)
PROMETHEUS_CPP_CORE_LIBDIR=$(PROMETHEUS_CPP_LIBDIR)/core
SUBMODULE_DIRS=$(CPP_NETLIB_DIR) $(YAML_CPP_DIR) $(PROMETHEUS_CPP_DIR)

GIT=git
GIT_VERSION=$(shell $(GIT) --version | grep -oh '[0-9]\+\.[0-9]\+\.[0-9]\+')
Expand All @@ -33,14 +36,16 @@ CMAKE=cmake
CPPFLAGS=\
-DAGENT_VERSION='$(PKG_VERSION)-$(PKG_RELEASE)' \
-DENABLE_DOCKER_METADATA \
-I$(CPP_NETLIB_DIR) -I$(NETWORK_URI_DIR)/include -I$(YAML_CPP_DIR)/include
-I$(CPP_NETLIB_DIR) -I$(NETWORK_URI_DIR)/include -I$(YAML_CPP_DIR)/include \
-I$(PROMETHEUS_CPP_CORE_LIBDIR)/include
CXXFLAGS=\
-std=c++11 -g -pthread -Wno-write-strings -Wno-deprecated
LDFLAGS=-L$(CPP_NETLIB_LIBDIR) -L$(NETWORK_URI_LIBDIR) -L$(YAML_CPP_LIBDIR)
LDFLAGS=-L$(CPP_NETLIB_LIBDIR) -L$(NETWORK_URI_LIBDIR) -L$(YAML_CPP_LIBDIR) \
-L$(PROMETHEUS_CPP_CORE_LIBDIR)
LDLIBS=\
-lcppnetlib-client-connections -lcppnetlib-server-parsers -lnetwork-uri \
-lboost_program_options -lboost_system -lboost_thread -lboost_filesystem \
-lpthread -lyajl -lssl -lcrypto -lyaml-cpp
-lpthread -lyajl -lssl -lcrypto -lyaml-cpp -lprometheus-cpp-core
SED_EXTRA=-e 's/-Wall/-Wall -Wno-deprecated/'

UNAME_S=$(shell uname -s)
Expand Down Expand Up @@ -88,7 +93,9 @@ CPP_NETLIB_LIBS=\
$(NETWORK_URI_LIBDIR)/libnetwork-uri.a
YAML_CPP_LIBS=\
$(YAML_CPP_LIBDIR)/libyaml-cpp.a
LIBS=$(CPP_NETLIB_LIBS) $(YAML_CPP_LIBS)
PROMETHEUS_CPP_LIBS=\
$(PROMETHEUS_CPP_CORE_LIBDIR)/libprometheus-cpp-core.a
LIBS=$(CPP_NETLIB_LIBS) $(YAML_CPP_LIBS) $(PROMETHEUS_CPP_LIBS)

sbindir=/opt/stackdriver/metadata/sbin
INSTALL=/usr/bin/install
Expand Down Expand Up @@ -167,7 +174,7 @@ clean:
$(RM) metadatad $(OBJS)

purge: clean
$(RM) -r init-submodules build-cpp-netlib build-yaml-cpp
$(RM) -r init-submodules build-cpp-netlib build-yaml-cpp build-prometheus-cpp
(cd .. && git submodule deinit -f $(SUBMODULE_DIRS:../%=%))

init-submodules:
Expand All @@ -183,6 +190,8 @@ ifneq ($(findstring $(GIT_VERSION),2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.8
deps/*/.git
cd $(CPP_NETLIB_DIR) && $(SED_I) -e 's@/src/.git/@../../../../../../../.git/@' \
libs/network/doc/_ext/breathe/.git
cd $(PROMETHEUS_CPP_DIR) && $(SED_I) -e 's@/src/.git/@../../../../.git/@' \
3rdparty/*/.git
endif
touch init-submodules

Expand Down Expand Up @@ -217,12 +226,28 @@ build-yaml-cpp: $(YAML_CPP_DIR)/Makefile
$(MAKE)
touch build-yaml-cpp

$(PROMETHEUS_CPP_DIR)/Makefile: init-submodules
cd $(PROMETHEUS_CPP_DIR) && \
$(CMAKE) -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS=-std=c++11 \
-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ \
-DENABLE_PUSH=OFF -DENABLE_PULL=OFF -DENABLE_COMPRESSION=OFF \
-DENABLE_TESTING=OFF

$(PROMETHEUS_CPP_LIBS): build-prometheus-cpp

build-prometheus-cpp: $(PROMETHEUS_CPP_DIR)/Makefile
cd $(PROMETHEUS_CPP_DIR) && \
$(MAKE)
touch build-prometheus-cpp

cpp-netlib: $(CPP_NETLIB_LIBS)

yaml-cpp: $(YAML_CPP_LIBS)

submodules: cpp-netlib yaml-cpp
prometheus-cpp: $(PROMETHEUS_CPP_LIBS)

submodules: cpp-netlib yaml-cpp prometheus-cpp

all: submodules metadatad

.PHONY: all submodules cpp-netlib yaml-cpp purge clean install deb rpm
.PHONY: all submodules cpp-netlib yaml-cpp prometheus-cpp purge clean install deb rpm
5 changes: 3 additions & 2 deletions src/agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
namespace google {

MetadataAgent::MetadataAgent(const Configuration& config)
: config_(config), store_(config_), health_checker_(config, store_) {}
: config_(config), store_(config_), health_checker_(config, store_),
registry_(std::make_shared<prometheus::Registry>()) {}

MetadataAgent::~MetadataAgent() {}

void MetadataAgent::Start() {
metadata_api_server_.reset(new MetadataApiServer(
config_, &health_checker_, store_, config_.MetadataApiNumThreads(),
config_, &health_checker_, registry_, store_, config_.MetadataApiNumThreads(),
config_.MetadataApiBindAddress(), config_.MetadataApiPort()));
reporter_.reset(new MetadataReporter(
config_, &store_, config_.MetadataReporterIntervalSeconds()));
Expand Down
7 changes: 7 additions & 0 deletions src/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define AGENT_H_

#include <memory>
#include <prometheus/registry.h>

#include "store.h"
#include "health_checker.h"
Expand Down Expand Up @@ -60,6 +61,10 @@ class MetadataAgent {
return &health_checker_;
}

std::shared_ptr<prometheus::Registry> prometheus_registry() {
return registry_;
}

private:
const Configuration& config_;

Expand All @@ -68,6 +73,8 @@ class MetadataAgent {

HealthChecker health_checker_;

std::shared_ptr<prometheus::Registry> registry_;

// The Metadata API server.
std::unique_ptr<MetadataApiServer> metadata_api_server_;
// The metadata reporter.
Expand Down
32 changes: 31 additions & 1 deletion src/api_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "api_server.h"

#include <boost/range/irange.hpp>
#include <prometheus/text_serializer.h>

#include "configuration.h"
#include "health_checker.h"
Expand Down Expand Up @@ -69,10 +70,12 @@ void MetadataApiServer::Dispatcher::log(const HttpServer::string_type& info) con

MetadataApiServer::MetadataApiServer(const Configuration& config,
const HealthChecker* health_checker,
const std::shared_ptr<prometheus::Collectable> collectable,
const MetadataStore& store,
int server_threads,
const std::string& host, int port)
: config_(config), health_checker_(health_checker), store_(store),
: config_(config), health_checker_(health_checker), collectable_(collectable),
store_(store),
dispatcher_({
{{"GET", "/monitoredResource/"},
[=](const HttpServer::request& request,
Expand All @@ -84,6 +87,11 @@ MetadataApiServer::MetadataApiServer(const Configuration& config,
std::shared_ptr<HttpServer::connection> conn) {
HandleHealthz(request, conn);
}},
{{"GET", "/metrics"},
[=](const HttpServer::request& request,
std::shared_ptr<HttpServer::connection> conn) {
HandleMetrics(request, conn);
}},
}, config_.VerboseLogging()),
server_(
HttpServer::options(dispatcher_)
Expand Down Expand Up @@ -200,4 +208,26 @@ void MetadataApiServer::HandleHealthz(
}
}

void MetadataApiServer::HandleMetrics(
const HttpServer::request& request,
std::shared_ptr<HttpServer::connection> conn) {
std::string response = SerializeMetricsToPrometheusTextFormat();
conn->set_status(HttpServer::connection::ok);
conn->set_headers(std::map<std::string, std::string>({
{"Connection", "close"},
{"Content-Length", std::to_string(response.size())},
{"Content-Type", "application/json"},
}));
conn->write(response);
}

std::string MetadataApiServer::SerializeMetricsToPrometheusTextFormat() const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest creating a Telemetry class to wrap this whole Collectable business and the registry and text serialization. Then you could have a field telemetry_ of type const Telemetry&, and this method can be replaced with telemetry_.MetricsToString() (or even telemetry_.ToString()).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the current implementation, it is implemented as function inside google::internal namespace, https://github.com/Stackdriver/metadata-agent/pull/210/files#diff-bcca2b17db5d9ee49a78774f858b95e4R25 . Two reasons:

  1. It can be implemented as a function, instead of a class
  2. implement the function inside google::internal namespace enables us to test this function, but does not need to keep an member telemetry_ inside the MetadataApiServer class

if (!collectable_) {
return "";
}

prometheus::TextSerializer text_serializer;
return std::move(text_serializer.Serialize(collectable_->Collect()));
}

}
6 changes: 6 additions & 0 deletions src/api_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#define BOOST_NETWORK_ENABLE_HTTPS
#include <boost/network/protocol/http/server.hpp>
#include <prometheus/collectable.h>
#include <functional>
#include <map>
#include <memory>
Expand All @@ -43,6 +44,7 @@ class MetadataApiServer {
public:
MetadataApiServer(const Configuration& config,
const HealthChecker* health_checker,
const std::shared_ptr<prometheus::Collectable> collectable,
const MetadataStore& store, int server_threads,
const std::string& host, int port);
~MetadataApiServer();
Expand Down Expand Up @@ -77,13 +79,17 @@ class MetadataApiServer {
std::shared_ptr<HttpServer::connection> conn);
void HandleHealthz(const HttpServer::request& request,
std::shared_ptr<HttpServer::connection> conn);
void HandleMetrics(const HttpServer::request& request,
std::shared_ptr<HttpServer::connection> conn);
std::string SerializeMetricsToPrometheusTextFormat() const;

const Configuration& config_;
const HealthChecker* health_checker_;
const MetadataStore& store_;
Dispatcher dispatcher_;
HttpServer server_;
std::vector<std::thread> server_pool_;
std::shared_ptr<prometheus::Collectable> collectable_;
};

}
Expand Down
26 changes: 18 additions & 8 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,31 @@ NETWORK_URI_DIR=$(CPP_NETLIB_DIR)/deps/uri
NETWORK_URI_LIBDIR=$(NETWORK_URI_DIR)/src
YAML_CPP_DIR=$(LIBDIR)/yaml-cpp
YAML_CPP_LIBDIR=$(YAML_CPP_DIR)
PROMETHEUS_CPP_DIR=$(LIBDIR)/prometheus-cpp
PROMETHEUS_CPP_LIBDIR=$(PROMETHEUS_CPP_DIR)
PROMETHEUS_CPP_CORE_LIBDIR=$(PROMETHEUS_CPP_LIBDIR)/core

CPP_NETLIB_LIBS=\
$(CPP_NETLIB_LIBDIR)/libcppnetlib-client-connections.a \
$(CPP_NETLIB_LIBDIR)/libcppnetlib-server-parsers.a \
$(NETWORK_URI_LIBDIR)/libnetwork-uri.a
YAML_CPP_LIBS=$(YAML_CPP_LIBDIR)/libyaml-cpp.a
PROMETHEUS_CPP_LIBS=\
$(PROMETHEUS_CPP_CORE_LIBDIR)/libprometheus-cpp-core.a

CPPFLAGS+= \
-isystem $(GTEST_DIR)/include -I$(GMOCK_DIR)/include \
-I$(CPP_NETLIB_DIR) -I$(NETWORK_URI_DIR)/include -I$(YAML_CPP_DIR)/include
-I$(CPP_NETLIB_DIR) -I$(NETWORK_URI_DIR)/include -I$(YAML_CPP_DIR)/include \
-I$(PROMETHEUS_CPP_CORE_LIBDIR)/include
CXXFLAGS=\
-std=c++11 -g -pthread -Wno-write-strings -Wno-deprecated
LDFLAGS=-L$(CPP_NETLIB_LIBDIR) -L$(NETWORK_URI_LIBDIR) -L$(YAML_CPP_LIBDIR)
LDFLAGS=-L$(CPP_NETLIB_LIBDIR) -L$(NETWORK_URI_LIBDIR) -L$(YAML_CPP_LIBDIR) \
-L$(PROMETHEUS_CPP_CORE_LIBDIR)
LDLIBS=\
-lcppnetlib-client-connections -lcppnetlib-server-parsers -lnetwork-uri \
-lboost_program_options -lboost_system -lboost_thread -lboost_filesystem \
-lboost_regex -lpthread -lyajl -lssl -lcrypto -lyaml-cpp
-lboost_regex -lpthread -lyajl -lssl -lcrypto -lyaml-cpp \
-lprometheus-cpp-core

UNAME_S=$(shell uname -s)
ifeq ($(UNAME_S),Darwin)
Expand Down Expand Up @@ -115,14 +123,16 @@ init-submodules:
$(SRC_DIR)/init-submodules:
cd $(SRC_DIR) && $(MAKE) $(@:$(SRC_DIR)/%=%)

$(SRC_DIR)/build-cpp-netlib $(SRC_DIR)/build-yaml-cpp: $(SRC_DIR)/init-submodules
$(SRC_DIR)/build-cpp-netlib $(SRC_DIR)/build-yaml-cpp $(SRC_DIR)/build-prometheus-cpp: $(SRC_DIR)/init-submodules
cd $(SRC_DIR) && $(MAKE) $(@:$(SRC_DIR)/%=%)

$(CPP_NETLIB_LIBS): $(SRC_DIR)/build-cpp-netlib

$(YAML_CPP_LIBS): $(SRC_DIR)/build-yaml-cpp

$(SRC_DIR)/%.o: $(SRC_DIR)/build-cpp-netlib $(SRC_DIR)/build-yaml-cpp $(SRC_DIR)/%.cc
$(PROMETHEUS_CPP_LIBS): $(SRC_DIR)/build-prometheus-cpp

$(SRC_DIR)/%.o: $(SRC_DIR)/build-cpp-netlib $(SRC_DIR)/build-yaml-cpp $(SRC_DIR)/build-prometheus-cpp $(SRC_DIR)/%.cc
cd $(SRC_DIR) && $(MAKE) $(@:$(SRC_DIR)/%=%)

$(GTEST_SOURCEDIR)/gtest-all.cc: init-submodules
Expand All @@ -139,14 +149,14 @@ gmock_main.o: $(GMOCK_SOURCEDIR)/gmock_main.cc
$(GTEST_LIB): gtest-all.o gmock-all.o gmock_main.o
$(AR) $(ARFLAGS) $@ $^

$(TESTS): $(GTEST_LIB) $(CPP_NETLIB_LIBS) $(YAML_CPP_LIBS)
$(TESTS): $(GTEST_LIB) $(CPP_NETLIB_LIBS) $(YAML_CPP_LIBS) $(PROMETHEUS_CPP_LIBS)

# All unittest objects depend on GTEST_LIB.
# Some headers need CPP_NETLIB_LIBS and YAML_CPP_LIBS.
$(TESTS:%=%.o): $(GTEST_LIB) $(CPP_NETLIB_LIBS) $(YAML_CPP_LIBS)
$(TESTS:%=%.o): $(GTEST_LIB) $(CPP_NETLIB_LIBS) $(YAML_CPP_LIBS) $(PROMETHEUS_CPP_LIBS)

api_server_unittest: api_server_unittest.o $(SRC_DIR)/api_server.o $(SRC_DIR)/configuration.o $(SRC_DIR)/store.o $(SRC_DIR)/json.o $(SRC_DIR)/resource.o $(SRC_DIR)/logging.o $(SRC_DIR)/time.o $(SRC_DIR)/health_checker.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -lprometheus-cpp-core -o $@
base64_unittest: base64_unittest.o $(SRC_DIR)/base64.o
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
configuration_unittest: configuration_unittest.o $(SRC_DIR)/configuration.o
Expand Down
Loading