From 5360131ef2820aded372789fa8ec189c67d1c9e3 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Sun, 14 Jun 2020 18:50:12 +0000 Subject: [PATCH 01/12] enhanced CMake support ( work in progress ) - support for package export and instal - opencensus_lib() now has a HDRS section just like Bazel. - opencensus_lib() allows declaration of headers in subdirectories, and installs them in proper directory - still issues with segregating public and private headers ( HDRS of non PUBLIC opencenus_lib ) ( it looks like customer can write code that includes internal files ) - still issues linking private static libraries together into bigger public ones. ( as Bazel does ) - attempt at allowing build as shared libraries - added helper code to request no undefined symbols in shared libraries. - attempt failed due to circular dependencies ( opencensus_trace and opencensus_context ). Until those are fixed, only static libs will work. - outlined many missing dependencies declarations, only fixed in CMake for now. ideally similar changes would be done to Bazel build, and Bazel would allow to build shared libs as well... - stackdriver / googleapis / google-cloud-cpp changed recently, does not export monitoring/logging and cloud trace/tracing libs. - attempt at building with FetchContent if and only if no existing package has been found. same ideas as using shared libraries : - reuse and to link ability into programs that may already use some of the same deps. - third party ( opencensus-cpp ) should not require significant alterations in consuming client build system ( hence CMake ) and strategy ( - linux-specifc example : using stackdriver exporter on Google Cloud Engine - parse /proc/meminfo and /proc/vmstat to stackdriver - rely via FetchContent on third party software - cpr ( to implement succintly access go GCE instance metadata server ) - re2 ( to brace against variable level of support/correctness in implementation of with old GCC versions ) - not yet verified on anything but CentOS 7 - in particular no effort has been yet made so that CI via travis or appveyor actually works. As such, not ready to merge --- CMakeLists.txt | 35 ++ cmake/OpenCensusDeps.cmake | 86 ++-- cmake/OpenCensusHelpers.cmake | 76 ++- cmake/config-version.cmake.in | 13 + cmake/config.cmake.in | 60 +++ examples/CMakeLists.txt | 2 + examples/memory/CMakeLists.txt | 65 +++ examples/memory/memory_stats.cc | 442 ++++++++++++++++++ opencensus/common/internal/CMakeLists.txt | 21 +- .../common/internal/grpc/CMakeLists.txt | 30 ++ opencensus/context/CMakeLists.txt | 3 + opencensus/exporters/stats/CMakeLists.txt | 8 +- .../exporters/stats/prometheus/CMakeLists.txt | 5 + .../stats/stackdriver/CMakeLists.txt | 69 +++ .../internal/stackdriver_e2e_test.cc | 6 +- .../exporters/stats/stdout/CMakeLists.txt | 2 + opencensus/exporters/trace/CMakeLists.txt | 8 +- .../trace/stackdriver/CMakeLists.txt | 24 + .../exporters/trace/stdout/CMakeLists.txt | 3 + opencensus/stats/CMakeLists.txt | 48 +- opencensus/tags/CMakeLists.txt | 9 + opencensus/trace/CMakeLists.txt | 76 ++- 22 files changed, 1029 insertions(+), 62 deletions(-) create mode 100644 cmake/config-version.cmake.in create mode 100644 cmake/config.cmake.in create mode 100644 examples/memory/CMakeLists.txt create mode 100644 examples/memory/memory_stats.cc create mode 100644 opencensus/common/internal/grpc/CMakeLists.txt create mode 100644 opencensus/exporters/stats/stackdriver/CMakeLists.txt create mode 100644 opencensus/exporters/trace/stackdriver/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ecec534..612606b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,10 +38,30 @@ enable_testing() list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +# There are cyclical dependencies ( betwen libopencensus_trace.so and libopencensus_context.so ) +# that prevent from building the code as is as shared libraries without undefined symbols... +# +# Moreover, because of the way I built gRPC c++ apis within google-cloud-cpp as shared libs, with a specific abseil version _as shared lib_ + +if ( BUILD_SHARED_LIBS ) + set( POSITION_INDEPENDENT_CODE ON ) + set( BUILD_SHARED_LIBS NO ) +endif() +# clearly there are problems with cyclical deps in this code ( context, trace ) +if ( BUILD_SHARED_LIBS ) + add_link_options("LINKER:--no-undefined") +endif() + +include(GNUInstallDirs) + include(OpenCensusDeps) include(OpenCensusHelpers) +if ( BUILD_SHARED_LIBS ) + add_link_options("LINKER:--no-undefined") +endif() + # OpenCensus code. add_subdirectory(opencensus) @@ -49,3 +69,18 @@ add_subdirectory(opencensus) if(BUILD_TESTING) add_subdirectory(examples) endif() + +install( EXPORT opencensus-cpp-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp ) + +# we still need to create and export opencensus-cpp-config.cmake + +# Create and install the CMake configuration files. +configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/config.cmake.in" + "opencensus-cpp-config.cmake" @ONLY) +configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/config-version.cmake.in" + "opencensus-cpp-config-version.cmake" @ONLY) + +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp") diff --git a/cmake/OpenCensusDeps.cmake b/cmake/OpenCensusDeps.cmake index 25f3f8aa..e62f21c7 100644 --- a/cmake/OpenCensusDeps.cmake +++ b/cmake/OpenCensusDeps.cmake @@ -14,22 +14,13 @@ include(FetchContent) + +find_package( googletest ) +if ( NOT googletest_FOUND ) FetchContent_Declare( googletest GIT_REPOSITORY https://github.com/google/googletest GIT_TAG master) -FetchContent_Declare( - abseil - GIT_REPOSITORY https://github.com/abseil/abseil-cpp - GIT_TAG master) -FetchContent_Declare( - prometheus - GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp - GIT_TAG master) -FetchContent_Declare( - benchmark - GIT_REPOSITORY https://github.com/google/benchmark - GIT_TAG master) FetchContent_GetProperties(googletest) if(BUILD_TESTING) @@ -53,17 +44,39 @@ if(BUILD_TESTING) EXCLUDE_FROM_ALL) endif() endif() +endif() + +# the right thing to do would be to _always_ build abseil as a static library +# with -fPIC in case BUILD_SHARED_LIBS is on. +# it is problematic with the way I currently build googleapis cpp files within google-cloud-cpp +# +# the crux of the matter is that I need to link opencensus-cpp shared libs +# into applications that already have their own libcurl, libgrpc* ... -FetchContent_GetProperties(abseil) -if(NOT abseil_POPULATED) - message(STATUS "Dependency: abseil") - set(orig_BUILD_TESTING "${BUILD_TESTING}") - set(BUILD_TESTING OFF) # Don't include abseil tests. - FetchContent_Populate(abseil) - add_subdirectory(${abseil_SOURCE_DIR} ${abseil_BINARY_DIR} EXCLUDE_FROM_ALL) - set(BUILD_TESTING "${orig_BUILD_TESTING}") # Restore value. +find_package( absl ) +if ( NOT absl_FOUND ) + FetchContent_Declare( + abseil + GIT_REPOSITORY https://github.com/abseil/abseil-cpp + GIT_TAG master) + + FetchContent_GetProperties(abseil) + if(NOT abseil_POPULATED) + message(STATUS "Dependency: abseil") + set(orig_BUILD_TESTING "${BUILD_TESTING}") + set(BUILD_TESTING OFF) # Don't include abseil tests. + FetchContent_Populate(abseil) + add_subdirectory(${abseil_SOURCE_DIR} ${abseil_BINARY_DIR} EXCLUDE_FROM_ALL) + set(BUILD_TESTING "${orig_BUILD_TESTING}") # Restore value. + endif() endif() + +FetchContent_Declare( + prometheus + GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp + GIT_TAG master) + FetchContent_GetProperties(prometheus) if(NOT prometheus_POPULATED) message(STATUS "Dependency: prometheus") @@ -84,18 +97,25 @@ if(NOT prometheus_POPULATED) EXCLUDE_FROM_ALL) endif() -FetchContent_GetProperties(benchmark) -if(BUILD_TESTING) - message(STATUS "Dependency: benchmark (BUILD_TESTING=${BUILD_TESTING})") - if(NOT benchmark_POPULATED) - set(BENCHMARK_ENABLE_TESTING - OFF - CACHE BOOL "Enable testing of the benchmark library." FORCE) - set(BENCHMARK_ENABLE_GTEST_TESTS - OFF - CACHE BOOL "Enable building the unit tests which depend on gtest" FORCE) - FetchContent_Populate(benchmark) - add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR} - EXCLUDE_FROM_ALL) +find_package( benchmark ) +if ( NOT benchmark_FOUND ) + FetchContent_Declare( + benchmark + GIT_REPOSITORY https://github.com/google/benchmark + GIT_TAG master) + FetchContent_GetProperties(benchmark) + if(BUILD_TESTING) + message(STATUS "Dependency: benchmark (BUILD_TESTING=${BUILD_TESTING})") + if(NOT benchmark_POPULATED) + set(BENCHMARK_ENABLE_TESTING + OFF + CACHE BOOL "Enable testing of the benchmark library." FORCE) + set(BENCHMARK_ENABLE_GTEST_TESTS + OFF + CACHE BOOL "Enable building the unit tests which depend on gtest" FORCE) + FetchContent_Populate(benchmark) + add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR} + EXCLUDE_FROM_ALL) + endif() endif() endif() diff --git a/cmake/OpenCensusHelpers.cmake b/cmake/OpenCensusHelpers.cmake index 8fdfe967..ddffd803 100644 --- a/cmake/OpenCensusHelpers.cmake +++ b/cmake/OpenCensusHelpers.cmake @@ -53,23 +53,93 @@ function(opencensus_benchmark NAME SRC) endif() endfunction() + +include(GNUInstallDirs) + +# +# install( TARGETS PUBLIC_HEADERS DESTINATION foo ) +# does not work well when we declare +# opencensus_lib( libfoo PUBLIC HDRS bar/baz.h SRCS bar/bar.cc ) +# it will generate foo/baz.h not foo/bar/baz.hh +# +# +# install_headers_with_subdirectories( PUBLIC_HEADERS item1 item2 .. INSTALL_FOLDER dir EXPORT export_name ) +# +function( install_headers_with_subdirectories ) + set(options "" ) + set(singleValued "INSTALL_FOLDER") + set( multiValued "PUBLIC_HEADER" ) + + cmake_parse_arguments(ARG "${options}" "${singleValued}" "${multiValued}" ${ARGN} ) + + foreach( header ${ARG_PUBLIC_HEADER} ) + + get_filename_component( dir ${header} DIRECTORY ) + + install( FILES ${header} DESTINATION "${ARG_INSTALL_FOLDER}/${dir}" ) + + endforeach() + +endfunction( install_headers_with_subdirectories ) + # Helper function like bazel's cc_library. Libraries are namespaced as # opencensus_* and public libraries are also aliased as opencensus-cpp::*. function(opencensus_lib NAME) - cmake_parse_arguments(ARG "PUBLIC" "" "SRCS;DEPS" ${ARGN}) + cmake_parse_arguments(ARG "PUBLIC" "" "HDRS;SRCS;DEPS" ${ARGN}) set(_NAME "opencensus_${NAME}") prepend_opencensus(ARG_DEPS "${ARG_DEPS}") + + string( REPLACE "${PROJECT_SOURCE_DIR}/" "" _current_dir_relative_path "${CMAKE_CURRENT_LIST_DIR}" ) + if(ARG_SRCS) add_library(${_NAME} ${ARG_SRCS}) target_link_libraries(${_NAME} PUBLIC ${ARG_DEPS}) - target_include_directories(${_NAME} PUBLIC ${PROJECT_SOURCE_DIR}) + target_include_directories(${_NAME} PUBLIC $ $ ) else() add_library(${_NAME} INTERFACE) target_link_libraries(${_NAME} INTERFACE ${ARG_DEPS}) - target_include_directories(${_NAME} INTERFACE ${PROJECT_SOURCE_DIR}) + target_include_directories(${_NAME} INTERFACE $ $ ) endif() if(ARG_PUBLIC) add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME}) + + if (ARG_HDRS) + #this will install them + #set_target_properties( ${_NAME} PROPERTIES PUBLIC_HEADER "${ARG_HDRS}" ) + install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} + INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + ) + endif() + + install( TARGETS ${_NAME} + EXPORT opencensus-cpp-targets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" +# PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + ) + else() + + if (ARG_HDRS) + # I think we have API problems whereby the internal includes are required by clients... + # Comment out this line if it is not the case... + #set_target_properties( ${_NAME} PROPERTIES PRIVATE_HEADER "${ARG_HDRS}" ) + install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} + INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + ) + endif() + + # fight export bug ? + # I don't want these installed, I don't want the associated headers installed + # I want them as depedencies to public static libraries that aggregate them.... + install( TARGETS ${_NAME} + EXPORT opencensus-cpp-targets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" +# PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + ) + endif() endfunction() diff --git a/cmake/config-version.cmake.in b/cmake/config-version.cmake.in new file mode 100644 index 00000000..db0e7158 --- /dev/null +++ b/cmake/config-version.cmake.in @@ -0,0 +1,13 @@ +# +set(PACKAGE_VERSION "@CMAKE_PROJECT_VERSION@") + +# Check whether the requested PACKAGE_FIND_VERSION is compatible +if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") + set(PACKAGE_VERSION_COMPATIBLE FALSE) +else() + set(PACKAGE_VERSION_COMPATIBLE TRUE) + if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") + set(PACKAGE_VERSION_EXACT TRUE) + endif() +endif() + diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in new file mode 100644 index 00000000..9bfff2fd --- /dev/null +++ b/cmake/config.cmake.in @@ -0,0 +1,60 @@ +# +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + +find_package( gRPC ) +find_package( googleapis ) + +include ( "${CMAKE_CURRENT_LIST_DIR}/opencensus-cpp-targets.cmake" ) + +# FIXME : can I obtain this list via pre-processing of targets file variable ? +foreach( _target + # FIXME : this makes no diference between internal libraries and + # and public ones + # this is because of a bug makeing / exporting static libs + opencensus_common_hostname + opencensus_common_random + opencensus_common_grpc_status + opencensus_common_grpc_with_user_agent + opencensus_common_stats_object + opencensus_common_string_vector_hash + opencensus_common_varint + opencensus_common_timestamp + opencensus_context + opencensus_exporters_stats_prometheus + opencensus_exporters_stats_prometheus_utils + opencensus_exporters_stats_stackdriver + opencensus_exporters_stats_stackdriver_utils + opencensus_time_series_matcher + opencensus_exporters_stats_stdout + opencensus_exporters_trace_stackdriver + opencensus_exporters_trace_stdout + opencensus_stats + opencensus_stats_test_utils + opencensus_stats_core + opencensus_stats_recording + opencensus_tags + opencensus_tags_context_util + opencensus_tags_grpc_tags_bin + opencensus_tags_with_tag_map + opencensus_trace + opencensus_trace_b3 + opencensus_trace_cloud_trace_context + opencensus_trace_context_util + opencensus_trace_grpc_trace_bin + opencensus_trace_span_context + opencensus_trace_trace_context + opencensus_trace_with_span + ) + + string( REPLACE "opencensus_" "" _target_suffix "${_target}" ) + + set(scoped_name "opencensus-c++::${_target_suffix}") + set(imported_name "${_target}") + + if (NOT TARGET ${scoped_name}) + add_library(${scoped_name} IMPORTED INTERFACE) + set_target_properties(${scoped_name} + PROPERTIES INTERFACE_LINK_LIBRARIES + ${imported_name}) + endif() +endforeach() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f503a840..61ff3e63 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -15,3 +15,5 @@ # add_subdirectory(grpc) TODO add_subdirectory(helloworld) + +add_subdirectory(memory) diff --git a/examples/memory/CMakeLists.txt b/examples/memory/CMakeLists.txt new file mode 100644 index 00000000..cc69b446 --- /dev/null +++ b/examples/memory/CMakeLists.txt @@ -0,0 +1,65 @@ +if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + + include( FetchContent ) + + FetchContent_Declare( + re2 + URL https://github.com/google/re2/archive/2020-06-01.tar.gz + URL_HASH SHA256=fb8e0f4ed7a212e3420507f27933ef5a8c01aec70e5148c6a35313573269fae6 + ) + + #find_package( absl REQUIRED ) + + FetchContent_GetProperties(re2) + if(NOT re2_POPULATED) + # build re2 as as a private static library with ashared + set(orig_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}" ) + set( orig_POSITION_INDEPENDENT_CODE "${POSITION_INDEPENDENT_CODE}" ) + + set( BUILD_SHARED_LIBS OFF ) + set( POSITION_INDEPENDENT_CODE YES ) + + FetchContent_Populate(re2) + add_subdirectory(${re2_SOURCE_DIR} ${re2_BINARY_DIR} EXCLUDE_FROM_ALL ) + + set( BUILD_SHARED_LIBS "${orig_BUILD_SHARED_LIB}" ) + set ( POSITION_INDEPENDENT_CODE "${orig_POSITION_INDEPENDENT_CODE}" ) + + endif() + + set ( USE_SYSTEM_CURL YES ) + include(FetchContent) + FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/whoshuu/cpr.git) + if ( NOT cpr_POPULATED ) + + set(orig_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}" ) + set( orig_POSITION_INDEPENDENT_CODE "${POSITION_INDEPENDENT_CODE}" ) + + set( BUILD_SHARED_LIBS OFF ) + set( POSITION_INDEPENDENT_CODE YES ) + + FetchContent_Populate(cpr) + add_subdirectory(${cpr_SOURCE_DIR} ${cpr_BINARY_DIR} EXCLUDE_FROM_ALL ) + + set( BUILD_SHARED_LIBS "${orig_BUILD_SHARED_LIB}" ) + set ( POSITION_INDEPENDENT_CODE "${orig_POSITION_INDEPENDENT_CODE}" ) + + endif() + + + add_executable(linux_memory_stats_to_stackdriver memory_stats.cc ) + target_include_directories( linux_memory_stats_to_stackdriver PRIVATE "${re2_SOURCE_DIR}" ) + target_link_libraries( + linux_memory_stats_to_stackdriver + absl::flags + absl::flags_parse + absl::strings + absl::time + opencensus-cpp::exporters_stats_stackdriver + opencensus-cpp::exporters_stats_stdout + opencensus-cpp::stats + re2 + cpr::cpr + ) + +endif() diff --git a/examples/memory/memory_stats.cc b/examples/memory/memory_stats.cc new file mode 100644 index 00000000..aff7d8a8 --- /dev/null +++ b/examples/memory/memory_stats.cc @@ -0,0 +1,442 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "absl/flags/flag.h" +#include "absl/flags/parse.h" +#include "absl/flags/usage.h" +#include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_split.h" +#include "absl/time/time.h" +#include "absl/time/clock.h" + +#include "cpr/cpr.h" + +#include "google/protobuf/util/json_util.h" + +#include "opencensus/stats/stats.h" +#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" +#include "opencensus/exporters/stats/stdout/stdout_exporter.h" + +#include "re2/re2.h" + +ABSL_FLAG( std::string, project_id, "", "stackdriver project id" ); +ABSL_FLAG( std::string, instance_id, "", "local GCE instance id" ); +ABSL_FLAG( std::string, zone, "", "local instance zone" ); +ABSL_FLAG( bool, debug, false, "debug : print to stdout" ); +ABSL_FLAG( int, period_seconds, 60, "perform a measurement every N seconds" ); +ABSL_FLAG( bool, meminfo, true, "parse /proc/meminfo ( creates ~20 metrics )" ); +ABSL_FLAG( bool, vmstat, false, "parse /proc/vmstat ( creates 130+ metrics! )" ); + +#ifndef PRODUCT_NAME_LABEL +#define PRODUCT_NAME_LABEL OpenCensus +#endif + +namespace { + // Monitored resource + // FIXME : how do I detect if I am on a gce instance, a container inside a k8s_pod in gke ? a container in gce ? + constexpr char kGCEInstanceMonitoredResource[] = "gce_instance"; + constexpr char kProjectIDLabel[] = "project_id"; + constexpr char kContainerNameLabel[] = "container_name"; + constexpr char kGCEInstanceIDLabel[] = "instance_id"; + constexpr char kZoneLabel[] = "zone"; + + constexpr char kPodMonitoredResource[] = "k8s_pod"; + constexpr char kContainerMonitoredResource[] = "k8s_container"; + constexpr char kLocationLabel[] = "location"; + constexpr char kClusterNameLabel[] = "cluster_name"; + constexpr char kNamespaceNameLabel[] = "namespace_name"; + constexpr char kPodNameLabel[] = "pod_name"; + + constexpr char kBytesUnits[] = "bytes"; + constexpr char kNoUnits[] = ""; + + constexpr char kProductName[] = #PRODUCT_NAME_LABEL ; + + + std::string ConvertMessageToJson( google::protobuf::Message const * poMsg ) + { + std::string strMsg; + + google::protobuf::util::JsonOptions stOpt; + + stOpt.always_print_enums_as_ints = true; + stOpt.always_print_primitive_fields = true; + stOpt.preserve_proto_field_names = true; + + google::protobuf::util::MessageToJsonString( *poMsg, &strMsg, stOpt ); + + return strMsg; + } + +} + +/* + The purpose of this executable is to collect /proc/meminfo and /proc/vmstat + into stackdriver monitoring metrics, using C+, OpenCensus, CMake... + + FUNCTIONALITY TODO: + - detect which metrics are never implemented and therefore should not be exported at all + - more complex views ( spool metrics for a while, resample/denoise and submit ? ) + - stackdriver project id different from monitoring resource project id ? + - detect if we are running from GCE VM, container in a GCE VM, container in GKE ... + + - discriminate by tags ? + - gce_instance instance_id numeric ( as stackdriver ) or hostname ? ( not part of data structure ) + + - create correct local aggregates for /proc/meminfo that make sense... + - observe specific process names ? /proc/[pid]/status, /proc[pid]/smap_rollup ? /proc/[pid]/oom_* ? + - I have an example to that effect that uses tags for pid, ppid, and executable name + + - somewhere else I did a python collectd plugin for nstat. implement as a similar opencensus program ? + + TODO: + - fix all warnings. + - clang-format like the rest of repository ? + - build as a separate CMake project + - build as a Bazel example + - build as a separate Bazel project + - review all the licenses ( re2, cpr, opencensus-cpp, protobuf and abseil ) + + Ideally this should be implemented as a collectd (C) plugin and exporter, and widely distributed... + + */ +int main( int argc, char** argv ) +{ + + absl::SetProgramUsageMessage("Simple example reading /proc/meminfo and eventually /proc/vmstat and moving them to stackdriver" ); + + std::vector positionalArgs = absl::ParseCommandLine( argc, argv ); + + if ( absl::GetFlag( FLAGS_debug ) ) { + + std::cout << "DEBUG MODE: registering an stdout stats exporter only" << std::endl; + + opencensus::exporters::stats::StdoutExporter::Register(); + + } else { + + std::cout << "PROD MODE: registering a stackdriver exporter only" << std::endl; + + opencensus::exporters::stats::StackdriverOptions statsOps; + + // FIXME : configurable prefix. + // this is decided at compile time for now. + statsOps.metric_name_prefix = absl::StrCat( "custom.googleapis.com/", kProductName ); + + std::string project_id = absl::GetFlag( FLAGS_project_id ); + std::string instance_id = absl::GetFlag( FLAGS_instance_id ); + std::string zone = absl::GetFlag( FLAGS_zone ); + std::string instance_name; + + if ( project_id.empty() ){ + + auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/project/project-id"}, + cpr::Header{{"Metadata-Flavor", "Google"}} ); + // FIXME : handle retry, errors, + if ( r.status_code == 200 ){ + project_id = r.text; + } else { + std::cerr << "could not obtain vm project name from metadata server" << std::endl; + return 1; + } + } + + if ( instance_id.empty() ){ + // id does the numeric id, what stackdriver expect + // name does the hostname, what a human would like + auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/id"}, + cpr::Header{{"Metadata-Flavor", "Google"}} ); + + if ( r.status_code == 200 ){ + instance_id = r.text; + } else { + std::cerr << "could not obtain vm instance name from metadata server" << std::endl; + return 1; + } + } + + if ( instance_name.empty() ){ + // id does the numeric id, what stackdriver expect + // name does the hostname, what a human would like + auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/name"}, + cpr::Header{{"Metadata-Flavor", "Google"}} ); + + if ( r.status_code == 200 ){ + instance_name = r.text; + } else { + + char hostname[HOST_NAME_MAX+1]; + int status = gethostname(hostname, HOST_NAME_MAX+1); + if ( status == 0 ){ + instance_name = hostname; + } else { + std::cerr << "could not obtain host name using gethostname(2) ..." << std::endl; + return 1; + } + } + + if ( zone.empty() ){ + + auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/zone"}, + //cpr::Parameters{{"recursive", "true"}, {"alt", "text"}}, + cpr::Header{{"Metadata-Flavor", "Google"}} ); + + // handle retry, errors, + if ( r.status_code == 200 ){ + // projects/REDACTED/zones/ZONE + zone = std::vector< std::string >( absl::StrSplit(r.text, '/') ).back(); + } else { + std::cerr << "could not obtain vm zone from metadata server" << std::endl; + return 1; + } + } + + statsOps.project_id = project_id; + statsOps.monitored_resource.set_type( kGCEInstanceMonitoredResource ); + + // FIXME : handle a stackdriver project id different from gce project id ? + (*statsOps.monitored_resource.mutable_labels())[ kProjectIDLabel ] = project_id; + (*statsOps.monitored_resource.mutable_labels())[ kGCEInstanceIDLabel ] = instance_id; // instance_name; ? + (*statsOps.monitored_resource.mutable_labels())[ kZoneLabel ] = zone; + + std::cout << "using stackdriver project_id " << statsOps.project_id << "\n"; + std::cout << "using monitored resource " << ConvertMessageToJson( &( statsOps.monitored_resource ) ) << std::endl; + + opencensus::exporters::stats::StackdriverExporter::Register( std::move( statsOps ) ); + } + + std::cout << "parsing /proc/meminfo and /proc/vmstat every " << absl::GetFlag( FLAGS_period_seconds ) << " seconds " << std::endl; + + // man proc + std::map< std::string, std::string > meminfoDescriptions = { + { "MemTotal", "Total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code)." }, + { "MemFree", "The sum of LowFree+HighFree." } , + { "MemAvailable", "An estimate of how much memory is available for starting new applications, without swapping." } , + { "Buffers", "Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or so)." } , + { "Cached", "In-memory cache for files read from the disk (the page cache). Doesn't include SwapCached." } , + { "SwapCached", "Memory that once was swapped out, is swapped back in but still also is in the swap file. " + "(If memory pressure is high, these pages don't need to be swapped out again because they are already in the swap file. This saves I/O.)" } , + { "Active", "Memory that has been used more recently and usually not reclaimed unless absolutely necessary." } , + { "Inactive", "Memory which has been less recently used. It is more eligible to be reclaimed for other purposes." } , + { "Active(anon)", "[To be documented.]" } , + { "Inactive(anon)", "[To be documented.]" } , + { "Active(file)", "[To be documented.]" } , + { "Inactive(file)", "[To be documented.]" } , + { "Unevictable", "[To be documented.]" } , + { "Mlocked", "[To be documented.]" } , + { "HighTotal", "Total amount of highmem. Highmem is all memory above ~860MB of physical memory. Highmem areas are for use by user-space programs, or for the page cache. " + "The kernel must use tricks to access this memory, making it slower to access than lowmem." } , + { "HighFree", "Amount of free highmem." } , + { "LowTotal", "Total amount of lowmem. Lowmem is memory which can be used for everything that highmem can be used for, but it is also available for the kernel's use for its own data structures. " + "Among many other things, it is where everything from Slab is allocated. Bad things happen when you're out of lowmem." } , + { "LowFree", "Amount of free lowmem." } , + { "MmapCopy", "[To be documented.]" } , + { "SwapTotal", "Total amount of swap space available." } , + { "SwapFree", "Amount of swap space that is currently unused." } , + { "Dirty", "Memory which is waiting to get written back to the disk." } , + { "Writeback", "Memory which is actively being written back to the disk." } , + { "AnonPages", "Non-file backed pages mapped into user-space page tables." } , + { "Mapped", "Files which have been mapped into memory (with mmap(2)), such as libraries." } , + { "Shmem", "Amount of memory consumed in tmpfs(5) filesystems." } , + { "Slab", "In-kernel data structures cache. (See slabinfo(5).)" } , + { "SReclaimable", "Part of Slab, that might be reclaimed, such as caches." } , + { "SUnreclaim", "Part of Slab, that cannot be reclaimed on memory pressure." } , + { "KernelStack", "Amount of memory allocated to kernel stacks." } , + { "PageTables", "Amount of memory dedicated to the lowest level of page tables." } , + { "Quicklists", "[To be documented.]" } , + { "NFS_Unstable", "NFS pages sent to the server, but not yet committed to stable storage." } , + { "Bounce", "Memory used for block device \"bounce buffers\"." } , + { "WritebackTmp", "Memory used by FUSE for temporary writeback buffers." } , + { "CommitLimit", "This is the total amount of memory currently available to be allocated on the system, expressed in kilobytes. " + "This limit is adhered to only if strict overcommit accounting is enabled (mode 2 in /proc/sys/vm/overcommit_memory). " + "The limit is calculated according to the formula described under /proc/sys/vm/overcommit_memory. " + "For further details, see the kernel source file Documentation/vm/overcommit-accounting." + } , + { "Committed_AS", "The amount of memory presently allocated on the system. The committed memory is a sum of all of the memory which has been allocated by processes," + " even if it has not been \"used\" by them as of yet. A process which allocates 1GB of memory (using malloc(3) or similar), but touches only 300MB" + " of that memory will show up as using only 300MB of memory even if it has the address space allocated for the entire 1GB." + "\n" + "This 1GB is memory which has been \"committed\" to by the VM and can be used at any time by the allocating application. " + "With strict overcommit enabled on the system (mode 2 in /proc/sys/vm/overcommit_memory), allocations which would exceed the CommitLimit will not be permitted. " + "This is useful if one needs to guarantee that processes will not fail due to lack of memory once that memory has been successfully allocated." + } , + { "VmallocTotal", "Total size of vmalloc memory area." } , + { "VmallocUsed", "Amount of vmalloc area which is used." } , + { "VmallocChunk", "Largest contiguous block of vmalloc area which is free." } , + { "HardwareCorrupted", "[To be documented.]" } , + { "AnonHugePages", "Non-file backed huge pages mapped into user-space page tables." } , + { "ShmemHugePages", "Memory used by shared memory (shmem) and tmpfs(5) allocated with huge pages" } , + { "ShmemPmdMapped", "Shared memory mapped into user space with huge pages." } , + { "CmaTotal", "Total CMA (Contiguous Memory Allocator) pages." } , + { "CmaFree", "Free CMA (Contiguous Memory Allocator) pages." } , + { "HugePages_Total", "The size of the pool of huge pages." } , + { "HugePages_Free", "The number of huge pages in the pool that are not yet allocated." } , + { "HugePages_Rsvd", "This is the number of huge pages for which a commitment to allocate from the pool has been made, but no allocation has yet been made. " + "These reserved huge pages guarantee that an application will be able to allocate a huge page from the pool of huge " + "pages at fault time." + } , + { "HugePages_Surp", "This is the number of huge pages in the pool above the value in /proc/sys/vm/nr_hugepages. " + "The maximum number of surplus huge pages is controlled by /proc/sys/vm/nr_overcommit_hugepages." + } , + { "Hugepagesize", "The size of huge pages." } , + { "DirectMap4k", "Number of bytes of RAM linearly mapped by kernel in 4kB pages." } , + { "DirectMap4M", "Number of bytes of RAM linearly mapped by kernel in 4MB pages." } , + { "DirectMap2M", "Number of bytes of RAM linearly mapped by kernel in 2MB pages." } , + { "DirectMap1G", "Number of bytes of RAM linearly mapped by kernel in 1GB pages." } + }; + + // is there _any_ documentation for /proc/vmstat _anywhere ? + + bool firstTime = true; + + std::map< std::string, std::int64_t > meminfoByteValues, meminfoCountValues, vmstatCountValues; + std::map< std::string, std::pair< opencensus::stats::MeasureInt64, std::int64_t > > meminfoByteMeasures, meminfoCountMeasures, vmstatCountMeasures; + + // use libre2 because CentOS 7 devtoolset packages a gcc 7+ which reuses the default GCC 4.8.x libstdc++ + // which has buggy / incomplete support for regex + // as a result, unix-style regular expression are buggy... + + re2::RE2 static const countRe( "(.+):\\s+([[:alnum:]]+)" ); + re2::RE2 static const byteRe( "(.+):\\s+([[:alnum:]]+) kB"); + re2::RE2 static const vmstatRe( "(.+)\\s+([[:alnum:]]+)" ); + + bool const doMemInfo = absl::GetFlag( FLAGS_meminfo ); + bool const doVmStat = absl::GetFlag( FLAGS_vmstat ); + + if ( ! ( doMemInfo || doVmStat ) ){ + std::cerr << "Nothing to do, please select --meminfo or --vmstat or both" << std::endl; + return 0; + } + + while ( true ){ + + std::ifstream ifile; + + if( doMemInfo ){ + + ifile.open( "/proc/meminfo" ); + + for ( std::string line; std::getline( ifile, line ); ){ + + std::string label; + std::int64_t count; + + if ( re2::RE2::FullMatch( line, byteRe, &label, &count ) ){ + meminfoByteValues[label] = count * 1024 ; + } else if ( re2::RE2::FullMatch( line, countRe, &label, &count ) ){ + meminfoCountValues[label] = count; + } + } + + ifile.close(); + } + + if ( doVmStat ){ + + ifile.open( "/proc/vmstat"); + + for ( std::string line; std::getline( ifile, line ); ){ + + std::string label; + std::int64_t count; + + if ( re2::RE2::FullMatch( line, vmstatRe, &label, &count ) ){ + vmstatCountValues[label] = count; + } + } + + ifile.close(); + } + + + // FIXME : hardly elegant. + if ( firstTime ){ + + // empty if doMemInfo == false + for ( auto const & p : meminfoByteValues ){ + + // FIXME : metric vs measure name ? + + // register a measure + auto mn = absl::StrCat( "proc/meminfo/", p.first ); + opencensus::stats::MeasureInt64 m( opencensus::stats::MeasureInt64::Register( mn, p.first, kBytesUnits ) ); + + auto vn = absl::StrCat( "proc/meminfo/", p.first ); // do I want a suffix for aggregation / units ? + auto desc = ( meminfoDescriptions.count( p.first ) > 0 ) ? meminfoDescriptions.at( p.first ) : absl::StrCat( p.first, " in bytes as per /proc/meminfo" ); + auto vd = opencensus::stats::ViewDescriptor() + .set_name( vn ) + .set_measure( mn ) + .set_aggregation( opencensus::stats::Aggregation::LastValue() ) // this is correct. instantaneous measure. + .set_description( desc ); + + vd.RegisterForExport(); + meminfoByteMeasures.insert( { p.first, { m, meminfoByteValues.at(p.first) } } ); + } + + for ( auto const & p : meminfoCountValues ){ + auto mn = absl::StrCat( "proc/meminfo/", p.first ); + opencensus::stats::MeasureInt64 m( opencensus::stats::MeasureInt64::Register( mn, p.first, kNoUnits ) ); + + auto vn = absl::StrCat( "proc/meminfo/", p.first ); + auto desc = ( meminfoDescriptions.count( p.first ) > 0 ) ? meminfoDescriptions.at( p.first ) : absl::StrCat( p.first, " count as per /proc/meminfo" ); + auto vd = opencensus::stats::ViewDescriptor() + .set_name( vn ) + .set_measure( mn ) + .set_aggregation( opencensus::stats::Aggregation::LastValue()) + .set_description( desc ); + + vd.RegisterForExport(); + + meminfoCountMeasures.insert( { p.first, { m, meminfoCountValues.at(p.first) } } ); + } + + // FIXME : add description labels... + // empty -f doVmStat == false ... + for ( auto const & p : vmstatCountValues ){ + auto mn = absl::StrCat( "proc/vmstat/", p.first ); + opencensus::stats::MeasureInt64 m( opencensus::stats::MeasureInt64::Register( mn, p.first, kNoUnits ) ); + + auto vn = absl::StrCat( "proc/vmstat/", p.first ); + auto desc = absl::StrCat( p.first, " count as per /proc/vmstat" ); + auto vd = opencensus::stats::ViewDescriptor() + .set_name( vn ) + .set_measure( mn ) + .set_aggregation( opencensus::stats::Aggregation::LastValue() ) // FIXME: mostly _wrong_ some are cumulative ..., some may be deltas ? + .set_description( desc ); + + vd.RegisterForExport(); + + vmstatCountMeasures.insert( { p.first, { m, vmstatCountValues.at(p.first) } } ); + } + + firstTime = false; + } + + for ( auto & p : meminfoByteMeasures ){ + // FIXME, tag by host name ? + opencensus::stats::Record( {{ p.second.first, meminfoByteValues.at(p.first) }} ); + } + + for ( auto & p : meminfoCountMeasures ){ + // FIXME, tag by host name ? + opencensus::stats::Record( {{ p.second.first, meminfoCountValues.at(p.first) }} ); + } + + for ( auto & p : vmstatCountMeasures ){ + // FIXME, tag by host name ? + opencensus::stats::Record( {{ p.second.first, vmstatCountValues.at(p.first) }} ); + } + + absl::SleepFor( absl::Seconds( absl::GetFlag( FLAGS_period_seconds ) ) ); + + } // infinite loop + + return 0; +} diff --git a/opencensus/common/internal/CMakeLists.txt b/opencensus/common/internal/CMakeLists.txt index a9dbd87b..63efa0dc 100644 --- a/opencensus/common/internal/CMakeLists.txt +++ b/opencensus/common/internal/CMakeLists.txt @@ -12,10 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -opencensus_lib(common_hostname SRCS hostname.cc DEPS absl::strings) +opencensus_lib(common_hostname HDRS hostname.h SRCS hostname.cc DEPS absl::strings) opencensus_lib( common_random + HDRS + random.h SRCS random.cc DEPS @@ -23,16 +25,23 @@ opencensus_lib( absl::synchronization absl::time) -opencensus_lib(common_stats_object DEPS absl::time) +add_subdirectory( grpc ) + +opencensus_lib(common_stats_object HDRS stats_object.h DEPS absl::time) # Define NOMINMAX to fix build errors when compiling with MSVC. target_compile_definitions(opencensus_common_stats_object INTERFACE $<$:NOMINMAX>) -opencensus_lib(common_string_vector_hash DEPS absl::hash) +opencensus_lib(common_string_vector_hash HDRS string_vector_hash.h DEPS absl::hash) + +opencensus_lib(common_varint HDRS varint.h SRCS varint.cc DEPS absl::strings) + +find_package( protobuf REQUIRED ) -opencensus_lib(common_varint SRCS varint.cc DEPS absl::strings) +opencensus_lib( common_timestamp HDRS timestamp.h SRCS timestamp.cc DEPS absl::time ) +target_link_libraries( opencensus_common_timestamp PUBLIC protobuf::libprotobuf ) # Tests. opencensus_test(common_hostname_test hostname_test.cc common_hostname) @@ -44,6 +53,10 @@ opencensus_test(common_stats_object_test stats_object_test.cc opencensus_test(common_varint_test varint_test.cc common_varint) +opencensus_test(common_timestamp_test timestamp_test.cc common_timestamp) + # Benchmarks. opencensus_benchmark(common_random_benchmark random_benchmark.cc common_random) + +opencensus_benchmark(common_timestamp_benchmark timestamp_benchmark.cc common_timestamp) diff --git a/opencensus/common/internal/grpc/CMakeLists.txt b/opencensus/common/internal/grpc/CMakeLists.txt new file mode 100644 index 00000000..5db0e747 --- /dev/null +++ b/opencensus/common/internal/grpc/CMakeLists.txt @@ -0,0 +1,30 @@ + +find_package( protobuf CONFIG REQUIRED) # for now, I need that and protobuf_MODULE_COMPATIBLE=ON +find_package( gRPC CONFIG REQUIRED) + +opencensus_lib(common_grpc_status + HDRS + status.h + SRCS + status.cc + DEPS + absl::strings +) +get_target_property( _iface_incdir gRPC::grpc++ INTERFACE_INCLUDE_DIRECTORIES) +target_include_directories( opencensus_common_grpc_status PUBLIC $ ) +target_link_libraries ( opencensus_common_grpc_status PUBLIC gRPC::grpc++ ) + +opencensus_test(common_grpc_status_test + status_test.cc + common_grpc_status ) + +opencensus_lib(common_grpc_with_user_agent + HDRS + with_user_agent.h + SRCS + with_user_agent.cc + DEPS + absl::strings +) +target_include_directories( opencensus_common_grpc_with_user_agent PUBLIC $ ) +target_link_libraries ( opencensus_common_grpc_with_user_agent PUBLIC gRPC::grpc++ ) diff --git a/opencensus/context/CMakeLists.txt b/opencensus/context/CMakeLists.txt index 149958cc..8836d84f 100644 --- a/opencensus/context/CMakeLists.txt +++ b/opencensus/context/CMakeLists.txt @@ -15,6 +15,9 @@ opencensus_lib( context PUBLIC + HDRS + context.h + with_context.h SRCS internal/context.cc internal/with_context.cc diff --git a/opencensus/exporters/stats/CMakeLists.txt b/opencensus/exporters/stats/CMakeLists.txt index 73dd63f7..d7ec8ec8 100644 --- a/opencensus/exporters/stats/CMakeLists.txt +++ b/opencensus/exporters/stats/CMakeLists.txt @@ -14,6 +14,12 @@ add_subdirectory(prometheus) -# add_subdirectory(stackdriver) TODO +find_package( googleapis COMPONENTS googleapis_cpp_monitoring_v3 ) +if ( googleapis_FOUND ) + + add_subdirectory(stackdriver) + +endif() + add_subdirectory(stdout) diff --git a/opencensus/exporters/stats/prometheus/CMakeLists.txt b/opencensus/exporters/stats/prometheus/CMakeLists.txt index 01038cce..dce9edfc 100644 --- a/opencensus/exporters/stats/prometheus/CMakeLists.txt +++ b/opencensus/exporters/stats/prometheus/CMakeLists.txt @@ -15,14 +15,19 @@ opencensus_lib( exporters_stats_prometheus PUBLIC + HDRS + prometheus_exporter.h SRCS internal/prometheus_exporter.cc DEPS exporters_stats_prometheus_utils stats) +# internal library exposed only through the previous one _implementation opencensus_lib( exporters_stats_prometheus_utils + HDRS + internal/prometheus_utils.h SRCS internal/prometheus_utils.cc DEPS diff --git a/opencensus/exporters/stats/stackdriver/CMakeLists.txt b/opencensus/exporters/stats/stackdriver/CMakeLists.txt new file mode 100644 index 00000000..0d445b53 --- /dev/null +++ b/opencensus/exporters/stats/stackdriver/CMakeLists.txt @@ -0,0 +1,69 @@ +opencensus_lib( exporters_stats_stackdriver + PUBLIC + HDRS + stackdriver_exporter.h + SRCS + internal/stackdriver_exporter.cc + DEPS + common_hostname + common_grpc_status + common_grpc_with_user_agent + stats + exporters_stats_stackdriver_utils +) + +# FIXME : these are supposed to be INTERNAL LIBRARIES EXPOSED VIA PREVIOUS? +opencensus_lib( exporters_stats_stackdriver_utils + #PUBLIC + HDRS + internal/stackdriver_utils.h + SRCS + internal/stackdriver_utils.cc + DEPS + stats + common_timestamp +) + +opencensus_lib( time_series_matcher + #PUBLIC + HDRS + internal/time_series_matcher.h + SRCS + internal/time_series_matcher.cc + DEPS + stats +) + +# why can't I just declare googleapis-c++::foo_bar ? did I fuck up my exporting ? + + +find_package( googleapis CONFIG REQUIRED googleapis_cpp_monitoring_v3_protos ) + +# this works +get_target_property( _iface_incdir googleapis_cpp_monitoring_v3_protos INTERFACE_INCLUDE_DIRECTORIES ) +target_include_directories( opencensus_exporters_stats_stackdriver PRIVATE $ ) +target_include_directories( opencensus_exporters_stats_stackdriver_utils PRIVATE $ ) + +target_link_libraries( opencensus_exporters_stats_stackdriver PUBLIC googleapis_cpp_monitoring_v3_protos ) +target_link_libraries( opencensus_exporters_stats_stackdriver_utils PUBLIC googleapis_cpp_monitoring_v3_protos ) + +get_target_property( _gmock_iface_incdir gmock INTERFACE_INCLUDE_DIRECTORIES ) +target_include_directories( opencensus_time_series_matcher PUBLIC $ ) +#target_include_directories( opencensus_time_series_matcher PUBLIC $ ) +target_link_libraries ( opencensus_time_series_matcher PUBLIC gmock googleapis_cpp_monitoring_v3_protos ) + +opencensus_test(exporters_stats_stackdriver_utils_test + internal/stackdriver_utils_test.cc + exporters_stats_stackdriver_utils + time_series_matcher + stats + stats_test_utils +) + +opencensus_test(exporters_stats_stackdriver_e2e_test + internal/stackdriver_e2e_test.cc + exporters_stats_stackdriver + exporters_stats_stackdriver_utils + time_series_matcher + stats +) diff --git a/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc b/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc index a74f72b4..3c9df9b0 100644 --- a/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc +++ b/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc @@ -29,6 +29,8 @@ #include "opencensus/exporters/stats/stackdriver/internal/time_series_matcher.h" #include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" #include "opencensus/stats/stats.h" +#include "opencensus/common/internal/timestamp.h" + namespace opencensus { namespace exporters { @@ -110,9 +112,9 @@ StackdriverE2eTest::RetrieveData( request.set_filter( absl::StrCat("metric.type = \"custom.googleapis.com/opencensus/", descriptor.name(), "\"")); - SetTimestamp(absl::Now() - absl::Hours(1), + opencensus::common::SetTimestamp(absl::Now() - absl::Hours(1), request.mutable_interval()->mutable_start_time()); - SetTimestamp(absl::Now() + absl::Hours(1), + opencensus::common::SetTimestamp(absl::Now() + absl::Hours(1), request.mutable_interval()->mutable_end_time()); while (true) { diff --git a/opencensus/exporters/stats/stdout/CMakeLists.txt b/opencensus/exporters/stats/stdout/CMakeLists.txt index 5278e653..388dd521 100644 --- a/opencensus/exporters/stats/stdout/CMakeLists.txt +++ b/opencensus/exporters/stats/stdout/CMakeLists.txt @@ -15,6 +15,8 @@ opencensus_lib( exporters_stats_stdout PUBLIC + HDRS + stdout_exporter.h SRCS internal/stdout_exporter.cc DEPS diff --git a/opencensus/exporters/trace/CMakeLists.txt b/opencensus/exporters/trace/CMakeLists.txt index aab0ca46..2a549b65 100644 --- a/opencensus/exporters/trace/CMakeLists.txt +++ b/opencensus/exporters/trace/CMakeLists.txt @@ -12,7 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -# add_subdirectory(stackdriver) TODO + +find_package( googleapis COMPONENTS googleapis_cpp_devtools_cloudtrace_v2_protos ) +if ( googleapis_FOUND ) + + add_subdirectory(stackdriver) + +endif() add_subdirectory(stdout) diff --git a/opencensus/exporters/trace/stackdriver/CMakeLists.txt b/opencensus/exporters/trace/stackdriver/CMakeLists.txt new file mode 100644 index 00000000..282c21c7 --- /dev/null +++ b/opencensus/exporters/trace/stackdriver/CMakeLists.txt @@ -0,0 +1,24 @@ +opencensus_lib( exporters_trace_stackdriver + PUBLIC + HDRS + stackdriver_exporter.h + SRCS + internal/stackdriver_exporter.cc + DEPS + common_hostname + common_grpc_status + common_grpc_with_user_agent + trace + ) + +find_package( googleapis CONFIG REQUIRED googleapis_cpp_devtools_cloudtrace_v2_protos ) + +#get_target_property(_iface_incdir googleapis_cpp_devtools_cloudtrace_v2_protos INTERFACE_INCLUDE_DIRECTORIES ) +# +#target_include_directories( opencensus_exporters_trace_stackdriver PRIVATE ${_iface_incdir}) +target_link_libraries( opencensus_exporters_trace_stackdriver + PUBLIC + googleapis_cpp_devtools_cloudtrace_v2_trace_protos + googleapis_cpp_devtools_cloudtrace_v2_tracing_protos + googleapis_cpp_rpc_status_protos ) + diff --git a/opencensus/exporters/trace/stdout/CMakeLists.txt b/opencensus/exporters/trace/stdout/CMakeLists.txt index c7c580f1..c65b9091 100644 --- a/opencensus/exporters/trace/stdout/CMakeLists.txt +++ b/opencensus/exporters/trace/stdout/CMakeLists.txt @@ -15,10 +15,13 @@ opencensus_lib( exporters_trace_stdout PUBLIC + HDRS + stdout_exporter.h SRCS internal/stdout_exporter.cc DEPS trace + context absl::base absl::memory) diff --git a/opencensus/stats/CMakeLists.txt b/opencensus/stats/CMakeLists.txt index 9b0d7afa..70a64c44 100644 --- a/opencensus/stats/CMakeLists.txt +++ b/opencensus/stats/CMakeLists.txt @@ -12,11 +12,34 @@ # See the License for the specific language governing permissions and # limitations under the License. -opencensus_lib(stats PUBLIC DEPS stats_core stats_recording) +opencensus_lib( + stats + PUBLIC + HDRS + aggregation.h + bucket_boundaries.h + distribution.h + measure.h + measure_descriptor.h + measure_registry.h + recording.h + stats.h + stats_exporter.h + tag_key.h + tag_set.h + view.h + view_data.h + view_descriptor.h + DEPS + stats_core + stats_recording + ) opencensus_lib( stats_test_utils PUBLIC + HRDS + testing/test_utils.h SRCS testing/test_utils.cc DEPS @@ -26,6 +49,27 @@ opencensus_lib( opencensus_lib( stats_core + HDRS + aggregation.h + bucket_boundaries.h + distribution.h + internal/aggregation_window.h + internal/delta_producer.h + internal/measure_data.h + internal/measure_registry_impl.h + internal/set_aggregation_window.h + internal/stats_exporter_impl.h + internal/stats_manager.h + internal/view_data_impl.h + measure.h + measure_descriptor.h + measure_registry.h + stats_exporter.h + tag_key.h + tag_set.h + view.h + view_data.h + view_descriptor.h SRCS internal/aggregation.cc internal/aggregation_window.cc @@ -62,6 +106,8 @@ target_compile_definitions(opencensus_stats_core opencensus_lib( stats_recording + HDRS + recording.h SRCS internal/recording.cc DEPS diff --git a/opencensus/tags/CMakeLists.txt b/opencensus/tags/CMakeLists.txt index 7219a2af..8fb2a858 100644 --- a/opencensus/tags/CMakeLists.txt +++ b/opencensus/tags/CMakeLists.txt @@ -15,6 +15,9 @@ opencensus_lib( tags PUBLIC + HDRS + tag_key.h + tag_map.h SRCS internal/tag_key.cc internal/tag_map.cc @@ -27,6 +30,8 @@ opencensus_lib( opencensus_lib( tags_context_util PUBLIC + HDRS + context_util.h SRCS internal/context_util.cc DEPS @@ -36,6 +41,8 @@ opencensus_lib( opencensus_lib( tags_grpc_tags_bin PUBLIC + HDRS + propagation/grpc_tags_bin.h SRCS internal/grpc_tags_bin.cc DEPS @@ -46,6 +53,8 @@ opencensus_lib( opencensus_lib( tags_with_tag_map PUBLIC + HDRS + with_tag_map.h SRCS internal/with_tag_map.cc DEPS diff --git a/opencensus/trace/CMakeLists.txt b/opencensus/trace/CMakeLists.txt index 2c4e6563..003e1f4c 100644 --- a/opencensus/trace/CMakeLists.txt +++ b/opencensus/trace/CMakeLists.txt @@ -15,7 +15,31 @@ opencensus_lib( trace PUBLIC - SRCS + HDRS + attribute_value_ref.h + exporter/annotation.h + exporter/attribute_value.h + exporter/link.h + exporter/message_event.h + exporter/span_data.h + exporter/span_exporter.h + exporter/status.h + internal/attribute_list.h + internal/local_span_store.h + internal/local_span_store_impl.h + internal/running_span_store.h + internal/running_span_store_impl.h + internal/span_exporter_impl.h + internal/span_impl.h + internal/trace_config_impl.h + internal/trace_events.h + internal/trace_params_impl.h + sampler.h + span.h + status_code.h + trace_config.h + trace_params.h + SRCS internal/annotation.cc internal/attribute_list.cc internal/attribute_value.cc @@ -56,6 +80,8 @@ target_compile_definitions(opencensus_trace opencensus_lib( trace_b3 PUBLIC + HDRS + propagation/b3.h SRCS internal/b3.cc DEPS @@ -66,6 +92,8 @@ opencensus_lib( opencensus_lib( trace_cloud_trace_context PUBLIC + HDRS + propagation/cloud_trace_context.h SRCS internal/cloud_trace_context.cc DEPS @@ -76,6 +104,8 @@ opencensus_lib( opencensus_lib( trace_context_util PUBLIC + HDRS + context_util.h SRCS internal/context_util.cc DEPS @@ -85,6 +115,8 @@ opencensus_lib( opencensus_lib( trace_grpc_trace_bin PUBLIC + HDRS + propagation/grpc_trace_bin.h SRCS internal/grpc_trace_bin.cc DEPS @@ -95,6 +127,11 @@ opencensus_lib( opencensus_lib( trace_span_context PUBLIC + HDRS + span_context.h + span_id.h + trace_id.h + trace_options.h SRCS internal/span_context.cc internal/span_id.cc @@ -106,6 +143,8 @@ opencensus_lib( opencensus_lib( trace_trace_context PUBLIC + HDRS + propagation/trace_context.h SRCS internal/trace_context.cc DEPS @@ -116,6 +155,8 @@ opencensus_lib( opencensus_lib( trace_with_span PUBLIC + HDRS + with_span.h SRCS internal/with_span.cc DEPS @@ -126,13 +167,13 @@ opencensus_lib( # Tests # ---------------------------------------------------------------------- -opencensus_test(trace_annotation_test internal/annotation_test.cc trace) +opencensus_test(trace_annotation_test internal/annotation_test.cc trace context ) opencensus_test(trace_attribute_value_ref_test - internal/attribute_value_ref_test.cc trace) + internal/attribute_value_ref_test.cc trace context ) opencensus_test(trace_attribute_value_test internal/attribute_value_test.cc - trace) + trace context ) opencensus_test(trace_b3_test internal/b3_test.cc trace_b3) @@ -145,23 +186,23 @@ opencensus_test(trace_context_util_test internal/context_util_test.cc trace opencensus_test(trace_grpc_trace_bin_test internal/grpc_trace_bin_test.cc trace_grpc_trace_bin) -opencensus_test(trace_link_test internal/link_test.cc trace) +opencensus_test(trace_link_test internal/link_test.cc trace context ) opencensus_test(trace_local_span_store_test internal/local_span_store_test.cc - trace absl::memory absl::synchronization) + context trace absl::memory absl::synchronization) opencensus_test( - trace_running_span_store_test internal/running_span_store_test.cc trace + trace_running_span_store_test internal/running_span_store_test.cc context trace absl::base absl::memory absl::synchronization) -opencensus_test(trace_sampler_test internal/sampler_test.cc trace absl::strings +opencensus_test(trace_sampler_test internal/sampler_test.cc context trace absl::strings absl::synchronization absl::time) -opencensus_test(trace_span_test internal/span_test.cc trace absl::strings) +opencensus_test(trace_span_test internal/span_test.cc context trace absl::strings) -opencensus_test(trace_span_id_test internal/span_id_test.cc trace) +opencensus_test(trace_span_id_test internal/span_id_test.cc context trace) -opencensus_test(trace_span_options_test internal/span_options_test.cc trace +opencensus_test(trace_span_options_test internal/span_options_test.cc context trace absl::strings absl::synchronization) opencensus_test(trace_span_context_test internal/span_context_test.cc @@ -170,18 +211,19 @@ opencensus_test(trace_span_context_test internal/span_context_test.cc opencensus_test( trace_span_exporter_test internal/span_exporter_test.cc + context trace absl::memory absl::strings absl::synchronization absl::time) -opencensus_test(trace_status_test internal/status_test.cc trace absl::strings) +opencensus_test(trace_status_test internal/status_test.cc context trace absl::strings) -opencensus_test(trace_trace_config_test internal/trace_config_test.cc trace +opencensus_test(trace_trace_config_test internal/trace_config_test.cc context trace absl::time) -opencensus_test(trace_trace_options_test internal/trace_options_test.cc trace) +opencensus_test(trace_trace_options_test internal/trace_options_test.cc context trace) opencensus_test(trace_trace_context_test internal/trace_context_test.cc trace_trace_context) @@ -190,7 +232,7 @@ opencensus_test(trace_with_span_test internal/with_span_test.cc trace trace_with_span context) opencensus_benchmark(trace_attribute_value_ref_benchmark - internal/attribute_value_ref_benchmark.cc trace) + internal/attribute_value_ref_benchmark.cc context trace) opencensus_benchmark(trace_b3_benchmark internal/b3_benchmark.cc trace_b3) @@ -202,10 +244,10 @@ opencensus_benchmark(trace_grpc_trace_bin_benchmark internal/grpc_trace_bin_benchmark.cc trace_grpc_trace_bin) opencensus_benchmark(trace_sampler_benchmark internal/sampler_benchmark.cc - trace_span_context trace) + trace_span_context context trace) opencensus_benchmark(trace_span_benchmark internal/span_benchmark.cc - trace_span_context trace) + trace_span_context context trace) opencensus_benchmark(trace_span_id_benchmark internal/span_id_benchmark.cc trace_span_context) From 618a630edaf4d6913f741512cf221af7703ed7cc Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 11:21:22 -0700 Subject: [PATCH 02/12] clang-format --- examples/memory/memory_stats.cc | 734 ++++++++++++++++++-------------- 1 file changed, 413 insertions(+), 321 deletions(-) diff --git a/examples/memory/memory_stats.cc b/examples/memory/memory_stats.cc index aff7d8a8..92c5ea72 100644 --- a/examples/memory/memory_stats.cc +++ b/examples/memory/memory_stats.cc @@ -13,88 +13,84 @@ #include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" -#include "absl/time/time.h" #include "absl/time/clock.h" +#include "absl/time/time.h" #include "cpr/cpr.h" #include "google/protobuf/util/json_util.h" -#include "opencensus/stats/stats.h" #include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" #include "opencensus/exporters/stats/stdout/stdout_exporter.h" +#include "opencensus/stats/stats.h" #include "re2/re2.h" -ABSL_FLAG( std::string, project_id, "", "stackdriver project id" ); -ABSL_FLAG( std::string, instance_id, "", "local GCE instance id" ); -ABSL_FLAG( std::string, zone, "", "local instance zone" ); -ABSL_FLAG( bool, debug, false, "debug : print to stdout" ); -ABSL_FLAG( int, period_seconds, 60, "perform a measurement every N seconds" ); -ABSL_FLAG( bool, meminfo, true, "parse /proc/meminfo ( creates ~20 metrics )" ); -ABSL_FLAG( bool, vmstat, false, "parse /proc/vmstat ( creates 130+ metrics! )" ); +ABSL_FLAG(std::string, project_id, "", "stackdriver project id"); +ABSL_FLAG(std::string, instance_id, "", "local GCE instance id"); +ABSL_FLAG(std::string, zone, "", "local instance zone"); +ABSL_FLAG(bool, debug, false, "debug : print to stdout"); +ABSL_FLAG(int, period_seconds, 60, "perform a measurement every N seconds"); +ABSL_FLAG(bool, meminfo, true, "parse /proc/meminfo ( creates ~20 metrics )"); +ABSL_FLAG(bool, vmstat, false, "parse /proc/vmstat ( creates 130+ metrics! )"); #ifndef PRODUCT_NAME_LABEL #define PRODUCT_NAME_LABEL OpenCensus #endif namespace { - // Monitored resource - // FIXME : how do I detect if I am on a gce instance, a container inside a k8s_pod in gke ? a container in gce ? - constexpr char kGCEInstanceMonitoredResource[] = "gce_instance"; - constexpr char kProjectIDLabel[] = "project_id"; - constexpr char kContainerNameLabel[] = "container_name"; - constexpr char kGCEInstanceIDLabel[] = "instance_id"; - constexpr char kZoneLabel[] = "zone"; - - constexpr char kPodMonitoredResource[] = "k8s_pod"; - constexpr char kContainerMonitoredResource[] = "k8s_container"; - constexpr char kLocationLabel[] = "location"; - constexpr char kClusterNameLabel[] = "cluster_name"; - constexpr char kNamespaceNameLabel[] = "namespace_name"; - constexpr char kPodNameLabel[] = "pod_name"; +// Monitored resource +// FIXME : how do I detect if I am on a gce instance, a container inside a +// k8s_pod in gke ? a container in gce ? +constexpr char kGCEInstanceMonitoredResource[] = "gce_instance"; +constexpr char kProjectIDLabel[] = "project_id"; +constexpr char kContainerNameLabel[] = "container_name"; +constexpr char kGCEInstanceIDLabel[] = "instance_id"; +constexpr char kZoneLabel[] = "zone"; - constexpr char kBytesUnits[] = "bytes"; - constexpr char kNoUnits[] = ""; +constexpr char kPodMonitoredResource[] = "k8s_pod"; +constexpr char kContainerMonitoredResource[] = "k8s_container"; +constexpr char kLocationLabel[] = "location"; +constexpr char kClusterNameLabel[] = "cluster_name"; +constexpr char kNamespaceNameLabel[] = "namespace_name"; +constexpr char kPodNameLabel[] = "pod_name"; - constexpr char kProductName[] = #PRODUCT_NAME_LABEL ; +constexpr char kBytesUnits[] = "bytes"; +constexpr char kNoUnits[] = ""; +constexpr char kProductName[] = #PRODUCT_NAME_LABEL; - std::string ConvertMessageToJson( google::protobuf::Message const * poMsg ) - { - std::string strMsg; +std::string ConvertMessageToJson(google::protobuf::Message const* poMsg) { + std::string strMsg; - google::protobuf::util::JsonOptions stOpt; + google::protobuf::util::JsonOptions stOpt; - stOpt.always_print_enums_as_ints = true; - stOpt.always_print_primitive_fields = true; - stOpt.preserve_proto_field_names = true; + stOpt.always_print_enums_as_ints = true; + stOpt.always_print_primitive_fields = true; + stOpt.preserve_proto_field_names = true; - google::protobuf::util::MessageToJsonString( *poMsg, &strMsg, stOpt ); - - return strMsg; - } + google::protobuf::util::MessageToJsonString(*poMsg, &strMsg, stOpt); + return strMsg; } +} // namespace + /* The purpose of this executable is to collect /proc/meminfo and /proc/vmstat into stackdriver monitoring metrics, using C+, OpenCensus, CMake... FUNCTIONALITY TODO: - - detect which metrics are never implemented and therefore should not be exported at all - - more complex views ( spool metrics for a while, resample/denoise and submit ? ) + - detect which metrics are never implemented and dont export them + + - more complex views ( spool metrics,resample/denoise and submit ? ) - stackdriver project id different from monitoring resource project id ? - - detect if we are running from GCE VM, container in a GCE VM, container in GKE ... + - detect if we are running from GCE VM, container in a GCE VM, in GKE... - discriminate by tags ? - - gce_instance instance_id numeric ( as stackdriver ) or hostname ? ( not part of data structure ) + - gce_instance instance_id numeric ( as stackdriver ) or hostname ? - create correct local aggregates for /proc/meminfo that make sense... - - observe specific process names ? /proc/[pid]/status, /proc[pid]/smap_rollup ? /proc/[pid]/oom_* ? - - I have an example to that effect that uses tags for pid, ppid, and executable name - - - somewhere else I did a python collectd plugin for nstat. implement as a similar opencensus program ? TODO: - fix all warnings. @@ -102,341 +98,437 @@ namespace { - build as a separate CMake project - build as a Bazel example - build as a separate Bazel project - - review all the licenses ( re2, cpr, opencensus-cpp, protobuf and abseil ) - - Ideally this should be implemented as a collectd (C) plugin and exporter, and widely distributed... - + - review all the licenses (re2, cpr, opencensus-cpp, protobuf and abseil) */ -int main( int argc, char** argv ) -{ - absl::SetProgramUsageMessage("Simple example reading /proc/meminfo and eventually /proc/vmstat and moving them to stackdriver" ); +int main(int argc, char** argv) { + absl::SetProgramUsageMessage( + "Simple example reading /proc/meminfo and eventually /proc/vmstat and " + "moving them to stackdriver"); - std::vector positionalArgs = absl::ParseCommandLine( argc, argv ); + std::vector positionalArgs = absl::ParseCommandLine(argc, argv); - if ( absl::GetFlag( FLAGS_debug ) ) { - - std::cout << "DEBUG MODE: registering an stdout stats exporter only" << std::endl; + if (absl::GetFlag(FLAGS_debug)) { + std::cout << "DEBUG MODE: registering an stdout stats exporter only" + << std::endl; opencensus::exporters::stats::StdoutExporter::Register(); } else { - - std::cout << "PROD MODE: registering a stackdriver exporter only" << std::endl; + std::cout << "PROD MODE: registering a stackdriver exporter only" + << std::endl; opencensus::exporters::stats::StackdriverOptions statsOps; // FIXME : configurable prefix. // this is decided at compile time for now. - statsOps.metric_name_prefix = absl::StrCat( "custom.googleapis.com/", kProductName ); + statsOps.metric_name_prefix = + absl::StrCat("custom.googleapis.com/", kProductName); - std::string project_id = absl::GetFlag( FLAGS_project_id ); - std::string instance_id = absl::GetFlag( FLAGS_instance_id ); - std::string zone = absl::GetFlag( FLAGS_zone ); + std::string project_id = absl::GetFlag(FLAGS_project_id); + std::string instance_id = absl::GetFlag(FLAGS_instance_id); + std::string zone = absl::GetFlag(FLAGS_zone); std::string instance_name; - if ( project_id.empty() ){ - - auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/project/project-id"}, - cpr::Header{{"Metadata-Flavor", "Google"}} ); - // FIXME : handle retry, errors, - if ( r.status_code == 200 ){ - project_id = r.text; - } else { - std::cerr << "could not obtain vm project name from metadata server" << std::endl; - return 1; - } + if (project_id.empty()) { + auto r = cpr::Get( + cpr::Url{ + "http://169.254.169.254/computeMetadata/v1/project/project-id"}, + cpr::Header{{"Metadata-Flavor", "Google"}}); + // FIXME : handle retry, errors, + if (r.status_code == 200) { + project_id = r.text; + } else { + std::cerr << "could not obtain vm project name from metadata server" + << std::endl; + return 1; + } } - if ( instance_id.empty() ){ + if (instance_id.empty()) { // id does the numeric id, what stackdriver expect // name does the hostname, what a human would like - auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/id"}, - cpr::Header{{"Metadata-Flavor", "Google"}} ); - - if ( r.status_code == 200 ){ - instance_id = r.text; - } else { - std::cerr << "could not obtain vm instance name from metadata server" << std::endl; - return 1; - } + auto r = cpr::Get( + cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/id"}, + cpr::Header{{"Metadata-Flavor", "Google"}}); + + if (r.status_code == 200) { + instance_id = r.text; + } else { + std::cerr << "could not obtain vm instance name from metadata server" + << std::endl; + return 1; + } } - if ( instance_name.empty() ){ + if (instance_name.empty()) { // id does the numeric id, what stackdriver expect // name does the hostname, what a human would like - auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/name"}, - cpr::Header{{"Metadata-Flavor", "Google"}} ); - - if ( r.status_code == 200 ){ - instance_name = r.text; + auto r = cpr::Get( + cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/name"}, + cpr::Header{{"Metadata-Flavor", "Google"}}); + + if (r.status_code == 200) { + instance_name = r.text; + } else { + char hostname[HOST_NAME_MAX + 1]; + int status = gethostname(hostname, HOST_NAME_MAX + 1); + if (status == 0) { + instance_name = hostname; } else { - - char hostname[HOST_NAME_MAX+1]; - int status = gethostname(hostname, HOST_NAME_MAX+1); - if ( status == 0 ){ - instance_name = hostname; - } else { - std::cerr << "could not obtain host name using gethostname(2) ..." << std::endl; - return 1; + std::cerr << "could not obtain host name using gethostname(2) ..." + << std::endl; + return 1; } - } - - if ( zone.empty() ){ + } - auto r = cpr::Get(cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/zone"}, - //cpr::Parameters{{"recursive", "true"}, {"alt", "text"}}, - cpr::Header{{"Metadata-Flavor", "Google"}} ); + if (zone.empty()) { + auto r = cpr::Get( + cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/zone"}, + // cpr::Parameters{{"recursive", "true"}, {"alt", "text"}}, + cpr::Header{{"Metadata-Flavor", "Google"}}); // handle retry, errors, - if ( r.status_code == 200 ){ + if (r.status_code == 200) { // projects/REDACTED/zones/ZONE - zone = std::vector< std::string >( absl::StrSplit(r.text, '/') ).back(); + zone = std::vector(absl::StrSplit(r.text, '/')).back(); } else { - std::cerr << "could not obtain vm zone from metadata server" << std::endl; + std::cerr << "could not obtain vm zone from metadata server" + << std::endl; return 1; } - } - - statsOps.project_id = project_id; - statsOps.monitored_resource.set_type( kGCEInstanceMonitoredResource ); - - // FIXME : handle a stackdriver project id different from gce project id ? - (*statsOps.monitored_resource.mutable_labels())[ kProjectIDLabel ] = project_id; - (*statsOps.monitored_resource.mutable_labels())[ kGCEInstanceIDLabel ] = instance_id; // instance_name; ? - (*statsOps.monitored_resource.mutable_labels())[ kZoneLabel ] = zone; - - std::cout << "using stackdriver project_id " << statsOps.project_id << "\n"; - std::cout << "using monitored resource " << ConvertMessageToJson( &( statsOps.monitored_resource ) ) << std::endl; - - opencensus::exporters::stats::StackdriverExporter::Register( std::move( statsOps ) ); - } - - std::cout << "parsing /proc/meminfo and /proc/vmstat every " << absl::GetFlag( FLAGS_period_seconds ) << " seconds " << std::endl; - - // man proc - std::map< std::string, std::string > meminfoDescriptions = { - { "MemTotal", "Total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code)." }, - { "MemFree", "The sum of LowFree+HighFree." } , - { "MemAvailable", "An estimate of how much memory is available for starting new applications, without swapping." } , - { "Buffers", "Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or so)." } , - { "Cached", "In-memory cache for files read from the disk (the page cache). Doesn't include SwapCached." } , - { "SwapCached", "Memory that once was swapped out, is swapped back in but still also is in the swap file. " - "(If memory pressure is high, these pages don't need to be swapped out again because they are already in the swap file. This saves I/O.)" } , - { "Active", "Memory that has been used more recently and usually not reclaimed unless absolutely necessary." } , - { "Inactive", "Memory which has been less recently used. It is more eligible to be reclaimed for other purposes." } , - { "Active(anon)", "[To be documented.]" } , - { "Inactive(anon)", "[To be documented.]" } , - { "Active(file)", "[To be documented.]" } , - { "Inactive(file)", "[To be documented.]" } , - { "Unevictable", "[To be documented.]" } , - { "Mlocked", "[To be documented.]" } , - { "HighTotal", "Total amount of highmem. Highmem is all memory above ~860MB of physical memory. Highmem areas are for use by user-space programs, or for the page cache. " - "The kernel must use tricks to access this memory, making it slower to access than lowmem." } , - { "HighFree", "Amount of free highmem." } , - { "LowTotal", "Total amount of lowmem. Lowmem is memory which can be used for everything that highmem can be used for, but it is also available for the kernel's use for its own data structures. " - "Among many other things, it is where everything from Slab is allocated. Bad things happen when you're out of lowmem." } , - { "LowFree", "Amount of free lowmem." } , - { "MmapCopy", "[To be documented.]" } , - { "SwapTotal", "Total amount of swap space available." } , - { "SwapFree", "Amount of swap space that is currently unused." } , - { "Dirty", "Memory which is waiting to get written back to the disk." } , - { "Writeback", "Memory which is actively being written back to the disk." } , - { "AnonPages", "Non-file backed pages mapped into user-space page tables." } , - { "Mapped", "Files which have been mapped into memory (with mmap(2)), such as libraries." } , - { "Shmem", "Amount of memory consumed in tmpfs(5) filesystems." } , - { "Slab", "In-kernel data structures cache. (See slabinfo(5).)" } , - { "SReclaimable", "Part of Slab, that might be reclaimed, such as caches." } , - { "SUnreclaim", "Part of Slab, that cannot be reclaimed on memory pressure." } , - { "KernelStack", "Amount of memory allocated to kernel stacks." } , - { "PageTables", "Amount of memory dedicated to the lowest level of page tables." } , - { "Quicklists", "[To be documented.]" } , - { "NFS_Unstable", "NFS pages sent to the server, but not yet committed to stable storage." } , - { "Bounce", "Memory used for block device \"bounce buffers\"." } , - { "WritebackTmp", "Memory used by FUSE for temporary writeback buffers." } , - { "CommitLimit", "This is the total amount of memory currently available to be allocated on the system, expressed in kilobytes. " - "This limit is adhered to only if strict overcommit accounting is enabled (mode 2 in /proc/sys/vm/overcommit_memory). " - "The limit is calculated according to the formula described under /proc/sys/vm/overcommit_memory. " - "For further details, see the kernel source file Documentation/vm/overcommit-accounting." - } , - { "Committed_AS", "The amount of memory presently allocated on the system. The committed memory is a sum of all of the memory which has been allocated by processes," - " even if it has not been \"used\" by them as of yet. A process which allocates 1GB of memory (using malloc(3) or similar), but touches only 300MB" - " of that memory will show up as using only 300MB of memory even if it has the address space allocated for the entire 1GB." - "\n" - "This 1GB is memory which has been \"committed\" to by the VM and can be used at any time by the allocating application. " - "With strict overcommit enabled on the system (mode 2 in /proc/sys/vm/overcommit_memory), allocations which would exceed the CommitLimit will not be permitted. " - "This is useful if one needs to guarantee that processes will not fail due to lack of memory once that memory has been successfully allocated." - } , - { "VmallocTotal", "Total size of vmalloc memory area." } , - { "VmallocUsed", "Amount of vmalloc area which is used." } , - { "VmallocChunk", "Largest contiguous block of vmalloc area which is free." } , - { "HardwareCorrupted", "[To be documented.]" } , - { "AnonHugePages", "Non-file backed huge pages mapped into user-space page tables." } , - { "ShmemHugePages", "Memory used by shared memory (shmem) and tmpfs(5) allocated with huge pages" } , - { "ShmemPmdMapped", "Shared memory mapped into user space with huge pages." } , - { "CmaTotal", "Total CMA (Contiguous Memory Allocator) pages." } , - { "CmaFree", "Free CMA (Contiguous Memory Allocator) pages." } , - { "HugePages_Total", "The size of the pool of huge pages." } , - { "HugePages_Free", "The number of huge pages in the pool that are not yet allocated." } , - { "HugePages_Rsvd", "This is the number of huge pages for which a commitment to allocate from the pool has been made, but no allocation has yet been made. " - "These reserved huge pages guarantee that an application will be able to allocate a huge page from the pool of huge " - "pages at fault time." - } , - { "HugePages_Surp", "This is the number of huge pages in the pool above the value in /proc/sys/vm/nr_hugepages. " - "The maximum number of surplus huge pages is controlled by /proc/sys/vm/nr_overcommit_hugepages." - } , - { "Hugepagesize", "The size of huge pages." } , - { "DirectMap4k", "Number of bytes of RAM linearly mapped by kernel in 4kB pages." } , - { "DirectMap4M", "Number of bytes of RAM linearly mapped by kernel in 4MB pages." } , - { "DirectMap2M", "Number of bytes of RAM linearly mapped by kernel in 2MB pages." } , - { "DirectMap1G", "Number of bytes of RAM linearly mapped by kernel in 1GB pages." } - }; - - // is there _any_ documentation for /proc/vmstat _anywhere ? - - bool firstTime = true; - - std::map< std::string, std::int64_t > meminfoByteValues, meminfoCountValues, vmstatCountValues; - std::map< std::string, std::pair< opencensus::stats::MeasureInt64, std::int64_t > > meminfoByteMeasures, meminfoCountMeasures, vmstatCountMeasures; - - // use libre2 because CentOS 7 devtoolset packages a gcc 7+ which reuses the default GCC 4.8.x libstdc++ - // which has buggy / incomplete support for regex - // as a result, unix-style regular expression are buggy... - - re2::RE2 static const countRe( "(.+):\\s+([[:alnum:]]+)" ); - re2::RE2 static const byteRe( "(.+):\\s+([[:alnum:]]+) kB"); - re2::RE2 static const vmstatRe( "(.+)\\s+([[:alnum:]]+)" ); - - bool const doMemInfo = absl::GetFlag( FLAGS_meminfo ); - bool const doVmStat = absl::GetFlag( FLAGS_vmstat ); - - if ( ! ( doMemInfo || doVmStat ) ){ - std::cerr << "Nothing to do, please select --meminfo or --vmstat or both" << std::endl; - return 0; - } - - while ( true ){ - - std::ifstream ifile; - - if( doMemInfo ){ - - ifile.open( "/proc/meminfo" ); - - for ( std::string line; std::getline( ifile, line ); ){ - - std::string label; - std::int64_t count; - - if ( re2::RE2::FullMatch( line, byteRe, &label, &count ) ){ - meminfoByteValues[label] = count * 1024 ; - } else if ( re2::RE2::FullMatch( line, countRe, &label, &count ) ){ - meminfoCountValues[label] = count; - } } - ifile.close(); - } - - if ( doVmStat ){ - - ifile.open( "/proc/vmstat"); + statsOps.project_id = project_id; + statsOps.monitored_resource.set_type(kGCEInstanceMonitoredResource); - for ( std::string line; std::getline( ifile, line ); ){ + // FIXME : handle a stackdriver project id different from gce project id ? + (*statsOps.monitored_resource.mutable_labels())[kProjectIDLabel] = + project_id; + (*statsOps.monitored_resource.mutable_labels())[kGCEInstanceIDLabel] = + instance_id; // instance_name; ? + (*statsOps.monitored_resource.mutable_labels())[kZoneLabel] = zone; - std::string label; - std::int64_t count; + std::cout << "using stackdriver project_id " << statsOps.project_id + << "\n"; + std::cout << "using monitored resource " + << ConvertMessageToJson(&(statsOps.monitored_resource)) + << std::endl; - if ( re2::RE2::FullMatch( line, vmstatRe, &label, &count ) ){ - vmstatCountValues[label] = count; - } - } - - ifile.close(); + opencensus::exporters::stats::StackdriverExporter::Register( + std::move(statsOps)); } + std::cout << "parsing /proc/meminfo and /proc/vmstat every " + << absl::GetFlag(FLAGS_period_seconds) << " seconds " + << std::endl; + + // man proc + std::map meminfoDescriptions = { + {"MemTotal", + "Total usable RAM (i.e., physical RAM minus a few reserved bits and " + "the kernel binary code)."}, + {"MemFree", "The sum of LowFree+HighFree."}, + {"MemAvailable", + "An estimate of how much memory is available for starting new " + "applications, without swapping."}, + {"Buffers", + "Relatively temporary storage for raw disk blocks that shouldn't get " + "tremendously large (20MB or so)."}, + {"Cached", + "In-memory cache for files read from the disk (the page cache). " + "Doesn't include SwapCached."}, + {"SwapCached", + "Memory that once was swapped out, is swapped back in but still also " + "is in the swap file. " + "(If memory pressure is high, these pages don't need to be swapped " + "out again because they are already in the swap file. This saves " + "I/O.)"}, + {"Active", + "Memory that has been used more recently and usually not reclaimed " + "unless absolutely necessary."}, + {"Inactive", + "Memory which has been less recently used. It is more eligible to be " + "reclaimed for other purposes."}, + {"Active(anon)", "[To be documented.]"}, + {"Inactive(anon)", "[To be documented.]"}, + {"Active(file)", "[To be documented.]"}, + {"Inactive(file)", "[To be documented.]"}, + {"Unevictable", "[To be documented.]"}, + {"Mlocked", "[To be documented.]"}, + {"HighTotal", + "Total amount of highmem. Highmem is all memory above ~860MB of " + "physical memory. Highmem areas are for use by user-space programs, " + "or for the page cache. " + "The kernel must use tricks to access this memory, making it slower " + "to access than lowmem."}, + {"HighFree", "Amount of free highmem."}, + {"LowTotal", + "Total amount of lowmem. Lowmem is memory which can be used for " + "everything that highmem can be used for, but it is also available " + "for the kernel's use for its own data structures. " + "Among many other things, it is where everything from Slab is " + "allocated. Bad things happen when you're out of lowmem."}, + {"LowFree", "Amount of free lowmem."}, + {"MmapCopy", "[To be documented.]"}, + {"SwapTotal", "Total amount of swap space available."}, + {"SwapFree", "Amount of swap space that is currently unused."}, + {"Dirty", "Memory which is waiting to get written back to the disk."}, + {"Writeback", + "Memory which is actively being written back to the disk."}, + {"AnonPages", + "Non-file backed pages mapped into user-space page tables."}, + {"Mapped", + "Files which have been mapped into memory (with mmap(2)), such as " + "libraries."}, + {"Shmem", "Amount of memory consumed in tmpfs(5) filesystems."}, + {"Slab", "In-kernel data structures cache. (See slabinfo(5).)"}, + {"SReclaimable", + "Part of Slab, that might be reclaimed, such as caches."}, + {"SUnreclaim", + "Part of Slab, that cannot be reclaimed on memory pressure."}, + {"KernelStack", "Amount of memory allocated to kernel stacks."}, + {"PageTables", + "Amount of memory dedicated to the lowest level of page tables."}, + {"Quicklists", "[To be documented.]"}, + {"NFS_Unstable", + "NFS pages sent to the server, but not yet committed to stable " + "storage."}, + {"Bounce", "Memory used for block device \"bounce buffers\"."}, + {"WritebackTmp", + "Memory used by FUSE for temporary writeback buffers."}, + {"CommitLimit", + "This is the total amount of memory currently available to be " + "allocated on the system, expressed in kilobytes. " + "This limit is adhered to only if strict overcommit accounting is " + "enabled (mode 2 in /proc/sys/vm/overcommit_memory). " + "The limit is calculated according to the formula described under " + "/proc/sys/vm/overcommit_memory. " + "For further details, see the kernel source file " + "Documentation/vm/overcommit-accounting."}, + {"Committed_AS", + "The amount of memory presently allocated on the system. The " + "committed memory is a sum of all of the memory which has been " + "allocated by processes," + " even if it has not been \"used\" by them as of yet. A process " + "which allocates 1GB of memory (using malloc(3) or similar), but " + "touches only 300MB" + " of that memory will show up as using only 300MB of memory even if " + "it has the address space allocated for the entire 1GB." + "\n" + "This 1GB is memory which has been \"committed\" to by the VM and " + "can be used at any time by the allocating application. " + "With strict overcommit enabled on the system (mode 2 in " + "/proc/sys/vm/overcommit_memory), allocations which would exceed the " + "CommitLimit will not be permitted. " + "This is useful if one needs to guarantee that processes will not " + "fail due to lack of memory once that memory has been successfully " + "allocated."}, + {"VmallocTotal", "Total size of vmalloc memory area."}, + {"VmallocUsed", "Amount of vmalloc area which is used."}, + {"VmallocChunk", + "Largest contiguous block of vmalloc area which is free."}, + {"HardwareCorrupted", "[To be documented.]"}, + {"AnonHugePages", + "Non-file backed huge pages mapped into user-space page tables."}, + {"ShmemHugePages", + "Memory used by shared memory (shmem) and tmpfs(5) allocated with " + "huge pages"}, + {"ShmemPmdMapped", + "Shared memory mapped into user space with huge pages."}, + {"CmaTotal", "Total CMA (Contiguous Memory Allocator) pages."}, + {"CmaFree", "Free CMA (Contiguous Memory Allocator) pages."}, + {"HugePages_Total", "The size of the pool of huge pages."}, + {"HugePages_Free", + "The number of huge pages in the pool that are not yet allocated."}, + {"HugePages_Rsvd", + "This is the number of huge pages for which a commitment to allocate " + "from the pool has been made, but no allocation has yet been made. " + "These reserved huge pages guarantee that an application will be able " + "to allocate a huge page from the pool of huge " + "pages at fault time."}, + {"HugePages_Surp", + "This is the number of huge pages in the pool above the value in " + "/proc/sys/vm/nr_hugepages. " + "The maximum number of surplus huge pages is controlled by " + "/proc/sys/vm/nr_overcommit_hugepages."}, + {"Hugepagesize", "The size of huge pages."}, + {"DirectMap4k", + "Number of bytes of RAM linearly mapped by kernel in 4kB pages."}, + {"DirectMap4M", + "Number of bytes of RAM linearly mapped by kernel in 4MB pages."}, + {"DirectMap2M", + "Number of bytes of RAM linearly mapped by kernel in 2MB pages."}, + {"DirectMap1G", + "Number of bytes of RAM linearly mapped by kernel in 1GB pages."}}; + + // is there _any_ documentation for /proc/vmstat _anywhere ? + + bool firstTime = true; + + std::map meminfoByteValues, meminfoCountValues, + vmstatCountValues; + std::map > + meminfoByteMeasures, meminfoCountMeasures, vmstatCountMeasures; + + // use libre2 because CentOS 7 devtoolset packages a gcc 7+ which reuses the + // default GCC 4.8.x libstdc++ which has buggy / incomplete support for + // regex as a result, unix-style regular expression are buggy... + + re2::RE2 static const countRe("(.+):\\s+([[:alnum:]]+)"); + re2::RE2 static const byteRe("(.+):\\s+([[:alnum:]]+) kB"); + re2::RE2 static const vmstatRe("(.+)\\s+([[:alnum:]]+)"); + + bool const doMemInfo = absl::GetFlag(FLAGS_meminfo); + bool const doVmStat = absl::GetFlag(FLAGS_vmstat); + + if (!(doMemInfo || doVmStat)) { + std::cerr << "Nothing to do, please select --meminfo or --vmstat or both" + << std::endl; + return 0; + } - // FIXME : hardly elegant. - if ( firstTime ){ - - // empty if doMemInfo == false - for ( auto const & p : meminfoByteValues ){ + while (true) { + std::ifstream ifile; - // FIXME : metric vs measure name ? + if (doMemInfo) { + ifile.open("/proc/meminfo"); - // register a measure - auto mn = absl::StrCat( "proc/meminfo/", p.first ); - opencensus::stats::MeasureInt64 m( opencensus::stats::MeasureInt64::Register( mn, p.first, kBytesUnits ) ); + for (std::string line; std::getline(ifile, line);) { + std::string label; + std::int64_t count; - auto vn = absl::StrCat( "proc/meminfo/", p.first ); // do I want a suffix for aggregation / units ? - auto desc = ( meminfoDescriptions.count( p.first ) > 0 ) ? meminfoDescriptions.at( p.first ) : absl::StrCat( p.first, " in bytes as per /proc/meminfo" ); - auto vd = opencensus::stats::ViewDescriptor() - .set_name( vn ) - .set_measure( mn ) - .set_aggregation( opencensus::stats::Aggregation::LastValue() ) // this is correct. instantaneous measure. - .set_description( desc ); + if (re2::RE2::FullMatch(line, byteRe, &label, &count)) { + meminfoByteValues[label] = count * 1024; + } else if (re2::RE2::FullMatch(line, countRe, &label, &count)) { + meminfoCountValues[label] = count; + } + } - vd.RegisterForExport(); - meminfoByteMeasures.insert( { p.first, { m, meminfoByteValues.at(p.first) } } ); + ifile.close(); } - for ( auto const & p : meminfoCountValues ){ - auto mn = absl::StrCat( "proc/meminfo/", p.first ); - opencensus::stats::MeasureInt64 m( opencensus::stats::MeasureInt64::Register( mn, p.first, kNoUnits ) ); + if (doVmStat) { + ifile.open("/proc/vmstat"); - auto vn = absl::StrCat( "proc/meminfo/", p.first ); - auto desc = ( meminfoDescriptions.count( p.first ) > 0 ) ? meminfoDescriptions.at( p.first ) : absl::StrCat( p.first, " count as per /proc/meminfo" ); - auto vd = opencensus::stats::ViewDescriptor() - .set_name( vn ) - .set_measure( mn ) - .set_aggregation( opencensus::stats::Aggregation::LastValue()) - .set_description( desc ); + for (std::string line; std::getline(ifile, line);) { + std::string label; + std::int64_t count; - vd.RegisterForExport(); + if (re2::RE2::FullMatch(line, vmstatRe, &label, &count)) { + vmstatCountValues[label] = count; + } + } - meminfoCountMeasures.insert( { p.first, { m, meminfoCountValues.at(p.first) } } ); + ifile.close(); } - // FIXME : add description labels... - // empty -f doVmStat == false ... - for ( auto const & p : vmstatCountValues ){ - auto mn = absl::StrCat( "proc/vmstat/", p.first ); - opencensus::stats::MeasureInt64 m( opencensus::stats::MeasureInt64::Register( mn, p.first, kNoUnits ) ); + // FIXME : hardly elegant. + if (firstTime) { + // empty if doMemInfo == false + for (auto const& p : meminfoByteValues) { + // FIXME : metric vs measure name ? + + // register a measure + auto mn = absl::StrCat("proc/meminfo/", p.first); + opencensus::stats::MeasureInt64 m( + opencensus::stats::MeasureInt64::Register(mn, p.first, + kBytesUnits)); + + auto vn = absl::StrCat( + "proc/meminfo/", + p.first); // do I want a suffix for aggregation / units ? + auto desc = + (meminfoDescriptions.count(p.first) > 0) + ? meminfoDescriptions.at(p.first) + : absl::StrCat(p.first, " in bytes as per /proc/meminfo"); + auto vd = + opencensus::stats::ViewDescriptor() + .set_name(vn) + .set_measure(mn) + .set_aggregation(opencensus::stats::Aggregation:: + LastValue()) // this is correct. + // instantaneous measure. + .set_description(desc); + + vd.RegisterForExport(); + meminfoByteMeasures.insert( + {p.first, {m, meminfoByteValues.at(p.first)}}); + } - auto vn = absl::StrCat( "proc/vmstat/", p.first ); - auto desc = absl::StrCat( p.first, " count as per /proc/vmstat" ); - auto vd = opencensus::stats::ViewDescriptor() - .set_name( vn ) - .set_measure( mn ) - .set_aggregation( opencensus::stats::Aggregation::LastValue() ) // FIXME: mostly _wrong_ some are cumulative ..., some may be deltas ? - .set_description( desc ); + for (auto const& p : meminfoCountValues) { + auto mn = absl::StrCat("proc/meminfo/", p.first); + opencensus::stats::MeasureInt64 m( + opencensus::stats::MeasureInt64::Register(mn, p.first, kNoUnits)); + + auto vn = absl::StrCat("proc/meminfo/", p.first); + auto desc = + (meminfoDescriptions.count(p.first) > 0) + ? meminfoDescriptions.at(p.first) + : absl::StrCat(p.first, " count as per /proc/meminfo"); + auto vd = + opencensus::stats::ViewDescriptor() + .set_name(vn) + .set_measure(mn) + .set_aggregation(opencensus::stats::Aggregation::LastValue()) + .set_description(desc); + + vd.RegisterForExport(); + + meminfoCountMeasures.insert( + {p.first, {m, meminfoCountValues.at(p.first)}}); + } - vd.RegisterForExport(); + // FIXME : add description labels... + // empty -f doVmStat == false ... + for (auto const& p : vmstatCountValues) { + auto mn = absl::StrCat("proc/vmstat/", p.first); + opencensus::stats::MeasureInt64 m( + opencensus::stats::MeasureInt64::Register(mn, p.first, kNoUnits)); + + auto vn = absl::StrCat("proc/vmstat/", p.first); + auto desc = absl::StrCat(p.first, " count as per /proc/vmstat"); + auto vd = + opencensus::stats::ViewDescriptor() + .set_name(vn) + .set_measure(mn) + .set_aggregation( + opencensus::stats::Aggregation:: + LastValue()) // FIXME: mostly _wrong_ some are + // cumulative ..., some may be deltas ? + .set_description(desc); + + vd.RegisterForExport(); + + vmstatCountMeasures.insert( + {p.first, {m, vmstatCountValues.at(p.first)}}); + } - vmstatCountMeasures.insert( { p.first, { m, vmstatCountValues.at(p.first) } } ); + firstTime = false; } - firstTime = false; - } - - for ( auto & p : meminfoByteMeasures ){ - // FIXME, tag by host name ? - opencensus::stats::Record( {{ p.second.first, meminfoByteValues.at(p.first) }} ); - } + for (auto& p : meminfoByteMeasures) { + // FIXME, tag by host name ? + opencensus::stats::Record( + {{p.second.first, meminfoByteValues.at(p.first)}}); + } - for ( auto & p : meminfoCountMeasures ){ - // FIXME, tag by host name ? - opencensus::stats::Record( {{ p.second.first, meminfoCountValues.at(p.first) }} ); - } + for (auto& p : meminfoCountMeasures) { + // FIXME, tag by host name ? + opencensus::stats::Record( + {{p.second.first, meminfoCountValues.at(p.first)}}); + } - for ( auto & p : vmstatCountMeasures ){ - // FIXME, tag by host name ? - opencensus::stats::Record( {{ p.second.first, vmstatCountValues.at(p.first) }} ); - } + for (auto& p : vmstatCountMeasures) { + // FIXME, tag by host name ? + opencensus::stats::Record( + {{p.second.first, vmstatCountValues.at(p.first)}}); + } - absl::SleepFor( absl::Seconds( absl::GetFlag( FLAGS_period_seconds ) ) ); + absl::SleepFor(absl::Seconds(absl::GetFlag(FLAGS_period_seconds))); - } // infinite loop + } // infinite loop - return 0; -} + return 0; + } From abb9edcd29894c23ae6f519de8e3f7dba379fe57 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 11:53:14 -0700 Subject: [PATCH 03/12] fix missing } to compile. product name prefix as cli option not preprocessor flag --- examples/memory/memory_stats.cc | 648 ++++++++++++++++---------------- 1 file changed, 321 insertions(+), 327 deletions(-) diff --git a/examples/memory/memory_stats.cc b/examples/memory/memory_stats.cc index 92c5ea72..6ba2500b 100644 --- a/examples/memory/memory_stats.cc +++ b/examples/memory/memory_stats.cc @@ -26,6 +26,9 @@ #include "re2/re2.h" +ABSL_FLAG(std::string, metric_product_prefix, "OpenCensus", + "product specifc prefix in google monitoring metric name ( " + "custom.googleapis.com/[PREFIX]/[METRIC])"); ABSL_FLAG(std::string, project_id, "", "stackdriver project id"); ABSL_FLAG(std::string, instance_id, "", "local GCE instance id"); ABSL_FLAG(std::string, zone, "", "local instance zone"); @@ -34,10 +37,6 @@ ABSL_FLAG(int, period_seconds, 60, "perform a measurement every N seconds"); ABSL_FLAG(bool, meminfo, true, "parse /proc/meminfo ( creates ~20 metrics )"); ABSL_FLAG(bool, vmstat, false, "parse /proc/vmstat ( creates 130+ metrics! )"); -#ifndef PRODUCT_NAME_LABEL -#define PRODUCT_NAME_LABEL OpenCensus -#endif - namespace { // Monitored resource // FIXME : how do I detect if I am on a gce instance, a container inside a @@ -58,8 +57,6 @@ constexpr char kPodNameLabel[] = "pod_name"; constexpr char kBytesUnits[] = "bytes"; constexpr char kNoUnits[] = ""; -constexpr char kProductName[] = #PRODUCT_NAME_LABEL; - std::string ConvertMessageToJson(google::protobuf::Message const* poMsg) { std::string strMsg; @@ -123,7 +120,8 @@ int main(int argc, char** argv) { // FIXME : configurable prefix. // this is decided at compile time for now. statsOps.metric_name_prefix = - absl::StrCat("custom.googleapis.com/", kProductName); + absl::StrCat("custom.googleapis.com/", + absl::GetFlag(FLAGS_metric_product_prefix), "/"); std::string project_id = absl::GetFlag(FLAGS_project_id); std::string instance_id = absl::GetFlag(FLAGS_instance_id); @@ -181,354 +179,350 @@ int main(int argc, char** argv) { return 1; } } + } - if (zone.empty()) { - auto r = cpr::Get( - cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/zone"}, - // cpr::Parameters{{"recursive", "true"}, {"alt", "text"}}, - cpr::Header{{"Metadata-Flavor", "Google"}}); + if (zone.empty()) { + auto r = cpr::Get( + cpr::Url{"http://169.254.169.254/computeMetadata/v1/instance/zone"}, + // cpr::Parameters{{"recursive", "true"}, {"alt", "text"}}, + cpr::Header{{"Metadata-Flavor", "Google"}}); - // handle retry, errors, - if (r.status_code == 200) { - // projects/REDACTED/zones/ZONE - zone = std::vector(absl::StrSplit(r.text, '/')).back(); - } else { - std::cerr << "could not obtain vm zone from metadata server" - << std::endl; - return 1; - } + // handle retry, errors, + if (r.status_code == 200) { + // projects/REDACTED/zones/ZONE + zone = std::vector(absl::StrSplit(r.text, '/')).back(); + } else { + std::cerr << "could not obtain vm zone from metadata server" + << std::endl; + return 1; } + } - statsOps.project_id = project_id; - statsOps.monitored_resource.set_type(kGCEInstanceMonitoredResource); + statsOps.project_id = project_id; + statsOps.monitored_resource.set_type(kGCEInstanceMonitoredResource); - // FIXME : handle a stackdriver project id different from gce project id ? - (*statsOps.monitored_resource.mutable_labels())[kProjectIDLabel] = - project_id; - (*statsOps.monitored_resource.mutable_labels())[kGCEInstanceIDLabel] = - instance_id; // instance_name; ? - (*statsOps.monitored_resource.mutable_labels())[kZoneLabel] = zone; + // FIXME : handle a stackdriver project id different from gce project id ? + (*statsOps.monitored_resource.mutable_labels())[kProjectIDLabel] = + project_id; + (*statsOps.monitored_resource.mutable_labels())[kGCEInstanceIDLabel] = + instance_id; // instance_name; ? + (*statsOps.monitored_resource.mutable_labels())[kZoneLabel] = zone; - std::cout << "using stackdriver project_id " << statsOps.project_id - << "\n"; - std::cout << "using monitored resource " - << ConvertMessageToJson(&(statsOps.monitored_resource)) - << std::endl; + std::cout << "using stackdriver project_id " << statsOps.project_id << "\n"; + std::cout << "using monitored resource " + << ConvertMessageToJson(&(statsOps.monitored_resource)) + << std::endl; - opencensus::exporters::stats::StackdriverExporter::Register( - std::move(statsOps)); - } + opencensus::exporters::stats::StackdriverExporter::Register( + std::move(statsOps)); + } - std::cout << "parsing /proc/meminfo and /proc/vmstat every " - << absl::GetFlag(FLAGS_period_seconds) << " seconds " + std::cout << "parsing /proc/meminfo and /proc/vmstat every " + << absl::GetFlag(FLAGS_period_seconds) << " seconds " << std::endl; + + // man proc + std::map meminfoDescriptions = { + {"MemTotal", + "Total usable RAM (i.e., physical RAM minus a few reserved bits and " + "the kernel binary code)."}, + {"MemFree", "The sum of LowFree+HighFree."}, + {"MemAvailable", + "An estimate of how much memory is available for starting new " + "applications, without swapping."}, + {"Buffers", + "Relatively temporary storage for raw disk blocks that shouldn't get " + "tremendously large (20MB or so)."}, + {"Cached", + "In-memory cache for files read from the disk (the page cache). " + "Doesn't include SwapCached."}, + {"SwapCached", + "Memory that once was swapped out, is swapped back in but still also " + "is in the swap file. " + "(If memory pressure is high, these pages don't need to be swapped " + "out again because they are already in the swap file. This saves " + "I/O.)"}, + {"Active", + "Memory that has been used more recently and usually not reclaimed " + "unless absolutely necessary."}, + {"Inactive", + "Memory which has been less recently used. It is more eligible to be " + "reclaimed for other purposes."}, + {"Active(anon)", "[To be documented.]"}, + {"Inactive(anon)", "[To be documented.]"}, + {"Active(file)", "[To be documented.]"}, + {"Inactive(file)", "[To be documented.]"}, + {"Unevictable", "[To be documented.]"}, + {"Mlocked", "[To be documented.]"}, + {"HighTotal", + "Total amount of highmem. Highmem is all memory above ~860MB of " + "physical memory. Highmem areas are for use by user-space programs, " + "or for the page cache. " + "The kernel must use tricks to access this memory, making it slower " + "to access than lowmem."}, + {"HighFree", "Amount of free highmem."}, + {"LowTotal", + "Total amount of lowmem. Lowmem is memory which can be used for " + "everything that highmem can be used for, but it is also available " + "for the kernel's use for its own data structures. " + "Among many other things, it is where everything from Slab is " + "allocated. Bad things happen when you're out of lowmem."}, + {"LowFree", "Amount of free lowmem."}, + {"MmapCopy", "[To be documented.]"}, + {"SwapTotal", "Total amount of swap space available."}, + {"SwapFree", "Amount of swap space that is currently unused."}, + {"Dirty", "Memory which is waiting to get written back to the disk."}, + {"Writeback", "Memory which is actively being written back to the disk."}, + {"AnonPages", + "Non-file backed pages mapped into user-space page tables."}, + {"Mapped", + "Files which have been mapped into memory (with mmap(2)), such as " + "libraries."}, + {"Shmem", "Amount of memory consumed in tmpfs(5) filesystems."}, + {"Slab", "In-kernel data structures cache. (See slabinfo(5).)"}, + {"SReclaimable", + "Part of Slab, that might be reclaimed, such as caches."}, + {"SUnreclaim", + "Part of Slab, that cannot be reclaimed on memory pressure."}, + {"KernelStack", "Amount of memory allocated to kernel stacks."}, + {"PageTables", + "Amount of memory dedicated to the lowest level of page tables."}, + {"Quicklists", "[To be documented.]"}, + {"NFS_Unstable", + "NFS pages sent to the server, but not yet committed to stable " + "storage."}, + {"Bounce", "Memory used for block device \"bounce buffers\"."}, + {"WritebackTmp", "Memory used by FUSE for temporary writeback buffers."}, + {"CommitLimit", + "This is the total amount of memory currently available to be " + "allocated on the system, expressed in kilobytes. " + "This limit is adhered to only if strict overcommit accounting is " + "enabled (mode 2 in /proc/sys/vm/overcommit_memory). " + "The limit is calculated according to the formula described under " + "/proc/sys/vm/overcommit_memory. " + "For further details, see the kernel source file " + "Documentation/vm/overcommit-accounting."}, + {"Committed_AS", + "The amount of memory presently allocated on the system. The " + "committed memory is a sum of all of the memory which has been " + "allocated by processes," + " even if it has not been \"used\" by them as of yet. A process " + "which allocates 1GB of memory (using malloc(3) or similar), but " + "touches only 300MB" + " of that memory will show up as using only 300MB of memory even if " + "it has the address space allocated for the entire 1GB." + "\n" + "This 1GB is memory which has been \"committed\" to by the VM and " + "can be used at any time by the allocating application. " + "With strict overcommit enabled on the system (mode 2 in " + "/proc/sys/vm/overcommit_memory), allocations which would exceed the " + "CommitLimit will not be permitted. " + "This is useful if one needs to guarantee that processes will not " + "fail due to lack of memory once that memory has been successfully " + "allocated."}, + {"VmallocTotal", "Total size of vmalloc memory area."}, + {"VmallocUsed", "Amount of vmalloc area which is used."}, + {"VmallocChunk", + "Largest contiguous block of vmalloc area which is free."}, + {"HardwareCorrupted", "[To be documented.]"}, + {"AnonHugePages", + "Non-file backed huge pages mapped into user-space page tables."}, + {"ShmemHugePages", + "Memory used by shared memory (shmem) and tmpfs(5) allocated with " + "huge pages"}, + {"ShmemPmdMapped", + "Shared memory mapped into user space with huge pages."}, + {"CmaTotal", "Total CMA (Contiguous Memory Allocator) pages."}, + {"CmaFree", "Free CMA (Contiguous Memory Allocator) pages."}, + {"HugePages_Total", "The size of the pool of huge pages."}, + {"HugePages_Free", + "The number of huge pages in the pool that are not yet allocated."}, + {"HugePages_Rsvd", + "This is the number of huge pages for which a commitment to allocate " + "from the pool has been made, but no allocation has yet been made. " + "These reserved huge pages guarantee that an application will be able " + "to allocate a huge page from the pool of huge " + "pages at fault time."}, + {"HugePages_Surp", + "This is the number of huge pages in the pool above the value in " + "/proc/sys/vm/nr_hugepages. " + "The maximum number of surplus huge pages is controlled by " + "/proc/sys/vm/nr_overcommit_hugepages."}, + {"Hugepagesize", "The size of huge pages."}, + {"DirectMap4k", + "Number of bytes of RAM linearly mapped by kernel in 4kB pages."}, + {"DirectMap4M", + "Number of bytes of RAM linearly mapped by kernel in 4MB pages."}, + {"DirectMap2M", + "Number of bytes of RAM linearly mapped by kernel in 2MB pages."}, + {"DirectMap1G", + "Number of bytes of RAM linearly mapped by kernel in 1GB pages."}}; + + // is there _any_ documentation for /proc/vmstat _anywhere ? + + bool firstTime = true; + + std::map meminfoByteValues, meminfoCountValues, + vmstatCountValues; + std::map > + meminfoByteMeasures, meminfoCountMeasures, vmstatCountMeasures; + + // use libre2 because CentOS 7 devtoolset packages a gcc 7+ which reuses the + // default GCC 4.8.x libstdc++ which has buggy / incomplete support for + // regex as a result, unix-style regular expression are buggy... + + re2::RE2 static const countRe("(.+):\\s+([[:alnum:]]+)"); + re2::RE2 static const byteRe("(.+):\\s+([[:alnum:]]+) kB"); + re2::RE2 static const vmstatRe("(.+)\\s+([[:alnum:]]+)"); + + bool const doMemInfo = absl::GetFlag(FLAGS_meminfo); + bool const doVmStat = absl::GetFlag(FLAGS_vmstat); + + if (!(doMemInfo || doVmStat)) { + std::cerr << "Nothing to do, please select --meminfo or --vmstat or both" << std::endl; + return 0; + } - // man proc - std::map meminfoDescriptions = { - {"MemTotal", - "Total usable RAM (i.e., physical RAM minus a few reserved bits and " - "the kernel binary code)."}, - {"MemFree", "The sum of LowFree+HighFree."}, - {"MemAvailable", - "An estimate of how much memory is available for starting new " - "applications, without swapping."}, - {"Buffers", - "Relatively temporary storage for raw disk blocks that shouldn't get " - "tremendously large (20MB or so)."}, - {"Cached", - "In-memory cache for files read from the disk (the page cache). " - "Doesn't include SwapCached."}, - {"SwapCached", - "Memory that once was swapped out, is swapped back in but still also " - "is in the swap file. " - "(If memory pressure is high, these pages don't need to be swapped " - "out again because they are already in the swap file. This saves " - "I/O.)"}, - {"Active", - "Memory that has been used more recently and usually not reclaimed " - "unless absolutely necessary."}, - {"Inactive", - "Memory which has been less recently used. It is more eligible to be " - "reclaimed for other purposes."}, - {"Active(anon)", "[To be documented.]"}, - {"Inactive(anon)", "[To be documented.]"}, - {"Active(file)", "[To be documented.]"}, - {"Inactive(file)", "[To be documented.]"}, - {"Unevictable", "[To be documented.]"}, - {"Mlocked", "[To be documented.]"}, - {"HighTotal", - "Total amount of highmem. Highmem is all memory above ~860MB of " - "physical memory. Highmem areas are for use by user-space programs, " - "or for the page cache. " - "The kernel must use tricks to access this memory, making it slower " - "to access than lowmem."}, - {"HighFree", "Amount of free highmem."}, - {"LowTotal", - "Total amount of lowmem. Lowmem is memory which can be used for " - "everything that highmem can be used for, but it is also available " - "for the kernel's use for its own data structures. " - "Among many other things, it is where everything from Slab is " - "allocated. Bad things happen when you're out of lowmem."}, - {"LowFree", "Amount of free lowmem."}, - {"MmapCopy", "[To be documented.]"}, - {"SwapTotal", "Total amount of swap space available."}, - {"SwapFree", "Amount of swap space that is currently unused."}, - {"Dirty", "Memory which is waiting to get written back to the disk."}, - {"Writeback", - "Memory which is actively being written back to the disk."}, - {"AnonPages", - "Non-file backed pages mapped into user-space page tables."}, - {"Mapped", - "Files which have been mapped into memory (with mmap(2)), such as " - "libraries."}, - {"Shmem", "Amount of memory consumed in tmpfs(5) filesystems."}, - {"Slab", "In-kernel data structures cache. (See slabinfo(5).)"}, - {"SReclaimable", - "Part of Slab, that might be reclaimed, such as caches."}, - {"SUnreclaim", - "Part of Slab, that cannot be reclaimed on memory pressure."}, - {"KernelStack", "Amount of memory allocated to kernel stacks."}, - {"PageTables", - "Amount of memory dedicated to the lowest level of page tables."}, - {"Quicklists", "[To be documented.]"}, - {"NFS_Unstable", - "NFS pages sent to the server, but not yet committed to stable " - "storage."}, - {"Bounce", "Memory used for block device \"bounce buffers\"."}, - {"WritebackTmp", - "Memory used by FUSE for temporary writeback buffers."}, - {"CommitLimit", - "This is the total amount of memory currently available to be " - "allocated on the system, expressed in kilobytes. " - "This limit is adhered to only if strict overcommit accounting is " - "enabled (mode 2 in /proc/sys/vm/overcommit_memory). " - "The limit is calculated according to the formula described under " - "/proc/sys/vm/overcommit_memory. " - "For further details, see the kernel source file " - "Documentation/vm/overcommit-accounting."}, - {"Committed_AS", - "The amount of memory presently allocated on the system. The " - "committed memory is a sum of all of the memory which has been " - "allocated by processes," - " even if it has not been \"used\" by them as of yet. A process " - "which allocates 1GB of memory (using malloc(3) or similar), but " - "touches only 300MB" - " of that memory will show up as using only 300MB of memory even if " - "it has the address space allocated for the entire 1GB." - "\n" - "This 1GB is memory which has been \"committed\" to by the VM and " - "can be used at any time by the allocating application. " - "With strict overcommit enabled on the system (mode 2 in " - "/proc/sys/vm/overcommit_memory), allocations which would exceed the " - "CommitLimit will not be permitted. " - "This is useful if one needs to guarantee that processes will not " - "fail due to lack of memory once that memory has been successfully " - "allocated."}, - {"VmallocTotal", "Total size of vmalloc memory area."}, - {"VmallocUsed", "Amount of vmalloc area which is used."}, - {"VmallocChunk", - "Largest contiguous block of vmalloc area which is free."}, - {"HardwareCorrupted", "[To be documented.]"}, - {"AnonHugePages", - "Non-file backed huge pages mapped into user-space page tables."}, - {"ShmemHugePages", - "Memory used by shared memory (shmem) and tmpfs(5) allocated with " - "huge pages"}, - {"ShmemPmdMapped", - "Shared memory mapped into user space with huge pages."}, - {"CmaTotal", "Total CMA (Contiguous Memory Allocator) pages."}, - {"CmaFree", "Free CMA (Contiguous Memory Allocator) pages."}, - {"HugePages_Total", "The size of the pool of huge pages."}, - {"HugePages_Free", - "The number of huge pages in the pool that are not yet allocated."}, - {"HugePages_Rsvd", - "This is the number of huge pages for which a commitment to allocate " - "from the pool has been made, but no allocation has yet been made. " - "These reserved huge pages guarantee that an application will be able " - "to allocate a huge page from the pool of huge " - "pages at fault time."}, - {"HugePages_Surp", - "This is the number of huge pages in the pool above the value in " - "/proc/sys/vm/nr_hugepages. " - "The maximum number of surplus huge pages is controlled by " - "/proc/sys/vm/nr_overcommit_hugepages."}, - {"Hugepagesize", "The size of huge pages."}, - {"DirectMap4k", - "Number of bytes of RAM linearly mapped by kernel in 4kB pages."}, - {"DirectMap4M", - "Number of bytes of RAM linearly mapped by kernel in 4MB pages."}, - {"DirectMap2M", - "Number of bytes of RAM linearly mapped by kernel in 2MB pages."}, - {"DirectMap1G", - "Number of bytes of RAM linearly mapped by kernel in 1GB pages."}}; - - // is there _any_ documentation for /proc/vmstat _anywhere ? - - bool firstTime = true; - - std::map meminfoByteValues, meminfoCountValues, - vmstatCountValues; - std::map > - meminfoByteMeasures, meminfoCountMeasures, vmstatCountMeasures; - - // use libre2 because CentOS 7 devtoolset packages a gcc 7+ which reuses the - // default GCC 4.8.x libstdc++ which has buggy / incomplete support for - // regex as a result, unix-style regular expression are buggy... - - re2::RE2 static const countRe("(.+):\\s+([[:alnum:]]+)"); - re2::RE2 static const byteRe("(.+):\\s+([[:alnum:]]+) kB"); - re2::RE2 static const vmstatRe("(.+)\\s+([[:alnum:]]+)"); - - bool const doMemInfo = absl::GetFlag(FLAGS_meminfo); - bool const doVmStat = absl::GetFlag(FLAGS_vmstat); - - if (!(doMemInfo || doVmStat)) { - std::cerr << "Nothing to do, please select --meminfo or --vmstat or both" - << std::endl; - return 0; - } - - while (true) { - std::ifstream ifile; + while (true) { + std::ifstream ifile; - if (doMemInfo) { - ifile.open("/proc/meminfo"); + if (doMemInfo) { + ifile.open("/proc/meminfo"); - for (std::string line; std::getline(ifile, line);) { - std::string label; - std::int64_t count; + for (std::string line; std::getline(ifile, line);) { + std::string label; + std::int64_t count; - if (re2::RE2::FullMatch(line, byteRe, &label, &count)) { - meminfoByteValues[label] = count * 1024; - } else if (re2::RE2::FullMatch(line, countRe, &label, &count)) { - meminfoCountValues[label] = count; - } + if (re2::RE2::FullMatch(line, byteRe, &label, &count)) { + meminfoByteValues[label] = count * 1024; + } else if (re2::RE2::FullMatch(line, countRe, &label, &count)) { + meminfoCountValues[label] = count; } - - ifile.close(); } - if (doVmStat) { - ifile.open("/proc/vmstat"); - - for (std::string line; std::getline(ifile, line);) { - std::string label; - std::int64_t count; + ifile.close(); + } - if (re2::RE2::FullMatch(line, vmstatRe, &label, &count)) { - vmstatCountValues[label] = count; - } - } + if (doVmStat) { + ifile.open("/proc/vmstat"); - ifile.close(); - } + for (std::string line; std::getline(ifile, line);) { + std::string label; + std::int64_t count; - // FIXME : hardly elegant. - if (firstTime) { - // empty if doMemInfo == false - for (auto const& p : meminfoByteValues) { - // FIXME : metric vs measure name ? - - // register a measure - auto mn = absl::StrCat("proc/meminfo/", p.first); - opencensus::stats::MeasureInt64 m( - opencensus::stats::MeasureInt64::Register(mn, p.first, - kBytesUnits)); - - auto vn = absl::StrCat( - "proc/meminfo/", - p.first); // do I want a suffix for aggregation / units ? - auto desc = - (meminfoDescriptions.count(p.first) > 0) - ? meminfoDescriptions.at(p.first) - : absl::StrCat(p.first, " in bytes as per /proc/meminfo"); - auto vd = - opencensus::stats::ViewDescriptor() - .set_name(vn) - .set_measure(mn) - .set_aggregation(opencensus::stats::Aggregation:: - LastValue()) // this is correct. - // instantaneous measure. - .set_description(desc); - - vd.RegisterForExport(); - meminfoByteMeasures.insert( - {p.first, {m, meminfoByteValues.at(p.first)}}); - } - - for (auto const& p : meminfoCountValues) { - auto mn = absl::StrCat("proc/meminfo/", p.first); - opencensus::stats::MeasureInt64 m( - opencensus::stats::MeasureInt64::Register(mn, p.first, kNoUnits)); - - auto vn = absl::StrCat("proc/meminfo/", p.first); - auto desc = - (meminfoDescriptions.count(p.first) > 0) - ? meminfoDescriptions.at(p.first) - : absl::StrCat(p.first, " count as per /proc/meminfo"); - auto vd = - opencensus::stats::ViewDescriptor() - .set_name(vn) - .set_measure(mn) - .set_aggregation(opencensus::stats::Aggregation::LastValue()) - .set_description(desc); - - vd.RegisterForExport(); - - meminfoCountMeasures.insert( - {p.first, {m, meminfoCountValues.at(p.first)}}); + if (re2::RE2::FullMatch(line, vmstatRe, &label, &count)) { + vmstatCountValues[label] = count; } + } - // FIXME : add description labels... - // empty -f doVmStat == false ... - for (auto const& p : vmstatCountValues) { - auto mn = absl::StrCat("proc/vmstat/", p.first); - opencensus::stats::MeasureInt64 m( - opencensus::stats::MeasureInt64::Register(mn, p.first, kNoUnits)); - - auto vn = absl::StrCat("proc/vmstat/", p.first); - auto desc = absl::StrCat(p.first, " count as per /proc/vmstat"); - auto vd = - opencensus::stats::ViewDescriptor() - .set_name(vn) - .set_measure(mn) - .set_aggregation( - opencensus::stats::Aggregation:: - LastValue()) // FIXME: mostly _wrong_ some are - // cumulative ..., some may be deltas ? - .set_description(desc); - - vd.RegisterForExport(); - - vmstatCountMeasures.insert( - {p.first, {m, vmstatCountValues.at(p.first)}}); - } + ifile.close(); + } - firstTime = false; + // FIXME : hardly elegant. + if (firstTime) { + // empty if doMemInfo == false + for (auto const& p : meminfoByteValues) { + // FIXME : metric vs measure name ? + + // register a measure + auto mn = absl::StrCat("proc/meminfo/", p.first); + opencensus::stats::MeasureInt64 m( + opencensus::stats::MeasureInt64::Register(mn, p.first, + kBytesUnits)); + + auto vn = absl::StrCat( + "proc/meminfo/", + p.first); // do I want a suffix for aggregation / units ? + auto desc = + (meminfoDescriptions.count(p.first) > 0) + ? meminfoDescriptions.at(p.first) + : absl::StrCat(p.first, " in bytes as per /proc/meminfo"); + auto vd = + opencensus::stats::ViewDescriptor() + .set_name(vn) + .set_measure(mn) + .set_aggregation(opencensus::stats::Aggregation:: + LastValue()) // this is correct. + // instantaneous measure. + .set_description(desc); + + vd.RegisterForExport(); + meminfoByteMeasures.insert( + {p.first, {m, meminfoByteValues.at(p.first)}}); } - for (auto& p : meminfoByteMeasures) { - // FIXME, tag by host name ? - opencensus::stats::Record( - {{p.second.first, meminfoByteValues.at(p.first)}}); + for (auto const& p : meminfoCountValues) { + auto mn = absl::StrCat("proc/meminfo/", p.first); + opencensus::stats::MeasureInt64 m( + opencensus::stats::MeasureInt64::Register(mn, p.first, kNoUnits)); + + auto vn = absl::StrCat("proc/meminfo/", p.first); + auto desc = (meminfoDescriptions.count(p.first) > 0) + ? meminfoDescriptions.at(p.first) + : absl::StrCat(p.first, " count as per /proc/meminfo"); + auto vd = + opencensus::stats::ViewDescriptor() + .set_name(vn) + .set_measure(mn) + .set_aggregation(opencensus::stats::Aggregation::LastValue()) + .set_description(desc); + + vd.RegisterForExport(); + + meminfoCountMeasures.insert( + {p.first, {m, meminfoCountValues.at(p.first)}}); } - for (auto& p : meminfoCountMeasures) { - // FIXME, tag by host name ? - opencensus::stats::Record( - {{p.second.first, meminfoCountValues.at(p.first)}}); + // FIXME : add description labels... + // empty -f doVmStat == false ... + for (auto const& p : vmstatCountValues) { + auto mn = absl::StrCat("proc/vmstat/", p.first); + opencensus::stats::MeasureInt64 m( + opencensus::stats::MeasureInt64::Register(mn, p.first, kNoUnits)); + + auto vn = absl::StrCat("proc/vmstat/", p.first); + auto desc = absl::StrCat(p.first, " count as per /proc/vmstat"); + auto vd = + opencensus::stats::ViewDescriptor() + .set_name(vn) + .set_measure(mn) + .set_aggregation( + opencensus::stats::Aggregation:: + LastValue()) // FIXME: mostly _wrong_ some are + // cumulative ..., some may be deltas ? + .set_description(desc); + + vd.RegisterForExport(); + + vmstatCountMeasures.insert( + {p.first, {m, vmstatCountValues.at(p.first)}}); } - for (auto& p : vmstatCountMeasures) { - // FIXME, tag by host name ? - opencensus::stats::Record( - {{p.second.first, vmstatCountValues.at(p.first)}}); - } + firstTime = false; + } - absl::SleepFor(absl::Seconds(absl::GetFlag(FLAGS_period_seconds))); + for (auto& p : meminfoByteMeasures) { + // FIXME, tag by host name ? + opencensus::stats::Record( + {{p.second.first, meminfoByteValues.at(p.first)}}); + } - } // infinite loop + for (auto& p : meminfoCountMeasures) { + // FIXME, tag by host name ? + opencensus::stats::Record( + {{p.second.first, meminfoCountValues.at(p.first)}}); + } - return 0; - } + for (auto& p : vmstatCountMeasures) { + // FIXME, tag by host name ? + opencensus::stats::Record( + {{p.second.first, vmstatCountValues.at(p.first)}}); + } + + absl::SleepFor(absl::Seconds(absl::GetFlag(FLAGS_period_seconds))); + + } // infinite loop + + return 0; +} From b1af7a664cb58bfefde14fc2a753119d89ca32a9 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 16:23:39 -0700 Subject: [PATCH 04/12] fix exports : no-more exporting of gmock, prometheus-cpp::core when installing --- cmake/OpenCensusHelpers.cmake | 29 ++++++++++--------- cmake/config.cmake.in | 9 +++--- .../exporters/stats/prometheus/CMakeLists.txt | 4 +-- .../stats/stackdriver/CMakeLists.txt | 23 +++++++-------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/cmake/OpenCensusHelpers.cmake b/cmake/OpenCensusHelpers.cmake index ddffd803..00f9031c 100644 --- a/cmake/OpenCensusHelpers.cmake +++ b/cmake/OpenCensusHelpers.cmake @@ -58,9 +58,9 @@ include(GNUInstallDirs) # # install( TARGETS PUBLIC_HEADERS DESTINATION foo ) -# does not work well when we declare +# does not work well when we declare # opencensus_lib( libfoo PUBLIC HDRS bar/baz.h SRCS bar/bar.cc ) -# it will generate foo/baz.h not foo/bar/baz.hh +# it will generate foo/baz.h not foo/bar/baz.hh # # # install_headers_with_subdirectories( PUBLIC_HEADERS item1 item2 .. INSTALL_FOLDER dir EXPORT export_name ) @@ -73,9 +73,9 @@ function( install_headers_with_subdirectories ) cmake_parse_arguments(ARG "${options}" "${singleValued}" "${multiValued}" ${ARGN} ) foreach( header ${ARG_PUBLIC_HEADER} ) - + get_filename_component( dir ${header} DIRECTORY ) - + install( FILES ${header} DESTINATION "${ARG_INSTALL_FOLDER}/${dir}" ) endforeach() @@ -85,7 +85,7 @@ endfunction( install_headers_with_subdirectories ) # Helper function like bazel's cc_library. Libraries are namespaced as # opencensus_* and public libraries are also aliased as opencensus-cpp::*. function(opencensus_lib NAME) - cmake_parse_arguments(ARG "PUBLIC" "" "HDRS;SRCS;DEPS" ${ARGN}) + cmake_parse_arguments(ARG "PUBLIC;PRIVATE" "" "HDRS;SRCS;DEPS" ${ARGN}) set(_NAME "opencensus_${NAME}") prepend_opencensus(ARG_DEPS "${ARG_DEPS}") @@ -102,12 +102,12 @@ function(opencensus_lib NAME) endif() if(ARG_PUBLIC) add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME}) - + if (ARG_HDRS) #this will install them #set_target_properties( ${_NAME} PROPERTIES PUBLIC_HEADER "${ARG_HDRS}" ) - install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} - INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} + INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" ) endif() @@ -117,15 +117,18 @@ function(opencensus_lib NAME) LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" # PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" - ) + ) + elseif(ARG_PRIVATE) + # what we really wanted to do, if linking private static libraries into public ones worked + # without having to export the private one... else() - + if (ARG_HDRS) # I think we have API problems whereby the internal includes are required by clients... # Comment out this line if it is not the case... #set_target_properties( ${_NAME} PROPERTIES PRIVATE_HEADER "${ARG_HDRS}" ) - install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} - INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} + INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" ) endif() @@ -133,7 +136,7 @@ function(opencensus_lib NAME) # I don't want these installed, I don't want the associated headers installed # I want them as depedencies to public static libraries that aggregate them.... install( TARGETS ${_NAME} - EXPORT opencensus-cpp-targets + EXPORT opencensus-cpp-targets RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in index 9bfff2fd..addbc08a 100644 --- a/cmake/config.cmake.in +++ b/cmake/config.cmake.in @@ -1,4 +1,4 @@ -# +# list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") find_package( gRPC ) @@ -7,10 +7,10 @@ find_package( googleapis ) include ( "${CMAKE_CURRENT_LIST_DIR}/opencensus-cpp-targets.cmake" ) # FIXME : can I obtain this list via pre-processing of targets file variable ? -foreach( _target +foreach( _target # FIXME : this makes no diference between internal libraries and # and public ones - # this is because of a bug makeing / exporting static libs + # this is because of a bug makeing / exporting static libs opencensus_common_hostname opencensus_common_random opencensus_common_grpc_status @@ -24,7 +24,6 @@ foreach( _target opencensus_exporters_stats_prometheus_utils opencensus_exporters_stats_stackdriver opencensus_exporters_stats_stackdriver_utils - opencensus_time_series_matcher opencensus_exporters_stats_stdout opencensus_exporters_trace_stackdriver opencensus_exporters_trace_stdout @@ -46,7 +45,7 @@ foreach( _target opencensus_trace_with_span ) - string( REPLACE "opencensus_" "" _target_suffix "${_target}" ) + string( REPLACE "opencensus_" "" _target_suffix "${_target}" ) set(scoped_name "opencensus-c++::${_target_suffix}") set(imported_name "${_target}") diff --git a/opencensus/exporters/stats/prometheus/CMakeLists.txt b/opencensus/exporters/stats/prometheus/CMakeLists.txt index dce9edfc..f98ad0ff 100644 --- a/opencensus/exporters/stats/prometheus/CMakeLists.txt +++ b/opencensus/exporters/stats/prometheus/CMakeLists.txt @@ -23,7 +23,7 @@ opencensus_lib( exporters_stats_prometheus_utils stats) -# internal library exposed only through the previous one _implementation +# internal library exposed only through the previous one _implementation opencensus_lib( exporters_stats_prometheus_utils HDRS @@ -34,7 +34,7 @@ opencensus_lib( stats absl::strings absl::time - prometheus-cpp::core) + $ ) opencensus_test( exporters_stats_prometheus_utils_test internal/prometheus_utils_test.cc diff --git a/opencensus/exporters/stats/stackdriver/CMakeLists.txt b/opencensus/exporters/stats/stackdriver/CMakeLists.txt index 0d445b53..90637605 100644 --- a/opencensus/exporters/stats/stackdriver/CMakeLists.txt +++ b/opencensus/exporters/stats/stackdriver/CMakeLists.txt @@ -1,7 +1,7 @@ opencensus_lib( exporters_stats_stackdriver PUBLIC - HDRS - stackdriver_exporter.h + HDRS + stackdriver_exporter.h SRCS internal/stackdriver_exporter.cc DEPS @@ -15,31 +15,29 @@ opencensus_lib( exporters_stats_stackdriver # FIXME : these are supposed to be INTERNAL LIBRARIES EXPOSED VIA PREVIOUS? opencensus_lib( exporters_stats_stackdriver_utils #PUBLIC - HDRS - internal/stackdriver_utils.h + HDRS + internal/stackdriver_utils.h SRCS internal/stackdriver_utils.cc DEPS stats - common_timestamp + common_timestamp ) opencensus_lib( time_series_matcher - #PUBLIC - HDRS - internal/time_series_matcher.h + PRIVATE + HDRS + internal/time_series_matcher.h SRCS internal/time_series_matcher.cc DEPS stats ) -# why can't I just declare googleapis-c++::foo_bar ? did I fuck up my exporting ? - +# why can't I just declare googleapis-c++::foo_bar ? most likely my exporting was messed up... find_package( googleapis CONFIG REQUIRED googleapis_cpp_monitoring_v3_protos ) -# this works get_target_property( _iface_incdir googleapis_cpp_monitoring_v3_protos INTERFACE_INCLUDE_DIRECTORIES ) target_include_directories( opencensus_exporters_stats_stackdriver PRIVATE $ ) target_include_directories( opencensus_exporters_stats_stackdriver_utils PRIVATE $ ) @@ -49,8 +47,7 @@ target_link_libraries( opencensus_exporters_stats_stackdriver_utils PUBLIC googl get_target_property( _gmock_iface_incdir gmock INTERFACE_INCLUDE_DIRECTORIES ) target_include_directories( opencensus_time_series_matcher PUBLIC $ ) -#target_include_directories( opencensus_time_series_matcher PUBLIC $ ) -target_link_libraries ( opencensus_time_series_matcher PUBLIC gmock googleapis_cpp_monitoring_v3_protos ) +target_link_libraries ( opencensus_time_series_matcher PUBLIC gmock ) opencensus_test(exporters_stats_stackdriver_utils_test internal/stackdriver_utils_test.cc From d21434d959b0115cbcd36e0a66853cd507888bce Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 16:32:17 -0700 Subject: [PATCH 05/12] WIP: fix travis-ci ubuntu/clang build --- opencensus/common/internal/grpc/CMakeLists.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/opencensus/common/internal/grpc/CMakeLists.txt b/opencensus/common/internal/grpc/CMakeLists.txt index 5db0e747..2a548734 100644 --- a/opencensus/common/internal/grpc/CMakeLists.txt +++ b/opencensus/common/internal/grpc/CMakeLists.txt @@ -1,6 +1,4 @@ - -find_package( protobuf CONFIG REQUIRED) # for now, I need that and protobuf_MODULE_COMPATIBLE=ON -find_package( gRPC CONFIG REQUIRED) +find_package( gRPC CONFIG REQUIRED) opencensus_lib(common_grpc_status HDRS From 9262d39638d3490a95e0e1df5ad9e49213337594 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 17:00:47 -0700 Subject: [PATCH 06/12] CMake: stackdriver exporter build only behind an option. does a find_package() on gRPC and googleapis. No FetchContent yet --- CMakeLists.txt | 4 +- examples/memory/CMakeLists.txt | 23 +++++++- examples/memory/memory_stats.cc | 14 +++++ .../common/internal/grpc/CMakeLists.txt | 57 +++++++++++-------- opencensus/exporters/stats/CMakeLists.txt | 13 +++-- opencensus/exporters/trace/CMakeLists.txt | 11 ++-- 6 files changed, 86 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 612606b2..a0eb0eb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,8 @@ project( option(FUZZER "Either OFF or e.g. -fsanitize=fuzzer,address" OFF) +option(BUILD_STACKDRIVER_EXPORTER "Build the stackdriver exporter" OFF ) + if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -72,7 +74,7 @@ endif() install( EXPORT opencensus-cpp-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp ) -# we still need to create and export opencensus-cpp-config.cmake +# we still need to create and export opencensus-cpp-config.cmake # Create and install the CMake configuration files. configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/config.cmake.in" diff --git a/examples/memory/CMakeLists.txt b/examples/memory/CMakeLists.txt index cc69b446..33f10334 100644 --- a/examples/memory/CMakeLists.txt +++ b/examples/memory/CMakeLists.txt @@ -27,6 +27,8 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") endif() + if( BUILD_STACKDRIVER_EXPORTER ) + set ( USE_SYSTEM_CURL YES ) include(FetchContent) FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/whoshuu/cpr.git) @@ -49,6 +51,8 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") add_executable(linux_memory_stats_to_stackdriver memory_stats.cc ) target_include_directories( linux_memory_stats_to_stackdriver PRIVATE "${re2_SOURCE_DIR}" ) + target_compile_definitions( linux_memory_stats_to_stackdriver PRIVATE USE_STACKDRIVER_EXPORTER ) + target_link_libraries( linux_memory_stats_to_stackdriver absl::flags @@ -62,4 +66,21 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") cpr::cpr ) -endif() +endif( BUILD_STACKDRIVER_EXPORTER ) + + + add_executable(linux_memory_stats_to_stdout memory_stats.cc ) + target_include_directories( linux_memory_stats_to_stdout PRIVATE "${re2_SOURCE_DIR}" ) + target_link_libraries( + linux_memory_stats_to_stdout + absl::flags + absl::flags_parse + absl::strings + absl::time + opencensus-cpp::exporters_stats_stdout + opencensus-cpp::stats + re2 + ) + + +endif() # Linux diff --git a/examples/memory/memory_stats.cc b/examples/memory/memory_stats.cc index 6ba2500b..4b104970 100644 --- a/examples/memory/memory_stats.cc +++ b/examples/memory/memory_stats.cc @@ -16,27 +16,34 @@ #include "absl/time/clock.h" #include "absl/time/time.h" +#ifdef USE_STACKDRIVER_EXPORTER #include "cpr/cpr.h" #include "google/protobuf/util/json_util.h" #include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" +#endif + #include "opencensus/exporters/stats/stdout/stdout_exporter.h" #include "opencensus/stats/stats.h" #include "re2/re2.h" +#ifdef USE_STACKDRIVER_EXPORTER ABSL_FLAG(std::string, metric_product_prefix, "OpenCensus", "product specifc prefix in google monitoring metric name ( " "custom.googleapis.com/[PREFIX]/[METRIC])"); ABSL_FLAG(std::string, project_id, "", "stackdriver project id"); ABSL_FLAG(std::string, instance_id, "", "local GCE instance id"); ABSL_FLAG(std::string, zone, "", "local instance zone"); +#endif + ABSL_FLAG(bool, debug, false, "debug : print to stdout"); ABSL_FLAG(int, period_seconds, 60, "perform a measurement every N seconds"); ABSL_FLAG(bool, meminfo, true, "parse /proc/meminfo ( creates ~20 metrics )"); ABSL_FLAG(bool, vmstat, false, "parse /proc/vmstat ( creates 130+ metrics! )"); +#ifdef USE_STACKDRIVER_EXPORTER namespace { // Monitored resource // FIXME : how do I detect if I am on a gce instance, a container inside a @@ -72,6 +79,7 @@ std::string ConvertMessageToJson(google::protobuf::Message const* poMsg) { } } // namespace +#endif /* The purpose of this executable is to collect /proc/meminfo and /proc/vmstat @@ -105,6 +113,7 @@ int main(int argc, char** argv) { std::vector positionalArgs = absl::ParseCommandLine(argc, argv); +#ifdef USE_STACKDRIVER_EXPORTER if (absl::GetFlag(FLAGS_debug)) { std::cout << "DEBUG MODE: registering an stdout stats exporter only" << std::endl; @@ -216,6 +225,11 @@ int main(int argc, char** argv) { opencensus::exporters::stats::StackdriverExporter::Register( std::move(statsOps)); } +#else + + opencensus::exporters::stats::StdoutExporter::Register(); + +#endif std::cout << "parsing /proc/meminfo and /proc/vmstat every " << absl::GetFlag(FLAGS_period_seconds) << " seconds " << std::endl; diff --git a/opencensus/common/internal/grpc/CMakeLists.txt b/opencensus/common/internal/grpc/CMakeLists.txt index 2a548734..233942e9 100644 --- a/opencensus/common/internal/grpc/CMakeLists.txt +++ b/opencensus/common/internal/grpc/CMakeLists.txt @@ -1,28 +1,35 @@ -find_package( gRPC CONFIG REQUIRED) +if ( BUILD_STACKDRIVER_EXPORTER ) -opencensus_lib(common_grpc_status - HDRS - status.h - SRCS - status.cc - DEPS - absl::strings -) -get_target_property( _iface_incdir gRPC::grpc++ INTERFACE_INCLUDE_DIRECTORIES) -target_include_directories( opencensus_common_grpc_status PUBLIC $ ) -target_link_libraries ( opencensus_common_grpc_status PUBLIC gRPC::grpc++ ) + find_package( gRPC CONFIG REQUIRED) -opencensus_test(common_grpc_status_test - status_test.cc - common_grpc_status ) + opencensus_lib(common_grpc_status + HDRS + status.h + SRCS + status.cc + DEPS + absl::strings + ) -opencensus_lib(common_grpc_with_user_agent - HDRS - with_user_agent.h - SRCS - with_user_agent.cc - DEPS - absl::strings -) -target_include_directories( opencensus_common_grpc_with_user_agent PUBLIC $ ) -target_link_libraries ( opencensus_common_grpc_with_user_agent PUBLIC gRPC::grpc++ ) + get_target_property( _iface_incdir gRPC::grpc++ INTERFACE_INCLUDE_DIRECTORIES) + target_include_directories( opencensus_common_grpc_status PUBLIC $ ) + target_link_libraries ( opencensus_common_grpc_status PUBLIC gRPC::grpc++ ) + + opencensus_test(common_grpc_status_test + status_test.cc + common_grpc_status + ) + + opencensus_lib(common_grpc_with_user_agent + HDRS + with_user_agent.h + SRCS + with_user_agent.cc + DEPS + absl::strings + ) + + target_include_directories( opencensus_common_grpc_with_user_agent PUBLIC $ ) + target_link_libraries ( opencensus_common_grpc_with_user_agent PUBLIC gRPC::grpc++ ) + +endif( BUILD_STACKDRIVER_EXPORTER ) diff --git a/opencensus/exporters/stats/CMakeLists.txt b/opencensus/exporters/stats/CMakeLists.txt index d7ec8ec8..f8669142 100644 --- a/opencensus/exporters/stats/CMakeLists.txt +++ b/opencensus/exporters/stats/CMakeLists.txt @@ -14,12 +14,17 @@ add_subdirectory(prometheus) -find_package( googleapis COMPONENTS googleapis_cpp_monitoring_v3 ) -if ( googleapis_FOUND ) +if ( BUILD_STACKDRIVER_EXPORTER ) - add_subdirectory(stackdriver) + # unless magic is perfomed default builds of google-cloud-cpp or cpp-makefiles do not export this + find_package( googleapis COMPONENTS googleapis_cpp_monitoring_v3 ) + if ( googleapis_FOUND ) -endif() + add_subdirectory(stackdriver) + + endif() + +endif( BUILD_STACKDRIVER_EXPORTER ) add_subdirectory(stdout) diff --git a/opencensus/exporters/trace/CMakeLists.txt b/opencensus/exporters/trace/CMakeLists.txt index 2a549b65..529c9815 100644 --- a/opencensus/exporters/trace/CMakeLists.txt +++ b/opencensus/exporters/trace/CMakeLists.txt @@ -12,13 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +if ( BUILD_STACKDRIVER_EXPORTER ) + find_package( googleapis COMPONENTS googleapis_cpp_devtools_cloudtrace_v2_protos ) + if ( googleapis_FOUND ) -find_package( googleapis COMPONENTS googleapis_cpp_devtools_cloudtrace_v2_protos ) -if ( googleapis_FOUND ) + add_subdirectory(stackdriver) - add_subdirectory(stackdriver) - -endif() + endif() +endif( BUILD_STACKDRIVER_EXPORTER ) add_subdirectory(stdout) From 21e7a6b218b9e57129eb1c1f36e5c096e07b99ba Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 17:09:02 -0700 Subject: [PATCH 07/12] make protobuf/grpc/stackdriver dependent target contingent to BUILD_STACKDRIVER_EXPORTER CMake option --- opencensus/common/internal/CMakeLists.txt | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/opencensus/common/internal/CMakeLists.txt b/opencensus/common/internal/CMakeLists.txt index 63efa0dc..34b31185 100644 --- a/opencensus/common/internal/CMakeLists.txt +++ b/opencensus/common/internal/CMakeLists.txt @@ -37,11 +37,6 @@ opencensus_lib(common_string_vector_hash HDRS string_vector_hash.h DEPS absl::ha opencensus_lib(common_varint HDRS varint.h SRCS varint.cc DEPS absl::strings) -find_package( protobuf REQUIRED ) - -opencensus_lib( common_timestamp HDRS timestamp.h SRCS timestamp.cc DEPS absl::time ) - -target_link_libraries( opencensus_common_timestamp PUBLIC protobuf::libprotobuf ) # Tests. opencensus_test(common_hostname_test hostname_test.cc common_hostname) @@ -53,10 +48,22 @@ opencensus_test(common_stats_object_test stats_object_test.cc opencensus_test(common_varint_test varint_test.cc common_varint) -opencensus_test(common_timestamp_test timestamp_test.cc common_timestamp) - # Benchmarks. opencensus_benchmark(common_random_benchmark random_benchmark.cc common_random) -opencensus_benchmark(common_timestamp_benchmark timestamp_benchmark.cc common_timestamp) + +# +if ( BUILD_STACKDRIVER_EXPORTER ) + + find_package( protobuf REQUIRED ) + + opencensus_lib( common_timestamp HDRS timestamp.h SRCS timestamp.cc DEPS absl::time ) + + target_link_libraries( opencensus_common_timestamp PUBLIC protobuf::libprotobuf ) + + opencensus_test(common_timestamp_test timestamp_test.cc common_timestamp) + + opencensus_benchmark(common_timestamp_benchmark timestamp_benchmark.cc common_timestamp) + +endif() From 1818f72d2d25b12f9d9da02826e9b4eaa8bb77b5 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 18:25:57 -0700 Subject: [PATCH 08/12] Fix travis-ci build : abseil as FetchContent means linking rule needs change, fix newest example --- cmake/OpenCensusHelpers.cmake | 2 +- examples/memory/memory_stats.cc | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmake/OpenCensusHelpers.cmake b/cmake/OpenCensusHelpers.cmake index 00f9031c..8c1142bf 100644 --- a/cmake/OpenCensusHelpers.cmake +++ b/cmake/OpenCensusHelpers.cmake @@ -17,7 +17,7 @@ function(prepend_opencensus OUT DEPS) set(_DEPS "") foreach(dep ${DEPS}) if("${dep}" MATCHES "::") - list(APPEND _DEPS "${dep}") + list(APPEND _DEPS "$") else() list(APPEND _DEPS "opencensus_${dep}") endif() diff --git a/examples/memory/memory_stats.cc b/examples/memory/memory_stats.cc index 4b104970..4234ab36 100644 --- a/examples/memory/memory_stats.cc +++ b/examples/memory/memory_stats.cc @@ -43,8 +43,12 @@ ABSL_FLAG(int, period_seconds, 60, "perform a measurement every N seconds"); ABSL_FLAG(bool, meminfo, true, "parse /proc/meminfo ( creates ~20 metrics )"); ABSL_FLAG(bool, vmstat, false, "parse /proc/vmstat ( creates 130+ metrics! )"); -#ifdef USE_STACKDRIVER_EXPORTER namespace { + +constexpr char kBytesUnits[] = "bytes"; +constexpr char kNoUnits[] = ""; + +#ifdef USE_STACKDRIVER_EXPORTER // Monitored resource // FIXME : how do I detect if I am on a gce instance, a container inside a // k8s_pod in gke ? a container in gce ? @@ -61,9 +65,6 @@ constexpr char kClusterNameLabel[] = "cluster_name"; constexpr char kNamespaceNameLabel[] = "namespace_name"; constexpr char kPodNameLabel[] = "pod_name"; -constexpr char kBytesUnits[] = "bytes"; -constexpr char kNoUnits[] = ""; - std::string ConvertMessageToJson(google::protobuf::Message const* poMsg) { std::string strMsg; @@ -77,9 +78,8 @@ std::string ConvertMessageToJson(google::protobuf::Message const* poMsg) { return strMsg; } - -} // namespace #endif +} // namespace /* The purpose of this executable is to collect /proc/meminfo and /proc/vmstat From 357b50b94ef4ed1aa2a8daa85ecf067f31cf6b42 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 22:41:15 -0700 Subject: [PATCH 09/12] removed re2 as dependency for the new memory example, as I can\'t get it to pass its own unit tests when used with FetchContent ... --- examples/memory/CMakeLists.txt | 25 ----------------------- examples/memory/memory_stats.cc | 35 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/examples/memory/CMakeLists.txt b/examples/memory/CMakeLists.txt index 33f10334..a517127e 100644 --- a/examples/memory/CMakeLists.txt +++ b/examples/memory/CMakeLists.txt @@ -2,31 +2,8 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") include( FetchContent ) - FetchContent_Declare( - re2 - URL https://github.com/google/re2/archive/2020-06-01.tar.gz - URL_HASH SHA256=fb8e0f4ed7a212e3420507f27933ef5a8c01aec70e5148c6a35313573269fae6 - ) - #find_package( absl REQUIRED ) - FetchContent_GetProperties(re2) - if(NOT re2_POPULATED) - # build re2 as as a private static library with ashared - set(orig_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}" ) - set( orig_POSITION_INDEPENDENT_CODE "${POSITION_INDEPENDENT_CODE}" ) - - set( BUILD_SHARED_LIBS OFF ) - set( POSITION_INDEPENDENT_CODE YES ) - - FetchContent_Populate(re2) - add_subdirectory(${re2_SOURCE_DIR} ${re2_BINARY_DIR} EXCLUDE_FROM_ALL ) - - set( BUILD_SHARED_LIBS "${orig_BUILD_SHARED_LIB}" ) - set ( POSITION_INDEPENDENT_CODE "${orig_POSITION_INDEPENDENT_CODE}" ) - - endif() - if( BUILD_STACKDRIVER_EXPORTER ) set ( USE_SYSTEM_CURL YES ) @@ -62,7 +39,6 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") opencensus-cpp::exporters_stats_stackdriver opencensus-cpp::exporters_stats_stdout opencensus-cpp::stats - re2 cpr::cpr ) @@ -79,7 +55,6 @@ endif( BUILD_STACKDRIVER_EXPORTER ) absl::time opencensus-cpp::exporters_stats_stdout opencensus-cpp::stats - re2 ) diff --git a/examples/memory/memory_stats.cc b/examples/memory/memory_stats.cc index 4234ab36..67ed76cb 100644 --- a/examples/memory/memory_stats.cc +++ b/examples/memory/memory_stats.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "absl/flags/flag.h" @@ -27,8 +28,6 @@ #include "opencensus/exporters/stats/stdout/stdout_exporter.h" #include "opencensus/stats/stats.h" -#include "re2/re2.h" - #ifdef USE_STACKDRIVER_EXPORTER ABSL_FLAG(std::string, metric_product_prefix, "OpenCensus", "product specifc prefix in google monitoring metric name ( " @@ -103,7 +102,7 @@ std::string ConvertMessageToJson(google::protobuf::Message const* poMsg) { - build as a separate CMake project - build as a Bazel example - build as a separate Bazel project - - review all the licenses (re2, cpr, opencensus-cpp, protobuf and abseil) + - review all the licenses (cpr, opencensus-cpp, protobuf and abseil) */ int main(int argc, char** argv) { @@ -381,13 +380,10 @@ int main(int argc, char** argv) { std::pair > meminfoByteMeasures, meminfoCountMeasures, vmstatCountMeasures; - // use libre2 because CentOS 7 devtoolset packages a gcc 7+ which reuses the - // default GCC 4.8.x libstdc++ which has buggy / incomplete support for - // regex as a result, unix-style regular expression are buggy... - re2::RE2 static const countRe("(.+):\\s+([[:alnum:]]+)"); - re2::RE2 static const byteRe("(.+):\\s+([[:alnum:]]+) kB"); - re2::RE2 static const vmstatRe("(.+)\\s+([[:alnum:]]+)"); + std::regex static const countRe("(.+):[[:space:]]+([[:digit:]]+)"); + std::regex static const byteRe("(.+):[[:space:]]+([[:digit:]]+) kB"); + std::regex static const vmstatRe("(.+) ([[:alnum:]]+)"); bool const doMemInfo = absl::GetFlag(FLAGS_meminfo); bool const doVmStat = absl::GetFlag(FLAGS_vmstat); @@ -404,13 +400,21 @@ int main(int argc, char** argv) { if (doMemInfo) { ifile.open("/proc/meminfo"); + std::smatch match; + for (std::string line; std::getline(ifile, line);) { std::string label; std::int64_t count; - if (re2::RE2::FullMatch(line, byteRe, &label, &count)) { - meminfoByteValues[label] = count * 1024; - } else if (re2::RE2::FullMatch(line, countRe, &label, &count)) { + if (std::regex_match(line, match, byteRe)){ + label = match[1].str(); + count = 1024 * std::stoll(match[2].str()); + + meminfoByteValues[label] = count; + } else if (std::regex_match(line, match, countRe)){ + label = match[1].str(); + count = std::stoll(match[2].str()); + meminfoCountValues[label] = count; } } @@ -421,11 +425,16 @@ int main(int argc, char** argv) { if (doVmStat) { ifile.open("/proc/vmstat"); + std::smatch match; + for (std::string line; std::getline(ifile, line);) { std::string label; std::int64_t count; - if (re2::RE2::FullMatch(line, vmstatRe, &label, &count)) { + if (std::regex_match(line, match, vmstatRe)){ + label = match[1].str(); + count = std::stoll(match[2].str()); + vmstatCountValues[label] = count; } } From 72215290fefe5e00527098b070e18196ae6ba950 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Fri, 19 Jun 2020 23:21:59 -0700 Subject: [PATCH 10/12] finally managed to run tools/format.sh --- examples/memory/memory_stats.cc | 25 +++++++++---------- .../internal/stackdriver_e2e_test.cc | 13 +++++----- .../trace/stackdriver/CMakeLists.txt | 10 ++++---- opencensus/trace/CMakeLists.txt | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/memory/memory_stats.cc b/examples/memory/memory_stats.cc index 67ed76cb..bad7e032 100644 --- a/examples/memory/memory_stats.cc +++ b/examples/memory/memory_stats.cc @@ -380,9 +380,8 @@ int main(int argc, char** argv) { std::pair > meminfoByteMeasures, meminfoCountMeasures, vmstatCountMeasures; - - std::regex static const countRe("(.+):[[:space:]]+([[:digit:]]+)"); - std::regex static const byteRe("(.+):[[:space:]]+([[:digit:]]+) kB"); + std::regex static const countRe("(.+):[[:space:]]+([[:digit:]]+)"); + std::regex static const byteRe("(.+):[[:space:]]+([[:digit:]]+) kB"); std::regex static const vmstatRe("(.+) ([[:alnum:]]+)"); bool const doMemInfo = absl::GetFlag(FLAGS_meminfo); @@ -401,19 +400,19 @@ int main(int argc, char** argv) { ifile.open("/proc/meminfo"); std::smatch match; - + for (std::string line; std::getline(ifile, line);) { std::string label; std::int64_t count; - if (std::regex_match(line, match, byteRe)){ - label = match[1].str(); - count = 1024 * std::stoll(match[2].str()); + if (std::regex_match(line, match, byteRe)) { + label = match[1].str(); + count = 1024 * std::stoll(match[2].str()); meminfoByteValues[label] = count; - } else if (std::regex_match(line, match, countRe)){ - label = match[1].str(); - count = std::stoll(match[2].str()); + } else if (std::regex_match(line, match, countRe)) { + label = match[1].str(); + count = std::stoll(match[2].str()); meminfoCountValues[label] = count; } @@ -431,9 +430,9 @@ int main(int argc, char** argv) { std::string label; std::int64_t count; - if (std::regex_match(line, match, vmstatRe)){ - label = match[1].str(); - count = std::stoll(match[2].str()); + if (std::regex_match(line, match, vmstatRe)) { + label = match[1].str(); + count = std::stoll(match[2].str()); vmstatCountValues[label] = count; } diff --git a/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc b/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc index 3c9df9b0..1d9ecc0c 100644 --- a/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc +++ b/opencensus/exporters/stats/stackdriver/internal/stackdriver_e2e_test.cc @@ -25,12 +25,11 @@ #include "google/monitoring/v3/metric_service.grpc.pb.h" #include "google/protobuf/empty.pb.h" #include "gtest/gtest.h" +#include "opencensus/common/internal/timestamp.h" #include "opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.h" #include "opencensus/exporters/stats/stackdriver/internal/time_series_matcher.h" #include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" #include "opencensus/stats/stats.h" -#include "opencensus/common/internal/timestamp.h" - namespace opencensus { namespace exporters { @@ -112,10 +111,12 @@ StackdriverE2eTest::RetrieveData( request.set_filter( absl::StrCat("metric.type = \"custom.googleapis.com/opencensus/", descriptor.name(), "\"")); - opencensus::common::SetTimestamp(absl::Now() - absl::Hours(1), - request.mutable_interval()->mutable_start_time()); - opencensus::common::SetTimestamp(absl::Now() + absl::Hours(1), - request.mutable_interval()->mutable_end_time()); + opencensus::common::SetTimestamp( + absl::Now() - absl::Hours(1), + request.mutable_interval()->mutable_start_time()); + opencensus::common::SetTimestamp( + absl::Now() + absl::Hours(1), + request.mutable_interval()->mutable_end_time()); while (true) { google::monitoring::v3::ListTimeSeriesResponse response; diff --git a/opencensus/exporters/trace/stackdriver/CMakeLists.txt b/opencensus/exporters/trace/stackdriver/CMakeLists.txt index 282c21c7..fc1472b7 100644 --- a/opencensus/exporters/trace/stackdriver/CMakeLists.txt +++ b/opencensus/exporters/trace/stackdriver/CMakeLists.txt @@ -1,4 +1,4 @@ -opencensus_lib( exporters_trace_stackdriver +opencensus_lib( exporters_trace_stackdriver PUBLIC HDRS stackdriver_exporter.h @@ -11,14 +11,14 @@ opencensus_lib( exporters_trace_stackdriver trace ) -find_package( googleapis CONFIG REQUIRED googleapis_cpp_devtools_cloudtrace_v2_protos ) +find_package( googleapis CONFIG REQUIRED googleapis_cpp_devtools_cloudtrace_v2_protos ) #get_target_property(_iface_incdir googleapis_cpp_devtools_cloudtrace_v2_protos INTERFACE_INCLUDE_DIRECTORIES ) # #target_include_directories( opencensus_exporters_trace_stackdriver PRIVATE ${_iface_incdir}) -target_link_libraries( opencensus_exporters_trace_stackdriver - PUBLIC - googleapis_cpp_devtools_cloudtrace_v2_trace_protos +target_link_libraries( opencensus_exporters_trace_stackdriver + PUBLIC + googleapis_cpp_devtools_cloudtrace_v2_trace_protos googleapis_cpp_devtools_cloudtrace_v2_tracing_protos googleapis_cpp_rpc_status_protos ) diff --git a/opencensus/trace/CMakeLists.txt b/opencensus/trace/CMakeLists.txt index 003e1f4c..9bd3048d 100644 --- a/opencensus/trace/CMakeLists.txt +++ b/opencensus/trace/CMakeLists.txt @@ -39,7 +39,7 @@ opencensus_lib( status_code.h trace_config.h trace_params.h - SRCS + SRCS internal/annotation.cc internal/attribute_list.cc internal/attribute_value.cc From 8675d05f12eb7b3e79e2ed4a507c42ff09c55dce Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Sat, 20 Jun 2020 00:18:43 -0700 Subject: [PATCH 11/12] finally manually assembled the right versions of buildifier,clang-format and cmake-format manually as docker-format Dockerfile is obsolete. format check should match travis one --- CMakeLists.txt | 30 ++--- cmake/OpenCensusDeps.cmake | 71 ++++++----- cmake/OpenCensusHelpers.cmake | 109 +++++++++-------- examples/memory/CMakeLists.txt | 74 ++++++------ opencensus/common/internal/CMakeLists.txt | 43 +++++-- .../common/internal/grpc/CMakeLists.txt | 56 ++++----- opencensus/exporters/stats/CMakeLists.txt | 12 +- .../exporters/stats/prometheus/CMakeLists.txt | 2 +- .../stats/stackdriver/CMakeLists.txt | 113 +++++++++--------- opencensus/exporters/trace/CMakeLists.txt | 9 +- .../trace/stackdriver/CMakeLists.txt | 44 +++---- opencensus/stats/CMakeLists.txt | 3 +- opencensus/trace/CMakeLists.txt | 44 ++++--- tools/docker-format/Dockerfile | 4 + 14 files changed, 332 insertions(+), 282 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0eb0eb2..5c977967 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ project( option(FUZZER "Either OFF or e.g. -fsanitize=fuzzer,address" OFF) -option(BUILD_STACKDRIVER_EXPORTER "Build the stackdriver exporter" OFF ) +option(BUILD_STACKDRIVER_EXPORTER "Build the stackdriver exporter" OFF) if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 11) @@ -40,17 +40,19 @@ enable_testing() list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) -# There are cyclical dependencies ( betwen libopencensus_trace.so and libopencensus_context.so ) -# that prevent from building the code as is as shared libraries without undefined symbols... +# There are cyclical dependencies ( betwen libopencensus_trace.so and +# libopencensus_context.so ) that prevent from building the code as is as shared +# libraries without undefined symbols... # -# Moreover, because of the way I built gRPC c++ apis within google-cloud-cpp as shared libs, with a specific abseil version _as shared lib_ +# Moreover, because of the way I built gRPC c++ apis within google-cloud-cpp as +# shared libs, with a specific abseil version _as shared lib_ -if ( BUILD_SHARED_LIBS ) - set( POSITION_INDEPENDENT_CODE ON ) - set( BUILD_SHARED_LIBS NO ) +if(BUILD_SHARED_LIBS) + set(POSITION_INDEPENDENT_CODE ON) + set(BUILD_SHARED_LIBS NO) endif() # clearly there are problems with cyclical deps in this code ( context, trace ) -if ( BUILD_SHARED_LIBS ) +if(BUILD_SHARED_LIBS) add_link_options("LINKER:--no-undefined") endif() @@ -60,7 +62,7 @@ include(OpenCensusDeps) include(OpenCensusHelpers) -if ( BUILD_SHARED_LIBS ) +if(BUILD_SHARED_LIBS) add_link_options("LINKER:--no-undefined") endif() @@ -72,7 +74,8 @@ if(BUILD_TESTING) add_subdirectory(examples) endif() -install( EXPORT opencensus-cpp-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp ) +install(EXPORT opencensus-cpp-targets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp) # we still need to create and export opencensus-cpp-config.cmake @@ -82,7 +85,6 @@ configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/config.cmake.in" configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/config-version.cmake.in" "opencensus-cpp-config-version.cmake" @ONLY) -install( - FILES "${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config-version.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp") diff --git a/cmake/OpenCensusDeps.cmake b/cmake/OpenCensusDeps.cmake index e62f21c7..1c70a122 100644 --- a/cmake/OpenCensusDeps.cmake +++ b/cmake/OpenCensusDeps.cmake @@ -14,47 +14,46 @@ include(FetchContent) +find_package(googletest) +if(NOT googletest_FOUND) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest + GIT_TAG master) -find_package( googletest ) -if ( NOT googletest_FOUND ) -FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest - GIT_TAG master) + FetchContent_GetProperties(googletest) + if(BUILD_TESTING) + message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})") + if(NOT googletest_POPULATED) + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + # All the libraries in the build must use either /MD or /MT (runtime + # library to link) + # + # force this option to ON so that Google Test will use /MD instead of + # /MT /MD is now the default for Visual Studio, so it should be our + # default, too + option( + gtest_force_shared_crt + "Use shared (DLL) run-time lib even when Google Test is built as static lib." + ON) + endif() -FetchContent_GetProperties(googletest) -if(BUILD_TESTING) - message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})") - if(NOT googletest_POPULATED) - if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - # All the libraries in the build must use either /MD or /MT (runtime - # library to link) - # - # force this option to ON so that Google Test will use /MD instead of /MT - # /MD is now the default for Visual Studio, so it should be our default, - # too - option( - gtest_force_shared_crt - "Use shared (DLL) run-time lib even when Google Test is built as static lib." - ON) + FetchContent_Populate(googletest) + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} + EXCLUDE_FROM_ALL) endif() - - FetchContent_Populate(googletest) - add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} - EXCLUDE_FROM_ALL) endif() endif() -endif() # the right thing to do would be to _always_ build abseil as a static library -# with -fPIC in case BUILD_SHARED_LIBS is on. -# it is problematic with the way I currently build googleapis cpp files within google-cloud-cpp +# with -fPIC in case BUILD_SHARED_LIBS is on. it is problematic with the way I +# currently build googleapis cpp files within google-cloud-cpp # -# the crux of the matter is that I need to link opencensus-cpp shared libs -# into applications that already have their own libcurl, libgrpc* ... +# the crux of the matter is that I need to link opencensus-cpp shared libs into +# applications that already have their own libcurl, libgrpc* ... -find_package( absl ) -if ( NOT absl_FOUND ) +find_package(absl) +if(NOT absl_FOUND) FetchContent_Declare( abseil GIT_REPOSITORY https://github.com/abseil/abseil-cpp @@ -71,7 +70,6 @@ if ( NOT absl_FOUND ) endif() endif() - FetchContent_Declare( prometheus GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp @@ -97,8 +95,8 @@ if(NOT prometheus_POPULATED) EXCLUDE_FROM_ALL) endif() -find_package( benchmark ) -if ( NOT benchmark_FOUND ) +find_package(benchmark) +if(NOT benchmark_FOUND) FetchContent_Declare( benchmark GIT_REPOSITORY https://github.com/google/benchmark @@ -112,7 +110,8 @@ if ( NOT benchmark_FOUND ) CACHE BOOL "Enable testing of the benchmark library." FORCE) set(BENCHMARK_ENABLE_GTEST_TESTS OFF - CACHE BOOL "Enable building the unit tests which depend on gtest" FORCE) + CACHE BOOL "Enable building the unit tests which depend on gtest" + FORCE) FetchContent_Populate(benchmark) add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR} EXCLUDE_FROM_ALL) diff --git a/cmake/OpenCensusHelpers.cmake b/cmake/OpenCensusHelpers.cmake index 8c1142bf..a9395dd8 100644 --- a/cmake/OpenCensusHelpers.cmake +++ b/cmake/OpenCensusHelpers.cmake @@ -53,34 +53,33 @@ function(opencensus_benchmark NAME SRC) endif() endfunction() - include(GNUInstallDirs) # -# install( TARGETS PUBLIC_HEADERS DESTINATION foo ) -# does not work well when we declare -# opencensus_lib( libfoo PUBLIC HDRS bar/baz.h SRCS bar/bar.cc ) -# it will generate foo/baz.h not foo/bar/baz.hh -# +# install( TARGETS PUBLIC_HEADERS DESTINATION foo ) does not work well when we +# declare opencensus_lib( libfoo PUBLIC HDRS bar/baz.h SRCS bar/bar.cc ) it +# will generate foo/baz.h not foo/bar/baz.hh # -# install_headers_with_subdirectories( PUBLIC_HEADERS item1 item2 .. INSTALL_FOLDER dir EXPORT export_name ) +# install_headers_with_subdirectories( PUBLIC_HEADERS item1 item2 .. +# INSTALL_FOLDER dir EXPORT export_name ) # -function( install_headers_with_subdirectories ) - set(options "" ) +function(install_headers_with_subdirectories) + set(options "") set(singleValued "INSTALL_FOLDER") - set( multiValued "PUBLIC_HEADER" ) + set(multiValued "PUBLIC_HEADER") - cmake_parse_arguments(ARG "${options}" "${singleValued}" "${multiValued}" ${ARGN} ) + cmake_parse_arguments(ARG "${options}" "${singleValued}" "${multiValued}" + ${ARGN}) - foreach( header ${ARG_PUBLIC_HEADER} ) + foreach(header ${ARG_PUBLIC_HEADER}) - get_filename_component( dir ${header} DIRECTORY ) + get_filename_component(dir ${header} DIRECTORY) - install( FILES ${header} DESTINATION "${ARG_INSTALL_FOLDER}/${dir}" ) + install(FILES ${header} DESTINATION "${ARG_INSTALL_FOLDER}/${dir}") endforeach() -endfunction( install_headers_with_subdirectories ) +endfunction(install_headers_with_subdirectories) # Helper function like bazel's cc_library. Libraries are namespaced as # opencensus_* and public libraries are also aliased as opencensus-cpp::*. @@ -89,59 +88,69 @@ function(opencensus_lib NAME) set(_NAME "opencensus_${NAME}") prepend_opencensus(ARG_DEPS "${ARG_DEPS}") - string( REPLACE "${PROJECT_SOURCE_DIR}/" "" _current_dir_relative_path "${CMAKE_CURRENT_LIST_DIR}" ) + string(REPLACE "${PROJECT_SOURCE_DIR}/" "" _current_dir_relative_path + "${CMAKE_CURRENT_LIST_DIR}") if(ARG_SRCS) add_library(${_NAME} ${ARG_SRCS}) target_link_libraries(${_NAME} PUBLIC ${ARG_DEPS}) - target_include_directories(${_NAME} PUBLIC $ $ ) + target_include_directories( + ${_NAME} PUBLIC $ + $) else() add_library(${_NAME} INTERFACE) target_link_libraries(${_NAME} INTERFACE ${ARG_DEPS}) - target_include_directories(${_NAME} INTERFACE $ $ ) + target_include_directories( + ${_NAME} INTERFACE $ + $) endif() if(ARG_PUBLIC) add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME}) - if (ARG_HDRS) - #this will install them - #set_target_properties( ${_NAME} PROPERTIES PUBLIC_HEADER "${ARG_HDRS}" ) - install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} - INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" - ) + if(ARG_HDRS) + # this will install them set_target_properties( ${_NAME} PROPERTIES + # PUBLIC_HEADER "${ARG_HDRS}" ) + install_headers_with_subdirectories( + PUBLIC_HEADER ${ARG_HDRS} INSTALL_FOLDER + "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}") endif() - install( TARGETS ${_NAME} - EXPORT opencensus-cpp-targets - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" -# PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" - ) + install( + TARGETS ${_NAME} + EXPORT opencensus-cpp-targets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + # PUBLIC_HEADER DESTINATION + # "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + ) elseif(ARG_PRIVATE) - # what we really wanted to do, if linking private static libraries into public ones worked - # without having to export the private one... + # what we really wanted to do, if linking private static libraries into + # public ones worked without having to export the private one... else() - if (ARG_HDRS) - # I think we have API problems whereby the internal includes are required by clients... - # Comment out this line if it is not the case... - #set_target_properties( ${_NAME} PROPERTIES PRIVATE_HEADER "${ARG_HDRS}" ) - install_headers_with_subdirectories( PUBLIC_HEADER ${ARG_HDRS} - INSTALL_FOLDER "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" - ) + if(ARG_HDRS) + # I think we have API problems whereby the internal includes are required + # by clients... Comment out this line if it is not the case... + # set_target_properties( ${_NAME} PROPERTIES PRIVATE_HEADER "${ARG_HDRS}" + # ) + install_headers_with_subdirectories( + PUBLIC_HEADER ${ARG_HDRS} INSTALL_FOLDER + "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}") endif() - # fight export bug ? - # I don't want these installed, I don't want the associated headers installed - # I want them as depedencies to public static libraries that aggregate them.... - install( TARGETS ${_NAME} - EXPORT opencensus-cpp-targets - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" -# PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" - ) + # fight export bug ? I don't want these installed, I don't want the + # associated headers installed I want them as depedencies to public static + # libraries that aggregate them.... + install( + TARGETS ${_NAME} + EXPORT opencensus-cpp-targets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + # PUBLIC_HEADER DESTINATION + # "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" + ) endif() endfunction() diff --git a/examples/memory/CMakeLists.txt b/examples/memory/CMakeLists.txt index a517127e..f2bd0931 100644 --- a/examples/memory/CMakeLists.txt +++ b/examples/memory/CMakeLists.txt @@ -1,52 +1,52 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - include( FetchContent ) - - #find_package( absl REQUIRED ) - - if( BUILD_STACKDRIVER_EXPORTER ) - - set ( USE_SYSTEM_CURL YES ) include(FetchContent) - FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/whoshuu/cpr.git) - if ( NOT cpr_POPULATED ) - set(orig_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}" ) - set( orig_POSITION_INDEPENDENT_CODE "${POSITION_INDEPENDENT_CODE}" ) + # find_package( absl REQUIRED ) - set( BUILD_SHARED_LIBS OFF ) - set( POSITION_INDEPENDENT_CODE YES ) + if(BUILD_STACKDRIVER_EXPORTER) - FetchContent_Populate(cpr) - add_subdirectory(${cpr_SOURCE_DIR} ${cpr_BINARY_DIR} EXCLUDE_FROM_ALL ) + set(USE_SYSTEM_CURL YES) + include(FetchContent) + FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/whoshuu/cpr.git) + if(NOT cpr_POPULATED) - set( BUILD_SHARED_LIBS "${orig_BUILD_SHARED_LIB}" ) - set ( POSITION_INDEPENDENT_CODE "${orig_POSITION_INDEPENDENT_CODE}" ) + set(orig_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}") + set(orig_POSITION_INDEPENDENT_CODE "${POSITION_INDEPENDENT_CODE}") - endif() + set(BUILD_SHARED_LIBS OFF) + set(POSITION_INDEPENDENT_CODE YES) + FetchContent_Populate(cpr) + add_subdirectory(${cpr_SOURCE_DIR} ${cpr_BINARY_DIR} EXCLUDE_FROM_ALL) - add_executable(linux_memory_stats_to_stackdriver memory_stats.cc ) - target_include_directories( linux_memory_stats_to_stackdriver PRIVATE "${re2_SOURCE_DIR}" ) - target_compile_definitions( linux_memory_stats_to_stackdriver PRIVATE USE_STACKDRIVER_EXPORTER ) + set(BUILD_SHARED_LIBS "${orig_BUILD_SHARED_LIB}") + set(POSITION_INDEPENDENT_CODE "${orig_POSITION_INDEPENDENT_CODE}") - target_link_libraries( - linux_memory_stats_to_stackdriver - absl::flags - absl::flags_parse - absl::strings - absl::time - opencensus-cpp::exporters_stats_stackdriver - opencensus-cpp::exporters_stats_stdout - opencensus-cpp::stats - cpr::cpr - ) + endif() -endif( BUILD_STACKDRIVER_EXPORTER ) + add_executable(linux_memory_stats_to_stackdriver memory_stats.cc) + target_include_directories(linux_memory_stats_to_stackdriver + PRIVATE "${re2_SOURCE_DIR}") + target_compile_definitions(linux_memory_stats_to_stackdriver + PRIVATE USE_STACKDRIVER_EXPORTER) + target_link_libraries( + linux_memory_stats_to_stackdriver + absl::flags + absl::flags_parse + absl::strings + absl::time + opencensus-cpp::exporters_stats_stackdriver + opencensus-cpp::exporters_stats_stdout + opencensus-cpp::stats + cpr::cpr) - add_executable(linux_memory_stats_to_stdout memory_stats.cc ) - target_include_directories( linux_memory_stats_to_stdout PRIVATE "${re2_SOURCE_DIR}" ) + endif(BUILD_STACKDRIVER_EXPORTER) + + add_executable(linux_memory_stats_to_stdout memory_stats.cc) + target_include_directories(linux_memory_stats_to_stdout + PRIVATE "${re2_SOURCE_DIR}") target_link_libraries( linux_memory_stats_to_stdout absl::flags @@ -54,8 +54,6 @@ endif( BUILD_STACKDRIVER_EXPORTER ) absl::strings absl::time opencensus-cpp::exporters_stats_stdout - opencensus-cpp::stats - ) - + opencensus-cpp::stats) endif() # Linux diff --git a/opencensus/common/internal/CMakeLists.txt b/opencensus/common/internal/CMakeLists.txt index 34b31185..935fe08f 100644 --- a/opencensus/common/internal/CMakeLists.txt +++ b/opencensus/common/internal/CMakeLists.txt @@ -12,7 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -opencensus_lib(common_hostname HDRS hostname.h SRCS hostname.cc DEPS absl::strings) +opencensus_lib( + common_hostname + HDRS + hostname.h + SRCS + hostname.cc + DEPS + absl::strings) opencensus_lib( common_random @@ -25,7 +32,7 @@ opencensus_lib( absl::synchronization absl::time) -add_subdirectory( grpc ) +add_subdirectory(grpc) opencensus_lib(common_stats_object HDRS stats_object.h DEPS absl::time) @@ -33,9 +40,17 @@ opencensus_lib(common_stats_object HDRS stats_object.h DEPS absl::time) target_compile_definitions(opencensus_common_stats_object INTERFACE $<$:NOMINMAX>) -opencensus_lib(common_string_vector_hash HDRS string_vector_hash.h DEPS absl::hash) +opencensus_lib(common_string_vector_hash HDRS string_vector_hash.h DEPS + absl::hash) -opencensus_lib(common_varint HDRS varint.h SRCS varint.cc DEPS absl::strings) +opencensus_lib( + common_varint + HDRS + varint.h + SRCS + varint.cc + DEPS + absl::strings) # Tests. @@ -52,18 +67,26 @@ opencensus_test(common_varint_test varint_test.cc common_varint) opencensus_benchmark(common_random_benchmark random_benchmark.cc common_random) - # -if ( BUILD_STACKDRIVER_EXPORTER ) +if(BUILD_STACKDRIVER_EXPORTER) - find_package( protobuf REQUIRED ) + find_package(protobuf REQUIRED) - opencensus_lib( common_timestamp HDRS timestamp.h SRCS timestamp.cc DEPS absl::time ) + opencensus_lib( + common_timestamp + HDRS + timestamp.h + SRCS + timestamp.cc + DEPS + absl::time) - target_link_libraries( opencensus_common_timestamp PUBLIC protobuf::libprotobuf ) + target_link_libraries(opencensus_common_timestamp + PUBLIC protobuf::libprotobuf) opencensus_test(common_timestamp_test timestamp_test.cc common_timestamp) - opencensus_benchmark(common_timestamp_benchmark timestamp_benchmark.cc common_timestamp) + opencensus_benchmark(common_timestamp_benchmark timestamp_benchmark.cc + common_timestamp) endif() diff --git a/opencensus/common/internal/grpc/CMakeLists.txt b/opencensus/common/internal/grpc/CMakeLists.txt index 233942e9..043bc3af 100644 --- a/opencensus/common/internal/grpc/CMakeLists.txt +++ b/opencensus/common/internal/grpc/CMakeLists.txt @@ -1,35 +1,35 @@ -if ( BUILD_STACKDRIVER_EXPORTER ) +if(BUILD_STACKDRIVER_EXPORTER) - find_package( gRPC CONFIG REQUIRED) + find_package(gRPC CONFIG REQUIRED) - opencensus_lib(common_grpc_status - HDRS - status.h - SRCS - status.cc - DEPS - absl::strings - ) + opencensus_lib( + common_grpc_status + HDRS + status.h + SRCS + status.cc + DEPS + absl::strings) - get_target_property( _iface_incdir gRPC::grpc++ INTERFACE_INCLUDE_DIRECTORIES) - target_include_directories( opencensus_common_grpc_status PUBLIC $ ) - target_link_libraries ( opencensus_common_grpc_status PUBLIC gRPC::grpc++ ) + get_target_property(_iface_incdir gRPC::grpc++ INTERFACE_INCLUDE_DIRECTORIES) + target_include_directories(opencensus_common_grpc_status + PUBLIC $) + target_link_libraries(opencensus_common_grpc_status PUBLIC gRPC::grpc++) - opencensus_test(common_grpc_status_test - status_test.cc - common_grpc_status - ) + opencensus_test(common_grpc_status_test status_test.cc common_grpc_status) - opencensus_lib(common_grpc_with_user_agent - HDRS - with_user_agent.h - SRCS - with_user_agent.cc - DEPS - absl::strings - ) + opencensus_lib( + common_grpc_with_user_agent + HDRS + with_user_agent.h + SRCS + with_user_agent.cc + DEPS + absl::strings) - target_include_directories( opencensus_common_grpc_with_user_agent PUBLIC $ ) - target_link_libraries ( opencensus_common_grpc_with_user_agent PUBLIC gRPC::grpc++ ) + target_include_directories(opencensus_common_grpc_with_user_agent + PUBLIC $) + target_link_libraries(opencensus_common_grpc_with_user_agent + PUBLIC gRPC::grpc++) -endif( BUILD_STACKDRIVER_EXPORTER ) +endif(BUILD_STACKDRIVER_EXPORTER) diff --git a/opencensus/exporters/stats/CMakeLists.txt b/opencensus/exporters/stats/CMakeLists.txt index f8669142..78d898e7 100644 --- a/opencensus/exporters/stats/CMakeLists.txt +++ b/opencensus/exporters/stats/CMakeLists.txt @@ -14,17 +14,17 @@ add_subdirectory(prometheus) -if ( BUILD_STACKDRIVER_EXPORTER ) +if(BUILD_STACKDRIVER_EXPORTER) - # unless magic is perfomed default builds of google-cloud-cpp or cpp-makefiles do not export this - find_package( googleapis COMPONENTS googleapis_cpp_monitoring_v3 ) - if ( googleapis_FOUND ) + # unless magic is perfomed default builds of google-cloud-cpp or cpp-makefiles + # do not export this + find_package(googleapis COMPONENTS googleapis_cpp_monitoring_v3) + if(googleapis_FOUND) add_subdirectory(stackdriver) endif() -endif( BUILD_STACKDRIVER_EXPORTER ) - +endif(BUILD_STACKDRIVER_EXPORTER) add_subdirectory(stdout) diff --git a/opencensus/exporters/stats/prometheus/CMakeLists.txt b/opencensus/exporters/stats/prometheus/CMakeLists.txt index f98ad0ff..bc90f2f9 100644 --- a/opencensus/exporters/stats/prometheus/CMakeLists.txt +++ b/opencensus/exporters/stats/prometheus/CMakeLists.txt @@ -34,7 +34,7 @@ opencensus_lib( stats absl::strings absl::time - $ ) + $) opencensus_test( exporters_stats_prometheus_utils_test internal/prometheus_utils_test.cc diff --git a/opencensus/exporters/stats/stackdriver/CMakeLists.txt b/opencensus/exporters/stats/stackdriver/CMakeLists.txt index 90637605..9a508dfb 100644 --- a/opencensus/exporters/stats/stackdriver/CMakeLists.txt +++ b/opencensus/exporters/stats/stackdriver/CMakeLists.txt @@ -1,66 +1,65 @@ -opencensus_lib( exporters_stats_stackdriver - PUBLIC - HDRS - stackdriver_exporter.h - SRCS - internal/stackdriver_exporter.cc - DEPS - common_hostname - common_grpc_status - common_grpc_with_user_agent - stats - exporters_stats_stackdriver_utils -) +opencensus_lib( + exporters_stats_stackdriver + PUBLIC + HDRS + stackdriver_exporter.h + SRCS + internal/stackdriver_exporter.cc + DEPS + common_hostname + common_grpc_status + common_grpc_with_user_agent + stats + exporters_stats_stackdriver_utils) # FIXME : these are supposed to be INTERNAL LIBRARIES EXPOSED VIA PREVIOUS? -opencensus_lib( exporters_stats_stackdriver_utils - #PUBLIC - HDRS - internal/stackdriver_utils.h - SRCS - internal/stackdriver_utils.cc - DEPS - stats - common_timestamp -) +opencensus_lib( + exporters_stats_stackdriver_utils + # PUBLIC + HDRS + internal/stackdriver_utils.h + SRCS + internal/stackdriver_utils.cc + DEPS + stats + common_timestamp) -opencensus_lib( time_series_matcher - PRIVATE - HDRS - internal/time_series_matcher.h - SRCS - internal/time_series_matcher.cc - DEPS - stats -) +opencensus_lib( + time_series_matcher + PRIVATE + HDRS + internal/time_series_matcher.h + SRCS + internal/time_series_matcher.cc + DEPS + stats) +# why can't I just declare googleapis-c++::foo_bar ? most likely my exporting +# was messed up... +find_package(googleapis CONFIG REQUIRED googleapis_cpp_monitoring_v3_protos) -# why can't I just declare googleapis-c++::foo_bar ? most likely my exporting was messed up... -find_package( googleapis CONFIG REQUIRED googleapis_cpp_monitoring_v3_protos ) +get_target_property(_iface_incdir googleapis_cpp_monitoring_v3_protos + INTERFACE_INCLUDE_DIRECTORIES) +target_include_directories(opencensus_exporters_stats_stackdriver + PRIVATE $) +target_include_directories(opencensus_exporters_stats_stackdriver_utils + PRIVATE $) -get_target_property( _iface_incdir googleapis_cpp_monitoring_v3_protos INTERFACE_INCLUDE_DIRECTORIES ) -target_include_directories( opencensus_exporters_stats_stackdriver PRIVATE $ ) -target_include_directories( opencensus_exporters_stats_stackdriver_utils PRIVATE $ ) +target_link_libraries(opencensus_exporters_stats_stackdriver + PUBLIC googleapis_cpp_monitoring_v3_protos) +target_link_libraries(opencensus_exporters_stats_stackdriver_utils + PUBLIC googleapis_cpp_monitoring_v3_protos) -target_link_libraries( opencensus_exporters_stats_stackdriver PUBLIC googleapis_cpp_monitoring_v3_protos ) -target_link_libraries( opencensus_exporters_stats_stackdriver_utils PUBLIC googleapis_cpp_monitoring_v3_protos ) +get_target_property(_gmock_iface_incdir gmock INTERFACE_INCLUDE_DIRECTORIES) +target_include_directories(opencensus_time_series_matcher + PUBLIC $) +target_link_libraries(opencensus_time_series_matcher PUBLIC gmock) -get_target_property( _gmock_iface_incdir gmock INTERFACE_INCLUDE_DIRECTORIES ) -target_include_directories( opencensus_time_series_matcher PUBLIC $ ) -target_link_libraries ( opencensus_time_series_matcher PUBLIC gmock ) +opencensus_test( + exporters_stats_stackdriver_utils_test internal/stackdriver_utils_test.cc + exporters_stats_stackdriver_utils time_series_matcher stats stats_test_utils) -opencensus_test(exporters_stats_stackdriver_utils_test - internal/stackdriver_utils_test.cc - exporters_stats_stackdriver_utils - time_series_matcher - stats - stats_test_utils -) - -opencensus_test(exporters_stats_stackdriver_e2e_test - internal/stackdriver_e2e_test.cc - exporters_stats_stackdriver - exporters_stats_stackdriver_utils - time_series_matcher - stats -) +opencensus_test( + exporters_stats_stackdriver_e2e_test internal/stackdriver_e2e_test.cc + exporters_stats_stackdriver exporters_stats_stackdriver_utils + time_series_matcher stats) diff --git a/opencensus/exporters/trace/CMakeLists.txt b/opencensus/exporters/trace/CMakeLists.txt index 529c9815..5e51bd1e 100644 --- a/opencensus/exporters/trace/CMakeLists.txt +++ b/opencensus/exporters/trace/CMakeLists.txt @@ -12,14 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -if ( BUILD_STACKDRIVER_EXPORTER ) - find_package( googleapis COMPONENTS googleapis_cpp_devtools_cloudtrace_v2_protos ) - if ( googleapis_FOUND ) +if(BUILD_STACKDRIVER_EXPORTER) + find_package(googleapis + COMPONENTS googleapis_cpp_devtools_cloudtrace_v2_protos) + if(googleapis_FOUND) add_subdirectory(stackdriver) endif() -endif( BUILD_STACKDRIVER_EXPORTER ) +endif(BUILD_STACKDRIVER_EXPORTER) add_subdirectory(stdout) diff --git a/opencensus/exporters/trace/stackdriver/CMakeLists.txt b/opencensus/exporters/trace/stackdriver/CMakeLists.txt index fc1472b7..0023065d 100644 --- a/opencensus/exporters/trace/stackdriver/CMakeLists.txt +++ b/opencensus/exporters/trace/stackdriver/CMakeLists.txt @@ -1,24 +1,26 @@ -opencensus_lib( exporters_trace_stackdriver - PUBLIC - HDRS - stackdriver_exporter.h - SRCS - internal/stackdriver_exporter.cc - DEPS - common_hostname - common_grpc_status - common_grpc_with_user_agent - trace - ) +opencensus_lib( + exporters_trace_stackdriver + PUBLIC + HDRS + stackdriver_exporter.h + SRCS + internal/stackdriver_exporter.cc + DEPS + common_hostname + common_grpc_status + common_grpc_with_user_agent + trace) -find_package( googleapis CONFIG REQUIRED googleapis_cpp_devtools_cloudtrace_v2_protos ) +find_package(googleapis CONFIG REQUIRED + googleapis_cpp_devtools_cloudtrace_v2_protos) -#get_target_property(_iface_incdir googleapis_cpp_devtools_cloudtrace_v2_protos INTERFACE_INCLUDE_DIRECTORIES ) +# get_target_property(_iface_incdir googleapis_cpp_devtools_cloudtrace_v2_protos +# INTERFACE_INCLUDE_DIRECTORIES ) # -#target_include_directories( opencensus_exporters_trace_stackdriver PRIVATE ${_iface_incdir}) -target_link_libraries( opencensus_exporters_trace_stackdriver - PUBLIC - googleapis_cpp_devtools_cloudtrace_v2_trace_protos - googleapis_cpp_devtools_cloudtrace_v2_tracing_protos - googleapis_cpp_rpc_status_protos ) - +# target_include_directories( opencensus_exporters_trace_stackdriver PRIVATE +# ${_iface_incdir}) +target_link_libraries( + opencensus_exporters_trace_stackdriver + PUBLIC googleapis_cpp_devtools_cloudtrace_v2_trace_protos + googleapis_cpp_devtools_cloudtrace_v2_tracing_protos + googleapis_cpp_rpc_status_protos) diff --git a/opencensus/stats/CMakeLists.txt b/opencensus/stats/CMakeLists.txt index 70a64c44..0089c1ee 100644 --- a/opencensus/stats/CMakeLists.txt +++ b/opencensus/stats/CMakeLists.txt @@ -32,8 +32,7 @@ opencensus_lib( view_descriptor.h DEPS stats_core - stats_recording - ) + stats_recording) opencensus_lib( stats_test_utils diff --git a/opencensus/trace/CMakeLists.txt b/opencensus/trace/CMakeLists.txt index 9bd3048d..93559b00 100644 --- a/opencensus/trace/CMakeLists.txt +++ b/opencensus/trace/CMakeLists.txt @@ -167,13 +167,13 @@ opencensus_lib( # Tests # ---------------------------------------------------------------------- -opencensus_test(trace_annotation_test internal/annotation_test.cc trace context ) +opencensus_test(trace_annotation_test internal/annotation_test.cc trace context) opencensus_test(trace_attribute_value_ref_test - internal/attribute_value_ref_test.cc trace context ) + internal/attribute_value_ref_test.cc trace context) opencensus_test(trace_attribute_value_test internal/attribute_value_test.cc - trace context ) + trace context) opencensus_test(trace_b3_test internal/b3_test.cc trace_b3) @@ -186,24 +186,36 @@ opencensus_test(trace_context_util_test internal/context_util_test.cc trace opencensus_test(trace_grpc_trace_bin_test internal/grpc_trace_bin_test.cc trace_grpc_trace_bin) -opencensus_test(trace_link_test internal/link_test.cc trace context ) +opencensus_test(trace_link_test internal/link_test.cc trace context) opencensus_test(trace_local_span_store_test internal/local_span_store_test.cc context trace absl::memory absl::synchronization) opencensus_test( - trace_running_span_store_test internal/running_span_store_test.cc context trace - absl::base absl::memory absl::synchronization) + trace_running_span_store_test + internal/running_span_store_test.cc + context + trace + absl::base + absl::memory + absl::synchronization) -opencensus_test(trace_sampler_test internal/sampler_test.cc context trace absl::strings - absl::synchronization absl::time) +opencensus_test( + trace_sampler_test + internal/sampler_test.cc + context + trace + absl::strings + absl::synchronization + absl::time) -opencensus_test(trace_span_test internal/span_test.cc context trace absl::strings) +opencensus_test(trace_span_test internal/span_test.cc context trace + absl::strings) opencensus_test(trace_span_id_test internal/span_id_test.cc context trace) -opencensus_test(trace_span_options_test internal/span_options_test.cc context trace - absl::strings absl::synchronization) +opencensus_test(trace_span_options_test internal/span_options_test.cc context + trace absl::strings absl::synchronization) opencensus_test(trace_span_context_test internal/span_context_test.cc trace_span_context absl::strings absl::span) @@ -218,12 +230,14 @@ opencensus_test( absl::synchronization absl::time) -opencensus_test(trace_status_test internal/status_test.cc context trace absl::strings) +opencensus_test(trace_status_test internal/status_test.cc context trace + absl::strings) -opencensus_test(trace_trace_config_test internal/trace_config_test.cc context trace - absl::time) +opencensus_test(trace_trace_config_test internal/trace_config_test.cc context + trace absl::time) -opencensus_test(trace_trace_options_test internal/trace_options_test.cc context trace) +opencensus_test(trace_trace_options_test internal/trace_options_test.cc context + trace) opencensus_test(trace_trace_context_test internal/trace_context_test.cc trace_trace_context) diff --git a/tools/docker-format/Dockerfile b/tools/docker-format/Dockerfile index 29bc871b..d3679723 100644 --- a/tools/docker-format/Dockerfile +++ b/tools/docker-format/Dockerfile @@ -1,5 +1,9 @@ FROM ubuntu:cosmic +RUN perl -pi -e 's@//archive@//old-releases@g' /etc/apt/sources.list +RUN perl -pi -e 's@^(.*security.*)$@#$1@g' /etc/apt/sources.list +RUN cat /etc/apt/sources.list + RUN apt update && \ apt install -y clang-format golang git python-pip && \ go get -v github.com/bazelbuild/buildtools/buildifier && \ From c1fb5294e3dc75a32717a12cb6895f7b94551af9 Mon Sep 17 00:00:00 2001 From: Anthony Lichnewsky Date: Sat, 20 Jun 2020 00:27:59 -0700 Subject: [PATCH 12/12] WIP : obviously, still no good fix for formatting docker image. one last file to add --- cmake/OpenCensusHelpers.cmake | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmake/OpenCensusHelpers.cmake b/cmake/OpenCensusHelpers.cmake index a9395dd8..4a1ee989 100644 --- a/cmake/OpenCensusHelpers.cmake +++ b/cmake/OpenCensusHelpers.cmake @@ -120,8 +120,7 @@ function(opencensus_lib NAME) EXPORT opencensus-cpp-targets RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - # PUBLIC_HEADER DESTINATION + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" # PUBLIC_HEADER DESTINATION # "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" ) elseif(ARG_PRIVATE) @@ -147,8 +146,7 @@ function(opencensus_lib NAME) EXPORT opencensus-cpp-targets RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - # PUBLIC_HEADER DESTINATION + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" # PUBLIC_HEADER DESTINATION # "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}" )