From 465158c75c30b2750da713e5ec05b1c0064790ea Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Mon, 25 Apr 2022 23:48:02 +0800 Subject: [PATCH] Merge `main` into `async-changes` (#1348) * install sdk config (#1273) * Bump actions/cache from 2 to 3 (#1277) * Add owent as an Approver (#1276) * add owent as reviewer * fix order * Disable benchmark action failure (#1284) * metrics exemplar round 1 (#1264) * [Metrics SDK] - fix spelling (AggregationTemporarily to AggregationTemporality) (#1288) * fix compilation error with protobuf 3.5 (#1289) * Fix span SetAttribute crash (#1283) * Synchronous Metric collection (Delta , Cumulative) (#1265) * Rename `http_client_curl` to `opentelemetry_http_client_curl` (#1301) Signed-off-by: owent * Don't show coverage annotation for pull requests (#1304) * Implement periodic exporting metric reader (#1286) * Add `async-changes` branch to pull_request of github action (#1309) Signed-off-by: owent * Add InstrumentationInfo and Resource to the metrics data to be exported. (#1299) * Excempt should be applied on issue instead of PR (#1316) * Bump codecov/codecov-action from 2.1.0 to 3 (#1318) * Move public definitions into `opentelemetry_api`. (#1314) Signed-off-by: owent * Add building test without RTTI (#1294) * Remove implicitly deleted default constructor (#1267) Co-authored-by: Tom Tan Co-authored-by: Lalit Kumar Bhasin * [ETW Exporter] - ETW provider handle cleanup (#1322) * Bump actions/stale from 4 to 5 (#1323) * ostream metrics example (#1312) * Prepare v1.3.0 release (#1324) * Update yield logic for ARM processor (#1325) * Fix for #1292 (#1326) * Implement Merge and Diff operation for Histogram Aggregation (#1303) * fix metrics compiler warnings (#1328) * Replace deprecated googletest API (#1327) * Remove redundant tail / in CMake install (#1329) * dependencies image as artifact (#1333) Co-authored-by: Lalit Kumar Bhasin * metrics histogram example (#1330) * Link `opentelemetry_ext` with `opentelemetry_api` (#1336) * ostream metrics cmake (#1344) * Fix conflicts Signed-off-by: owent * Using clang-format-10 to format codes(clang-format-14 has a different output) Signed-off-by: owent Co-authored-by: Ehsan Saei <71217171+esigo@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Lalit Kumar Bhasin Co-authored-by: Tom Tan Co-authored-by: Ben Landrum <71329856+benlandrum@users.noreply.github.com> Co-authored-by: jmanjon <67091862+juandemanjon@users.noreply.github.com> --- .github/workflows/ci.yml | 22 ++ .github/workflows/dependencies_image.yml | 46 +++ .github/workflows/stale.yml | 2 +- CHANGELOG.md | 47 ++- CMakeLists.txt | 51 +--- api/CMakeLists.txt | 33 ++- api/include/opentelemetry/common/macros.h | 16 + .../opentelemetry/common/spin_lock_mutex.h | 4 +- .../opentelemetry/context/runtime_context.h | 2 - api/include/opentelemetry/version.h | 2 +- api/test/common/spinlock_benchmark.cc | 2 +- ci/README.md | 2 + ci/do_ci.sh | 12 +- ci/setup_grpc.sh | 10 +- ci/setup_thrift.sh | 15 +- docker/Dockerfile.debian.deps | 43 +++ docs/public/conf.py | 2 +- examples/CMakeLists.txt | 4 +- examples/common/CMakeLists.txt | 1 + examples/common/metrics_foo_library/BUILD | 17 ++ .../common/metrics_foo_library/CMakeLists.txt | 2 + .../common/metrics_foo_library/foo_library.cc | 60 ++++ .../common/metrics_foo_library/foo_library.h | 14 + examples/metrics_simple/BUILD | 15 + examples/metrics_simple/CMakeLists.txt | 17 +- examples/metrics_simple/metrics_ostream.cc | 102 +++++++ examples/multi_processor/main.cc | 12 +- exporters/elasticsearch/CMakeLists.txt | 2 +- exporters/etw/CMakeLists.txt | 2 +- .../opentelemetry/exporters/etw/etw_logger.h | 2 +- .../exporters/etw/etw_provider.h | 8 +- .../opentelemetry/exporters/etw/etw_tracer.h | 4 +- .../opentelemetry/exporters/etw/utils.h | 17 +- exporters/etw/test/etw_provider_test.cc | 6 +- exporters/jaeger/CMakeLists.txt | 2 +- exporters/memory/CMakeLists.txt | 2 +- .../memory/in_memory_span_exporter.h | 2 +- exporters/ostream/BUILD | 59 ++-- exporters/ostream/CMakeLists.txt | 35 ++- .../exporters/ostream/metric_exporter.h | 10 +- exporters/ostream/src/metric_exporter.cc | 90 +++--- exporters/ostream/test/ostream_metric_test.cc | 278 ++++++++++-------- exporters/otlp/CMakeLists.txt | 2 +- exporters/prometheus/CMakeLists.txt | 2 +- exporters/zipkin/CMakeLists.txt | 2 +- ext/CMakeLists.txt | 1 + ext/src/http/client/nosend/CMakeLists.txt | 2 +- ext/src/zpages/CMakeLists.txt | 5 +- sdk/CMakeLists.txt | 14 +- .../opentelemetry/sdk/_metrics/controller.h | 6 + .../sdk/_metrics/sync_instruments.h | 14 + .../sdk/_metrics/ungrouped_processor.h | 56 ++++ .../aggregation/histogram_aggregation.h | 39 +++ .../sdk/metrics/metric_exporter.h | 1 + .../sdk/metrics/view/instrument_selector.h | 2 +- .../opentelemetry/sdk/version/version.h | 2 +- sdk/src/_metrics/meter.cc | 14 +- .../aggregation/histogram_aggregation.cc | 33 ++- sdk/src/metrics/meter.cc | 2 +- sdk/src/version/version.cc | 2 +- sdk/test/_metrics/metric_instrument_test.cc | 126 ++++---- sdk/test/metrics/aggregation_test.cc | 64 +++- .../metrics/attributes_hashmap_benchmark.cc | 5 +- sdk/test/metrics/attributes_hashmap_test.cc | 3 +- sdk/test/metrics/meter_provider_sdk_test.cc | 2 +- sdk/test/metrics/sync_metric_storage_test.cc | 16 +- sdk/test/trace/tracer_provider_test.cc | 6 +- sdk/test/trace/tracer_test.cc | 4 +- tools/build.cmd | 2 +- 69 files changed, 1094 insertions(+), 407 deletions(-) create mode 100644 .github/workflows/dependencies_image.yml create mode 100644 docker/Dockerfile.debian.deps create mode 100644 examples/common/metrics_foo_library/BUILD create mode 100644 examples/common/metrics_foo_library/CMakeLists.txt create mode 100644 examples/common/metrics_foo_library/foo_library.cc create mode 100644 examples/common/metrics_foo_library/foo_library.h create mode 100644 examples/metrics_simple/metrics_ostream.cc diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b0af15acb..7030c719d5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -216,6 +216,28 @@ jobs: - name: run tests run: ./ci/do_ci.sh bazel.noexcept + bazel_nortti: + name: Bazel nortti + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: 'recursive' + - name: Mount Bazel Cache + uses: actions/cache@v3 + env: + cache-name: bazel_cache + with: + path: /home/runner/.cache/bazel + key: bazel_nortti + - name: setup + run: | + sudo ./ci/setup_thrift.sh dependencies_only + sudo ./ci/setup_ci_environment.sh + sudo ./ci/install_bazelisk.sh + - name: run tests + run: ./ci/do_ci.sh bazel.nortti + bazel_asan: name: Bazel asan config runs-on: ubuntu-latest diff --git a/.github/workflows/dependencies_image.yml b/.github/workflows/dependencies_image.yml new file mode 100644 index 0000000000..aa7d923071 --- /dev/null +++ b/.github/workflows/dependencies_image.yml @@ -0,0 +1,46 @@ +name: 'OpenTelemetry-cpp dependencies image' +on: + schedule: + - cron: "0 3 * * 6" + +jobs: + docker_image: + name: Docker Image + runs-on: ubuntu-latest + timeout-minutes: 300 + steps: + - + name: checkout + uses: actions/checkout@v2 + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + - + name: Build Image + uses: docker/build-push-action@v2 + with: + builder: ${{ steps.buildx.outputs.name }} + context: ci/ + file: ./docker/Dockerfile + build-args: BASE_IMAGE=ubuntu:latest + platforms: linux/amd64 + # platforms: linux/amd64,linux/arm64 + push: false + tags: otel-cpp-deps + load: true + - + name: Save Image + run: | + docker images + docker save -o /opt/otel-cpp-deps-debian.tar otel-cpp-deps + - + name: Upload Image + uses: actions/upload-artifact@v3 + with: + name: otel-cpp-deps + path: /opt/otel-cpp-deps-debian.tar + retention-days: 14 diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index e55a93d08d..ff3e02df62 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -7,7 +7,7 @@ jobs: stale: runs-on: ubuntu-latest steps: - - uses: actions/stale@v4 + - uses: actions/stale@v5 with: stale-issue-message: 'This issue was marked as stale due to lack of activity. It will be closed in 7 days if no furthur activity occurs.' close-issue-message: 'Closed as inactive. Feel free to reopen if this is still an issue.' diff --git a/CHANGELOG.md b/CHANGELOG.md index 0eac7fa35a..ffc68d6a1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,9 +15,54 @@ Increment the: ## [Unreleased] +* [EXPORTER] OTLP http exporter allow concurrency session ([#1209](https://github.com/open-telemetry/opentelemetry-cpp/pull/1209)) + +## [1.3.0] 2022-04-11 + +* [ETW EXPORTER] ETW provider handle cleanup ([#1322](https://github.com/open-telemetry/opentelemetry-cpp/pull/1322)) +* [BUILD] Move public definitions into `opentelemetry_api`. ([#1314](https://github.com/open-telemetry/opentelemetry-cpp/pull/1314)) +* [METRICS] OStream example ([#1312](https://github.com/open-telemetry/opentelemetry-cpp/pull/1312)) +* [BUILD] Rename `http_client_curl` to `opentelemetry_http_client_curl` ([#1301](https://github.com/open-telemetry/opentelemetry-cpp/pull/1301)) +* [METRICS SDK] Add InstrumentationInfo and Resource to the metrics data to be + exported. + ([#1299](https://github.com/open-telemetry/opentelemetry-cpp/pull/1299)) +* [TESTS] Add building test without RTTI ([#1294](https://github.com/open-telemetry/opentelemetry-cpp/pull/1294)) +* [METRICS SDK] Implement periodic exporting metric reader ([#1286](https://github.com/open-telemetry/opentelemetry-cpp/pull/1286)) * [SDK] Bugfix: span SetAttribute crash ([#1283](https://github.com/open-telemetry/opentelemetry-cpp/pull/1283)) +* [BUG] Remove implicitly deleted default constructor ([#1267](https://github.com/open-telemetry/opentelemetry-cpp/pull/1267)) +* [METRICS SDK] Synchronous Metric collection (Delta , Cumulative) ([#1265](https://github.com/open-telemetry/opentelemetry-cpp/pull/1265)) +* [METRICS SDK] Metrics exemplar round 1 ([#1264](https://github.com/open-telemetry/opentelemetry-cpp/pull/1264)) +* [EXPORTER] Fix: use CURLOPT_TIMEOUT_MS to config OtlpHttpExporter's timeout + instead of CURLOPT_TIMEOUT + ([#1261](https://github.com/open-telemetry/opentelemetry-cpp/pull/1261)) * [EXPORTER] Jaeger Exporter - Populate Span Links ([#1251](https://github.com/open-telemetry/opentelemetry-cpp/pull/1251)) -* [EXPORTER] OTLP http exporter allow concurrency session ([#1209](https://github.com/open-telemetry/opentelemetry-cpp/pull/1209)) +* [SDK] Reorder the destructor of members in LoggerProvider and TracerProvider ([#1245](https://github.com/open-telemetry/opentelemetry-cpp/pull/1245)) +* [METRICS SDK] Enable metric collection from MetricReader ([#1241](https://github.com/open-telemetry/opentelemetry-cpp/pull/1241)) +* [METRICS SDK] Asynchronous Aggregation storage ([#1232](https://github.com/open-telemetry/opentelemetry-cpp/pull/1232)) +* [METRICS SDK] Synchronous Instruments - Aggregation Storage(s) creation for + configured views + ([#1219](https://github.com/open-telemetry/opentelemetry-cpp/pull/1219)) +* [BUILD] Added s390x arch into CMake build. ([#1216](https://github.com/open-telemetry/opentelemetry-cpp/pull/1216)) +* [API] Allow extension of the lifetime of ContextStorage. ([#1214](https://github.com/open-telemetry/opentelemetry-cpp/pull/1214)) +* [METRICS SDK] Add Aggregation storage ([#1213](https://github.com/open-telemetry/opentelemetry-cpp/pull/1213)) +* [TESTS] Fix ostream_log_test Mac ([#1208](https://github.com/open-telemetry/opentelemetry-cpp/pull/1208)) +* [BUILD] Update grpc to v1.43.2 to support VS2022/MSVC 19.30 and bazel 5.0 ([#1207](https://github.com/open-telemetry/opentelemetry-cpp/pull/1207)) +* [DOCS] Benchmark documentation ([#1205](https://github.com/open-telemetry/opentelemetry-cpp/pull/1205)) +* [DOCS] Fix errors in SDK documentation ([#1201](https://github.com/open-telemetry/opentelemetry-cpp/pull/1201)) +* [METRICS EXPORTER] Ostream metric exporter ([#1196](https://github.com/open-telemetry/opentelemetry-cpp/pull/1196)) +* [Metrics SDK] Filtering metrics attributes ([#1191](https://github.com/open-telemetry/opentelemetry-cpp/pull/1191)) +* [Metrics SDK] Sync and Async Instruments SDK ([#1184](https://github.com/open-telemetry/opentelemetry-cpp/pull/1184)) +* [Metrics SDK] Add Aggregation as part of metrics SDK. ([#1178](https://github.com/open-telemetry/opentelemetry-cpp/pull/1178)) +* [BUILD] Cmake: thrift requires boost headers, include them as + Boost_INCLUDE_DIRS + ([#1100](https://github.com/open-telemetry/opentelemetry-cpp/pull/1100)) + +Notes: + +[#1301](https://github.com/open-telemetry/opentelemetry-cpp/pull/1301) added +`opentelemetry_` as prefix to http_client_curl library for resolving potential +naming conflict, this could break existing cmake build if http_client_curl is +listed as explicit dependency in user's cmake file. ## [1.2.0] 2022-01-31 diff --git a/CMakeLists.txt b/CMakeLists.txt index efc63249e1..128b7bf132 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,24 +125,11 @@ if(NOT DEFINED CMAKE_CXX_STANDARD) endif() endif() -if(WITH_ABSEIL) - find_package(absl CONFIG REQUIRED) - - set(CORE_RUNTIME_LIBS absl::bad_variant_access absl::any absl::base - absl::bits absl::city) - - # target_link_libraries(main PRIVATE absl::any absl::base absl::bits - # absl::city) -endif() - if(WITH_STL) # These definitions are needed for test projects that do not link against # opentelemetry-api library directly. We ensure that variant implementation # (absl::variant or std::variant) in variant unit test code is consistent with - # the global project build definitions. - add_definitions(-DHAVE_CPP_STDLIB) - - # Optimize for speed to reduce the hops + # the global project build definitions. Optimize for speed to reduce the hops if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(CMAKE_BUILD_TYPE MATCHES Debug) # Turn off optimizations for DEBUG @@ -157,20 +144,6 @@ if(WITH_STL) endif() endif() -if(WITH_GSL) - add_definitions(-DHAVE_GSL) - - # Guidelines Support Library path. Used if we are not on not get C++20. - # - find_package(Microsoft.GSL QUIET) - if(TARGET Microsoft.GSL::GSL) - list(APPEND CORE_RUNTIME_LIBS Microsoft.GSL::GSL) - else() - set(GSL_DIR third_party/ms-gsl) - include_directories(${GSL_DIR}/include) - endif() -endif() - option(WITH_OTLP "Whether to include the OpenTelemetry Protocol in the SDK" OFF) option(WITH_ZIPKIN "Whether to include the Zipkin exporter in the SDK" OFF) @@ -190,12 +163,7 @@ option(BUILD_TESTING "Whether to enable tests" ON) option(BUILD_W3CTRACECONTEXT_TEST "Whether to build w3c trace context" OFF) -if(WITH_NO_GENENV) - add_definitions(-DNO_GETENV) -endif() - if(WIN32) - add_definitions(-DNOMINMAX) if(BUILD_TESTING) if(MSVC) # GTest bug: https://github.com/google/googletest/issues/860 @@ -203,9 +171,6 @@ if(WIN32) endif() endif() option(WITH_ETW "Whether to include the ETW Exporter in the SDK" ON) - if(WITH_ETW) - add_definitions(-DHAVE_MSGPACK) - endif(WITH_ETW) endif(WIN32) option( @@ -215,23 +180,9 @@ option( option(WITH_EXAMPLES "Whether to build examples" ON) option(WITH_METRICS_PREVIEW "Whether to build metrics preview" OFF) - -if(WITH_METRICS_PREVIEW) - add_definitions(-DENABLE_METRICS_PREVIEW) -endif() - option(WITH_LOGS_PREVIEW "Whether to build logs preview" OFF) - -if(WITH_LOGS_PREVIEW) - add_definitions(-DENABLE_LOGS_PREVIEW) -endif() - option(WITH_ASYNC_EXPORT_PREVIEW "Whether enable async export" OFF) -if(WITH_ASYNC_EXPORT_PREVIEW) - add_definitions(-DENABLE_ASYNC_EXPORT) -endif() - find_package(Threads) function(install_windows_deps) diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index 17ad746fff..a87bf54cb3 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -42,7 +42,14 @@ if(BUILD_TESTING) endif() if(WITH_ABSEIL) + + find_package(absl CONFIG REQUIRED) + target_compile_definitions(opentelemetry_api INTERFACE HAVE_ABSEIL) + target_link_libraries( + opentelemetry_api INTERFACE absl::bad_variant_access absl::any absl::base + absl::bits absl::city) + endif() if(WITH_STL) @@ -52,6 +59,21 @@ else() message("Building with nostd types...") endif() +if(WITH_GSL) + target_compile_definitions(opentelemetry_api INTERFACE HAVE_GSL) + + # Guidelines Support Library path. Used if we are not on not get C++20. + # + find_package(Microsoft.GSL QUIET) + if(TARGET Microsoft.GSL::GSL) + target_link_libraries(opentelemetry_api INTERFACE Microsoft.GSL::GSL) + else() + set(GSL_DIR third_party/ms-gsl) + target_include_directories( + opentelemetry_api INTERFACE "$") + endif() +endif() + if(WITH_METRICS_PREVIEW) target_compile_definitions(opentelemetry_api INTERFACE ENABLE_METRICS_PREVIEW) endif() @@ -60,8 +82,15 @@ if(WITH_LOGS_PREVIEW) target_compile_definitions(opentelemetry_api INTERFACE ENABLE_LOGS_PREVIEW) endif() -if(CORE_RUNTIME_LIBS) - target_link_libraries(opentelemetry_api INTERFACE ${CORE_RUNTIME_LIBS}) +if(WITH_NO_GENENV) + target_compile_definitions(opentelemetry_api INTERFACE NO_GETENV) +endif() + +if(WIN32) + target_compile_definitions(opentelemetry_api INTERFACE NOMINMAX) + if(WITH_ETW) + target_compile_definitions(opentelemetry_api INTERFACE HAVE_MSGPACK) + endif() endif() if(WITH_ASYNC_EXPORT_PREVIEW) diff --git a/api/include/opentelemetry/common/macros.h b/api/include/opentelemetry/common/macros.h index 8c2b6bd175..982e368ce3 100644 --- a/api/include/opentelemetry/common/macros.h +++ b/api/include/opentelemetry/common/macros.h @@ -24,3 +24,19 @@ #else # define OPENTELEMETRY_MAYBE_UNUSED #endif + +#ifndef OPENTELEMETRY_RTTI_ENABLED +# if defined(__clang__) +# if __has_feature(cxx_rtti) +# define OPENTELEMETRY_RTTI_ENABLED +# endif +# elif defined(__GNUG__) +# if defined(__GXX_RTTI) +# define OPENTELEMETRY_RTTI_ENABLED +# endif +# elif defined(_MSC_VER) +# if defined(_CPPRTTI) +# define OPENTELEMETRY_RTTI_ENABLED +# endif +# endif +#endif diff --git a/api/include/opentelemetry/common/spin_lock_mutex.h b/api/include/opentelemetry/common/spin_lock_mutex.h index 9c51857567..764b5fc6b8 100644 --- a/api/include/opentelemetry/common/spin_lock_mutex.h +++ b/api/include/opentelemetry/common/spin_lock_mutex.h @@ -71,9 +71,7 @@ class SpinLockMutex __builtin_ia32_pause(); # endif #elif defined(__arm__) - // This intrinsic should fail to be found if YIELD is not supported on the current - // processor. - __yield(); + __asm__ volatile("yield" ::: "memory"); #else // TODO: Issue PAGE/YIELD on other architectures. #endif diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index fbcf425c2c..74fa2151ea 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -22,8 +22,6 @@ class Token private: friend class RuntimeContextStorage; - Token() noexcept = default; - // A constructor that sets the token's Context object to the // one that was passed in. Token(const Context &context) : context_(context) {} diff --git a/api/include/opentelemetry/version.h b/api/include/opentelemetry/version.h index 2fe333f3d4..1e01e242ed 100644 --- a/api/include/opentelemetry/version.h +++ b/api/include/opentelemetry/version.h @@ -6,7 +6,7 @@ #include "opentelemetry/detail/preprocessor.h" #define OPENTELEMETRY_ABI_VERSION_NO 1 -#define OPENTELEMETRY_VERSION "1.2.0" +#define OPENTELEMETRY_VERSION "1.3.0" #define OPENTELEMETRY_ABI_VERSION OPENTELEMETRY_STRINGIFY(OPENTELEMETRY_ABI_VERSION_NO) // clang-format off diff --git a/api/test/common/spinlock_benchmark.cc b/api/test/common/spinlock_benchmark.cc index 28692bee56..07579579c7 100644 --- a/api/test/common/spinlock_benchmark.cc +++ b/api/test/common/spinlock_benchmark.cc @@ -93,7 +93,7 @@ static void BM_ProcYieldSpinLockThrashing(benchmark::State &s) __builtin_ia32_pause(); # endif #elif defined(__arm__) - __yield(); + __asm__ volatile("yield" ::: "memory"); #endif } }, diff --git a/ci/README.md b/ci/README.md index d315965962..14a6107eb8 100644 --- a/ci/README.md +++ b/ci/README.md @@ -12,6 +12,8 @@ CI tests can be run on docker by invoking the script `./ci/run_docker.sh * `bazel.legacy.test`: build bazel targets and run tests for the targets meant to work with older compilers. * `bazel.noexcept`: build bazel targets and run tests with exceptions disabled. +* `bazel.nortti`: build bazel targets and run tests with runtime type + identification disabled. * `bazel.asan`: build bazel targets and run tests with AddressSanitizer. * `bazel.tsan`: build bazel targets and run tests with ThreadSanitizer. * `bazel.valgrind`: build bazel targets and run tests under the valgrind memory diff --git a/ci/do_ci.sh b/ci/do_ci.sh index f9fc8ed215..008e983842 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -59,7 +59,11 @@ mkdir -p "${BUILD_DIR}" [ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin mkdir -p "${PLUGIN_DIR}" -BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST" +BAZEL_OPTIONS="--copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST" +# Previous legacy metrics use virtual drive, which can not be used without RTTI +if [[ "$1" != "bazel.nortti" ]]; then + BAZEL_OPTIONS="$BAZEL_OPTIONS --copt=-DENABLE_METRICS_PREVIEW" +fi BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" # https://github.com/bazelbuild/bazel/issues/4341 @@ -221,6 +225,12 @@ elif [[ "$1" == "bazel.noexcept" ]]; then bazel $BAZEL_STARTUP_OPTIONS build --copt=-fno-exceptions --build_tag_filters=-jaeger $BAZEL_OPTIONS -- //... -//exporters/prometheus/... -//exporters/jaeger/... bazel $BAZEL_STARTUP_OPTIONS test --copt=-fno-exceptions --build_tag_filters=-jaeger $BAZEL_TEST_OPTIONS -- //... -//exporters/prometheus/... -//exporters/jaeger/... exit 0 +elif [[ "$1" == "bazel.nortti" ]]; then + # there are some exceptions and error handling code from the Prometheus and Jaeger Clients + # that make this test always fail. ignore Prometheus and Jaeger exporters in the noexcept here. + bazel $BAZEL_STARTUP_OPTIONS build --cxxopt=-fno-rtti --build_tag_filters=-jaeger $BAZEL_OPTIONS -- //... -//exporters/prometheus/... -//exporters/jaeger/... + bazel $BAZEL_STARTUP_OPTIONS test --cxxopt=-fno-rtti --build_tag_filters=-jaeger $BAZEL_TEST_OPTIONS -- //... -//exporters/prometheus/... -//exporters/jaeger/... + exit 0 elif [[ "$1" == "bazel.asan" ]]; then bazel $BAZEL_STARTUP_OPTIONS test --config=asan $BAZEL_TEST_OPTIONS //... exit 0 diff --git a/ci/setup_grpc.sh b/ci/setup_grpc.sh index 2995f7b4ba..d42fbb7d2e 100755 --- a/ci/setup_grpc.sh +++ b/ci/setup_grpc.sh @@ -10,13 +10,17 @@ new_grpc_version='v1.43.2' gcc_version_for_new_grpc='5.1' install_grpc_version=${new_grpc_version} grpc_version='v1.39.0' -usage() { echo "Usage: $0 -v " 1>&2; exit 1; } +install_dir='/usr/local/' +usage() { echo "Usage: $0 [-v ] [-i "] 1>&2; exit 1;} -while getopts ":v:" o; do +while getopts ":v:i:" o; do case "${o}" in v) gcc_version=${OPTARG} ;; + i) + install_dir=${OPTARG} + ;; *) usage ;; @@ -34,7 +38,7 @@ if ! type cmake > /dev/null; then exit 1 fi export BUILD_DIR=/tmp/ -export INSTALL_DIR=/usr/local/ +export INSTALL_DIR=${install_dir} pushd $BUILD_DIR echo "installing grpc version: ${install_grpc_version}" git clone --depth=1 -b ${install_grpc_version} https://github.com/grpc/grpc diff --git a/ci/setup_thrift.sh b/ci/setup_thrift.sh index af8cc3ffc8..98e14d5e71 100755 --- a/ci/setup_thrift.sh +++ b/ci/setup_thrift.sh @@ -4,6 +4,17 @@ set -e export DEBIAN_FRONTEND=noninteractive export THRIFT_VERSION=0.14.1 +install_dir='/usr/local/' +while getopts ":i:" o; do + case "${o}" in + i) + install_dir=${OPTARG} + ;; + *) + ;; + esac +done + apt update if ! type cmake > /dev/null; then @@ -11,7 +22,7 @@ if ! type cmake > /dev/null; then exit 1 fi export BUILD_DIR=/tmp/ -export INSTALL_DIR=/usr/local/ +export INSTALL_DIR=${install_dir} apt install -y --no-install-recommends \ libboost-locale-dev \ @@ -44,6 +55,8 @@ cmake -G Ninja .. \ -DWITH_BOOSTTHREADS=OFF \ -DWITH_BOOST_FUNCTIONAL=OFF \ -DWITH_BOOST_SMART_PTR=OFF \ + -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \ + -DCMAKE_PREFIX_PATH=$INSTALL_DIR \ .. ninja -j $(nproc) diff --git a/docker/Dockerfile.debian.deps b/docker/Dockerfile.debian.deps new file mode 100644 index 0000000000..0634c11970 --- /dev/null +++ b/docker/Dockerfile.debian.deps @@ -0,0 +1,43 @@ +ARG BASE_IMAGE=ubuntu:latest +ARG CORES=${nproc} + +FROM ${BASE_IMAGE} as base + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y build-essential autoconf \ + libtool pkg-config cmake git libssl-dev curl \ + libcurl4-openssl-dev libgtest-dev libgmock-dev libbenchmark-dev + +WORKDIR /work +RUN mkdir -p /opt/otel-cpp + +FROM base as grpc +# install grpc, protobuf and abseil +ARG GRPC_VERSION=1.43.2 + +ADD setup_grpc.sh . +RUN ./setup_grpc.sh -i "/opt/otel-cpp" -v ${GRPC_VERSION} + +FROM base as thrift +RUN apt-get install -y --no-install-recommends wget + +# install thrift +ARG THRIFT_VERSION=0.14.1 +ADD setup_thrift.sh . +RUN ./setup_thrift.sh -i "/opt/otel-cpp" + +FROM scratch as final + +COPY --from=grpc /opt/otel-cpp / +COPY --from=thrift /opt/otel-cpp / + +# how to use: +# +# docker create -ti --name deps otel-cpp-deps bash +# docker cp deps:/ ./ +# docker rm -f deps +# +# or: +# +# COPY --from=otel-cpp-deps /usr diff --git a/docs/public/conf.py b/docs/public/conf.py index cd6c7ec6df..910cfc6346 100644 --- a/docs/public/conf.py +++ b/docs/public/conf.py @@ -21,7 +21,7 @@ author = 'OpenTelemetry authors' # The full version, including alpha/beta/rc tags -release = '1.2.0' +release = '1.3.0' # Run sphinx on subprojects and copy output # ----------------------------------------- diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 70bca10221..db0fe159be 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -18,9 +18,7 @@ endif() add_subdirectory(plugin) add_subdirectory(simple) add_subdirectory(batch) -if(WITH_METRICS_PREVIEW) - add_subdirectory(metrics_simple) -endif() +add_subdirectory(metrics_simple) add_subdirectory(multithreaded) add_subdirectory(multi_processor) add_subdirectory(http) diff --git a/examples/common/CMakeLists.txt b/examples/common/CMakeLists.txt index f89faf4a2f..c2fdca2594 100644 --- a/examples/common/CMakeLists.txt +++ b/examples/common/CMakeLists.txt @@ -2,3 +2,4 @@ add_subdirectory(foo_library) if(WITH_LOGS_PREVIEW) add_subdirectory(logs_foo_library) endif() +add_subdirectory(metrics_foo_library) diff --git a/examples/common/metrics_foo_library/BUILD b/examples/common/metrics_foo_library/BUILD new file mode 100644 index 0000000000..bc84949732 --- /dev/null +++ b/examples/common/metrics_foo_library/BUILD @@ -0,0 +1,17 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "common_metrics_foo_library", + srcs = [ + "foo_library.cc", + ], + hdrs = [ + "foo_library.h", + ], + defines = ["BAZEL_BUILD"], + deps = [ + "//api", + "//sdk:headers", + "//sdk/src/metrics", + ], +) diff --git a/examples/common/metrics_foo_library/CMakeLists.txt b/examples/common/metrics_foo_library/CMakeLists.txt new file mode 100644 index 0000000000..4f2bca2a0b --- /dev/null +++ b/examples/common/metrics_foo_library/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(common_metrics_foo_library foo_library.h foo_library.cc) +target_link_libraries(common_metrics_foo_library PUBLIC opentelemetry_api) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc new file mode 100644 index 0000000000..cd15adad5b --- /dev/null +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -0,0 +1,60 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "foo_library.h" +# include +# include +# include +# include "opentelemetry/metrics/provider.h" + +namespace nostd = opentelemetry::nostd; +namespace metrics_api = opentelemetry::metrics; + +namespace +{ +std::map get_random_attr() +{ + static const std::vector> labels = {{"key1", "value1"}, + {"key2", "value2"}, + {"key3", "value3"}, + {"key4", "value4"}, + {"key5", "value5"}}; + return std::map{labels[rand() % (labels.size() - 1)], + labels[rand() % (labels.size() - 1)]}; +} +} // namespace + +void foo_library::counter_example(const std::string &name) +{ + std::string counter_name = name + "_counter"; + auto provider = metrics_api::Provider::GetMeterProvider(); + nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); + auto double_counter = meter->CreateDoubleCounter(counter_name); + + while (true) + { + double val = (rand() % 700) + 1.1; + double_counter->Add(val); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } +} + +void foo_library::histogram_example(const std::string &name) +{ + std::string histogram_name = name + "_histogram"; + auto provider = metrics_api::Provider::GetMeterProvider(); + nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); + auto histogram_counter = meter->CreateDoubleHistogram(histogram_name); + auto context = opentelemetry::context::Context{}; + while (true) + { + double val = (rand() % 700) + 1.1; + std::map labels = get_random_attr(); + auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; + histogram_counter->Record(val, labelkv, context); + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + } +} + +#endif diff --git a/examples/common/metrics_foo_library/foo_library.h b/examples/common/metrics_foo_library/foo_library.h new file mode 100644 index 0000000000..73cd30d2d5 --- /dev/null +++ b/examples/common/metrics_foo_library/foo_library.h @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include + +class foo_library +{ +public: + static void counter_example(const std::string &name); + static void histogram_example(const std::string &name); +}; +#endif diff --git a/examples/metrics_simple/BUILD b/examples/metrics_simple/BUILD index b845a2f890..95d6490ba2 100644 --- a/examples/metrics_simple/BUILD +++ b/examples/metrics_simple/BUILD @@ -11,3 +11,18 @@ cc_binary( "//sdk/src/_metrics:metrics_deprecated", ], ) + +cc_binary( + name = "metrics_ostream_example", + srcs = [ + "metrics_ostream.cc", + ], + linkopts = ["-pthread"], + tags = ["ostream"], + deps = [ + "//api", + "//examples/common/metrics_foo_library:common_metrics_foo_library", + "//exporters/ostream:ostream_metric_exporter", + "//sdk/src/metrics", + ], +) diff --git a/examples/metrics_simple/CMakeLists.txt b/examples/metrics_simple/CMakeLists.txt index 94b441061b..a2088fd510 100644 --- a/examples/metrics_simple/CMakeLists.txt +++ b/examples/metrics_simple/CMakeLists.txt @@ -1,6 +1,13 @@ include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include) - -add_executable(simple_metrics main.cc) -target_link_libraries( - simple_metrics ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics_deprecated - opentelemetry_exporter_ostream_metrics_deprecated) +if(WITH_METRICS_PREVIEW) + add_executable(simple_metrics main.cc) + target_link_libraries( + simple_metrics ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics_deprecated + opentelemetry_exporter_ostream_metrics_deprecated) +else() + add_executable(metrics_ostream_example metrics_ostream.cc) + target_link_libraries( + metrics_ostream_example ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics + opentelemetry_exporter_ostream_metrics opentelemetry_resources + common_metrics_foo_library) +endif() diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc new file mode 100644 index 0000000000..d0e4dccfeb --- /dev/null +++ b/examples/metrics_simple/metrics_ostream.cc @@ -0,0 +1,102 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include +# include +# include "opentelemetry/exporters/ostream/metric_exporter.h" +# include "opentelemetry/metrics/provider.h" +# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" +# include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" +# include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" +# include "opentelemetry/sdk/metrics/meter.h" +# include "opentelemetry/sdk/metrics/meter_provider.h" + +# ifdef BAZEL_BUILD +# include "examples/common/metrics_foo_library/foo_library.h" +# else +# include "metrics_foo_library/foo_library.h" +# endif + +namespace metric_sdk = opentelemetry::sdk::metrics; +namespace nostd = opentelemetry::nostd; +namespace common = opentelemetry::common; +namespace exportermetrics = opentelemetry::exporter::metrics; +namespace metrics_api = opentelemetry::metrics; + +namespace +{ + +void initMetrics(const std::string &name) +{ + std::unique_ptr exporter{new exportermetrics::OStreamMetricExporter}; + std::vector> exporters; + + std::string version{"1.2.0"}; + std::string schema{"https://opentelemetry.io/schemas/1.2.0"}; + + // Initialize and set the global MeterProvider + metric_sdk::PeriodicExportingMetricReaderOptions options; + options.export_interval_millis = std::chrono::milliseconds(1000); + options.export_timeout_millis = std::chrono::milliseconds(500); + std::unique_ptr reader{ + new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options)}; + auto provider = std::shared_ptr( + new metric_sdk::MeterProvider(std::move(exporters))); + auto p = std::static_pointer_cast(provider); + p->AddMetricReader(std::move(reader)); + + // counter view + std::string counter_name = name + "_counter"; + std::unique_ptr instrument_selector{ + new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, counter_name)}; + std::unique_ptr meter_selector{ + new metric_sdk::MeterSelector(name, version, schema)}; + std::unique_ptr sum_view{ + new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}}; + p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view)); + + // histogram view + std::string histogram_name = name + "_histogram"; + std::unique_ptr histogram_instrument_selector{ + new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)}; + std::unique_ptr histogram_meter_selector{ + new metric_sdk::MeterSelector(name, version, schema)}; + std::unique_ptr histogram_view{ + new metric_sdk::View{name, "description", metric_sdk::AggregationType::kHistogram}}; + p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), + std::move(histogram_view)); + metrics_api::Provider::SetMeterProvider(provider); +} +} // namespace + +int main(int argc, char **argv) +{ + std::string example_type; + if (argc >= 2) + { + example_type = argv[1]; + } + + std::string name{"ostream_metric_example"}; + initMetrics(name); + + if (example_type == "counter") + { + foo_library::counter_example(name); + } + else if (example_type == "histogram") + { + foo_library::histogram_example(name); + } + else + { + std::thread counter_example{&foo_library::counter_example, name}; + std::thread histogram_example{&foo_library::histogram_example, name}; + counter_example.join(); + histogram_example.join(); + } +} +#else +int main() {} +#endif diff --git a/examples/multi_processor/main.cc b/examples/multi_processor/main.cc index f57da5e228..2b722b6688 100644 --- a/examples/multi_processor/main.cc +++ b/examples/multi_processor/main.cc @@ -20,21 +20,19 @@ namespace trace_api = opentelemetry::trace; namespace trace_sdk = opentelemetry::sdk::trace; namespace nostd = opentelemetry::nostd; -InMemorySpanExporter *memory_span_exporter; - namespace { -void initTracer() +InMemorySpanExporter *initTracer() { auto exporter1 = std::unique_ptr( new opentelemetry::exporter::trace::OStreamSpanExporter); auto processor1 = std::unique_ptr( new trace_sdk::SimpleSpanProcessor(std::move(exporter1))); - auto exporter2 = std::unique_ptr(new InMemorySpanExporter()); + InMemorySpanExporter *memory_span_exporter = new InMemorySpanExporter(); + auto exporter2 = std::unique_ptr(memory_span_exporter); // fetch the exporter for dumping data later - memory_span_exporter = dynamic_cast(exporter2.get()); auto processor2 = std::unique_ptr( new trace_sdk::SimpleSpanProcessor(std::move(exporter2))); @@ -44,6 +42,8 @@ void initTracer() provider->AddProcessor(std::move(processor2)); // Set the global trace provider trace_api::Provider::SetTracerProvider(std::move(provider)); + + return memory_span_exporter; } void dumpSpans(std::vector> &spans) @@ -81,7 +81,7 @@ void dumpSpans(std::vector> &spans) int main() { // Removing this line will leave the default noop TracerProvider in place. - initTracer(); + InMemorySpanExporter *memory_span_exporter = initTracer(); foo_library(); auto memory_spans = memory_span_exporter->GetData()->GetSpans(); diff --git a/exporters/elasticsearch/CMakeLists.txt b/exporters/elasticsearch/CMakeLists.txt index 9b2833854e..b21dae400a 100644 --- a/exporters/elasticsearch/CMakeLists.txt +++ b/exporters/elasticsearch/CMakeLists.txt @@ -21,7 +21,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/elasticsearch - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters FILES_MATCHING PATTERN "*.h" PATTERN "es_log_recordable.h" EXCLUDE) diff --git a/exporters/etw/CMakeLists.txt b/exporters/etw/CMakeLists.txt index 0489b41af6..47791b4abf 100644 --- a/exporters/etw/CMakeLists.txt +++ b/exporters/etw/CMakeLists.txt @@ -23,7 +23,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/etw - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters FILES_MATCHING PATTERN "*.h") diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h index 6f3a394b0d..84f6b6970f 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h @@ -110,7 +110,7 @@ class Logger : public opentelemetry::logs::Logger common::SystemTimestamp timestamp) noexcept override { -# ifdef RTTI_ENABLED +# ifdef OPENTELEMETRY_RTTI_ENABLED common::KeyValueIterable &attribs = const_cast(attributes); Properties *evt = dynamic_cast(&attribs); // Properties *res = dynamic_cast(&resr); diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_provider.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_provider.h index 5b00f05574..51cca03a60 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_provider.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_provider.h @@ -203,7 +203,8 @@ class ETWProvider { std::lock_guard lock(m_providerMapLock); - auto m = providers(); + // use reference to provider list, NOT it' copy. + auto &m = providers(); auto it = m.begin(); while (it != m.end()) { @@ -228,7 +229,10 @@ class ETWProvider } it->second.providerHandle = INVALID_HANDLE; - m.erase(it); + if (result == STATUS_OK) + { + m.erase(it); + } } return result; } diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h index a946e70a6f..65fe2770f2 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -377,7 +377,7 @@ class Tracer : public opentelemetry::trace::Tracer const opentelemetry::trace::SpanContextKeyValueIterable &links, const opentelemetry::trace::StartSpanOptions &options = {}) noexcept override { -#ifdef RTTI_ENABLED +#ifdef OPENTELEMETRY_RTTI_ENABLED common::KeyValueIterable &attribs = const_cast(attributes); Properties *evt = dynamic_cast(&attribs); if (evt != nullptr) @@ -531,7 +531,7 @@ class Tracer : public opentelemetry::trace::Tracer common::SystemTimestamp timestamp, const common::KeyValueIterable &attributes) noexcept { -#ifdef RTTI_ENABLED +#ifdef OPENTELEMETRY_RTTI_ENABLED common::KeyValueIterable &attribs = const_cast(attributes); Properties *evt = dynamic_cast(&attribs); if (evt != nullptr) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/utils.h b/exporters/etw/include/opentelemetry/exporters/etw/utils.h index f1f2898954..8b0a015821 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/utils.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/utils.h @@ -12,6 +12,7 @@ #include #include +#include "opentelemetry/common/macros.h" #include "opentelemetry/exporters/etw/uuid.h" #include "opentelemetry/version.h" @@ -25,22 +26,6 @@ # pragma comment(lib, "Ole32.Lib") #endif -#ifndef RTTI_ENABLED -# if defined(__clang__) -# if __has_feature(cxx_rtti) -# define RTTI_ENABLED -# endif -# elif defined(__GNUG__) -# if defined(__GXX_RTTI) -# define RTTI_ENABLED -# endif -# elif defined(_MSC_VER) -# if defined(_CPPRTTI) -# define RTTI_ENABLED -# endif -# endif -#endif - OPENTELEMETRY_BEGIN_NAMESPACE namespace utils diff --git a/exporters/etw/test/etw_provider_test.cc b/exporters/etw/test/etw_provider_test.cc index 433630270f..d5ebbcad43 100644 --- a/exporters/etw/test/etw_provider_test.cc +++ b/exporters/etw/test/etw_provider_test.cc @@ -18,6 +18,7 @@ TEST(ETWProvider, ProviderIsRegisteredSuccessfully) bool registered = etw.is_registered(providerName); ASSERT_TRUE(registered); + etw.close(handle); } TEST(ETWProvider, ProviderIsNotRegisteredSuccessfully) @@ -46,6 +47,7 @@ TEST(ETWProvider, CheckOpenGUIDDataSuccessfully) auto guidStrName = uuid_name.to_string(); ASSERT_STREQ(guidStrHandle.c_str(), guidStrName.c_str()); + etw.close(handle); } TEST(ETWProvider, CheckCloseSuccess) @@ -53,10 +55,10 @@ TEST(ETWProvider, CheckCloseSuccess) std::string providerName = "OpenTelemetry-ETW-Provider"; static ETWProvider etw; - auto handle = etw.open(providerName.c_str()); - + auto handle = etw.open(providerName.c_str(), ETWProvider::EventFormat::ETW_MANIFEST); auto result = etw.close(handle); ASSERT_NE(result, etw.STATUS_ERROR); + ASSERT_FALSE(etw.is_registered(providerName)); } #endif diff --git a/exporters/jaeger/CMakeLists.txt b/exporters/jaeger/CMakeLists.txt index 6d3d8b88fb..a95c0cc941 100644 --- a/exporters/jaeger/CMakeLists.txt +++ b/exporters/jaeger/CMakeLists.txt @@ -52,7 +52,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/jaeger - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters FILES_MATCHING PATTERN "*.h" PATTERN "recordable.h" EXCLUDE) diff --git a/exporters/memory/CMakeLists.txt b/exporters/memory/CMakeLists.txt index a4b8d837fd..d489ec1606 100644 --- a/exporters/memory/CMakeLists.txt +++ b/exporters/memory/CMakeLists.txt @@ -17,7 +17,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/memory - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters FILES_MATCHING PATTERN "*.h") diff --git a/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h b/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h index 8d620d7ee6..2eae4fc3c0 100644 --- a/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h +++ b/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h @@ -54,7 +54,7 @@ class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporte for (auto &recordable : recordables) { auto span = std::unique_ptr( - dynamic_cast(recordable.release())); + static_cast(recordable.release())); if (span != nullptr) { data_->Add(std::move(span)); diff --git a/exporters/ostream/BUILD b/exporters/ostream/BUILD index f74d896344..cca74d6693 100644 --- a/exporters/ostream/BUILD +++ b/exporters/ostream/BUILD @@ -43,37 +43,36 @@ cc_library( ], ) -# TODO - Uncomment once MetricData interface is finalised -#cc_library( -# name = "ostream_metric_exporter", -# srcs = [ -# "src/metric_exporter.cc", -# ], -# hdrs = [ -# "include/opentelemetry/exporters/ostream/metric_exporter.h", -# ], -# strip_include_prefix = "include", -# tags = [ -# "metrics", -# "ostream", -# ], -# deps = [ -# "//sdk/src/metrics", -# ], -#) +cc_library( + name = "ostream_metric_exporter", + srcs = [ + "src/metric_exporter.cc", + ], + hdrs = [ + "include/opentelemetry/exporters/ostream/metric_exporter.h", + ], + strip_include_prefix = "include", + tags = [ + "metrics", + "ostream", + ], + deps = [ + "//sdk/src/metrics", + ], +) -#cc_test( -# name = "ostream_metric_test", -# srcs = ["test/ostream_metric_test.cc"], -# tags = [ -# "ostream", -# "test", -# ], -# deps = [ -# ":ostream_metric_exporter", -# "@com_google_googletest//:gtest_main", -# ], -#) +cc_test( + name = "ostream_metric_test", + srcs = ["test/ostream_metric_test.cc"], + tags = [ + "ostream", + "test", + ], + deps = [ + ":ostream_metric_exporter", + "@com_google_googletest//:gtest_main", + ], +) cc_test( name = "ostream_metrics_test_deprecated", diff --git a/exporters/ostream/CMakeLists.txt b/exporters/ostream/CMakeLists.txt index e83719cce3..db2562ddde 100644 --- a/exporters/ostream/CMakeLists.txt +++ b/exporters/ostream/CMakeLists.txt @@ -19,7 +19,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/ostream - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters PATTERN "*.h" PATTERN "metrics_exporter.h" EXCLUDE PATTERN "log_Exporter.h" EXCLUDE) @@ -52,7 +52,7 @@ if(WITH_METRICS_PREVIEW) ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install( DIRECTORY include/opentelemetry/exporters/ostream - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters PATTERN "metrics_exporter.h") if(BUILD_TESTING) add_executable(ostream_metrics_test test/ostream_metrics_test.cc) @@ -63,6 +63,35 @@ if(WITH_METRICS_PREVIEW) TEST_PREFIX exporter. TEST_LIST ostream_metrics_test) endif() +else() + add_library(opentelemetry_exporter_ostream_metrics src/metric_exporter.cc) + set_target_properties(opentelemetry_exporter_ostream_metrics + PROPERTIES EXPORT_NAME ostream_metrics_exporter) + target_include_directories( + opentelemetry_exporter_ostream_metrics + PUBLIC "$") + target_link_libraries(opentelemetry_exporter_ostream_metrics + PUBLIC opentelemetry_metrics) + install( + TARGETS opentelemetry_exporter_ostream_metrics + EXPORT "${PROJECT_NAME}-target" + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install( + DIRECTORY include/opentelemetry/exporters/ostream + DESTINATION include/opentelemetry/exporters + PATTERN "metric_exporter.h") + if(BUILD_TESTING) + add_executable(ostream_metric_test test/ostream_metric_test.cc) + target_link_libraries( + ostream_metric_test ${GTEST_BOTH_LIBRARIES} + opentelemetry_exporter_ostream_metrics opentelemetry_resources) + gtest_add_tests( + TARGET ostream_metric_test + TEST_PREFIX exporter. + TEST_LIST ostream_metric_test) + endif() endif() if(WITH_LOGS_PREVIEW) @@ -82,7 +111,7 @@ if(WITH_LOGS_PREVIEW) ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install( DIRECTORY include/opentelemetry/exporters/ostream - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters PATTERN "log_exporter.h") if(BUILD_TESTING) add_executable(ostream_log_test test/ostream_log_test.cc) diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h index 5f27db13d2..465ae02bb6 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h @@ -35,11 +35,9 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx /** * Export - * @param records a span of unique pointers to metrics data + * @param data metrics data */ - sdk::common::ExportResult Export( - const nostd::span> &records) noexcept - override; + sdk::common::ExportResult Export(const sdk::metrics::ResourceMetrics &data) noexcept override; /** * Force flush the exporter. @@ -60,7 +58,9 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricEx bool is_shutdown_ = false; mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; - void printPointData(opentelemetry::sdk::metrics::PointType &point_data); + void printInstrumentationInfoMetricData( + const sdk::metrics::InstrumentationInfoMetrics &info_metrics); + void printPointData(const opentelemetry::sdk::metrics::PointType &point_data); }; } // namespace metrics } // namespace exporter diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index f8d26a7fa5..5cdd3fe78d 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -1,68 +1,92 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/exporters/ostream/metric_exporter.h" # include +# include "opentelemetry/exporters/ostream/metric_exporter.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" # include "opentelemetry/sdk_config.h" +namespace +{ +std::string timeToString(opentelemetry::common::SystemTimestamp time_stamp) +{ + std::time_t epoch_time = std::chrono::system_clock::to_time_t(time_stamp); + return std::ctime(&epoch_time); +} +} // namespace + OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { namespace metrics { +template +inline void printVec(std::ostream &os, Container &vec) +{ + using T = typename std::decay::type; + os << '['; + if (vec.size() > 1) + { + std::copy(vec.begin(), vec.end(), std::ostream_iterator(os, ", ")); + } + os << ']'; +} + OStreamMetricExporter::OStreamMetricExporter(std::ostream &sout) noexcept : sout_(sout) {} sdk::common::ExportResult OStreamMetricExporter::Export( - const nostd::span> &records) noexcept + const sdk::metrics::ResourceMetrics &data) noexcept { if (isShutdown()) { OTEL_INTERNAL_LOG_ERROR("[OStream Metric] Exporting " - << records.size() << " records(s) failed, exporter is shutdown"); + << data.instrumentation_info_metric_data_.size() + << " records(s) failed, exporter is shutdown"); return sdk::common::ExportResult::kFailure; } - for (auto &record : records) + for (auto &record : data.instrumentation_info_metric_data_) { - sout_ << "{" - << "\n name : " << record->instrumentation_library_->GetName() - << "\n version : " << record->instrumentation_library_->GetVersion(); - printPointData(record->point_data_); - sout_ << "\n}\n"; + printInstrumentationInfoMetricData(record); } return sdk::common::ExportResult::kSuccess; } -template -inline void printVec(std::ostream &os, std::vector &vec) +void OStreamMetricExporter::printInstrumentationInfoMetricData( + const sdk::metrics::InstrumentationInfoMetrics &info_metric) { - os << '['; - if (vec.size() > 1) - { - std::copy(vec.begin(), vec.end() - 1, std::ostream_iterator(os, ", ")); - } - if (!vec.empty()) + // sout_ is shared + const std::lock_guard locked(lock_); + sout_ << "{"; + sout_ << "\n name\t\t: " << info_metric.instrumentation_library_->GetName() + << "\n schema url\t: " << info_metric.instrumentation_library_->GetSchemaURL() + << "\n version\t: " << info_metric.instrumentation_library_->GetVersion(); + for (const auto &record : info_metric.metric_data_) { - os << vec.back(); + sout_ << "\n start time\t: " << timeToString(record.start_ts) + << " end time\t: " << timeToString(record.end_ts) + << " description\t: " << record.instrument_descriptor.description_ + << "\n unit\t\t: " << record.instrument_descriptor.unit_; + + for (const auto &pd : record.point_data_attr_) + { + printPointData(pd.point_data); + } } - os << ']'; + sout_ << "\n}\n"; } -void OStreamMetricExporter::printPointData(opentelemetry::sdk::metrics::PointType &point_data) +void OStreamMetricExporter::printPointData(const opentelemetry::sdk::metrics::PointType &point_data) { if (nostd::holds_alternative(point_data)) { auto sum_point_data = nostd::get(point_data); - sout_ << "\n type : SumPointData"; - sout_ << "\n start timestamp : " - << std::to_string(sum_point_data.start_epoch_nanos_.time_since_epoch().count()); - sout_ << "\n end timestamp : " - << std::to_string(sum_point_data.end_epoch_nanos_.time_since_epoch().count()); - sout_ << "\n value : "; + sout_ << "\n type\t\t: SumPointData"; + sout_ << "\n value\t\t: "; if (nostd::holds_alternative(sum_point_data.value_)) { sout_ << nostd::get(sum_point_data.value_); @@ -76,8 +100,6 @@ void OStreamMetricExporter::printPointData(opentelemetry::sdk::metrics::PointTyp { auto histogram_point_data = nostd::get(point_data); sout_ << "\n type : HistogramPointData"; - sout_ << "\n timestamp : " - << std::to_string(histogram_point_data.epoch_nanos_.time_since_epoch().count()); sout_ << "\n count : " << histogram_point_data.count_; sout_ << "\n sum : "; if (nostd::holds_alternative(histogram_point_data.sum_)) @@ -90,14 +112,14 @@ void OStreamMetricExporter::printPointData(opentelemetry::sdk::metrics::PointTyp } sout_ << "\n buckets : "; - if (nostd::holds_alternative>(histogram_point_data.boundaries_)) + if (nostd::holds_alternative>(histogram_point_data.boundaries_)) { - auto &double_boundaries = nostd::get>(histogram_point_data.boundaries_); + auto &double_boundaries = nostd::get>(histogram_point_data.boundaries_); printVec(sout_, double_boundaries); } - else if (nostd::holds_alternative>(histogram_point_data.boundaries_)) + else if (nostd::holds_alternative>(histogram_point_data.boundaries_)) { - auto &long_boundaries = nostd::get>(histogram_point_data.boundaries_); + auto &long_boundaries = nostd::get>(histogram_point_data.boundaries_); printVec(sout_, long_boundaries); } @@ -109,8 +131,8 @@ void OStreamMetricExporter::printPointData(opentelemetry::sdk::metrics::PointTyp auto last_point_data = nostd::get(point_data); sout_ << "\n type : LastValuePointData"; sout_ << "\n timestamp : " - << std::to_string(last_point_data.epoch_nanos_.time_since_epoch().count()) - << std::boolalpha << "\n valid : " << last_point_data.is_lastvalue_valid_; + << std::to_string(last_point_data.sample_ts_.time_since_epoch().count()) << std::boolalpha + << "\n valid : " << last_point_data.is_lastvalue_valid_; sout_ << "\n value : "; if (nostd::holds_alternative(last_point_data.value_)) { diff --git a/exporters/ostream/test/ostream_metric_test.cc b/exporters/ostream/test/ostream_metric_test.cc index 9e98dd4da4..3618d93e7a 100644 --- a/exporters/ostream/test/ostream_metric_test.cc +++ b/exporters/ostream/test/ostream_metric_test.cc @@ -1,14 +1,19 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 - -#include #ifndef ENABLE_METRICS_PREVIEW +# include +# include +# include +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/resource/resource_detector.h" + +# include # include "opentelemetry/exporters/ostream/metric_exporter.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" - -# include +# include "opentelemetry/sdk/metrics/data/metric_data.h" +# include "opentelemetry/sdk/resource/resource.h" namespace metric_sdk = opentelemetry::sdk::metrics; namespace nostd = opentelemetry::nostd; @@ -19,7 +24,7 @@ TEST(OStreamMetricsExporter, Shutdown) auto exporter = std::unique_ptr(new exportermetrics::OStreamMetricExporter); ASSERT_TRUE(exporter->Shutdown()); - auto result = exporter->Export(nostd::span>{}); + auto result = exporter->Export(metric_sdk::ResourceMetrics{}); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure); } @@ -28,48 +33,51 @@ TEST(OStreamMetricsExporter, ExportSumPointData) auto exporter = std::unique_ptr(new exportermetrics::OStreamMetricExporter); - std::unique_ptr record(new metric_sdk::MetricData); + metric_sdk::SumPointData sum_point_data{}; + sum_point_data.value_ = 10.0; + metric_sdk::SumPointData sum_point_data2{}; + sum_point_data2.value_ = 20.0; + metric_sdk::ResourceMetrics data; + auto resource = opentelemetry::sdk::resource::Resource::Create( + opentelemetry::sdk::resource::ResourceAttributes{}); + data.resource_ = &resource; auto instrumentation_library = opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name", "1.2.0"); - record->instrumentation_library_ = instrumentation_library.get(); - record->point_data_ = metric_sdk::SumPointData{ - opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, 10.0, - metric_sdk::AggregationTemporality::kUnspecified, false}; - auto record2 = std::unique_ptr(new metric_sdk::MetricData(*record)); - record2->point_data_ = metric_sdk::SumPointData{ - opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, 20l, - metric_sdk::AggregationTemporality::kUnspecified, false}; - std::vector> records; - records.push_back(std::move(record)); - records.push_back(std::move(record2)); + metric_sdk::MetricData metric_data{ + metric_sdk::InstrumentDescriptor{"library_name", "description", "unit", + metric_sdk::InstrumentType::kCounter, + metric_sdk::InstrumentValueType::kDouble}, + opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, + std::vector{ + {metric_sdk::PointAttributes{}, sum_point_data}, + {metric_sdk::PointAttributes{}, sum_point_data2}}}; + data.instrumentation_info_metric_data_ = std::vector{ + {instrumentation_library.get(), std::vector{metric_data}}}; std::stringstream stdoutOutput; std::streambuf *sbuf = std::cout.rdbuf(); std::cout.rdbuf(stdoutOutput.rdbuf()); - auto result = exporter->Export(records); + auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); std::cout.rdbuf(sbuf); - std::string expectedOutput = - "{\n" - " name : library_name\n" - " version : 1.2.0\n" - " type : SumPointData\n" - " start timestamp : 0\n" - " end timestamp : 0\n" - " value : 10\n" - "}\n" - "{\n" - " name : library_name\n" - " version : 1.2.0\n" - " type : SumPointData\n" - " start timestamp : 0\n" - " end timestamp : 0\n" - " value : 20\n" - "}\n"; - ASSERT_EQ(stdoutOutput.str(), expectedOutput); + std::string expected_output = + "{" + "\n name\t\t: library_name" + "\n schema url\t: " + "\n version\t: 1.2.0" + "\n start time\t: Thu Jan 1 00:00:00 1970" + "\n end time\t: Thu Jan 1 00:00:00 1970" + "\n description\t: description" + "\n unit\t\t: unit" + "\n type\t\t: SumPointData" + "\n value\t\t: 10" + "\n type\t\t: SumPointData" + "\n value\t\t: 20" + "\n}\n"; + ASSERT_EQ(stdoutOutput.str(), expected_output); } TEST(OStreamMetricsExporter, ExportHistogramPointData) @@ -77,56 +85,63 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData) auto exporter = std::unique_ptr(new exportermetrics::OStreamMetricExporter); - std::unique_ptr record(new metric_sdk::MetricData); + metric_sdk::HistogramPointData histogram_point_data{}; + histogram_point_data.boundaries_ = std::list{10.1, 20.2, 30.2}; + histogram_point_data.count_ = 3; + histogram_point_data.counts_ = {200, 300, 400, 500}; + histogram_point_data.sum_ = 900.5; + metric_sdk::HistogramPointData histogram_point_data2{}; + histogram_point_data2.boundaries_ = std::list{10, 20, 30}; + histogram_point_data2.count_ = 3; + histogram_point_data2.counts_ = {200, 300, 400, 500}; + histogram_point_data2.sum_ = 900l; + metric_sdk::ResourceMetrics data; + auto resource = opentelemetry::sdk::resource::Resource::Create( + opentelemetry::sdk::resource::ResourceAttributes{}); + data.resource_ = &resource; auto instrumentation_library = opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name", "1.2.0"); - record->instrumentation_library_ = instrumentation_library.get(); - record->point_data_ = metric_sdk::HistogramPointData{opentelemetry::common::SystemTimestamp{}, - std::vector{10.1, 20.2, 30.2}, - 900.5, - {200, 300, 400, 500}, - 3}; - auto record2 = std::unique_ptr(new metric_sdk::MetricData(*record)); - record2->point_data_ = metric_sdk::HistogramPointData{opentelemetry::common::SystemTimestamp{}, - std::vector{10, 20, 30}, - 900l, - {200, 300, 400, 500}, - 3}; - std::vector> records; - records.push_back(std::move(record)); - records.push_back(std::move(record2)); + metric_sdk::MetricData metric_data{ + metric_sdk::InstrumentDescriptor{"library_name", "description", "unit", + metric_sdk::InstrumentType::kCounter, + metric_sdk::InstrumentValueType::kDouble}, + opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, + std::vector{ + {metric_sdk::PointAttributes{}, histogram_point_data}, + {metric_sdk::PointAttributes{}, histogram_point_data2}}}; + data.instrumentation_info_metric_data_ = std::vector{ + {instrumentation_library.get(), std::vector{metric_data}}}; std::stringstream stdoutOutput; std::streambuf *sbuf = std::cout.rdbuf(); std::cout.rdbuf(stdoutOutput.rdbuf()); - auto result = exporter->Export(records); + auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); std::cout.rdbuf(sbuf); - std::string expectedOutput = - "{\n" - " name : library_name\n" - " version : 1.2.0\n" - " type : HistogramPointData\n" - " timestamp : 0\n" - " count : 3\n" - " sum : 900.5\n" - " buckets : [10.1, 20.2, 30.2]\n" - " counts : [200, 300, 400, 500]\n" - "}\n" - "{\n" - " name : library_name\n" - " version : 1.2.0\n" - " type : HistogramPointData\n" - " timestamp : 0\n" - " count : 3\n" - " sum : 900\n" - " buckets : [10, 20, 30]\n" - " counts : [200, 300, 400, 500]\n" - "}\n"; - ASSERT_EQ(stdoutOutput.str(), expectedOutput); + std::string expected_output = + "{" + "\n name\t\t: library_name" + "\n schema url\t: " + "\n version\t: 1.2.0" + "\n start time\t: Thu Jan 1 00:00:00 1970" + "\n end time\t: Thu Jan 1 00:00:00 1970" + "\n description\t: description" + "\n unit\t\t: unit" + "\n type : HistogramPointData" + "\n count : 3" + "\n sum : 900.5" + "\n buckets : [10.1, 20.2, 30.2, ]" + "\n counts : [200, 300, 400, 500, ]" + "\n type : HistogramPointData" + "\n count : 3" + "\n sum : 900" + "\n buckets : [10, 20, 30, ]" + "\n counts : [200, 300, 400, 500, ]" + "\n}\n"; + ASSERT_EQ(stdoutOutput.str(), expected_output); } TEST(OStreamMetricsExporter, ExportLastValuePointData) @@ -134,46 +149,59 @@ TEST(OStreamMetricsExporter, ExportLastValuePointData) auto exporter = std::unique_ptr(new exportermetrics::OStreamMetricExporter); - std::unique_ptr record(new metric_sdk::MetricData); + metric_sdk::ResourceMetrics data; + auto resource = opentelemetry::sdk::resource::Resource::Create( + opentelemetry::sdk::resource::ResourceAttributes{}); + data.resource_ = &resource; auto instrumentation_library = opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name", "1.2.0"); - record->instrumentation_library_ = instrumentation_library.get(); - record->point_data_ = - metric_sdk::LastValuePointData{opentelemetry::common::SystemTimestamp{}, true, 10.0}; - auto record2 = std::unique_ptr(new metric_sdk::MetricData(*record)); - record2->point_data_ = - metric_sdk::LastValuePointData{opentelemetry::common::SystemTimestamp{}, true, 20l}; - std::vector> records; - records.push_back(std::move(record)); - records.push_back(std::move(record2)); + metric_sdk::LastValuePointData last_value_point_data{}; + last_value_point_data.value_ = 10.0; + last_value_point_data.is_lastvalue_valid_ = true; + last_value_point_data.sample_ts_ = opentelemetry::common::SystemTimestamp{}; + metric_sdk::LastValuePointData last_value_point_data2{}; + last_value_point_data2.value_ = 20l; + last_value_point_data2.is_lastvalue_valid_ = true; + last_value_point_data2.sample_ts_ = opentelemetry::common::SystemTimestamp{}; + metric_sdk::MetricData metric_data{ + metric_sdk::InstrumentDescriptor{"library_name", "description", "unit", + metric_sdk::InstrumentType::kCounter, + metric_sdk::InstrumentValueType::kDouble}, + opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, + std::vector{ + {metric_sdk::PointAttributes{}, last_value_point_data}, + {metric_sdk::PointAttributes{}, last_value_point_data2}}}; + data.instrumentation_info_metric_data_ = std::vector{ + {instrumentation_library.get(), std::vector{metric_data}}}; std::stringstream stdoutOutput; std::streambuf *sbuf = std::cout.rdbuf(); std::cout.rdbuf(stdoutOutput.rdbuf()); - auto result = exporter->Export(records); + auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); std::cout.rdbuf(sbuf); - std::string expectedOutput = - "{\n" - " name : library_name\n" - " version : 1.2.0\n" - " type : LastValuePointData\n" - " timestamp : 0\n" - " valid : true\n" - " value : 10\n" - "}\n" - "{\n" - " name : library_name\n" - " version : 1.2.0\n" - " type : LastValuePointData\n" - " timestamp : 0\n" - " valid : true\n" - " value : 20\n" - "}\n"; - ASSERT_EQ(stdoutOutput.str(), expectedOutput); + std::string expected_output = + "{" + "\n name\t\t: library_name" + "\n schema url\t: " + "\n version\t: 1.2.0" + "\n start time\t: Thu Jan 1 00:00:00 1970" + "\n end time\t: Thu Jan 1 00:00:00 1970" + "\n description\t: description" + "\n unit\t\t: unit" + "\n type : LastValuePointData" + "\n timestamp : 0" + "\n valid : true" + "\n value : 10" + "\n type : LastValuePointData" + "\n timestamp : 0" + "\n valid : true" + "\n value : 20" + "\n}\n"; + ASSERT_EQ(stdoutOutput.str(), expected_output); } TEST(OStreamMetricsExporter, ExportDropPointData) @@ -181,30 +209,46 @@ TEST(OStreamMetricsExporter, ExportDropPointData) auto exporter = std::unique_ptr(new exportermetrics::OStreamMetricExporter); - std::unique_ptr record(new metric_sdk::MetricData); + metric_sdk::ResourceMetrics data; + auto resource = opentelemetry::sdk::resource::Resource::Create( + opentelemetry::sdk::resource::ResourceAttributes{}); + data.resource_ = &resource; auto instrumentation_library = opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create("library_name", "1.2.0"); - record->instrumentation_library_ = instrumentation_library.get(); - record->point_data_ = metric_sdk::DropPointData{}; - std::vector> records; - records.push_back(std::move(record)); + metric_sdk::DropPointData drop_point_data{}; + metric_sdk::DropPointData drop_point_data2{}; + metric_sdk::MetricData metric_data{ + metric_sdk::InstrumentDescriptor{"library_name", "description", "unit", + metric_sdk::InstrumentType::kCounter, + metric_sdk::InstrumentValueType::kDouble}, + opentelemetry::common::SystemTimestamp{}, opentelemetry::common::SystemTimestamp{}, + std::vector{ + {metric_sdk::PointAttributes{}, drop_point_data}, + {metric_sdk::PointAttributes{}, drop_point_data2}}}; + data.instrumentation_info_metric_data_ = std::vector{ + {instrumentation_library.get(), std::vector{metric_data}}}; std::stringstream stdoutOutput; std::streambuf *sbuf = std::cout.rdbuf(); std::cout.rdbuf(stdoutOutput.rdbuf()); - auto result = exporter->Export(records); + auto result = exporter->Export(data); EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess); std::cout.rdbuf(sbuf); - std::string expectedOutput = - "{\n" - " name : library_name\n" - " version : 1.2.0\n" - "}\n"; - - ASSERT_EQ(stdoutOutput.str(), expectedOutput); + std::string expected_output = + "{" + "\n name\t\t: library_name" + "\n schema url\t: " + "\n version\t: 1.2.0" + "\n start time\t: Thu Jan 1 00:00:00 1970" + "\n end time\t: Thu Jan 1 00:00:00 1970" + "\n description\t: description" + "\n unit\t\t: unit" + "\n}\n"; + + ASSERT_EQ(stdoutOutput.str(), expected_output); } #endif diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 1dba7ea1c7..1b1b200504 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -98,7 +98,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/otlp - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters FILES_MATCHING PATTERN "*.h" PATTERN "otlp_recordable.h" EXCLUDE) diff --git a/exporters/prometheus/CMakeLists.txt b/exporters/prometheus/CMakeLists.txt index 9b2c92ef73..753fa18cac 100755 --- a/exporters/prometheus/CMakeLists.txt +++ b/exporters/prometheus/CMakeLists.txt @@ -46,7 +46,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/prometheus - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters FILES_MATCHING PATTERN "*.h") diff --git a/exporters/zipkin/CMakeLists.txt b/exporters/zipkin/CMakeLists.txt index b82339f1ee..559e8d5001 100644 --- a/exporters/zipkin/CMakeLists.txt +++ b/exporters/zipkin/CMakeLists.txt @@ -32,7 +32,7 @@ install( install( DIRECTORY include/opentelemetry/exporters/zipkin - DESTINATION include/opentelemetry/exporters/ + DESTINATION include/opentelemetry/exporters FILES_MATCHING PATTERN "*.h" PATTERN "recordable.h" EXCLUDE) diff --git a/ext/CMakeLists.txt b/ext/CMakeLists.txt index a2baa47a32..034328eaa5 100644 --- a/ext/CMakeLists.txt +++ b/ext/CMakeLists.txt @@ -5,6 +5,7 @@ target_include_directories( "$") set_target_properties(opentelemetry_ext PROPERTIES EXPORT_NAME "ext") +target_link_libraries(opentelemetry_ext INTERFACE opentelemetry_api) install( TARGETS opentelemetry_ext diff --git a/ext/src/http/client/nosend/CMakeLists.txt b/ext/src/http/client/nosend/CMakeLists.txt index 9118abbfb5..497daeb342 100644 --- a/ext/src/http/client/nosend/CMakeLists.txt +++ b/ext/src/http/client/nosend/CMakeLists.txt @@ -25,7 +25,7 @@ if(${BUILD_TESTING}) endif() target_link_libraries(http_client_nosend ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIB} - opentelemetry_api opentelemetry_ext) + opentelemetry_ext) install( TARGETS http_client_nosend diff --git a/ext/src/zpages/CMakeLists.txt b/ext/src/zpages/CMakeLists.txt index 64d9c73728..e3fd480df6 100644 --- a/ext/src/zpages/CMakeLists.txt +++ b/ext/src/zpages/CMakeLists.txt @@ -10,9 +10,8 @@ add_library( set_target_properties(opentelemetry_zpages PROPERTIES EXPORT_NAME zpages) -target_link_libraries( - opentelemetry_zpages PUBLIC opentelemetry_ext opentelemetry_api - opentelemetry_trace) +target_link_libraries(opentelemetry_zpages PUBLIC opentelemetry_ext + opentelemetry_trace) install( TARGETS opentelemetry_zpages diff --git a/sdk/CMakeLists.txt b/sdk/CMakeLists.txt index 6421e3ad15..1a824fe09b 100644 --- a/sdk/CMakeLists.txt +++ b/sdk/CMakeLists.txt @@ -14,8 +14,8 @@ install( ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) install( - DIRECTORY include/opentelemetry/ - DESTINATION include/opentelemetry/ + DIRECTORY include/opentelemetry + DESTINATION include/opentelemetry FILES_MATCHING PATTERN "*config.h") @@ -31,16 +31,10 @@ endif() install( DIRECTORY include/opentelemetry/sdk - DESTINATION include/opentelemetry/ - FILES_MATCHING - PATTERN "*.h" - PATTERN "${METRICS_EXCLUDE_PATTERN}" EXCLUDE) - -install( - DIRECTORY include/opentelemetry/sdk - DESTINATION include/opentelemetry/ + DESTINATION include/opentelemetry FILES_MATCHING PATTERN "*.h" + PATTERN "${METRICS_EXCLUDE_PATTERN}" EXCLUDE PATTERN "${LOGS_EXCLUDE_PATTERN}" EXCLUDE) add_subdirectory(src) diff --git a/sdk/include/opentelemetry/sdk/_metrics/controller.h b/sdk/include/opentelemetry/sdk/_metrics/controller.h index 9d80787bcd..66a57a2efa 100644 --- a/sdk/include/opentelemetry/sdk/_metrics/controller.h +++ b/sdk/include/opentelemetry/sdk/_metrics/controller.h @@ -9,7 +9,9 @@ # include # include # include + # include "opentelemetry/_metrics/instrument.h" +# include "opentelemetry/common/macros.h" # include "opentelemetry/nostd/unique_ptr.h" # include "opentelemetry/sdk/_metrics/exporter.h" # include "opentelemetry/sdk/_metrics/meter.h" @@ -120,7 +122,11 @@ class PushController void tick() { this->mu_.lock(); +# ifdef OPENTELEMETRY_RTTI_ENABLED std::vector collected = dynamic_cast(meter_.get())->Collect(); +# else + std::vector collected = static_cast(meter_.get())->Collect(); +# endif for (const auto &rec : collected) { processor_->process(rec); diff --git a/sdk/include/opentelemetry/sdk/_metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/_metrics/sync_instruments.h index 46274571af..80d9b6092d 100644 --- a/sdk/include/opentelemetry/sdk/_metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/_metrics/sync_instruments.h @@ -9,7 +9,9 @@ # include # include # include + # include "opentelemetry/_metrics/sync_instruments.h" +# include "opentelemetry/common/macros.h" # include "opentelemetry/sdk/_metrics/aggregator/counter_aggregator.h" # include "opentelemetry/sdk/_metrics/aggregator/min_max_sum_count_aggregator.h" # include "opentelemetry/sdk/_metrics/instrument.h" @@ -156,7 +158,11 @@ class Counter final : public SynchronousInstrument, public opentelemetry::met { toDelete.push_back(x.first); } +# ifdef OPENTELEMETRY_RTTI_ENABLED auto agg_ptr = dynamic_cast *>(x.second.get())->GetAggregator(); +# else + auto agg_ptr = static_cast *>(x.second.get())->GetAggregator(); +# endif if (agg_ptr->is_updated()) { agg_ptr->checkpoint(); @@ -287,7 +293,11 @@ class UpDownCounter final : public SynchronousInstrument, { toDelete.push_back(x.first); } +# ifdef OPENTELEMETRY_RTTI_ENABLED auto agg_ptr = dynamic_cast *>(x.second.get())->GetAggregator(); +# else + auto agg_ptr = static_cast *>(x.second.get())->GetAggregator(); +# endif if (agg_ptr->is_updated()) { agg_ptr->checkpoint(); @@ -417,7 +427,11 @@ class ValueRecorder final : public SynchronousInstrument, { toDelete.push_back(x.first); } +# ifdef OPENTELEMETRY_RTTI_ENABLED auto agg_ptr = dynamic_cast *>(x.second.get())->GetAggregator(); +# else + auto agg_ptr = static_cast *>(x.second.get())->GetAggregator(); +# endif if (agg_ptr->is_updated()) { agg_ptr->checkpoint(); diff --git a/sdk/include/opentelemetry/sdk/_metrics/ungrouped_processor.h b/sdk/include/opentelemetry/sdk/_metrics/ungrouped_processor.h index 5894144c7f..cc13ae52b6 100644 --- a/sdk/include/opentelemetry/sdk/_metrics/ungrouped_processor.h +++ b/sdk/include/opentelemetry/sdk/_metrics/ungrouped_processor.h @@ -5,6 +5,8 @@ #ifdef ENABLE_METRICS_PREVIEW # include + +# include "opentelemetry/common/macros.h" # include "opentelemetry/sdk/_metrics/aggregator/counter_aggregator.h" # include "opentelemetry/sdk/_metrics/aggregator/exact_aggregator.h" # include "opentelemetry/sdk/_metrics/aggregator/gauge_aggregator.h" @@ -216,12 +218,19 @@ class UngroupedMetricsProcessor : public MetricsProcessor auto agg_kind = batch_agg->get_aggregator_kind(); if (agg_kind == opentelemetry::sdk::metrics::AggregatorKind::Counter) { +# ifdef OPENTELEMETRY_RTTI_ENABLED std::shared_ptr> temp_batch_agg_counter = std::dynamic_pointer_cast>(batch_agg); std::shared_ptr> temp_record_agg_counter = std::dynamic_pointer_cast>(record_agg); +# else + std::shared_ptr> temp_batch_agg_counter = + std::static_pointer_cast>(batch_agg); + std::shared_ptr> temp_record_agg_counter = + std::static_pointer_cast>(record_agg); +# endif auto temp_batch_agg_raw_counter = temp_batch_agg_counter.get(); auto temp_record_agg_raw_counter = temp_record_agg_counter.get(); @@ -229,6 +238,7 @@ class UngroupedMetricsProcessor : public MetricsProcessor } else if (agg_kind == opentelemetry::sdk::metrics::AggregatorKind::MinMaxSumCount) { +# ifdef OPENTELEMETRY_RTTI_ENABLED std::shared_ptr> temp_batch_agg_mmsc = std::dynamic_pointer_cast>( @@ -238,6 +248,17 @@ class UngroupedMetricsProcessor : public MetricsProcessor temp_record_agg_mmsc = std::dynamic_pointer_cast>( record_agg); +# else + std::shared_ptr> + temp_batch_agg_mmsc = + std::static_pointer_cast>( + batch_agg); + + std::shared_ptr> + temp_record_agg_mmsc = + std::static_pointer_cast>( + record_agg); +# endif auto temp_batch_agg_raw_mmsc = temp_batch_agg_mmsc.get(); auto temp_record_agg_raw_mmsc = temp_record_agg_mmsc.get(); @@ -246,11 +267,19 @@ class UngroupedMetricsProcessor : public MetricsProcessor } else if (agg_kind == opentelemetry::sdk::metrics::AggregatorKind::Gauge) { +# ifdef OPENTELEMETRY_RTTI_ENABLED std::shared_ptr> temp_batch_agg_gauge = std::dynamic_pointer_cast>(batch_agg); std::shared_ptr> temp_record_agg_gauge = std::dynamic_pointer_cast>(record_agg); +# else + std::shared_ptr> temp_batch_agg_gauge = + std::static_pointer_cast>(batch_agg); + + std::shared_ptr> temp_record_agg_gauge = + std::static_pointer_cast>(record_agg); +# endif auto temp_batch_agg_raw_gauge = temp_batch_agg_gauge.get(); auto temp_record_agg_raw_gauge = temp_record_agg_gauge.get(); @@ -259,12 +288,19 @@ class UngroupedMetricsProcessor : public MetricsProcessor } else if (agg_kind == opentelemetry::sdk::metrics::AggregatorKind::Sketch) { +# ifdef OPENTELEMETRY_RTTI_ENABLED std::shared_ptr> temp_batch_agg_sketch = std::dynamic_pointer_cast>(batch_agg); std::shared_ptr> temp_record_agg_sketch = std::dynamic_pointer_cast>(record_agg); +# else + std::shared_ptr> temp_batch_agg_sketch = + std::static_pointer_cast>(batch_agg); + std::shared_ptr> temp_record_agg_sketch = + std::static_pointer_cast>(record_agg); +# endif auto temp_batch_agg_raw_sketch = temp_batch_agg_sketch.get(); auto temp_record_agg_raw_sketch = temp_record_agg_sketch.get(); @@ -272,6 +308,7 @@ class UngroupedMetricsProcessor : public MetricsProcessor } else if (agg_kind == opentelemetry::sdk::metrics::AggregatorKind::Histogram) { +# ifdef OPENTELEMETRY_RTTI_ENABLED std::shared_ptr> temp_batch_agg_histogram = std::dynamic_pointer_cast>( @@ -281,6 +318,17 @@ class UngroupedMetricsProcessor : public MetricsProcessor temp_record_agg_histogram = std::dynamic_pointer_cast>( record_agg); +# else + std::shared_ptr> + temp_batch_agg_histogram = + std::static_pointer_cast>( + batch_agg); + + std::shared_ptr> + temp_record_agg_histogram = + std::static_pointer_cast>( + record_agg); +# endif auto temp_batch_agg_raw_histogram = temp_batch_agg_histogram.get(); auto temp_record_agg_raw_histogram = temp_record_agg_histogram.get(); @@ -289,11 +337,19 @@ class UngroupedMetricsProcessor : public MetricsProcessor } else if (agg_kind == opentelemetry::sdk::metrics::AggregatorKind::Exact) { +# ifdef OPENTELEMETRY_RTTI_ENABLED std::shared_ptr> temp_batch_agg_exact = std::dynamic_pointer_cast>(batch_agg); std::shared_ptr> temp_record_agg_exact = std::dynamic_pointer_cast>(record_agg); +# else + std::shared_ptr> temp_batch_agg_exact = + std::static_pointer_cast>(batch_agg); + + std::shared_ptr> temp_record_agg_exact = + std::static_pointer_cast>(record_agg); +# endif auto temp_batch_agg_raw_exact = temp_batch_agg_exact.get(); auto temp_record_agg_raw_exact = temp_record_agg_exact.get(); diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h index 8f33fa27b4..b5cc2c349e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h @@ -24,8 +24,15 @@ class LongHistogramAggregation : public Aggregation void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} + /* Returns the result of merge of the existing aggregation with delta aggregation with same + * boundaries */ virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + /* Returns the new delta aggregation by comparing existing aggregation with next aggregation with + * same boundaries. Data points for `next` aggregation (sum , bucket-counts) should be more than + * the current aggregation - which is the normal scenario as measurements values are monotonic + * increasing. + */ virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; PointType ToPoint() const noexcept override; @@ -45,8 +52,15 @@ class DoubleHistogramAggregation : public Aggregation void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override; + /* Returns the result of merge of the existing aggregation with delta aggregation with same + * boundaries */ virtual std::unique_ptr Merge(const Aggregation &delta) const noexcept override; + /* Returns the new delta aggregation by comparing existing aggregation with next aggregation with + * same boundaries. Data points for `next` aggregation (sum , bucket-counts) should be more than + * the current aggregation - which is the normal scenario as measurements values are monotonic + * increasing. + */ virtual std::unique_ptr Diff(const Aggregation &next) const noexcept override; PointType ToPoint() const noexcept override; @@ -56,6 +70,31 @@ class DoubleHistogramAggregation : public Aggregation mutable HistogramPointData point_data_; }; +template +void HistogramMerge(HistogramPointData ¤t, + HistogramPointData &delta, + HistogramPointData &merge) +{ + for (size_t i = 0; i < current.counts_.size(); i++) + { + merge.counts_[i] = current.counts_[i] + delta.counts_[i]; + } + merge.boundaries_ = current.boundaries_; + merge.sum_ = nostd::get(current.sum_) + nostd::get(delta.sum_); + merge.count_ = current.count_ + delta.count_; +} + +template +void HistogramDiff(HistogramPointData ¤t, HistogramPointData &next, HistogramPointData &diff) +{ + for (size_t i = 0; i < current.counts_.size(); i++) + { + diff.counts_[i] = next.counts_[i] - current.counts_[i]; + } + diff.boundaries_ = current.boundaries_; + diff.count_ = next.count_ - current.count_; +} + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h index b6b5acf3a9..127ef468ac 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h @@ -6,6 +6,7 @@ # include "opentelemetry/nostd/span.h" # include "opentelemetry/sdk/common/exporter_utils.h" +# include "opentelemetry/sdk/metrics/export/metric_producer.h" # include "opentelemetry/version.h" # include diff --git a/sdk/include/opentelemetry/sdk/metrics/view/instrument_selector.h b/sdk/include/opentelemetry/sdk/metrics/view/instrument_selector.h index 4912277d09..e79a292c0b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/instrument_selector.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/instrument_selector.h @@ -27,8 +27,8 @@ class InstrumentSelector InstrumentType GetInstrumentType() { return instrument_type_; } private: - opentelemetry::sdk::metrics::InstrumentType instrument_type_; std::unique_ptr name_filter_; + opentelemetry::sdk::metrics::InstrumentType instrument_type_; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/version/version.h b/sdk/include/opentelemetry/sdk/version/version.h index 3155ea8e0b..155cba3f87 100644 --- a/sdk/include/opentelemetry/sdk/version/version.h +++ b/sdk/include/opentelemetry/sdk/version/version.h @@ -5,7 +5,7 @@ #include "opentelemetry/detail/preprocessor.h" -#define OPENTELEMETRY_SDK_VERSION "1.2.0" +#define OPENTELEMETRY_SDK_VERSION "1.3.0" #include "opentelemetry/version.h" diff --git a/sdk/src/_metrics/meter.cc b/sdk/src/_metrics/meter.cc index 5207865b9c..f48eccfbd6 100644 --- a/sdk/src/_metrics/meter.cc +++ b/sdk/src/_metrics/meter.cc @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 #ifdef ENABLE_METRICS_PREVIEW + # include "opentelemetry/sdk/_metrics/meter.h" +# include "opentelemetry/common/macros.h" namespace metrics_api = opentelemetry::metrics; @@ -664,7 +666,11 @@ void Meter::CollectSingleSyncInstrument( i++; return; } - auto cast_ptr = std::dynamic_pointer_cast>(i->second); +# ifdef OPENTELEMETRY_RTTI_ENABLED + auto cast_ptr = std::dynamic_pointer_cast>(i->second); +# else + auto cast_ptr = std::static_pointer_cast>(i->second); +# endif std::vector new_records = cast_ptr->GetRecords(); records.insert(records.begin(), new_records.begin(), new_records.end()); } @@ -734,7 +740,11 @@ void Meter::CollectSingleAsyncInstrument( i++; return; } - auto cast_ptr = std::dynamic_pointer_cast>(i->second); +# ifdef OPENTELEMETRY_RTTI_ENABLED + auto cast_ptr = std::dynamic_pointer_cast>(i->second); +# else + auto cast_ptr = std::static_pointer_cast>(i->second); +# endif std::vector new_records = cast_ptr->GetRecords(); records.insert(records.begin(), new_records.begin(), new_records.end()); } diff --git a/sdk/src/metrics/aggregation/histogram_aggregation.cc b/sdk/src/metrics/aggregation/histogram_aggregation.cc index 60f65c51b1..27405999c9 100644 --- a/sdk/src/metrics/aggregation/histogram_aggregation.cc +++ b/sdk/src/metrics/aggregation/histogram_aggregation.cc @@ -14,7 +14,6 @@ namespace metrics LongHistogramAggregation::LongHistogramAggregation() { - point_data_.boundaries_ = std::list{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l}; point_data_.counts_ = std::vector(nostd::get>(point_data_.boundaries_).size() + 1, 0); @@ -47,12 +46,22 @@ void LongHistogramAggregation::Aggregate(long value, const PointAttributes &attr std::unique_ptr LongHistogramAggregation::Merge( const Aggregation &delta) const noexcept { - return nullptr; + auto curr_value = nostd::get(ToPoint()); + auto delta_value = nostd::get( + (static_cast(delta).ToPoint())); + LongHistogramAggregation *aggr = new LongHistogramAggregation(); + HistogramMerge(curr_value, delta_value, aggr->point_data_); + return std::unique_ptr(aggr); } std::unique_ptr LongHistogramAggregation::Diff(const Aggregation &next) const noexcept { - return nullptr; + auto curr_value = nostd::get(ToPoint()); + auto next_value = nostd::get( + (static_cast(next).ToPoint())); + LongHistogramAggregation *aggr = new LongHistogramAggregation(); + HistogramDiff(curr_value, next_value, aggr->point_data_); + return std::unique_ptr(aggr); } PointType LongHistogramAggregation::ToPoint() const noexcept @@ -62,7 +71,6 @@ PointType LongHistogramAggregation::ToPoint() const noexcept DoubleHistogramAggregation::DoubleHistogramAggregation() { - point_data_.boundaries_ = std::list{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0}; point_data_.counts_ = @@ -96,20 +104,27 @@ void DoubleHistogramAggregation::Aggregate(double value, const PointAttributes & std::unique_ptr DoubleHistogramAggregation::Merge( const Aggregation &delta) const noexcept { - // TODO - Implement me - return nullptr; + auto curr_value = nostd::get(ToPoint()); + auto delta_value = nostd::get( + (static_cast(delta).ToPoint())); + DoubleHistogramAggregation *aggr = new DoubleHistogramAggregation(); + HistogramMerge(curr_value, delta_value, aggr->point_data_); + return std::unique_ptr(aggr); } std::unique_ptr DoubleHistogramAggregation::Diff( const Aggregation &next) const noexcept { - // TODO - Implement me - return nullptr; + auto curr_value = nostd::get(ToPoint()); + auto next_value = nostd::get( + (static_cast(next).ToPoint())); + DoubleHistogramAggregation *aggr = new DoubleHistogramAggregation(); + HistogramDiff(curr_value, next_value, aggr->point_data_); + return std::unique_ptr(aggr); } PointType DoubleHistogramAggregation::ToPoint() const noexcept { - // TODO Implement me return point_data_; } diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index e7ca822b6d..d5af7f9ec9 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -206,7 +206,7 @@ std::unique_ptr Meter::RegisterMetricStorage( "[Meter::RegisterMetricStorage] - Error during finding matching views." << "Some of the matching view configurations mayn't be used for metric collection"); } - return std::move(storages); + return storages; } /** collect metrics across all the meters **/ diff --git a/sdk/src/version/version.cc b/sdk/src/version/version.cc index 9c4aadf1c9..a153196514 100644 --- a/sdk/src/version/version.cc +++ b/sdk/src/version/version.cc @@ -9,7 +9,7 @@ namespace sdk namespace version { const int MAJOR_VERSION = 1; -const int MINOR_VERSION = 2; +const int MINOR_VERSION = 3; const int PATCH_VERSION = 0; const char *PRE_RELEASE = ""; const char *BUILD_METADATA = ""; diff --git a/sdk/test/_metrics/metric_instrument_test.cc b/sdk/test/_metrics/metric_instrument_test.cc index 089baf1211..28df53fc32 100644 --- a/sdk/test/_metrics/metric_instrument_test.cc +++ b/sdk/test/_metrics/metric_instrument_test.cc @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #ifdef ENABLE_METRICS_PREVIEW + # include # include # include @@ -9,11 +10,19 @@ # include # include # include + +# include "opentelemetry/common/macros.h" # include "opentelemetry/sdk/_metrics/async_instruments.h" # include "opentelemetry/sdk/_metrics/sync_instruments.h" namespace metrics_api = opentelemetry::metrics; +# ifdef OPENTELEMETRY_RTTI_ENABLED +# define METRICS_TEST_TYPE_CAST(TO, FROM) dynamic_cast(FROM) +# else +# define METRICS_TEST_TYPE_CAST(TO, FROM) static_cast(FROM) +# endif + OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { @@ -37,12 +46,15 @@ TEST(ApiSdkConversion, async) auto labelkv = common::KeyValueIterableView{labels}; alpha->observe(123456, labelkv); - EXPECT_EQ(dynamic_cast *>(alpha.get())->GetRecords()[0].GetLabels(), + EXPECT_EQ(METRICS_TEST_TYPE_CAST(AsynchronousInstrument *, alpha.get()) + ->GetRecords()[0] + .GetLabels(), "{key587:value264}"); alpha->observe(123456, labelkv); - AggregatorVariant canCollect = - dynamic_cast *>(alpha.get())->GetRecords()[0].GetAggregator(); + AggregatorVariant canCollect = METRICS_TEST_TYPE_CAST(AsynchronousInstrument *, alpha.get()) + ->GetRecords()[0] + .GetAggregator(); EXPECT_EQ(nostd::holds_alternative>>(canCollect), false); EXPECT_EQ(nostd::holds_alternative>>(canCollect), true); EXPECT_EQ(nostd::get>>(canCollect)->get_checkpoint()[0], 123456); @@ -292,11 +304,13 @@ TEST(Counter, StressAdd) second.join(); third.join(); - EXPECT_EQ(dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv)].get()) + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundCounter *, + alpha->boundInstruments_[KvToString(labelkv)].get()) ->GetAggregator() ->get_values()[0], 2000); - EXPECT_EQ(dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv1)].get()) + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundCounter *, + alpha->boundInstruments_[KvToString(labelkv1)].get()) ->GetAggregator() ->get_values()[0], 3000); @@ -343,16 +357,16 @@ TEST(IntUpDownCounter, StressAdd) third.join(); fourth.join(); - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv)].get()) - ->GetAggregator() - ->get_values()[0], - 12340 * 2); - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv1)].get()) - ->GetAggregator() - ->get_values()[0], - 56780 - 12340); + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundUpDownCounter *, + alpha->boundInstruments_[KvToString(labelkv)].get()) + ->GetAggregator() + ->get_values()[0], + 12340 * 2); + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundUpDownCounter *, + alpha->boundInstruments_[KvToString(labelkv1)].get()) + ->GetAggregator() + ->get_values()[0], + 56780 - 12340); } void RecorderCallback(std::shared_ptr> in, @@ -396,47 +410,47 @@ TEST(IntValueRecorder, StressRecord) third.join(); fourth.join(); - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv)].get()) - ->GetAggregator() - ->get_values()[0], - 0); // min - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv)].get()) - ->GetAggregator() - ->get_values()[1], - 49); // max - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv)].get()) - ->GetAggregator() - ->get_values()[2], - 1525); // sum - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv)].get()) - ->GetAggregator() - ->get_values()[3], - 75); // count - - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv1)].get()) - ->GetAggregator() - ->get_values()[0], - -99); // min - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv1)].get()) - ->GetAggregator() - ->get_values()[1], - 24); // max - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv1)].get()) - ->GetAggregator() - ->get_values()[2], - -4650); // sum - EXPECT_EQ( - dynamic_cast *>(alpha->boundInstruments_[KvToString(labelkv1)].get()) - ->GetAggregator() - ->get_values()[3], - 125); // count + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv)].get()) + ->GetAggregator() + ->get_values()[0], + 0); // min + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv)].get()) + ->GetAggregator() + ->get_values()[1], + 49); // max + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv)].get()) + ->GetAggregator() + ->get_values()[2], + 1525); // sum + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv)].get()) + ->GetAggregator() + ->get_values()[3], + 75); // count + + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv1)].get()) + ->GetAggregator() + ->get_values()[0], + -99); // min + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv1)].get()) + ->GetAggregator() + ->get_values()[1], + 24); // max + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv1)].get()) + ->GetAggregator() + ->get_values()[2], + -4650); // sum + EXPECT_EQ(METRICS_TEST_TYPE_CAST(BoundValueRecorder *, + alpha->boundInstruments_[KvToString(labelkv1)].get()) + ->GetAggregator() + ->get_values()[3], + 125); // count } TEST(Instruments, NoUpdateNoRecord) diff --git a/sdk/test/metrics/aggregation_test.cc b/sdk/test/metrics/aggregation_test.cc index a32826b145..f8051776d1 100644 --- a/sdk/test/metrics/aggregation_test.cc +++ b/sdk/test/metrics/aggregation_test.cc @@ -90,6 +90,37 @@ TEST(Aggregation, LongHistogramAggregation) EXPECT_EQ(histogram_data.count_, 4); EXPECT_EQ(histogram_data.counts_[3], 2); EXPECT_EQ(histogram_data.counts_[8], 1); + + // Merge + LongHistogramAggregation aggr1; + aggr1.Aggregate(1l, {}); + aggr1.Aggregate(11l, {}); + aggr1.Aggregate(26l, {}); + + LongHistogramAggregation aggr2; + aggr2.Aggregate(2l, {}); + aggr2.Aggregate(3l, {}); + aggr2.Aggregate(13l, {}); + aggr2.Aggregate(28l, {}); + aggr2.Aggregate(105l, {}); + + auto aggr3 = aggr1.Merge(aggr2); + histogram_data = nostd::get(aggr3->ToPoint()); + + EXPECT_EQ(histogram_data.count_, 8); // 3 each from aggr1 and aggr2 + EXPECT_EQ(histogram_data.counts_[1], 3); // 1, 2, 3 + EXPECT_EQ(histogram_data.counts_[3], 2); // 11, 13 + EXPECT_EQ(histogram_data.counts_[4], 2); // 25, 28 + EXPECT_EQ(histogram_data.counts_[7], 1); // 105 + + // Diff + auto aggr4 = aggr1.Diff(aggr2); + histogram_data = nostd::get(aggr4->ToPoint()); + EXPECT_EQ(histogram_data.count_, 2); // aggr2:5 - aggr1:3 + EXPECT_EQ(histogram_data.counts_[1], 1); // aggr2(2, 3) - aggr1(1) + EXPECT_EQ(histogram_data.counts_[3], 0); // aggr2(13) - aggr1(11) + EXPECT_EQ(histogram_data.counts_[4], 0); // aggr2(28) - aggr1(25) + EXPECT_EQ(histogram_data.counts_[7], 1); // aggr2(105) - aggr1(0) } TEST(Aggregation, DoubleHistogramAggregation) @@ -116,5 +147,36 @@ TEST(Aggregation, DoubleHistogramAggregation) EXPECT_EQ(histogram_data.counts_[3], 2); EXPECT_EQ(histogram_data.counts_[8], 1); EXPECT_EQ(nostd::get(histogram_data.sum_), 377); + + // Merge + DoubleHistogramAggregation aggr1; + aggr1.Aggregate(1.0, {}); + aggr1.Aggregate(11.0, {}); + aggr1.Aggregate(25.1, {}); + + DoubleHistogramAggregation aggr2; + aggr2.Aggregate(2.0, {}); + aggr2.Aggregate(3.0, {}); + aggr2.Aggregate(13.0, {}); + aggr2.Aggregate(28.1, {}); + aggr2.Aggregate(105.0, {}); + + auto aggr3 = aggr1.Merge(aggr2); + histogram_data = nostd::get(aggr3->ToPoint()); + + EXPECT_EQ(histogram_data.count_, 8); // 3 each from aggr1 and aggr2 + EXPECT_EQ(histogram_data.counts_[1], 3); // 1.0, 2.0, 3.0 + EXPECT_EQ(histogram_data.counts_[3], 2); // 11.0, 13.0 + EXPECT_EQ(histogram_data.counts_[4], 2); // 25.1, 28.1 + EXPECT_EQ(histogram_data.counts_[7], 1); // 105.0 + + // Diff + auto aggr4 = aggr1.Diff(aggr2); + histogram_data = nostd::get(aggr4->ToPoint()); + EXPECT_EQ(histogram_data.count_, 2); // aggr2:5 - aggr1:3 + EXPECT_EQ(histogram_data.counts_[1], 1); // aggr2(2.0, 3.0) - aggr1(1.0) + EXPECT_EQ(histogram_data.counts_[3], 0); // aggr2(13.0) - aggr1(11.0) + EXPECT_EQ(histogram_data.counts_[4], 0); // aggr2(28.1) - aggr1(25.1) + EXPECT_EQ(histogram_data.counts_[7], 1); // aggr2(105.0) - aggr1(0) } -#endif \ No newline at end of file +#endif diff --git a/sdk/test/metrics/attributes_hashmap_benchmark.cc b/sdk/test/metrics/attributes_hashmap_benchmark.cc index 68160aa579..38d515a7ea 100644 --- a/sdk/test/metrics/attributes_hashmap_benchmark.cc +++ b/sdk/test/metrics/attributes_hashmap_benchmark.cc @@ -27,13 +27,12 @@ void BM_AttributseHashMap(benchmark::State &state) std::function()> create_default_aggregation = []() -> std::unique_ptr { - auto agg = std::unique_ptr(new DropAggregation); - return std::move(agg); + return std::unique_ptr(new DropAggregation); }; while (state.KeepRunning()) { - for (int i = 0; i < MAX_THREADS; i++) + for (size_t i = 0; i < MAX_THREADS; i++) { workers.push_back(std::thread([&]() { hash_map.GetOrSetDefault(attributes[i % 2], create_default_aggregation)->Aggregate(1l); diff --git a/sdk/test/metrics/attributes_hashmap_test.cc b/sdk/test/metrics/attributes_hashmap_test.cc index 454ece1ea0..610744c8e1 100644 --- a/sdk/test/metrics/attributes_hashmap_test.cc +++ b/sdk/test/metrics/attributes_hashmap_test.cc @@ -49,8 +49,7 @@ TEST(AttributesHashMap, BasicTests) // GetOrSetDefault std::function()> create_default_aggregation = []() -> std::unique_ptr { - auto agg = std::unique_ptr(new DropAggregation); - return std::move(agg); + return std::unique_ptr(new DropAggregation); }; MetricAttributes m4 = {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}; EXPECT_NO_THROW(hash_map.GetOrSetDefault(m4, create_default_aggregation)->Aggregate(1l)); diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index 3c059cc44c..4a2792c3a7 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -68,7 +68,7 @@ TEST(MeterProvider, GetMeter) ASSERT_NE(m3, m6); // Should be an sdk::trace::Tracer with the processor attached. -# ifdef RTTI_ENABLED +# ifdef OPENTELEMETRY_RTTI_ENABLED auto sdkMeter1 = dynamic_cast(m1.get()); # else auto sdkMeter1 = static_cast(m1.get()); diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index b0440fdbb3..7dfc4f9475 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -130,10 +130,10 @@ TEST_P(WritableMetricStorageTestFixture, LongSumAggregation) return true; }); } -INSTANTIATE_TEST_CASE_P(WritableMetricStorageTestLong, - WritableMetricStorageTestFixture, - ::testing::Values(AggregationTemporality::kCumulative, - AggregationTemporality::kDelta)); +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative, + AggregationTemporality::kDelta)); TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) { @@ -238,9 +238,9 @@ TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) return true; }); } -INSTANTIATE_TEST_CASE_P(WritableMetricStorageTestDouble, - WritableMetricStorageTestFixture, - ::testing::Values(AggregationTemporality::kCumulative, - AggregationTemporality::kDelta)); +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestDouble, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative, + AggregationTemporality::kDelta)); #endif diff --git a/sdk/test/trace/tracer_provider_test.cc b/sdk/test/trace/tracer_provider_test.cc index d72d6bd40f..498f66127d 100644 --- a/sdk/test/trace/tracer_provider_test.cc +++ b/sdk/test/trace/tracer_provider_test.cc @@ -39,7 +39,7 @@ TEST(TracerProvider, GetTracer) ASSERT_NE(t3, t6); // Should be an sdk::trace::Tracer with the processor attached. -#ifdef RTTI_ENABLED +#ifdef OPENTELEMETRY_RTTI_ENABLED auto sdkTracer1 = dynamic_cast(t1.get()); #else auto sdkTracer1 = static_cast(t1.get()); @@ -53,7 +53,7 @@ TEST(TracerProvider, GetTracer) std::make_shared(std::move(processors2), Resource::Create({}), std::unique_ptr(new AlwaysOffSampler()), std::unique_ptr(new RandomIdGenerator))); -#ifdef RTTI_ENABLED +#ifdef OPENTELEMETRY_RTTI_ENABLED auto sdkTracer2 = dynamic_cast(tp2.GetTracer("test").get()); #else auto sdkTracer2 = static_cast(tp2.GetTracer("test").get()); @@ -65,7 +65,7 @@ TEST(TracerProvider, GetTracer) ASSERT_EQ(instrumentation_library1.GetVersion(), ""); // Should be an sdk::trace::Tracer with the processor attached. -#ifdef RTTI_ENABLED +#ifdef OPENTELEMETRY_RTTI_ENABLED auto sdkTracer3 = dynamic_cast(t3.get()); #else auto sdkTracer3 = static_cast(t3.get()); diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index dc806b079f..15a7566b9b 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -347,7 +347,7 @@ TEST(Tracer, GetSampler) // Create a Tracer with a default AlwaysOnSampler auto tracer_on = initTracer(nullptr); -#ifdef RTTI_ENABLED +#ifdef OPENTELEMETRY_RTTI_ENABLED auto &t1 = std::dynamic_pointer_cast(tracer_on)->GetSampler(); #else auto &t1 = std::static_pointer_cast(tracer_on)->GetSampler(); @@ -357,7 +357,7 @@ TEST(Tracer, GetSampler) // Create a Tracer with a AlwaysOffSampler auto tracer_off = initTracer(nullptr, new AlwaysOffSampler()); -#ifdef RTTI_ENABLED +#ifdef OPENTELEMETRY_RTTI_ENABLED auto &t2 = std::dynamic_pointer_cast(tracer_off)->GetSampler(); #else auto &t2 = std::static_pointer_cast(tracer_off)->GetSampler(); diff --git a/tools/build.cmd b/tools/build.cmd index 7558f4efff..5a47d87764 100644 --- a/tools/build.cmd +++ b/tools/build.cmd @@ -170,5 +170,5 @@ REM REM ########################################################################################## :build_config_ninja cmake -G "Ninja" -DCMAKE_MAKE_PROGRAM="!NINJA!" -DCMAKE_TOOLCHAIN_FILE="!VCPKG_CMAKE!" !CONFIG! "!ROOT!" -%NINJA% +"%NINJA%" exit /b