Skip to content

Commit

Permalink
stats: Capture all the strings used to accumulate http stats in an ob…
Browse files Browse the repository at this point in the history
…ject, plumbed through the system. (#4997)

* Make a class for Http::CodeUtility::chargeResponseTiming et al, plumb it in, and preconstruct strings. Add HttpContext:: to plumb through Http::CodeStats and HttpTracer and potentially other http-specific structures.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
  • Loading branch information
jmarantz authored Dec 5, 2018
1 parent 4087f80 commit 43fc779
Show file tree
Hide file tree
Showing 57 changed files with 528 additions and 240 deletions.
10 changes: 10 additions & 0 deletions include/envoy/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ envoy_cc_library(
envoy_cc_library(
name = "codes_interface",
hdrs = ["codes.h"],
deps = ["//include/envoy/stats:stats_interface"],
)

envoy_cc_library(
Expand All @@ -44,6 +45,15 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "context_interface",
hdrs = ["context.h"],
deps = [
":codes_interface",
"//include/envoy/tracing:http_tracer_interface",
],
)

envoy_cc_library(
name = "filter_interface",
hdrs = ["filter.h"],
Expand Down
58 changes: 58 additions & 0 deletions include/envoy/http/codes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <chrono>

#include "envoy/stats/scope.h"

namespace Envoy {
namespace Http {

Expand Down Expand Up @@ -73,5 +77,59 @@ enum class Code {
// clang-format on
};

/**
* Manages updating of statistics for HTTP Status Codes. Sets up string-tokens
* for fast combining of tokens based on scope, status-code buckets (2xx,
* 4xx...), and exact status code.
*/
class CodeStats {
public:
virtual ~CodeStats() = default;

struct ResponseStatInfo {
Stats::Scope& global_scope_;
Stats::Scope& cluster_scope_;
const std::string& prefix_;
uint64_t response_status_code_;
bool internal_request_;
const std::string& request_vhost_name_;
const std::string& request_vcluster_name_;
const std::string& from_zone_;
const std::string& to_zone_;
bool upstream_canary_;
};

struct ResponseTimingInfo {
Stats::Scope& global_scope_;
Stats::Scope& cluster_scope_;
const std::string& prefix_;
std::chrono::milliseconds response_time_;
bool upstream_canary_;
bool internal_request_;
const std::string& request_vhost_name_;
const std::string& request_vcluster_name_;
const std::string& from_zone_;
const std::string& to_zone_;
};

/**
* Charge a simple response stat to an upstream.
*/
virtual void chargeBasicResponseStat(Stats::Scope& scope, const std::string& prefix,
Code response_code) const PURE;

/**
* Charge a response stat to both agg counters (*xx) as well as code specific counters. This
* routine also looks for the x-envoy-upstream-canary header and if it is set, also charges
* canary stats.
*/
virtual void chargeResponseStat(const ResponseStatInfo& info) const PURE;

/**
* Charge a response timing to the various dynamic stat postfixes.
*/
virtual void chargeResponseTiming(const ResponseTimingInfo& info) const PURE;
};

} // namespace Http
} // namespace Envoy
24 changes: 24 additions & 0 deletions include/envoy/http/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <memory>

#include "envoy/http/codes.h"
#include "envoy/tracing/http_tracer.h"

namespace Envoy {
namespace Http {

/**
* Captures http-related structures with cardinality of one per server.
*/
class Context {
public:
virtual ~Context() = default;
virtual Tracing::HttpTracer& tracer() PURE;
virtual CodeStats& codeStats() PURE;
};

using ContextPtr = std::unique_ptr<Context>;

} // namespace Http
} // namespace Envoy
6 changes: 5 additions & 1 deletion include/envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ envoy_cc_library(
deps = [
":config_tracker_interface",
"//include/envoy/buffer:buffer_interface",
"//include/envoy/http:codes_interface",
"//include/envoy/http:filter_interface",
"//include/envoy/http:header_map_interface",
"//include/envoy/http:query_params_interface",
Expand All @@ -37,6 +36,8 @@ envoy_cc_library(
hdrs = ["configuration.h"],
external_deps = ["abseil_optional"],
deps = [
"//include/envoy/http:context_interface",
"//include/envoy/ratelimit:ratelimit_interface",
"//include/envoy/tracing:http_tracer_interface",
"//include/envoy/upstream:cluster_manager_interface",
],
Expand Down Expand Up @@ -94,6 +95,7 @@ envoy_cc_library(
"//include/envoy/api:api_interface",
"//include/envoy/common:mutex_tracer",
"//include/envoy/event:timer_interface",
"//include/envoy/http:context_interface",
"//include/envoy/http:query_params_interface",
"//include/envoy/init:init_interface",
"//include/envoy/local_info:local_info_interface",
Expand Down Expand Up @@ -141,6 +143,8 @@ envoy_cc_library(
deps = [
":admin_interface",
"//include/envoy/access_log:access_log_interface",
"//include/envoy/http:codes_interface",
"//include/envoy/http:context_interface",
"//include/envoy/http:filter_interface",
"//include/envoy/init:init_interface",
"//include/envoy/json:json_object_interface",
Expand Down
7 changes: 7 additions & 0 deletions include/envoy/server/filter_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "envoy/access_log/access_log.h"
#include "envoy/api/v2/core/base.pb.h"
#include "envoy/http/codes.h"
#include "envoy/http/context.h"
#include "envoy/http/filter.h"
#include "envoy/init/init.h"
#include "envoy/json/json_object.h"
Expand Down Expand Up @@ -133,6 +135,11 @@ class FactoryContext {
* @return OverloadManager& the overload manager for the server.
*/
virtual OverloadManager& overloadManager() PURE;

/**
* @return Http::Context& a reference to the http context.
*/
virtual Http::Context& httpContext() PURE;
};

class ListenerFactoryContext : public FactoryContext {
Expand Down
5 changes: 3 additions & 2 deletions include/envoy/server/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "envoy/api/api.h"
#include "envoy/common/mutex_tracer.h"
#include "envoy/event/timer.h"
#include "envoy/http/context.h"
#include "envoy/init/init.h"
#include "envoy/local_info/local_info.h"
#include "envoy/network/listen_socket.h"
Expand Down Expand Up @@ -182,9 +183,9 @@ class Instance {
virtual Stats::Store& stats() PURE;

/**
* @return the server-wide http tracer.
* @return the server-wide http context.
*/
virtual Tracing::HttpTracer& httpTracer() PURE;
virtual Http::Context& httpContext() PURE;

/**
* @return ThreadLocal::Instance& the thread local storage engine for the server. This is used to
Expand Down
14 changes: 13 additions & 1 deletion source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ envoy_cc_library(
"//include/envoy/event:dispatcher_interface",
"//include/envoy/http:async_client_interface",
"//include/envoy/http:codec_interface",
"//include/envoy/http:context_interface",
"//include/envoy/http:header_map_interface",
"//include/envoy/http:message_interface",
"//include/envoy/router:router_interface",
Expand Down Expand Up @@ -92,6 +93,17 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "context_lib",
srcs = ["context_impl.cc"],
hdrs = ["context_impl.h"],
deps = [
":codes_lib",
"//include/envoy/http:context_interface",
"//source/common/tracing:http_tracer_lib",
],
)

envoy_cc_library(
name = "conn_pool_base_lib",
srcs = ["conn_pool_base.cc"],
Expand Down Expand Up @@ -137,6 +149,7 @@ envoy_cc_library(
"//include/envoy/event:deferred_deletable",
"//include/envoy/event:dispatcher_interface",
"//include/envoy/http:codec_interface",
"//include/envoy/http:context_interface",
"//include/envoy/http:filter_interface",
"//include/envoy/http:header_map_interface",
"//include/envoy/local_info:local_info_interface",
Expand All @@ -150,7 +163,6 @@ envoy_cc_library(
"//include/envoy/stats:stats_interface",
"//include/envoy/stats:stats_macros",
"//include/envoy/stats:timespan",
"//include/envoy/tracing:http_tracer_interface",
"//include/envoy/upstream:upstream_interface",
"//source/common/access_log:access_log_formatter_lib",
"//source/common/buffer:buffer_lib",
Expand Down
5 changes: 3 additions & 2 deletions source/common/http/async_client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ AsyncClientImpl::AsyncClientImpl(Upstream::ClusterInfoConstSharedPtr cluster,
const LocalInfo::LocalInfo& local_info,
Upstream::ClusterManager& cm, Runtime::Loader& runtime,
Runtime::RandomGenerator& random,
Router::ShadowWriterPtr&& shadow_writer)
Router::ShadowWriterPtr&& shadow_writer,
Http::Context& http_context)
: cluster_(cluster),
config_("http.async-client.", local_info, stats_store, cm, runtime, random,
std::move(shadow_writer), true, false, false, dispatcher.timeSystem()),
std::move(shadow_writer), true, false, false, dispatcher.timeSystem(), http_context),
dispatcher_(dispatcher) {}

AsyncClientImpl::~AsyncClientImpl() {
Expand Down
6 changes: 4 additions & 2 deletions source/common/http/async_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "envoy/event/dispatcher.h"
#include "envoy/http/async_client.h"
#include "envoy/http/codec.h"
#include "envoy/http/context.h"
#include "envoy/http/header_map.h"
#include "envoy/http/message.h"
#include "envoy/router/router.h"
Expand Down Expand Up @@ -42,8 +43,9 @@ class AsyncClientImpl final : public AsyncClient {
AsyncClientImpl(Upstream::ClusterInfoConstSharedPtr cluster, Stats::Store& stats_store,
Event::Dispatcher& dispatcher, const LocalInfo::LocalInfo& local_info,
Upstream::ClusterManager& cm, Runtime::Loader& runtime,
Runtime::RandomGenerator& random, Router::ShadowWriterPtr&& shadow_writer);
~AsyncClientImpl();
Runtime::RandomGenerator& random, Router::ShadowWriterPtr&& shadow_writer,
Http::Context& http_context);
~AsyncClientImpl() override;

// Http::AsyncClient
Request* send(MessagePtr&& request, Callbacks& callbacks,
Expand Down
Loading

0 comments on commit 43fc779

Please sign in to comment.