Skip to content

Commit 6dfbf75

Browse files
committed
use Abseil's string_view in Bazel builds, for Envoy.
1 parent 523f704 commit 6dfbf75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+205
-146
lines changed

BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cc_library(
3838
"src/datadog/span_matcher.cpp",
3939
"src/datadog/span_sampler_config.cpp",
4040
"src/datadog/span_sampler.cpp",
41+
"src/datadog/string_view.cpp",
4142
"src/datadog/tag_propagation.cpp",
4243
"src/datadog/tags.cpp",
4344
"src/datadog/threaded_event_scheduler.cpp",
@@ -87,6 +88,7 @@ cc_library(
8788
"src/datadog/span_matcher.h",
8889
"src/datadog/span_sampler_config.h",
8990
"src/datadog/span_sampler.h",
91+
"src/datadog/string_view.h",
9092
"src/datadog/tag_propagation.h",
9193
"src/datadog/tags.h",
9294
"src/datadog/threaded_event_scheduler.h",
@@ -103,7 +105,11 @@ cc_library(
103105
"-Werror",
104106
"-pedantic",
105107
"-std=c++17",
108+
"-DDD_USE_ABSEIL_FOR_ENVOY",
106109
],
107110
strip_include_prefix = "src/",
108111
visibility = ["//visibility:public"],
112+
deps = [
113+
"@com_google_absl//absl/strings",
114+
],
109115
)

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ target_sources(dd_trace_cpp PRIVATE
9090
src/datadog/span_matcher.cpp
9191
src/datadog/span_sampler_config.cpp
9292
src/datadog/span_sampler.cpp
93+
src/datadog/string_view.cpp
9394
src/datadog/tag_propagation.cpp
9495
src/datadog/tags.cpp
9596
src/datadog/threaded_event_scheduler.cpp
@@ -145,6 +146,7 @@ target_sources(dd_trace_cpp PUBLIC
145146
src/datadog/span_matcher.h
146147
src/datadog/span_sampler_config.h
147148
src/datadog/span_sampler.h
149+
src/datadog/string_view.h
148150
src/datadog/tag_propagation.h
149151
src/datadog/tags.h
150152
src/datadog/threaded_event_scheduler.h

WORKSPACE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# The Bazel build is primarily for use by Envoy.
2+
#
3+
# Envoy forbids use of std::string_view and std::optional, preferring use of
4+
# Abseil's absl::string_view and absl::optional instead.
5+
#
6+
# In the context of an Envoy build, the Abseil libraries point to whatever
7+
# versions Envoy uses.
8+
#
9+
# To test this library's Bazel build independent of Envoy, we need to specify
10+
# versions of the Abseil libraries. That is what this file is for.
11+
12+
# These rules are based on <https://abseil.io/docs/cpp/quickstart>,
13+
# accessed December 6, 2022.
14+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
15+
16+
http_archive(
17+
name = "com_google_absl",
18+
urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"],
19+
sha256 = "aabf6c57e3834f8dc3873a927f37eaf69975d4b28117fc7427dfb1c661542a87",
20+
strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea",
21+
)
22+
23+
http_archive(
24+
name = "bazel_skylib",
25+
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.1/bazel-skylib-1.2.1.tar.gz"],
26+
sha256 = "f7be3474d42aae265405a592bb7da8e171919d74c16f082a5457840f06054728",
27+
)

WORKSPACE.bazel

Whitespace-only changes.

src/datadog/collector_response.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace datadog {
44
namespace tracing {
55

6-
std::string CollectorResponse::key(std::string_view service,
7-
std::string_view environment) {
6+
std::string CollectorResponse::key(StringView service,
7+
StringView environment) {
88
std::string result;
99
result += "service:";
1010
result += service;

src/datadog/collector_response.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// information.
1515

1616
#include <string>
17-
#include <string_view>
17+
#include "string_view.h"
1818
#include <unordered_map>
1919

2020
#include "rate.h"
@@ -23,8 +23,8 @@ namespace datadog {
2323
namespace tracing {
2424

2525
struct CollectorResponse {
26-
static std::string key(std::string_view service,
27-
std::string_view environment);
26+
static std::string key(StringView service,
27+
StringView environment);
2828
static const std::string key_of_default_rate;
2929
std::unordered_map<std::string, Rate> sample_rate_by_key;
3030
};

src/datadog/curl.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <list>
1212
#include <memory>
1313
#include <mutex>
14-
#include <string_view>
14+
#include "string_view.h"
1515
#include <thread>
1616
#include <unordered_map>
1717
#include <unordered_set>
@@ -61,7 +61,7 @@ class CurlImpl {
6161
public:
6262
~HeaderWriter();
6363
curl_slist *release();
64-
void set(std::string_view key, std::string_view value) override;
64+
void set(StringView key, StringView value) override;
6565
};
6666

6767
class HeaderReader : public DictReader {
@@ -71,9 +71,9 @@ class CurlImpl {
7171
public:
7272
explicit HeaderReader(
7373
std::unordered_map<std::string, std::string> *response_headers_lower);
74-
std::optional<std::string_view> lookup(std::string_view key) const override;
74+
std::optional<StringView> lookup(StringView key) const override;
7575
void visit(
76-
const std::function<void(std::string_view key, std::string_view value)>
76+
const std::function<void(StringView key, StringView value)>
7777
&visitor) const override;
7878
};
7979

@@ -88,7 +88,7 @@ class CurlImpl {
8888
void *user_data);
8989
static bool is_non_whitespace(unsigned char);
9090
static char to_lower(unsigned char);
91-
static std::string_view trim(std::string_view);
91+
static StringView trim(StringView);
9292

9393
public:
9494
explicit CurlImpl(const std::shared_ptr<Logger> &logger);
@@ -426,7 +426,7 @@ curl_slist *CurlImpl::HeaderWriter::release() {
426426
return list;
427427
}
428428

429-
void CurlImpl::HeaderWriter::set(std::string_view key, std::string_view value) {
429+
void CurlImpl::HeaderWriter::set(StringView key, StringView value) {
430430
buffer_.clear();
431431
buffer_ += key;
432432
buffer_ += ": ";
@@ -439,8 +439,8 @@ CurlImpl::HeaderReader::HeaderReader(
439439
std::unordered_map<std::string, std::string> *response_headers_lower)
440440
: response_headers_lower_(response_headers_lower) {}
441441

442-
std::optional<std::string_view> CurlImpl::HeaderReader::lookup(
443-
std::string_view key) const {
442+
std::optional<StringView> CurlImpl::HeaderReader::lookup(
443+
StringView key) const {
444444
buffer_.clear();
445445
std::transform(key.begin(), key.end(), std::back_inserter(buffer_),
446446
&to_lower);
@@ -453,7 +453,7 @@ std::optional<std::string_view> CurlImpl::HeaderReader::lookup(
453453
}
454454

455455
void CurlImpl::HeaderReader::visit(
456-
const std::function<void(std::string_view key, std::string_view value)>
456+
const std::function<void(StringView key, StringView value)>
457457
&visitor) const {
458458
for (const auto &[key, value] : *response_headers_lower_) {
459459
visitor(key, value);

src/datadog/datadog_agent.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace datadog {
2121
namespace tracing {
2222
namespace {
2323

24-
const std::string_view traces_api_path = "/v0.4/traces";
24+
const StringView traces_api_path = "/v0.4/traces";
2525

2626
HTTPClient::URL traces_endpoint(const HTTPClient::URL& agent_url) {
2727
auto traces_url = agent_url;
@@ -49,10 +49,10 @@ Expected<void> msgpack_encode(
4949
}
5050

5151
std::variant<CollectorResponse, std::string> parse_agent_traces_response(
52-
std::string_view body) try {
52+
StringView body) try {
5353
nlohmann::json response = nlohmann::json::parse(body);
5454

55-
std::string_view type = response.type_name();
55+
StringView type = response.type_name();
5656
if (type != "object") {
5757
std::string message;
5858
message +=
@@ -66,7 +66,7 @@ std::variant<CollectorResponse, std::string> parse_agent_traces_response(
6666
return message;
6767
}
6868

69-
const std::string_view sample_rates_property = "rate_by_service";
69+
const StringView sample_rates_property = "rate_by_service";
7070
const auto found = response.find(sample_rates_property);
7171
if (found == response.end()) {
7272
return CollectorResponse{};

src/datadog/datadog_agent_config.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
namespace datadog {
1212
namespace tracing {
1313

14-
Expected<HTTPClient::URL> DatadogAgentConfig::parse(std::string_view input) {
15-
const std::string_view separator = "://";
14+
Expected<HTTPClient::URL> DatadogAgentConfig::parse(StringView input) {
15+
const StringView separator = "://";
1616
const auto after_scheme = std::search(input.begin(), input.end(),
1717
separator.begin(), separator.end());
1818
if (after_scheme == input.end()) {
@@ -23,8 +23,8 @@ Expected<HTTPClient::URL> DatadogAgentConfig::parse(std::string_view input) {
2323
return Error{Error::URL_MISSING_SEPARATOR, std::move(message)};
2424
}
2525

26-
const std::string_view scheme = range(input.begin(), after_scheme);
27-
const std::string_view supported[] = {"http", "https", "unix", "http+unix",
26+
const StringView scheme = range(input.begin(), after_scheme);
27+
const StringView supported[] = {"http", "https", "unix", "http+unix",
2828
"https+unix"};
2929
const auto found =
3030
std::find(std::begin(supported), std::end(supported), scheme);
@@ -42,7 +42,7 @@ Expected<HTTPClient::URL> DatadogAgentConfig::parse(std::string_view input) {
4242
return Error{Error::URL_UNSUPPORTED_SCHEME, std::move(message)};
4343
}
4444

45-
const std::string_view authority_and_path =
45+
const StringView authority_and_path =
4646
range(after_scheme + separator.size(), input.end());
4747
// If the scheme is for unix domain sockets, then there's no way to
4848
// distinguish the path-to-socket from the path-to-resource. Some

src/datadog/datadog_agent_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <chrono>
1515
#include <memory>
1616
#include <string>
17-
#include <string_view>
17+
#include "string_view.h"
1818
#include <variant>
1919

2020
#include "expected.h"
@@ -50,7 +50,7 @@ struct DatadogAgentConfig {
5050
// How often, in milliseconds, to send batches of traces to the Datadog Agent.
5151
int flush_interval_milliseconds = 2000;
5252

53-
static Expected<HTTPClient::URL> parse(std::string_view);
53+
static Expected<HTTPClient::URL> parse(StringView);
5454
};
5555

5656
class FinalizedDatadogAgentConfig {

0 commit comments

Comments
 (0)