jupp0r / prometheus-cpp

Prometheus Client Library for Modern C++
Other
924 stars 326 forks source link

bazel build sample error #714

Closed lianghao208 closed 4 months ago

lianghao208 commented 4 months ago

my sample code sample_server.cc from doc:

#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>

#include <array>
#include <chrono>
#include <cstdlib>
#include <memory>
#include <string>
#include <thread>

int main() {
  using namespace prometheus;

  // create an http server running on port 8080
  Exposer exposer{"127.0.0.1:8080"};

  // create a metrics registry
  // @note it's the users responsibility to keep the object alive
  auto registry = std::make_shared<Registry>();

  // add a new counter family to the registry (families combine values with the
  // same name, but distinct label dimensions)
  //
  // @note please follow the metric-naming best-practices:
  // https://prometheus.io/docs/practices/naming/
  auto& packet_counter = BuildCounter()
                             .Name("observed_packets_total")
                             .Help("Number of observed packets")
                             .Register(*registry);

  // add and remember dimensional data, incrementing those is very cheap
  auto& tcp_rx_counter =
      packet_counter.Add({{"protocol", "tcp"}, {"direction", "rx"}});
  auto& tcp_tx_counter =
      packet_counter.Add({{"protocol", "tcp"}, {"direction", "tx"}});
  auto& udp_rx_counter =
      packet_counter.Add({{"protocol", "udp"}, {"direction", "rx"}});
  auto& udp_tx_counter =
      packet_counter.Add({{"protocol", "udp"}, {"direction", "tx"}});

  // add a counter whose dimensional data is not known at compile time
  // nevertheless dimensional values should only occur in low cardinality:
  // https://prometheus.io/docs/practices/naming/#labels
  auto& http_requests_counter = BuildCounter()
                                    .Name("http_requests_total")
                                    .Help("Number of HTTP requests")
                                    .Register(*registry);

  // ask the exposer to scrape the registry on incoming HTTP requests
  exposer.RegisterCollectable(registry);

  for (;;) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    const auto random_value = std::rand();

    if (random_value & 1) tcp_rx_counter.Increment();
    if (random_value & 2) tcp_tx_counter.Increment();
    if (random_value & 4) udp_rx_counter.Increment();
    if (random_value & 8) udp_tx_counter.Increment();

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

my directory:

main----BUILD
main----sample_server.cc
WORKSPACE

BUILD:

cc_binary(
    name = "sample_server",
    srcs = ["sample_server.cc"],
    deps = ["@com_github_jupp0r_prometheus_cpp//pull"],
)

WORKSPACE:

http_archive(
    name = "com_github_jupp0r_prometheus_cpp",
    strip_prefix = "prometheus-cpp-master",
    urls = ["https://github.com/jupp0r/prometheus-cpp/archive/master.zip"],
)

load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories")

prometheus_cpp_repositories()

bazel build:

bazel build //main:sample_server --action_env=BAZEL_CXXOPTS="-std=c++11"

got error message:

DEBUG: Rule 'com_github_jupp0r_prometheus_cpp' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "7cd4bf63e59764e1b0d394522b081f812795be918459db5e3dfcf2d6df313344"
DEBUG: Call stack for the definition of repository 'com_github_jupp0r_prometheus_cpp' which is a http_archive (rule definition at /root/.cache/bazel/_bazel_root/a958b65b21a9ea574fad14e7e82f777a/external/bazel_tools/tools/build_defs/repo/http.bzl:292:16):
 - /root/prometheus-sample/WORKSPACE:2:1
INFO: Build option --action_env has changed, discarding analysis cache.
INFO: Analyzed target //main:sample_server (3 packages loaded, 143 targets configured).
INFO: Found 1 target...
INFO: From Compiling external/civetweb/src/civetweb.c:
external/civetweb/src/civetweb.c: In function 'handle_request':
external/civetweb/src/civetweb.c:14708:6: warning: variable 'uri_len' set but not used [-Wunused-but-set-variable]
  int uri_len, ssl_index;
      ^~~~~~~
ERROR: /root/prometheus-sample/main/BUILD:1:1: Linking of rule '//main:sample_server' failed (Exit 1) gcc failed: error executing command /opt/rh/devtoolset-7/root/usr/bin/gcc @bazel-out/k8-fastbuild/bin/main/sample_server-2.params

Use --sandbox_debug to see verbose messages from the sandbox
bazel-out/k8-fastbuild/bin/main/_objs/sample_server/sample_server.pic.o:sample_server.cc:function std::array<std::string, 4ul>::at(unsigned long) const: error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/pull/_objs/pull/basic_auth.pic.o:basic_auth.cc:function prometheus::detail::base64_decode(std::string const&): error: undefined reference to 'std::runtime_error::runtime_error(char const*)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/pull/_objs/pull/basic_auth.pic.o:basic_auth.cc:function prometheus::detail::base64_decode(std::string const&): error: undefined reference to 'std::runtime_error::runtime_error(char const*)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/pull/_objs/pull/basic_auth.pic.o:basic_auth.cc:function prometheus::detail::base64_decode(std::string const&): error: undefined reference to 'std::runtime_error::runtime_error(char const*)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/core/_objs/core/registry.pic.o:registry.cc:function prometheus::Family<prometheus::Counter>& prometheus::Registry::Add<prometheus::Counter>(std::string const&, std::string const&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&): error: undefined reference to 'std::invalid_argument::invalid_argument(char const*)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/core/_objs/core/registry.pic.o:registry.cc:function prometheus::Family<prometheus::Counter>& prometheus::Registry::Add<prometheus::Counter>(std::string const&, std::string const&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&): error: undefined reference to 'std::invalid_argument::invalid_argument(char const*)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/core/_objs/core/registry.pic.o:registry.cc:function prometheus::Family<prometheus::Counter>& prometheus::Registry::Add<prometheus::Counter>(std::string const&, std::string const&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&): error: undefined reference to 'std::invalid_argument::invalid_argument(char const*)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/core/_objs/core/registry.pic.o:registry.cc:function prometheus::Family<prometheus::Gauge>& prometheus::Registry::Add<prometheus::Gauge>(std::string const&, std::string const&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&): error: undefined reference to 'std::invalid_argument::invalid_argument(char const*)'
bazel-out/k8-fastbuild/bin/external/com_github_jupp0r_prometheus_cpp/core/_objs/core/histogram.pic.o:histogram.cc:function prometheus::Histogram::ObserveMultiple(std::vector<double, std::allocator<double> > const&, double): error: undefined reference to 'std::length_error::length_error(char const*)'
bazel-out/k8-fastbuild/bin/external/civetweb/_objs/civetserver/CivetServer.pic.o:CivetServer.cpp:function CivetServer::urlDecode(char const*, unsigned long, std::string&, bool): error: undefined reference to 'std::out_of_range::out_of_range(char const*)'
bazel-out/k8-fastbuild/bin/external/civetweb/_objs/civetserver/CivetServer.pic.o:CivetServer.cpp:function CivetServer::urlEncode(char const*, unsigned long, std::string&, bool): error: undefined reference to 'std::out_of_range::out_of_range(char const*)'
collect2: error: ld returned 1 exit status
Target //main:sample_server failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 2.512s, Critical Path: 1.56s
INFO: 37 processes: 37 processwrapper-sandbox.
FAILED: Build did NOT complete successfully

gcc version: 7.3.1 bazel version: 1.2.1

lianghao208 commented 4 months ago

problem resolved, due to low bazel version, it works after upgrading to the newest bazel version