diff --git a/ci/do_ci.sh b/ci/do_ci.sh index c6d638ffe3..791cc3b48a 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -306,9 +306,13 @@ elif [[ "$1" == "bazel.nortti" ]]; then exit 0 elif [[ "$1" == "bazel.asan" ]]; then bazel $BAZEL_STARTUP_OPTIONS test --config=asan $BAZEL_TEST_OPTIONS_ASYNC //... + bazel $BAZEL_STARTUP_OPTIONS run --config=asan $BAZEL_TEST_OPTIONS_ASYNC \ + //examples/metrics_simple:metrics_ostream_example > /dev/null exit 0 elif [[ "$1" == "bazel.tsan" ]]; then bazel $BAZEL_STARTUP_OPTIONS test --config=tsan $BAZEL_TEST_OPTIONS_ASYNC //... + bazel $BAZEL_STARTUP_OPTIONS run --config=tsan $BAZEL_TEST_OPTIONS_ASYNC \ + //examples/metrics_simple:metrics_ostream_example > /dev/null exit 0 elif [[ "$1" == "bazel.valgrind" ]]; then bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS_ASYNC //... diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 3a323a0545..4b4ea98fd2 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -18,13 +18,15 @@ namespace metrics_api = opentelemetry::metrics; namespace { +static nostd::shared_ptr double_observable_counter; + std::map get_random_attr() { - static const std::vector> labels = {{"key1", "value1"}, - {"key2", "value2"}, - {"key3", "value3"}, - {"key4", "value4"}, - {"key5", "value5"}}; + 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)]}; } @@ -34,8 +36,6 @@ class MeasurementFetcher public: static void Fetcher(opentelemetry::metrics::ObserverResult observer_result, void * /* state */) { - std::map labels = get_random_attr(); - auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; if (nostd::holds_alternative< nostd::shared_ptr>>(observer_result)) { @@ -58,7 +58,7 @@ void foo_library::counter_example(const std::string &name) nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); auto double_counter = meter->CreateDoubleCounter(counter_name); - while (true) + for (uint32_t i = 0; i < 20; ++i) { double val = (rand() % 700) + 1.1; double_counter->Add(val); @@ -71,9 +71,9 @@ void foo_library::observable_counter_example(const std::string &name) std::string counter_name = name + "_observable_counter"; auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); - auto counter = meter->CreateDoubleObservableCounter(counter_name); - counter->AddCallback(MeasurementFetcher::Fetcher, nullptr); - while (true) + double_observable_counter = meter->CreateDoubleObservableCounter(counter_name); + double_observable_counter->AddCallback(MeasurementFetcher::Fetcher, nullptr); + for (uint32_t i = 0; i < 20; ++i) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); } @@ -86,7 +86,7 @@ void foo_library::histogram_example(const std::string &name) nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); auto histogram_counter = meter->CreateDoubleHistogram(histogram_name, "des", "unit"); auto context = opentelemetry::context::Context{}; - while (true) + for (uint32_t i = 0; i < 20; ++i) { double val = (rand() % 700) + 1.1; std::map labels = get_random_attr();