Skip to content

Commit fa5f9fc

Browse files
authored
Add CMake OTELCPP_MAINTAINER_MODE (#1650)
1 parent 89eb1ec commit fa5f9fc

File tree

12 files changed

+236
-65
lines changed

12 files changed

+236
-65
lines changed

.github/workflows/ci.yml

+46
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,52 @@ jobs:
2323
sudo ./ci/setup_thrift.sh
2424
./ci/do_ci.sh cmake.test
2525
26+
cmake_gcc_maintainer_test:
27+
name: CMake gcc (maintainer mode)
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v3
31+
with:
32+
submodules: 'recursive'
33+
- name: setup
34+
run: |
35+
sudo CC=/usr/bin/gcc-10 CXX=/usr/bin/g++-10 ./ci/setup_cmake.sh
36+
sudo CC=/usr/bin/gcc-10 CXX=/usr/bin/g++-10 ./ci/setup_ci_environment.sh
37+
- name: run cmake gcc (maintainer mode)
38+
run: |
39+
sudo CC=/usr/bin/gcc-10 CXX=/usr/bin/g++-10 ./ci/setup_thrift.sh
40+
CC=/usr/bin/gcc-10 CXX=/usr/bin/g++-10 ./ci/do_ci.sh cmake.maintainer.test
41+
42+
cmake_clang_maintainer_test:
43+
name: CMake clang (maintainer mode)
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v3
47+
with:
48+
submodules: 'recursive'
49+
- name: setup
50+
run: |
51+
sudo CC=/usr/bin/clang-12 CXX=/usr/bin/clang++-12 ./ci/setup_cmake.sh
52+
sudo CC=/usr/bin/clang-12 CXX=/usr/bin/clang++-12 ./ci/setup_ci_environment.sh
53+
- name: run cmake clang (maintainer mode)
54+
run: |
55+
sudo CC=/usr/bin/clang-12 CXX=/usr/bin/clang++-12 ./ci/setup_thrift.sh
56+
CC=/usr/bin/clang-12 CXX=/usr/bin/clang++-12 ./ci/do_ci.sh cmake.maintainer.test
57+
58+
cmake_msvc_maintainer_test:
59+
name: CMake msvc (maintainer mode)
60+
runs-on: windows-latest
61+
steps:
62+
- uses: actions/checkout@v3
63+
with:
64+
submodules: 'recursive'
65+
- name: setup
66+
run: |
67+
./ci/setup_windows_cmake.ps1
68+
./ci/setup_windows_ci_environment.ps1
69+
- name: run tests
70+
run: ./ci/do_ci.ps1 cmake.maintainer.test
71+
2672
cmake_deprecated_metrics_test:
2773
name: CMake test (without otlp-exporter and with deprecated metrics)
2874
runs-on: ubuntu-latest

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [BUILD] Add CMake OTELCPP_MAINTAINER_MODE [#1650](https://github.com/open-telemetry/opentelemetry-cpp/pull/1650)
19+
1820
## [1.6.1] 2022-09-22
1921

2022
* [BUILD] Upgrade opentelemetry-proto to v0.19.0 [#1579](https://github.com/open-telemetry/opentelemetry-cpp/pull/1579)

CMakeLists.txt

+84
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ option(BUILD_TESTING "Whether to enable tests" ON)
166166

167167
option(BUILD_W3CTRACECONTEXT_TEST "Whether to build w3c trace context" OFF)
168168

169+
option(OTELCPP_MAINTAINER_MODE "Build in maintainer mode (-Wall -Werror)" OFF)
170+
169171
if(WIN32)
170172
if(BUILD_TESTING)
171173
if(MSVC)
@@ -358,6 +360,88 @@ if((NOT WITH_API_ONLY) AND USE_NLOHMANN_JSON)
358360
endif()
359361
endif()
360362

363+
if(OTELCPP_MAINTAINER_MODE)
364+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
365+
message("Building with gcc in maintainer mode.")
366+
367+
add_compile_options(-Wall)
368+
add_compile_options(-Werror)
369+
add_compile_options(-Wextra)
370+
371+
# Tested with GCC 9.4 on github.
372+
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 9.4)
373+
message("Building with additional warnings for gcc.")
374+
375+
# Relaxed warnings
376+
377+
# Enforced warnings
378+
379+
# C++ options only
380+
add_compile_options($<$<STREQUAL:$<COMPILE_LANGUAGE>,CXX>:-Wextra-semi>)
381+
add_compile_options(
382+
$<$<STREQUAL:$<COMPILE_LANGUAGE>,CXX>:-Woverloaded-virtual>)
383+
add_compile_options(
384+
$<$<STREQUAL:$<COMPILE_LANGUAGE>,CXX>:-Wsuggest-override>)
385+
386+
# C and C++
387+
add_compile_options(-Wcast-qual)
388+
add_compile_options(-Wformat-security)
389+
add_compile_options(-Wlogical-op)
390+
add_compile_options(-Wmissing-include-dirs)
391+
add_compile_options(-Wstringop-truncation)
392+
add_compile_options(-Wundef)
393+
add_compile_options(-Wvla)
394+
endif()
395+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
396+
message("Building with clang in maintainer mode.")
397+
398+
add_compile_options(-Wall)
399+
add_compile_options(-Werror)
400+
add_compile_options(-Wextra)
401+
402+
# Tested with Clang 11.0 on github.
403+
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0)
404+
message("Building with additional warnings for clang.")
405+
406+
# Relaxed warnings
407+
add_compile_options(-Wno-error=unused-private-field)
408+
409+
# Enforced warnings
410+
add_compile_options(-Wcast-qual)
411+
add_compile_options(-Wconditional-uninitialized)
412+
add_compile_options(-Wextra-semi)
413+
add_compile_options(-Wformat-security)
414+
add_compile_options(-Wheader-hygiene)
415+
add_compile_options(-Winconsistent-missing-destructor-override)
416+
add_compile_options(-Winconsistent-missing-override)
417+
add_compile_options(-Wnewline-eof)
418+
add_compile_options(-Wnon-virtual-dtor)
419+
add_compile_options(-Woverloaded-virtual)
420+
add_compile_options(-Wrange-loop-analysis)
421+
add_compile_options(-Wundef)
422+
add_compile_options(-Wundefined-reinterpret-cast)
423+
add_compile_options(-Wvla)
424+
endif()
425+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
426+
message("Building with msvc in maintainer mode.")
427+
428+
add_compile_options(/WX)
429+
add_compile_options(/W4)
430+
431+
# Relaxed warnings
432+
add_compile_options(/wd4100)
433+
add_compile_options(/wd4125)
434+
add_compile_options(/wd4566)
435+
add_compile_options(/wd4127)
436+
add_compile_options(/wd4512)
437+
add_compile_options(/wd4267)
438+
439+
# Enforced warnings
440+
elseif()
441+
message(FATAL_ERROR "Building with unknown compiler in maintainer mode.")
442+
endif()
443+
endif(OTELCPP_MAINTAINER_MODE)
444+
361445
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")
362446

363447
include(CTest)

api/test/nostd/unique_ptr_test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ TEST(UniquePtrTest, Reset)
122122
{
123123
bool was_destructed1;
124124
unique_ptr<A> ptr{new A{was_destructed1}};
125-
bool was_destructed2;
125+
bool was_destructed2 = true;
126126
ptr.reset(new A{was_destructed2});
127127
EXPECT_TRUE(was_destructed1);
128128
EXPECT_FALSE(was_destructed2);

api/test/trace/span_benchmark.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ void BM_SpanCreationWitContextPropagation(benchmark::State &state)
115115
nostd::shared_ptr<trace_api::Span>(new trace_api::DefaultSpan(outer_span_context));
116116
trace_api::SetSpan(current_ctx, outer_span);
117117
auto inner_child = tracer->StartSpan("inner");
118-
auto scope = tracer->WithActiveSpan(inner_child);
118+
auto inner_scope = tracer->WithActiveSpan(inner_child);
119119
{
120120
auto innermost_child = tracer->StartSpan("innermost");
121-
auto scope = tracer->WithActiveSpan(innermost_child);
121+
auto innermost_scope = tracer->WithActiveSpan(innermost_child);
122122
innermost_child->End();
123123
}
124124
inner_child->End();

ci/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CI tests can be run on docker by invoking the script `./ci/run_docker.sh
44
./ci/do_ci.sh {TARGET}` where the targets are:
55

66
* `cmake.test`: build cmake targets and run tests.
7+
* `cmake.maintainer.test`: build with cmake and test, in maintainer mode.
78
* `cmake.legacy.test`: build cmake targets with gcc 4.8 and run tests.
89
* `cmake.c++20.test`: build cmake targets with the C++20 standard and run tests.
910
* `cmake.test_example_plugin`: build and test an example OpenTelemetry plugin.

ci/do_ci.ps1

+21
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ switch ($action) {
4848
exit $exit
4949
}
5050
}
51+
"cmake.maintainer.test" {
52+
cd "$BUILD_DIR"
53+
cmake $SRC_DIR `
54+
-DOTELCPP_MAINTAINER_MODE=ON `
55+
-DVCPKG_TARGET_TRIPLET=x64-windows `
56+
"-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake"
57+
$exit = $LASTEXITCODE
58+
if ($exit -ne 0) {
59+
exit $exit
60+
}
61+
cmake --build .
62+
$exit = $LASTEXITCODE
63+
if ($exit -ne 0) {
64+
exit $exit
65+
}
66+
ctest -C Debug
67+
$exit = $LASTEXITCODE
68+
if ($exit -ne 0) {
69+
exit $exit
70+
}
71+
}
5172
"cmake.with_async_export.test" {
5273
cd "$BUILD_DIR"
5374
cmake $SRC_DIR `

ci/do_ci.sh

+16
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ if [[ "$1" == "cmake.test" ]]; then
8989
make
9090
make test
9191
exit 0
92+
elif [[ "$1" == "cmake.maintainer.test" ]]; then
93+
cd "${BUILD_DIR}"
94+
rm -rf *
95+
cmake -DCMAKE_BUILD_TYPE=Debug \
96+
-DWITH_PROMETHEUS=ON \
97+
-DWITH_ZIPKIN=ON \
98+
-DWITH_JAEGER=ON \
99+
-DWITH_ELASTICSEARCH=ON \
100+
-DWITH_LOGS_PREVIEW=ON \
101+
-DWITH_METRICS_PREVIEW=OFF \
102+
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
103+
-DOTELCPP_MAINTAINER_MODE=ON \
104+
"${SRC_DIR}"
105+
make -k
106+
make test
107+
exit 0
92108
elif [[ "$1" == "cmake.with_async_export.test" ]]; then
93109
cd "${BUILD_DIR}"
94110
rm -rf *

sdk/include/opentelemetry/sdk/metrics/exemplar/histogram_exemplar_reservoir.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class HistogramExemplarReservoir : public FixedSizeExemplarReservoir
5555
const MetricAttributes & /* attributes */,
5656
const opentelemetry::context::Context & /* context */) override
5757
{
58-
for (size_t i = 0; i < boundaries_.size(); ++i)
58+
int max_size = boundaries_.size();
59+
for (int i = 0; i < max_size; ++i)
5960
{
6061
if (value <= boundaries_[i])
6162
{

sdk/test/metrics/async_metric_storage_test.cc

+25-25
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,24 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation)
7777
storage.RecordLong(measurements1,
7878
opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()));
7979

80-
storage.Collect(collector.get(), collectors, sdk_start_ts, collection_ts,
81-
[&](const MetricData data) {
82-
for (auto data_attr : data.point_data_attr_)
83-
{
84-
auto data = opentelemetry::nostd::get<SumPointData>(data_attr.point_data);
85-
if (opentelemetry::nostd::get<std::string>(
86-
data_attr.attributes.find("RequestType")->second) == "GET")
87-
{
88-
EXPECT_EQ(opentelemetry::nostd::get<long>(data.value_), get_count1);
89-
}
90-
else if (opentelemetry::nostd::get<std::string>(
91-
data_attr.attributes.find("RequestType")->second) == "PUT")
92-
{
93-
EXPECT_EQ(opentelemetry::nostd::get<long>(data.value_), put_count1);
94-
}
95-
}
96-
return true;
97-
});
80+
storage.Collect(
81+
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
82+
for (const auto &data_attr : metric_data.point_data_attr_)
83+
{
84+
const auto &data = opentelemetry::nostd::get<SumPointData>(data_attr.point_data);
85+
if (opentelemetry::nostd::get<std::string>(
86+
data_attr.attributes.find("RequestType")->second) == "GET")
87+
{
88+
EXPECT_EQ(opentelemetry::nostd::get<long>(data.value_), get_count1);
89+
}
90+
else if (opentelemetry::nostd::get<std::string>(
91+
data_attr.attributes.find("RequestType")->second) == "PUT")
92+
{
93+
EXPECT_EQ(opentelemetry::nostd::get<long>(data.value_), put_count1);
94+
}
95+
}
96+
return true;
97+
});
9898
// subsequent recording after collection shouldn't fail
9999
// monotonic increasing values;
100100
long get_count2 = 50l;
@@ -105,10 +105,10 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation)
105105
storage.RecordLong(measurements2,
106106
opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()));
107107
storage.Collect(
108-
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) {
109-
for (auto data_attr : data.point_data_attr_)
108+
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) {
109+
for (const auto &data_attr : metric_data.point_data_attr_)
110110
{
111-
auto data = opentelemetry::nostd::get<SumPointData>(data_attr.point_data);
111+
const auto &data = opentelemetry::nostd::get<SumPointData>(data_attr.point_data);
112112
if (opentelemetry::nostd::get<std::string>(
113113
data_attr.attributes.find("RequestType")->second) == "GET")
114114
{
@@ -171,8 +171,8 @@ TEST_P(WritableMetricStorageTestObservableGaugeFixture, TestAggregation)
171171
opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()));
172172

173173
storage.Collect(
174-
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) {
175-
for (auto data_attr : data.point_data_attr_)
174+
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData metric_data) {
175+
for (auto data_attr : metric_data.point_data_attr_)
176176
{
177177
auto data = opentelemetry::nostd::get<LastValuePointData>(data_attr.point_data);
178178
if (opentelemetry::nostd::get<std::string>(data_attr.attributes.find("CPU")->second) ==
@@ -197,8 +197,8 @@ TEST_P(WritableMetricStorageTestObservableGaugeFixture, TestAggregation)
197197
storage.RecordLong(measurements2,
198198
opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()));
199199
storage.Collect(
200-
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) {
201-
for (auto data_attr : data.point_data_attr_)
200+
collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData metric_data) {
201+
for (auto data_attr : metric_data.point_data_attr_)
202202
{
203203
auto data = opentelemetry::nostd::get<LastValuePointData>(data_attr.point_data);
204204
if (opentelemetry::nostd::get<std::string>(data_attr.attributes.find("CPU")->second) ==

0 commit comments

Comments
 (0)