Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing: general tracing support #16049

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/envoy/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ envoy_cc_library(
name = "scope_tracker_interface",
hdrs = ["scope_tracker.h"],
)

envoy_cc_library(
name = "lower_case_string",
hdrs = ["lower_case_string.h"],
deps = [
"//include/envoy/http:valid_header",
"//source/common/common:assert_lib",
],
)
76 changes: 76 additions & 0 deletions include/envoy/common/lower_case_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <string>
#include <vector>

#include "envoy/http/valid_header.h"

#include "common/common/assert.h"

#include "absl/strings/ascii.h"
#include "absl/strings/string_view.h"

namespace Envoy {

/*
* Simple function pointer to validate lower case string. There is currently no need to use
* std::function.
*/
using LowerCaseStringValidator = bool (*)(absl::string_view);

/**
* Wrapper for a lower case string used in header operations to generally avoid needless case
* insensitive compares.
*/
class LowerCaseString {
public:
LowerCaseString(LowerCaseString&& rhs,
LowerCaseStringValidator valid = Http::validHeaderString) noexcept
: string_(std::move(rhs.string_)) {
ASSERT(valid != nullptr ? valid(string_) : true);
}
LowerCaseString& operator=(LowerCaseString&& rhs) noexcept {
string_ = std::move(rhs.string_);
return *this;
}

LowerCaseString(const LowerCaseString& rhs,
LowerCaseStringValidator valid = Http::validHeaderString)
: string_(rhs.string_) {
ASSERT(valid != nullptr ? valid(string_) : true);
}
LowerCaseString& operator=(const LowerCaseString& rhs) {
string_ = rhs.string_;
return *this;
}

explicit LowerCaseString(absl::string_view new_string,
LowerCaseStringValidator valid = Http::validHeaderString)
: string_(new_string) {
ASSERT(valid != nullptr ? valid(string_) : true);
lower();
}

const std::string& get() const { return string_; }
bool operator==(const LowerCaseString& rhs) const { return string_ == rhs.string_; }
bool operator!=(const LowerCaseString& rhs) const { return string_ != rhs.string_; }
bool operator<(const LowerCaseString& rhs) const { return string_.compare(rhs.string_) < 0; }

friend std::ostream& operator<<(std::ostream& os, const LowerCaseString& lower_case_string) {
return os << lower_case_string.string_;
}

private:
void lower() {
std::transform(string_.begin(), string_.end(), string_.begin(), absl::ascii_tolower);
}

std::string string_;
};

/**
* Convenient type for a vector of lower case string and string pair.
*/
using LowerCaseStrPairVector = std::vector<std::pair<const LowerCaseString, const std::string>>;

} // namespace Envoy
8 changes: 8 additions & 0 deletions include/envoy/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ envoy_cc_library(
],
deps = [
":header_formatter_interface",
":valid_header",
"//include/envoy/common:lower_case_string",
"//include/envoy/tracing:trace_context_interface",
"//source/common/common:assert_lib",
"//source/common/common:hash_lib",
],
Expand Down Expand Up @@ -151,3 +154,8 @@ envoy_cc_library(
"//include/envoy/config:typed_config_interface",
],
)

envoy_cc_library(
name = "valid_header",
hdrs = ["valid_header.h"],
)
78 changes: 7 additions & 71 deletions include/envoy/http/header_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
#include <string>
#include <vector>

#include "envoy/common/lower_case_string.h"
#include "envoy/common/optref.h"
#include "envoy/common/pure.h"
#include "envoy/http/header_formatter.h"
#include "envoy/http/valid_header.h"
#include "envoy/tracing/trace_context.h"

#include "common/common/assert.h"
#include "common/common/hash.h"
Expand All @@ -22,76 +25,8 @@
namespace Envoy {
namespace Http {

// Used by ASSERTs to validate internal consistency. E.g. valid HTTP header keys/values should
// never contain embedded NULLs.
static inline bool validHeaderString(absl::string_view s) {
// If you modify this list of illegal embedded characters you will probably
// want to change header_map_fuzz_impl_test at the same time.
for (const char c : s) {
switch (c) {
case '\0':
FALLTHRU;
case '\r':
FALLTHRU;
case '\n':
return false;
default:
continue;
}
}
return true;
}

/**
* Wrapper for a lower case string used in header operations to generally avoid needless case
* insensitive compares.
*/
class LowerCaseString {
public:
LowerCaseString(LowerCaseString&& rhs) noexcept : string_(std::move(rhs.string_)) {
ASSERT(valid());
}
LowerCaseString& operator=(LowerCaseString&& rhs) noexcept {
string_ = std::move(rhs.string_);
ASSERT(valid());
return *this;
}

LowerCaseString(const LowerCaseString& rhs) : string_(rhs.string_) { ASSERT(valid()); }
LowerCaseString& operator=(const LowerCaseString& rhs) {
string_ = std::move(rhs.string_);
ASSERT(valid());
return *this;
}

explicit LowerCaseString(const std::string& new_string) : string_(new_string) {
ASSERT(valid());
lower();
}

const std::string& get() const { return string_; }
bool operator==(const LowerCaseString& rhs) const { return string_ == rhs.string_; }
bool operator!=(const LowerCaseString& rhs) const { return string_ != rhs.string_; }
bool operator<(const LowerCaseString& rhs) const { return string_.compare(rhs.string_) < 0; }

friend std::ostream& operator<<(std::ostream& os, const LowerCaseString& lower_case_string) {
return os << lower_case_string.string_;
}

private:
void lower() {
std::transform(string_.begin(), string_.end(), string_.begin(), absl::ascii_tolower);
}
bool valid() const { return validHeaderString(string_); }

std::string string_;
};

/**
* Convenient type for a vector of lower case string and string pair.
*/
using LowerCaseStrPairVector =
std::vector<std::pair<const Http::LowerCaseString, const std::string>>;
using LowerCaseString = Envoy::LowerCaseString;
using LowerCaseStrPairVector = Envoy::LowerCaseStrPairVector;

/**
* Convenient type for an inline vector that will be used by HeaderString.
Expand Down Expand Up @@ -817,7 +752,8 @@ class RequestOrResponseHeaderMap : public HeaderMap {
// Request headers.
class RequestHeaderMap
: public RequestOrResponseHeaderMap,
public CustomInlineHeaderBase<CustomInlineHeaderRegistry::Type::RequestHeaders> {
public CustomInlineHeaderBase<CustomInlineHeaderRegistry::Type::RequestHeaders>,
public Tracing::TraceContext {
public:
INLINE_REQ_STRING_HEADERS(DEFINE_INLINE_STRING_HEADER)
INLINE_REQ_NUMERIC_HEADERS(DEFINE_INLINE_NUMERIC_HEADER)
Expand Down
31 changes: 31 additions & 0 deletions include/envoy/http/valid_header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "common/common/macros.h"

#include "absl/strings/string_view.h"

namespace Envoy {
namespace Http {

// Used by ASSERTs to validate internal consistency. E.g. valid HTTP header keys/values should
// never contain embedded NULLs.
static inline bool validHeaderString(absl::string_view s) {
// If you modify this list of illegal embedded characters you will probably
// want to change header_map_fuzz_impl_test at the same time.
for (const char c : s) {
switch (c) {
case '\0':
FALLTHRU;
case '\r':
FALLTHRU;
case '\n':
return false;
default:
continue;
}
}
return true;
}

} // namespace Http
} // namespace Envoy
12 changes: 6 additions & 6 deletions include/envoy/server/tracer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ class TracerFactory : public Config::TypedFactory {
~TracerFactory() override = default;

/**
* Create a particular HttpTracer implementation. If the implementation is unable to produce an
* HttpTracer with the provided parameters, it should throw an EnvoyException in the case of
* general error or a Json::Exception if the json configuration is erroneous. The returned
* Create a particular trace driver implementation. If the implementation is unable to produce
* a trace driver with the provided parameters, it should throw an EnvoyException in the case
* of general error or a Json::Exception if the json configuration is erroneous. The returned
* pointer should always be valid.
*
* NOTE: Due to the corner case of OpenCensus, who can only support a single tracing
* configuration per entire process, the returned HttpTracer instance is not guaranteed
* configuration per entire process, the returned Driver instance is not guaranteed
* to be unique.
* That is why the return type has been changed to std::shared_ptr<> instead of a more
* idiomatic std::unique_ptr<>.
*
* @param config supplies the proto configuration for the HttpTracer
* @param context supplies the factory context
*/
virtual Tracing::HttpTracerSharedPtr createHttpTracer(const Protobuf::Message& config,
TracerFactoryContext& context) PURE;
virtual Tracing::DriverSharedPtr createTracerDriver(const Protobuf::Message& config,
TracerFactoryContext& context) PURE;

std::string category() const override { return "envoy.tracers"; }
};
Expand Down
18 changes: 16 additions & 2 deletions include/envoy/tracing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ licenses(["notice"]) # Apache 2

envoy_package()

envoy_cc_library(
name = "trace_context_interface",
hdrs = ["trace_context.h"],
)

envoy_cc_library(
name = "trace_driver_interface",
hdrs = ["trace_driver.h"],
deps = [
":trace_context_interface",
":trace_reason_interface",
"//include/envoy/stream_info:stream_info_interface",
],
)

envoy_cc_library(
name = "http_tracer_interface",
hdrs = ["http_tracer.h"],
deps = [
":trace_reason_interface",
"//include/envoy/access_log:access_log_interface",
":trace_driver_interface",
"//include/envoy/http:header_map_interface",
],
)
Expand Down
Loading