Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 60b0e4c

Browse files
authored
refactor: copy TracingOptions from spanner (#161)
Copy the `TracingOptions` class from `google-cloud-cpp-spanner`. I need them in `google-cloud-cpp-pubsub`, and it seems terrible to just copy the code. The plan is to first copy the classes here (so we can start using them in `g-c-cpp-pubsub`) and then create aliases in `g-c-cpp-spanner` that point to the classes in this repository.
1 parent 1c4e283 commit 60b0e4c

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

google/cloud/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ add_library(
169169
status_or.h
170170
terminate_handler.cc
171171
terminate_handler.h
172+
tracing_options.h
173+
tracing_options.cc
172174
version.cc
173175
version.h)
174176
target_link_libraries(
@@ -230,7 +232,8 @@ if (BUILD_TESTING)
230232
optional_test.cc
231233
status_or_test.cc
232234
status_test.cc
233-
terminate_handler_test.cc)
235+
terminate_handler_test.cc
236+
tracing_options_test.cc)
234237

235238
# Export the list of unit tests so the Bazel BUILD file can pick it up.
236239
export_list_to_bazel("google_cloud_cpp_common_unit_tests.bzl"

google/cloud/google_cloud_cpp_common.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ google_cloud_cpp_common_hdrs = [
5454
"status.h",
5555
"status_or.h",
5656
"terminate_handler.h",
57+
"tracing_options.h",
5758
"version.h",
5859
]
5960

@@ -73,5 +74,6 @@ google_cloud_cpp_common_srcs = [
7374
"log.cc",
7475
"status.cc",
7576
"terminate_handler.cc",
77+
"tracing_options.cc",
7678
"version.cc",
7779
]

google/cloud/google_cloud_cpp_common_unit_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ google_cloud_cpp_common_unit_tests = [
4141
"status_or_test.cc",
4242
"status_test.cc",
4343
"terminate_handler_test.cc",
44+
"tracing_options_test.cc",
4445
]

google/cloud/tracing_options.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/tracing_options.h"
16+
#include "google/cloud/optional.h"
17+
#include <algorithm>
18+
#include <cstdint>
19+
#include <string>
20+
21+
namespace google {
22+
namespace cloud {
23+
inline namespace GOOGLE_CLOUD_CPP_NS {
24+
namespace {
25+
26+
optional<bool> ParseBoolean(std::string const& str) {
27+
for (const auto t : {"Y", "y", "T", "t", "1", "on"}) {
28+
if (str == t) return true;
29+
}
30+
for (const auto f : {"N", "n", "F", "f", "0", "off"}) {
31+
if (str == f) return false;
32+
}
33+
return {};
34+
}
35+
36+
optional<std::int64_t> ParseInteger(std::string const& str) {
37+
std::size_t econv = -1;
38+
auto val = std::stoll(str, &econv);
39+
if (econv != str.size()) return {};
40+
return val;
41+
}
42+
43+
} // namespace
44+
45+
TracingOptions& TracingOptions::SetOptions(std::string const& str) {
46+
auto const beg = str.begin();
47+
auto const end = str.end();
48+
for (auto pos = beg;;) {
49+
auto const comma = std::find(pos, end, ',');
50+
auto const equal = std::find(pos, comma, '=');
51+
std::string const opt{pos, equal};
52+
std::string const val{equal + (equal == comma ? 0 : 1), comma};
53+
if (opt == "single_line_mode") {
54+
if (auto v = ParseBoolean(val)) single_line_mode_ = *v;
55+
} else if (opt == "use_short_repeated_primitives") {
56+
if (auto v = ParseBoolean(val)) use_short_repeated_primitives_ = *v;
57+
} else if (opt == "truncate_string_field_longer_than") {
58+
if (auto v = ParseInteger(val)) truncate_string_field_longer_than_ = *v;
59+
}
60+
if (comma == end) break;
61+
pos = comma + 1;
62+
}
63+
return *this;
64+
}
65+
66+
} // namespace GOOGLE_CLOUD_CPP_NS
67+
} // namespace cloud
68+
} // namespace google

google/cloud/tracing_options.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TRACING_OPTIONS_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TRACING_OPTIONS_H
17+
18+
#include "google/cloud/version.h"
19+
#include <cstdint>
20+
#include <string>
21+
22+
namespace google {
23+
namespace cloud {
24+
inline namespace GOOGLE_CLOUD_CPP_NS {
25+
/**
26+
* The configuration parameters for RPC/protobuf tracing.
27+
*
28+
* The default options are:
29+
* single_line_mode=on
30+
* use_short_repeated_primitives=on
31+
* truncate_string_field_longer_than=128
32+
*/
33+
class TracingOptions {
34+
public:
35+
/// Override the default options with values from @p `str`.
36+
TracingOptions& SetOptions(std::string const& str);
37+
38+
/// The entire message will be output on a single line with no line breaks.
39+
bool single_line_mode() const { return single_line_mode_; }
40+
41+
/// Print repeated primitives in a compact format instead of each value on
42+
/// its own line.
43+
bool use_short_repeated_primitives() const {
44+
return use_short_repeated_primitives_;
45+
}
46+
47+
/// If non-zero, truncate all string/bytes fields longer than this.
48+
std::int64_t truncate_string_field_longer_than() const {
49+
return truncate_string_field_longer_than_;
50+
}
51+
52+
private:
53+
bool single_line_mode_ = true;
54+
bool use_short_repeated_primitives_ = true;
55+
std::int64_t truncate_string_field_longer_than_ = 128;
56+
};
57+
58+
} // namespace GOOGLE_CLOUD_CPP_NS
59+
} // namespace cloud
60+
} // namespace google
61+
62+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_TRACING_OPTIONS_H
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/tracing_options.h"
16+
#include <gmock/gmock.h>
17+
18+
namespace google {
19+
namespace cloud {
20+
inline namespace GOOGLE_CLOUD_CPP_NS {
21+
namespace {
22+
23+
TEST(TracingOptionsTest, Defaults) {
24+
TracingOptions tracing_options;
25+
EXPECT_TRUE(tracing_options.single_line_mode());
26+
EXPECT_TRUE(tracing_options.use_short_repeated_primitives());
27+
EXPECT_EQ(128, tracing_options.truncate_string_field_longer_than());
28+
29+
// Unknown/unparseable options are ignored.
30+
tracing_options.SetOptions("foo=1,bar=T,baz=no");
31+
EXPECT_TRUE(tracing_options.single_line_mode());
32+
EXPECT_TRUE(tracing_options.use_short_repeated_primitives());
33+
EXPECT_EQ(128, tracing_options.truncate_string_field_longer_than());
34+
}
35+
36+
TEST(TracingOptionsTest, Override) {
37+
TracingOptions tracing_options;
38+
tracing_options.SetOptions(
39+
",single_line_mode=F"
40+
",use_short_repeated_primitives=n"
41+
",truncate_string_field_longer_than=256");
42+
EXPECT_FALSE(tracing_options.single_line_mode());
43+
EXPECT_FALSE(tracing_options.use_short_repeated_primitives());
44+
EXPECT_EQ(256, tracing_options.truncate_string_field_longer_than());
45+
}
46+
47+
} // namespace
48+
} // namespace GOOGLE_CLOUD_CPP_NS
49+
} // namespace cloud
50+
} // namespace google

0 commit comments

Comments
 (0)