From 184b9a4e7261d2b0da62a04cee95da2f718c13e6 Mon Sep 17 00:00:00 2001 From: Matt Klein Date: Mon, 2 Apr 2018 18:49:17 -0400 Subject: [PATCH] repo reorg: move stat sinks Signed-off-by: Matt Klein --- source/common/config/well_known_names.h | 2 + source/common/stats/BUILD | 34 ----- source/exe/BUILD | 3 - source/extensions/all_extensions.bzl | 3 + .../extensions/stat_sinks/common/statsd/BUILD | 27 ++++ .../stat_sinks/common/statsd}/statsd.cc | 22 ++-- .../stat_sinks/common/statsd}/statsd.h | 32 ++--- source/extensions/stat_sinks/dog_statsd/BUILD | 26 ++++ .../stat_sinks/dog_statsd/config.cc} | 24 ++-- .../stat_sinks/dog_statsd/config.h} | 18 +-- .../stat_sinks/metrics_service/BUILD | 40 ++++++ .../stat_sinks/metrics_service/config.cc} | 27 ++-- .../stat_sinks/metrics_service/config.h} | 18 +-- .../grpc_metrics_service_impl.cc | 17 ++- .../grpc_metrics_service_impl.h | 24 ++-- source/extensions/stat_sinks/statsd/BUILD | 25 ++++ .../stat_sinks/statsd/config.cc} | 30 ++--- .../stat_sinks/statsd/config.h} | 18 +-- source/server/config/stats/BUILD | 53 -------- test/common/stats/BUILD | 52 -------- test/config_test/BUILD | 1 - .../stats_sinks/common/statsd/BUILD | 44 +++++++ .../stats_sinks/common/statsd}/statsd_test.cc | 21 +-- .../common/statsd}/udp_statsd_test.cc | 39 +++--- .../stats_sinks/dog_statsd}/BUILD | 4 +- .../stats_sinks/dog_statsd/config_test.cc | 69 ++++++++++ .../stats_sinks/metrics_service/BUILD | 40 ++++++ .../grpc_metrics_service_impl_test.cc | 22 ++-- .../metrics_service_integration_test.cc | 6 +- test/extensions/stats_sinks/statsd/BUILD | 25 ++++ .../stats_sinks/statsd/config_test.cc | 88 +++++++++++++ test/integration/BUILD | 16 --- test/server/BUILD | 4 +- test/server/config/stats/config_test.cc | 124 ------------------ 34 files changed, 561 insertions(+), 437 deletions(-) create mode 100644 source/extensions/stat_sinks/common/statsd/BUILD rename source/{common/stats => extensions/stat_sinks/common/statsd}/statsd.cc (92%) rename source/{common/stats => extensions/stat_sinks/common/statsd}/statsd.h (83%) create mode 100644 source/extensions/stat_sinks/dog_statsd/BUILD rename source/{server/config/stats/dog_statsd.cc => extensions/stat_sinks/dog_statsd/config.cc} (68%) rename source/{server/config/stats/dog_statsd.h => extensions/stat_sinks/dog_statsd/config.h} (61%) create mode 100644 source/extensions/stat_sinks/metrics_service/BUILD rename source/{server/config/stats/metrics_service.cc => extensions/stat_sinks/metrics_service/config.cc} (70%) rename source/{server/config/stats/metrics_service.h => extensions/stat_sinks/metrics_service/config.h} (59%) rename source/{common/stats => extensions/stat_sinks/metrics_service}/grpc_metrics_service_impl.cc (91%) rename source/{common/stats => extensions/stat_sinks/metrics_service}/grpc_metrics_service_impl.h (90%) create mode 100644 source/extensions/stat_sinks/statsd/BUILD rename source/{server/config/stats/statsd.cc => extensions/stat_sinks/statsd/config.cc} (70%) rename source/{server/config/stats/statsd.h => extensions/stat_sinks/statsd/config.h} (53%) delete mode 100644 source/server/config/stats/BUILD create mode 100644 test/extensions/stats_sinks/common/statsd/BUILD rename test/{common/stats => extensions/stats_sinks/common/statsd}/statsd_test.cc (92%) rename test/{common/stats => extensions/stats_sinks/common/statsd}/udp_statsd_test.cc (85%) rename test/{server/config/stats => extensions/stats_sinks/dog_statsd}/BUILD (80%) create mode 100644 test/extensions/stats_sinks/dog_statsd/config_test.cc create mode 100644 test/extensions/stats_sinks/metrics_service/BUILD rename test/{common/stats => extensions/stats_sinks/metrics_service}/grpc_metrics_service_impl_test.cc (91%) rename test/{integration => extensions/stats_sinks/metrics_service}/metrics_service_integration_test.cc (98%) create mode 100644 test/extensions/stats_sinks/statsd/BUILD create mode 100644 test/extensions/stats_sinks/statsd/config_test.cc delete mode 100644 test/server/config/stats/config_test.cc diff --git a/source/common/config/well_known_names.h b/source/common/config/well_known_names.h index f929955fbd06..58a36484d6f5 100644 --- a/source/common/config/well_known_names.h +++ b/source/common/config/well_known_names.h @@ -167,6 +167,8 @@ typedef ConstSingleton HttpTracerNames; /** * Well-known stats sink names. + * TODO(mattklein123): Move this to extensions directory when the migration is complete. + * TODO(mattklein123): New filters should use the well known name: envoy.stat_sinks.name. */ class StatsSinkNameValues { public: diff --git a/source/common/stats/BUILD b/source/common/stats/BUILD index d2a4bc822d6c..1d6698e72c95 100644 --- a/source/common/stats/BUILD +++ b/source/common/stats/BUILD @@ -27,40 +27,6 @@ envoy_cc_library( ], ) -envoy_cc_library( - name = "statsd_lib", - srcs = ["statsd.cc"], - hdrs = ["statsd.h"], - deps = [ - "//include/envoy/event:dispatcher_interface", - "//include/envoy/local_info:local_info_interface", - "//include/envoy/network:connection_interface", - "//include/envoy/stats:stats_interface", - "//include/envoy/thread_local:thread_local_interface", - "//include/envoy/upstream:cluster_manager_interface", - "//source/common/buffer:buffer_lib", - "//source/common/common:assert_lib", - "//source/common/common:utility_lib", - "//source/common/config:utility_lib", - ], -) - -envoy_cc_library( - name = "metrics_service_grpc_lib", - srcs = ["grpc_metrics_service_impl.cc"], - hdrs = ["grpc_metrics_service_impl.h"], - deps = [ - "//include/envoy/grpc:async_client_interface", - "//include/envoy/local_info:local_info_interface", - "//include/envoy/singleton:instance_interface", - "//include/envoy/thread_local:thread_local_interface", - "//include/envoy/upstream:cluster_manager_interface", - "//source/common/common:assert_lib", - "//source/common/grpc:async_client_lib", - "@envoy_api//envoy/service/metrics/v2:metrics_service_cc", - ], -) - envoy_cc_library( name = "thread_local_store_lib", srcs = ["thread_local_store.cc"], diff --git a/source/exe/BUILD b/source/exe/BUILD index cc3766cc1e5a..b35a71b0fab4 100644 --- a/source/exe/BUILD +++ b/source/exe/BUILD @@ -51,9 +51,6 @@ envoy_cc_library( "//source/server/config/network:http_connection_manager_lib", "//source/server/config/network:raw_buffer_socket_lib", "//source/server/config/network:ssl_socket_lib", - "//source/server/config/stats:dog_statsd_lib", - "//source/server/config/stats:metrics_service_lib", - "//source/server/config/stats:statsd_lib", "//source/server/http:health_check_lib", ] + envoy_all_extensions(), ) diff --git a/source/extensions/all_extensions.bzl b/source/extensions/all_extensions.bzl index 17eace95c82c..6ef00e75c30a 100644 --- a/source/extensions/all_extensions.bzl +++ b/source/extensions/all_extensions.bzl @@ -17,5 +17,8 @@ def envoy_all_extensions(repository = ""): repository + "//source/extensions/filters/network/ratelimit:config", repository + "//source/extensions/filters/network/redis_proxy:config", repository + "//source/extensions/filters/network/tcp_proxy:config", + repository + "//source/extensions/stat_sinks/dog_statsd:config", + repository + "//source/extensions/stat_sinks/metrics_service:config", + repository + "//source/extensions/stat_sinks/statsd:config", ] diff --git a/source/extensions/stat_sinks/common/statsd/BUILD b/source/extensions/stat_sinks/common/statsd/BUILD new file mode 100644 index 000000000000..302c822e00af --- /dev/null +++ b/source/extensions/stat_sinks/common/statsd/BUILD @@ -0,0 +1,27 @@ +licenses(["notice"]) # Apache 2 + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_library", + "envoy_package", +) + +envoy_package() + +envoy_cc_library( + name = "statsd_lib", + srcs = ["statsd.cc"], + hdrs = ["statsd.h"], + deps = [ + "//include/envoy/event:dispatcher_interface", + "//include/envoy/local_info:local_info_interface", + "//include/envoy/network:connection_interface", + "//include/envoy/stats:stats_interface", + "//include/envoy/thread_local:thread_local_interface", + "//include/envoy/upstream:cluster_manager_interface", + "//source/common/buffer:buffer_lib", + "//source/common/common:assert_lib", + "//source/common/common:utility_lib", + "//source/common/config:utility_lib", + ], +) diff --git a/source/common/stats/statsd.cc b/source/extensions/stat_sinks/common/statsd/statsd.cc similarity index 92% rename from source/common/stats/statsd.cc rename to source/extensions/stat_sinks/common/statsd/statsd.cc index cd5e069bbe18..4f30a049443d 100644 --- a/source/common/stats/statsd.cc +++ b/source/extensions/stat_sinks/common/statsd/statsd.cc @@ -1,4 +1,4 @@ -#include "common/stats/statsd.h" +#include "extensions/stat_sinks/common/statsd/statsd.h" #include #include @@ -14,7 +14,9 @@ #include "common/config/utility.h" namespace Envoy { -namespace Stats { +namespace Extensions { +namespace StatSinks { +namespace Common { namespace Statsd { Writer::Writer(Network::Address::InstanceConstSharedPtr address) { @@ -43,19 +45,19 @@ UdpStatsdSink::UdpStatsdSink(ThreadLocal::SlotAllocator& tls, }); } -void UdpStatsdSink::flushCounter(const Counter& counter, uint64_t delta) { +void UdpStatsdSink::flushCounter(const Stats::Counter& counter, uint64_t delta) { const std::string message( fmt::format("envoy.{}:{}|c{}", getName(counter), delta, buildTagStr(counter.tags()))); tls_->getTyped().write(message); } -void UdpStatsdSink::flushGauge(const Gauge& gauge, uint64_t value) { +void UdpStatsdSink::flushGauge(const Stats::Gauge& gauge, uint64_t value) { const std::string message( fmt::format("envoy.{}:{}|g{}", getName(gauge), value, buildTagStr(gauge.tags()))); tls_->getTyped().write(message); } -void UdpStatsdSink::onHistogramComplete(const Histogram& histogram, uint64_t value) { +void UdpStatsdSink::onHistogramComplete(const Stats::Histogram& histogram, uint64_t value) { // For statsd histograms are all timers. const std::string message(fmt::format("envoy.{}:{}|ms{}", getName(histogram), std::chrono::milliseconds(value).count(), @@ -63,7 +65,7 @@ void UdpStatsdSink::onHistogramComplete(const Histogram& histogram, uint64_t val tls_->getTyped().write(message); } -const std::string UdpStatsdSink::getName(const Metric& metric) { +const std::string UdpStatsdSink::getName(const Stats::Metric& metric) { if (use_tag_) { return metric.tagExtractedName(); } else { @@ -71,14 +73,14 @@ const std::string UdpStatsdSink::getName(const Metric& metric) { } } -const std::string UdpStatsdSink::buildTagStr(const std::vector& tags) { +const std::string UdpStatsdSink::buildTagStr(const std::vector& tags) { if (!use_tag_ || tags.empty()) { return ""; } std::vector tag_strings; tag_strings.reserve(tags.size()); - for (const Tag& tag : tags) { + for (const Stats::Tag& tag : tags) { tag_strings.emplace_back(tag.name_ + ":" + tag.value_); } return "|#" + StringUtil::join(tag_strings, ","); @@ -227,5 +229,7 @@ uint64_t TcpStatsdSink::TlsSink::usedBuffer() { } } // namespace Statsd -} // namespace Stats +} // namespace Common +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/common/stats/statsd.h b/source/extensions/stat_sinks/common/statsd/statsd.h similarity index 83% rename from source/common/stats/statsd.h rename to source/extensions/stat_sinks/common/statsd/statsd.h index 1a18cc22ae98..78f0a76fc9a7 100644 --- a/source/common/stats/statsd.h +++ b/source/extensions/stat_sinks/common/statsd/statsd.h @@ -1,9 +1,5 @@ #pragma once -#include -#include -#include - #include "envoy/local_info/local_info.h" #include "envoy/network/connection.h" #include "envoy/stats/stats.h" @@ -13,7 +9,9 @@ #include "common/buffer/buffer_impl.h" namespace Envoy { -namespace Stats { +namespace Extensions { +namespace StatSinks { +namespace Common { namespace Statsd { /** @@ -37,7 +35,7 @@ class Writer : public ThreadLocal::ThreadLocalObject { /** * Implementation of Sink that writes to a UDP statsd address. */ -class UdpStatsdSink : public Sink { +class UdpStatsdSink : public Stats::Sink { public: UdpStatsdSink(ThreadLocal::SlotAllocator& tls, Network::Address::InstanceConstSharedPtr address, const bool use_tag); @@ -51,18 +49,18 @@ class UdpStatsdSink : public Sink { // Stats::Sink void beginFlush() override {} - void flushCounter(const Counter& counter, uint64_t delta) override; - void flushGauge(const Gauge& gauge, uint64_t value) override; + void flushCounter(const Stats::Counter& counter, uint64_t delta) override; + void flushGauge(const Stats::Gauge& gauge, uint64_t value) override; void endFlush() override {} - void onHistogramComplete(const Histogram& histogram, uint64_t value) override; + void onHistogramComplete(const Stats::Histogram& histogram, uint64_t value) override; // Called in unit test to validate writer construction and address. int getFdForTests() { return tls_->getTyped().getFdForTests(); } bool getUseTagForTest() { return use_tag_; } private: - const std::string getName(const Metric& metric); - const std::string buildTagStr(const std::vector& tags); + const std::string getName(const Stats::Metric& metric); + const std::string buildTagStr(const std::vector& tags); ThreadLocal::SlotPtr tls_; Network::Address::InstanceConstSharedPtr server_address_; @@ -72,7 +70,7 @@ class UdpStatsdSink : public Sink { /** * Per thread implementation of a TCP stats flusher for statsd. */ -class TcpStatsdSink : public Sink { +class TcpStatsdSink : public Stats::Sink { public: TcpStatsdSink(const LocalInfo::LocalInfo& local_info, const std::string& cluster_name, ThreadLocal::SlotAllocator& tls, Upstream::ClusterManager& cluster_manager, @@ -81,17 +79,17 @@ class TcpStatsdSink : public Sink { // Stats::Sink void beginFlush() override { tls_->getTyped().beginFlush(true); } - void flushCounter(const Counter& counter, uint64_t delta) override { + void flushCounter(const Stats::Counter& counter, uint64_t delta) override { tls_->getTyped().flushCounter(counter.name(), delta); } - void flushGauge(const Gauge& gauge, uint64_t value) override { + void flushGauge(const Stats::Gauge& gauge, uint64_t value) override { tls_->getTyped().flushGauge(gauge.name(), value); } void endFlush() override { tls_->getTyped().endFlush(true); } - void onHistogramComplete(const Histogram& histogram, uint64_t value) override { + void onHistogramComplete(const Stats::Histogram& histogram, uint64_t value) override { // For statsd histograms are all timers. tls_->getTyped().onTimespanComplete(histogram.name(), std::chrono::milliseconds(value)); @@ -141,5 +139,7 @@ class TcpStatsdSink : public Sink { }; } // namespace Statsd -} // namespace Stats +} // namespace Common +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/extensions/stat_sinks/dog_statsd/BUILD b/source/extensions/stat_sinks/dog_statsd/BUILD new file mode 100644 index 000000000000..7f0e21201ce6 --- /dev/null +++ b/source/extensions/stat_sinks/dog_statsd/BUILD @@ -0,0 +1,26 @@ +licenses(["notice"]) # Apache 2 +# Stats sink for the DataDog (https://www.datadoghq.com/) variant of the statsd protocol +# (https://docs.datadoghq.com/developers/dogstatsd/). + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_library", + "envoy_package", +) + +envoy_package() + +envoy_cc_library( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + deps = [ + "//include/envoy/registry", + "//source/common/config:well_known_names", + "//source/common/network:address_lib", + "//source/common/network:resolver_lib", + "//source/extensions/stat_sinks/common/statsd:statsd_lib", + "//source/server:configuration_lib", + "@envoy_api//envoy/config/metrics/v2:stats_cc", + ], +) diff --git a/source/server/config/stats/dog_statsd.cc b/source/extensions/stat_sinks/dog_statsd/config.cc similarity index 68% rename from source/server/config/stats/dog_statsd.cc rename to source/extensions/stat_sinks/dog_statsd/config.cc index f7b943608811..50cc79bab1a3 100644 --- a/source/server/config/stats/dog_statsd.cc +++ b/source/extensions/stat_sinks/dog_statsd/config.cc @@ -1,6 +1,4 @@ -#include "server/config/stats/dog_statsd.h" - -#include +#include "extensions/stat_sinks/dog_statsd/config.h" #include "envoy/config/metrics/v2/stats.pb.h" #include "envoy/config/metrics/v2/stats.pb.validate.h" @@ -8,11 +6,13 @@ #include "common/config/well_known_names.h" #include "common/network/resolver_impl.h" -#include "common/stats/statsd.h" + +#include "extensions/stat_sinks/common/statsd/statsd.h" namespace Envoy { -namespace Server { -namespace Configuration { +namespace Extensions { +namespace StatSinks { +namespace DogStatsd { Stats::SinkPtr DogStatsdSinkFactory::createStatsSink(const Protobuf::Message& config, Server::Instance& server) { @@ -21,8 +21,8 @@ Stats::SinkPtr DogStatsdSinkFactory::createStatsSink(const Protobuf::Message& co Network::Address::InstanceConstSharedPtr address = Network::Address::resolveProtoAddress(sink_config.address()); ENVOY_LOG(debug, "dog_statsd UDP ip address: {}", address->asString()); - return Stats::SinkPtr( - new Stats::Statsd::UdpStatsdSink(server.threadLocal(), std::move(address), true)); + return std::make_unique(server.threadLocal(), std::move(address), + true); } ProtobufTypes::MessagePtr DogStatsdSinkFactory::createEmptyConfigProto() { @@ -35,8 +35,10 @@ std::string DogStatsdSinkFactory::name() { return Config::StatsSinkNames::get(). /** * Static registration for the this sink factory. @see RegisterFactory. */ -static Registry::RegisterFactory register_; +static Registry::RegisterFactory + register_; -} // namespace Configuration -} // namespace Server +} // namespace DogStatsd +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/server/config/stats/dog_statsd.h b/source/extensions/stat_sinks/dog_statsd/config.h similarity index 61% rename from source/server/config/stats/dog_statsd.h rename to source/extensions/stat_sinks/dog_statsd/config.h index d9fe946351cd..44e1ecaaa77f 100644 --- a/source/server/config/stats/dog_statsd.h +++ b/source/extensions/stat_sinks/dog_statsd/config.h @@ -1,27 +1,29 @@ #pragma once -#include - #include "envoy/server/instance.h" #include "server/configuration_impl.h" namespace Envoy { -namespace Server { -namespace Configuration { +namespace Extensions { +namespace StatSinks { +namespace DogStatsd { /** * Config registration for the DogStatsD compatible statsd sink. @see StatsSinkFactory. */ -class DogStatsdSinkFactory : Logger::Loggable, public StatsSinkFactory { +class DogStatsdSinkFactory : Logger::Loggable, + public Server::Configuration::StatsSinkFactory { public: - Stats::SinkPtr createStatsSink(const Protobuf::Message& config, Instance& server) override; + Stats::SinkPtr createStatsSink(const Protobuf::Message& config, + Server::Instance& server) override; ProtobufTypes::MessagePtr createEmptyConfigProto() override; std::string name() override; }; -} // namespace Configuration -} // namespace Server +} // namespace DogStatsd +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/extensions/stat_sinks/metrics_service/BUILD b/source/extensions/stat_sinks/metrics_service/BUILD new file mode 100644 index 000000000000..28e00fdf6e50 --- /dev/null +++ b/source/extensions/stat_sinks/metrics_service/BUILD @@ -0,0 +1,40 @@ +licenses(["notice"]) # Apache 2 +# Stats sink for the gRPC metrics service: https://github.com/envoyproxy/data-plane-api/blob/master/envoy/service/metrics/v2/metrics_service.proto + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_library", + "envoy_package", +) + +envoy_package() + +envoy_cc_library( + name = "metrics_service_grpc_lib", + srcs = ["grpc_metrics_service_impl.cc"], + hdrs = ["grpc_metrics_service_impl.h"], + deps = [ + "//include/envoy/grpc:async_client_interface", + "//include/envoy/local_info:local_info_interface", + "//include/envoy/singleton:instance_interface", + "//include/envoy/thread_local:thread_local_interface", + "//include/envoy/upstream:cluster_manager_interface", + "//source/common/common:assert_lib", + "//source/common/grpc:async_client_lib", + "@envoy_api//envoy/service/metrics/v2:metrics_service_cc", + ], +) + +envoy_cc_library( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + deps = [ + "//include/envoy/registry", + "//source/common/config:well_known_names", + "//source/extensions/stat_sinks/metrics_service:metrics_service_grpc_lib", + "//source/server:configuration_lib", + "@envoy_api//envoy/config/metrics/v2:stats_cc", + "@envoy_api//envoy/service/metrics/v2:metrics_service_cc", + ], +) diff --git a/source/server/config/stats/metrics_service.cc b/source/extensions/stat_sinks/metrics_service/config.cc similarity index 70% rename from source/server/config/stats/metrics_service.cc rename to source/extensions/stat_sinks/metrics_service/config.cc index 3294c38e342d..a4f3be45a91c 100644 --- a/source/server/config/stats/metrics_service.cc +++ b/source/extensions/stat_sinks/metrics_service/config.cc @@ -1,6 +1,4 @@ -#include "server/config/stats/metrics_service.h" - -#include +#include "extensions/stat_sinks/metrics_service/config.h" #include "envoy/config/metrics/v2/metrics_service.pb.h" #include "envoy/config/metrics/v2/metrics_service.pb.validate.h" @@ -9,11 +7,13 @@ #include "common/config/well_known_names.h" #include "common/grpc/async_client_impl.h" #include "common/network/resolver_impl.h" -#include "common/stats/grpc_metrics_service_impl.h" + +#include "extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h" namespace Envoy { -namespace Server { -namespace Configuration { +namespace Extensions { +namespace StatSinks { +namespace MetricsService { Stats::SinkPtr MetricsServiceSinkFactory::createStatsSink(const Protobuf::Message& config, Server::Instance& server) { @@ -23,14 +23,13 @@ Stats::SinkPtr MetricsServiceSinkFactory::createStatsSink(const Protobuf::Messag const auto& grpc_service = sink_config.grpc_service(); ENVOY_LOG(debug, "Metrics Service gRPC service configuration: {}", grpc_service.DebugString()); - std::shared_ptr grpc_metrics_streamer = - std::make_shared( + std::shared_ptr grpc_metrics_streamer = + std::make_shared( server.clusterManager().grpcAsyncClientManager().factoryForGrpcService( grpc_service, server.stats(), false), server.threadLocal(), server.localInfo()); - return Stats::SinkPtr( - std::make_unique(grpc_metrics_streamer)); + return std::make_unique(grpc_metrics_streamer); } ProtobufTypes::MessagePtr MetricsServiceSinkFactory::createEmptyConfigProto() { @@ -45,8 +44,10 @@ std::string MetricsServiceSinkFactory::name() { /** * Static registration for the this sink factory. @see RegisterFactory. */ -static Registry::RegisterFactory register_; +static Registry::RegisterFactory + register_; -} // namespace Configuration -} // namespace Server +} // namespace MetricsService +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/server/config/stats/metrics_service.h b/source/extensions/stat_sinks/metrics_service/config.h similarity index 59% rename from source/server/config/stats/metrics_service.h rename to source/extensions/stat_sinks/metrics_service/config.h index 3c3207d52f0c..3e144dbbe937 100644 --- a/source/server/config/stats/metrics_service.h +++ b/source/extensions/stat_sinks/metrics_service/config.h @@ -1,27 +1,29 @@ #pragma once -#include - #include "envoy/server/instance.h" #include "server/configuration_impl.h" namespace Envoy { -namespace Server { -namespace Configuration { +namespace Extensions { +namespace StatSinks { +namespace MetricsService { /** * Config registration for the MetricsService stats sink. @see StatsSinkFactory. */ -class MetricsServiceSinkFactory : Logger::Loggable, public StatsSinkFactory { +class MetricsServiceSinkFactory : Logger::Loggable, + public Server::Configuration::StatsSinkFactory { public: - Stats::SinkPtr createStatsSink(const Protobuf::Message& config, Instance& server) override; + Stats::SinkPtr createStatsSink(const Protobuf::Message& config, + Server::Instance& server) override; ProtobufTypes::MessagePtr createEmptyConfigProto() override; std::string name() override; }; -} // namespace Configuration -} // namespace Server +} // namespace MetricsService +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/common/stats/grpc_metrics_service_impl.cc b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.cc similarity index 91% rename from source/common/stats/grpc_metrics_service_impl.cc rename to source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.cc index 5ae70bb49e2d..5aa68b19a3b8 100644 --- a/source/common/stats/grpc_metrics_service_impl.cc +++ b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.cc @@ -1,8 +1,4 @@ -#include "common/stats/grpc_metrics_service_impl.h" - -#include -#include -#include +#include "extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h" #include "envoy/common/exception.h" #include "envoy/event/dispatcher.h" @@ -13,8 +9,9 @@ #include "common/config/utility.h" namespace Envoy { -namespace Stats { -namespace Metrics { +namespace Extensions { +namespace StatSinks { +namespace MetricsService { GrpcMetricsStreamerImpl::GrpcMetricsStreamerImpl(Grpc::AsyncClientFactoryPtr&& factory, ThreadLocal::SlotAllocator& tls, @@ -63,6 +60,8 @@ void GrpcMetricsStreamerImpl::ThreadLocalStreamer::send( MetricsServiceSink::MetricsServiceSink(const GrpcMetricsStreamerSharedPtr& grpc_metrics_streamer) : grpc_metrics_streamer_(grpc_metrics_streamer) {} -} // namespace Metrics -} // namespace Stats + +} // namespace MetricsService +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/common/stats/grpc_metrics_service_impl.h b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h similarity index 90% rename from source/common/stats/grpc_metrics_service_impl.h rename to source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h index 60a721a75d1b..cafaafc9ad03 100644 --- a/source/common/stats/grpc_metrics_service_impl.h +++ b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h @@ -1,9 +1,5 @@ #pragma once -#include -#include -#include - #include "envoy/grpc/async_client.h" #include "envoy/local_info/local_info.h" #include "envoy/network/connection.h" @@ -17,10 +13,11 @@ #include "common/buffer/buffer_impl.h" namespace Envoy { -namespace Stats { -namespace Metrics { +namespace Extensions { +namespace StatSinks { +namespace MetricsService { -// TODO : Move the common code to a base class so that Accesslog and Metrics Service can reuse. +// TODO: Move the common code to a base class so that Accesslog and Metrics Service can reuse. /** * Interface for metrics streamer. The streamer deals with threading and sends @@ -108,14 +105,14 @@ class GrpcMetricsStreamerImpl : public Singleton::Instance, public GrpcMetricsSt /** * Per thread implementation of a Metric Service flusher. */ -class MetricsServiceSink : public Sink { +class MetricsServiceSink : public Stats::Sink { public: // MetricsService::Sink MetricsServiceSink(const GrpcMetricsStreamerSharedPtr& grpc_metrics_streamer); void beginFlush() override { message_.clear_envoy_metrics(); } - void flushCounter(const Counter& counter, uint64_t) override { + void flushCounter(const Stats::Counter& counter, uint64_t) override { io::prometheus::client::MetricFamily* metrics_family = message_.add_envoy_metrics(); metrics_family->set_type(io::prometheus::client::MetricType::COUNTER); metrics_family->set_name(counter.name()); @@ -125,7 +122,7 @@ class MetricsServiceSink : public Sink { counter_metric->set_value(counter.value()); } - void flushGauge(const Gauge& gauge, uint64_t value) override { + void flushGauge(const Stats::Gauge& gauge, uint64_t value) override { io::prometheus::client::MetricFamily* metrics_family = message_.add_envoy_metrics(); metrics_family->set_type(io::prometheus::client::MetricType::GAUGE); metrics_family->set_name(gauge.name()); @@ -143,7 +140,7 @@ class MetricsServiceSink : public Sink { } } - void onHistogramComplete(const Histogram&, uint64_t) override { + void onHistogramComplete(const Stats::Histogram&, uint64_t) override { // TODO : Need to figure out how to map existing histogram to Proto Model } @@ -152,6 +149,7 @@ class MetricsServiceSink : public Sink { envoy::service::metrics::v2::StreamMetricsMessage message_; }; -} // namespace Metrics -} // namespace Stats +} // namespace MetricsService +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/extensions/stat_sinks/statsd/BUILD b/source/extensions/stat_sinks/statsd/BUILD new file mode 100644 index 000000000000..aefd0e0de7c7 --- /dev/null +++ b/source/extensions/stat_sinks/statsd/BUILD @@ -0,0 +1,25 @@ +licenses(["notice"]) # Apache 2 +# Stats sink for the basic version of the statsd protocol (https://github.com/b/statsd_spec). + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_library", + "envoy_package", +) + +envoy_package() + +envoy_cc_library( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + deps = [ + "//include/envoy/registry", + "//source/common/config:well_known_names", + "//source/common/network:address_lib", + "//source/common/network:resolver_lib", + "//source/extensions/stat_sinks/common/statsd:statsd_lib", + "//source/server:configuration_lib", + "@envoy_api//envoy/config/metrics/v2:stats_cc", + ], +) diff --git a/source/server/config/stats/statsd.cc b/source/extensions/stat_sinks/statsd/config.cc similarity index 70% rename from source/server/config/stats/statsd.cc rename to source/extensions/stat_sinks/statsd/config.cc index c4a7c2dd80d4..57fbcbe09115 100644 --- a/source/server/config/stats/statsd.cc +++ b/source/extensions/stat_sinks/statsd/config.cc @@ -1,6 +1,4 @@ -#include "server/config/stats/statsd.h" - -#include +#include "extensions/stat_sinks/statsd/config.h" #include "envoy/config/metrics/v2/stats.pb.h" #include "envoy/config/metrics/v2/stats.pb.validate.h" @@ -8,11 +6,13 @@ #include "common/config/well_known_names.h" #include "common/network/resolver_impl.h" -#include "common/stats/statsd.h" + +#include "extensions/stat_sinks/common/statsd/statsd.h" namespace Envoy { -namespace Server { -namespace Configuration { +namespace Extensions { +namespace StatSinks { +namespace Statsd { Stats::SinkPtr StatsdSinkFactory::createStatsSink(const Protobuf::Message& config, Server::Instance& server) { @@ -24,16 +24,14 @@ Stats::SinkPtr StatsdSinkFactory::createStatsSink(const Protobuf::Message& confi Network::Address::InstanceConstSharedPtr address = Network::Address::resolveProtoAddress(statsd_sink.address()); ENVOY_LOG(debug, "statsd UDP ip address: {}", address->asString()); - return Stats::SinkPtr( - new Stats::Statsd::UdpStatsdSink(server.threadLocal(), std::move(address), false)); - break; + return std::make_unique(server.threadLocal(), std::move(address), + false); } case envoy::config::metrics::v2::StatsdSink::kTcpClusterName: ENVOY_LOG(debug, "statsd TCP cluster: {}", statsd_sink.tcp_cluster_name()); - return Stats::SinkPtr(new Stats::Statsd::TcpStatsdSink( + return std::make_unique( server.localInfo(), statsd_sink.tcp_cluster_name(), server.threadLocal(), - server.clusterManager(), server.stats())); - break; + server.clusterManager(), server.stats()); default: // Verified by schema. NOT_REACHED; @@ -50,8 +48,10 @@ std::string StatsdSinkFactory::name() { return Config::StatsSinkNames::get().STA /** * Static registration for the statsd sink factory. @see RegisterFactory. */ -static Registry::RegisterFactory register_; +static Registry::RegisterFactory + register_; -} // namespace Configuration -} // namespace Server +} // namespace Statsd +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/server/config/stats/statsd.h b/source/extensions/stat_sinks/statsd/config.h similarity index 53% rename from source/server/config/stats/statsd.h rename to source/extensions/stat_sinks/statsd/config.h index 8569b283dd67..64d82f17624d 100644 --- a/source/server/config/stats/statsd.h +++ b/source/extensions/stat_sinks/statsd/config.h @@ -1,28 +1,30 @@ #pragma once -#include - #include "envoy/server/instance.h" #include "server/configuration_impl.h" namespace Envoy { -namespace Server { -namespace Configuration { +namespace Extensions { +namespace StatSinks { +namespace Statsd { /** * Config registration for the tcp statsd sink. @see StatsSinkFactory. */ -class StatsdSinkFactory : Logger::Loggable, public StatsSinkFactory { +class StatsdSinkFactory : Logger::Loggable, + public Server::Configuration::StatsSinkFactory { public: // StatsSinkFactory - Stats::SinkPtr createStatsSink(const Protobuf::Message& config, Instance& server) override; + Stats::SinkPtr createStatsSink(const Protobuf::Message& config, + Server::Instance& server) override; ProtobufTypes::MessagePtr createEmptyConfigProto() override; std::string name() override; }; -} // namespace Configuration -} // namespace Server +} // namespace Statsd +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/source/server/config/stats/BUILD b/source/server/config/stats/BUILD deleted file mode 100644 index 5d32031f9b88..000000000000 --- a/source/server/config/stats/BUILD +++ /dev/null @@ -1,53 +0,0 @@ -licenses(["notice"]) # Apache 2 - -load( - "//bazel:envoy_build_system.bzl", - "envoy_cc_library", - "envoy_package", -) - -envoy_package() - -envoy_cc_library( - name = "statsd_lib", - srcs = ["statsd.cc"], - hdrs = ["statsd.h"], - deps = [ - "//include/envoy/registry", - "//source/common/config:well_known_names", - "//source/common/network:address_lib", - "//source/common/network:resolver_lib", - "//source/common/stats:statsd_lib", - "//source/server:configuration_lib", - "@envoy_api//envoy/config/metrics/v2:stats_cc", - ], -) - -envoy_cc_library( - name = "dog_statsd_lib", - srcs = ["dog_statsd.cc"], - hdrs = ["dog_statsd.h"], - deps = [ - "//include/envoy/registry", - "//source/common/config:well_known_names", - "//source/common/network:address_lib", - "//source/common/network:resolver_lib", - "//source/common/stats:statsd_lib", - "//source/server:configuration_lib", - "@envoy_api//envoy/config/metrics/v2:stats_cc", - ], -) - -envoy_cc_library( - name = "metrics_service_lib", - srcs = ["metrics_service.cc"], - hdrs = ["metrics_service.h"], - deps = [ - "//include/envoy/registry", - "//source/common/config:well_known_names", - "//source/common/stats:metrics_service_grpc_lib", - "//source/server:configuration_lib", - "@envoy_api//envoy/config/metrics/v2:stats_cc", - "@envoy_api//envoy/service/metrics/v2:metrics_service_cc", - ], -) diff --git a/test/common/stats/BUILD b/test/common/stats/BUILD index a0a4e3556828..ce7cdf1052a1 100644 --- a/test/common/stats/BUILD +++ b/test/common/stats/BUILD @@ -18,58 +18,6 @@ envoy_cc_test( ], ) -envoy_cc_test( - name = "statsd_test", - srcs = ["statsd_test.cc"], - deps = [ - "//source/common/config:well_known_names", - "//source/common/event:dispatcher_lib", - "//source/common/network:utility_lib", - "//source/common/stats:statsd_lib", - "//source/common/upstream:upstream_includes", - "//source/common/upstream:upstream_lib", - "//test/common/upstream:utility_lib", - "//test/mocks/buffer:buffer_mocks", - "//test/mocks/local_info:local_info_mocks", - "//test/mocks/network:network_mocks", - "//test/mocks/stats:stats_mocks", - "//test/mocks/thread_local:thread_local_mocks", - "//test/mocks/upstream:upstream_mocks", - ], -) - -envoy_cc_test( - name = "metrics_service_test", - srcs = ["grpc_metrics_service_impl_test.cc"], - deps = [ - "//source/common/config:well_known_names", - "//source/common/event:dispatcher_lib", - "//source/common/stats:metrics_service_grpc_lib", - "//source/common/upstream:upstream_includes", - "//source/common/upstream:upstream_lib", - "//test/common/upstream:utility_lib", - "//test/mocks/grpc:grpc_mocks", - "//test/mocks/local_info:local_info_mocks", - "//test/mocks/thread_local:thread_local_mocks", - "//test/mocks/upstream:upstream_mocks", - ], -) - -envoy_cc_test( - name = "udp_statsd_test", - srcs = ["udp_statsd_test.cc"], - deps = [ - "//source/common/network:address_lib", - "//source/common/network:utility_lib", - "//source/common/stats:statsd_lib", - "//test/mocks/stats:stats_mocks", - "//test/mocks/thread_local:thread_local_mocks", - "//test/test_common:environment_lib", - "//test/test_common:network_utility_lib", - "//test/test_common:utility_lib", - ], -) - envoy_cc_test( name = "thread_local_store_test", srcs = ["thread_local_store_test.cc"], diff --git a/test/config_test/BUILD b/test/config_test/BUILD index 76418f267e17..b6684b0eec09 100644 --- a/test/config_test/BUILD +++ b/test/config_test/BUILD @@ -26,7 +26,6 @@ envoy_cc_test( ], deps = [ ":config_test_lib", - "//source/server/config/stats:statsd_lib", "//test/test_common:environment_lib", "//test/test_common:utility_lib", ], diff --git a/test/extensions/stats_sinks/common/statsd/BUILD b/test/extensions/stats_sinks/common/statsd/BUILD new file mode 100644 index 000000000000..1a1548f161ac --- /dev/null +++ b/test/extensions/stats_sinks/common/statsd/BUILD @@ -0,0 +1,44 @@ +licenses(["notice"]) # Apache 2 + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_test", + "envoy_package", +) + +envoy_package() + +envoy_cc_test( + name = "statsd_test", + srcs = ["statsd_test.cc"], + deps = [ + "//source/common/config:well_known_names", + "//source/common/event:dispatcher_lib", + "//source/common/network:utility_lib", + "//source/common/upstream:upstream_includes", + "//source/common/upstream:upstream_lib", + "//source/extensions/stat_sinks/common/statsd:statsd_lib", + "//test/common/upstream:utility_lib", + "//test/mocks/buffer:buffer_mocks", + "//test/mocks/local_info:local_info_mocks", + "//test/mocks/network:network_mocks", + "//test/mocks/stats:stats_mocks", + "//test/mocks/thread_local:thread_local_mocks", + "//test/mocks/upstream:upstream_mocks", + ], +) + +envoy_cc_test( + name = "udp_statsd_test", + srcs = ["udp_statsd_test.cc"], + deps = [ + "//source/common/network:address_lib", + "//source/common/network:utility_lib", + "//source/extensions/stat_sinks/common/statsd:statsd_lib", + "//test/mocks/stats:stats_mocks", + "//test/mocks/thread_local:thread_local_mocks", + "//test/test_common:environment_lib", + "//test/test_common:network_utility_lib", + "//test/test_common:utility_lib", + ], +) diff --git a/test/common/stats/statsd_test.cc b/test/extensions/stats_sinks/common/statsd/statsd_test.cc similarity index 92% rename from test/common/stats/statsd_test.cc rename to test/extensions/stats_sinks/common/statsd/statsd_test.cc index e697f08e9f19..b16fdb468079 100644 --- a/test/common/stats/statsd_test.cc +++ b/test/extensions/stats_sinks/common/statsd/statsd_test.cc @@ -2,9 +2,10 @@ #include #include "common/network/utility.h" -#include "common/stats/statsd.h" #include "common/upstream/upstream_impl.h" +#include "extensions/stat_sinks/common/statsd/statsd.h" + #include "test/common/upstream/utility.h" #include "test/mocks/buffer/mocks.h" #include "test/mocks/local_info/mocks.h" @@ -23,7 +24,9 @@ using testing::Return; using testing::_; namespace Envoy { -namespace Stats { +namespace Extensions { +namespace StatSinks { +namespace Common { namespace Statsd { class TcpStatsdSinkTest : public testing::Test { @@ -65,10 +68,10 @@ TEST_F(TcpStatsdSinkTest, EmptyFlush) { TEST_F(TcpStatsdSinkTest, BasicFlow) { InSequence s; - NiceMock counter; + NiceMock counter; counter.name_ = "test_counter"; - NiceMock gauge; + NiceMock gauge; gauge.name_ = "test_gauge"; sink_->beginFlush(); @@ -88,7 +91,7 @@ TEST_F(TcpStatsdSinkTest, BasicFlow) { expectCreateConnection(); - NiceMock timer; + NiceMock timer; timer.name_ = "test_timer"; EXPECT_CALL(*connection_, write(BufferStringEqual("envoy.test_timer:5|ms\n"), _)); sink_->onHistogramComplete(timer, 5); @@ -100,7 +103,7 @@ TEST_F(TcpStatsdSinkTest, BasicFlow) { TEST_F(TcpStatsdSinkTest, BufferReallocate) { InSequence s; - NiceMock counter; + NiceMock counter; counter.name_ = "test_counter"; sink_->beginFlush(); @@ -123,7 +126,7 @@ TEST_F(TcpStatsdSinkTest, BufferReallocate) { TEST_F(TcpStatsdSinkTest, Overflow) { InSequence s; - NiceMock counter; + NiceMock counter; counter.name_ = "test_counter"; // Synthetically set buffer above high watermark. Make sure we don't write anything. @@ -157,5 +160,7 @@ TEST_F(TcpStatsdSinkTest, Overflow) { } } // namespace Statsd -} // namespace Stats +} // namespace Common +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/test/common/stats/udp_statsd_test.cc b/test/extensions/stats_sinks/common/statsd/udp_statsd_test.cc similarity index 85% rename from test/common/stats/udp_statsd_test.cc rename to test/extensions/stats_sinks/common/statsd/udp_statsd_test.cc index f19efafd9ccd..ef47d6b5dcb4 100644 --- a/test/common/stats/udp_statsd_test.cc +++ b/test/extensions/stats_sinks/common/statsd/udp_statsd_test.cc @@ -2,7 +2,8 @@ #include "common/network/address_impl.h" #include "common/network/utility.h" -#include "common/stats/statsd.h" + +#include "extensions/stat_sinks/common/statsd/statsd.h" #include "test/mocks/stats/mocks.h" #include "test/mocks/thread_local/mocks.h" @@ -17,7 +18,9 @@ using testing::NiceMock; namespace Envoy { -namespace Stats { +namespace Extensions { +namespace StatSinks { +namespace Common { namespace Statsd { class MockWriter : public Writer { @@ -41,15 +44,15 @@ TEST_P(UdpStatsdSinkTest, InitWithIpAddress) { EXPECT_NE(fd, -1); // Check that fd has not changed. - NiceMock counter; + NiceMock counter; counter.name_ = "test_counter"; sink.flushCounter(counter, 1); - NiceMock gauge; + NiceMock gauge; gauge.name_ = "test_gauge"; sink.flushGauge(gauge, 1); - NiceMock timer; + NiceMock timer; timer.name_ = "test_timer"; sink.onHistogramComplete(timer, 5); @@ -79,19 +82,19 @@ TEST_P(UdpStatsdSinkWithTagsTest, InitWithIpAddress) { EXPECT_NE(fd, -1); // Check that fd has not changed. - std::vector tags = {Tag{"node", "test"}}; - NiceMock counter; + std::vector tags = {Stats::Tag{"node", "test"}}; + NiceMock counter; counter.name_ = "test_counter"; counter.tags_ = tags; sink.flushCounter(counter, 1); - NiceMock gauge; + NiceMock gauge; gauge.name_ = "test_gauge"; gauge.tags_ = tags; sink.flushGauge(gauge, 1); - NiceMock timer; + NiceMock timer; timer.name_ = "test_timer"; timer.tags_ = tags; sink.onHistogramComplete(timer, 5); @@ -111,19 +114,19 @@ TEST(UdpStatsdSinkTest, CheckActualStats) { NiceMock tls_; UdpStatsdSink sink(tls_, writer_ptr, false); - NiceMock counter; + NiceMock counter; counter.name_ = "test_counter"; EXPECT_CALL(*std::dynamic_pointer_cast>(writer_ptr), write("envoy.test_counter:1|c")); sink.flushCounter(counter, 1); - NiceMock gauge; + NiceMock gauge; gauge.name_ = "test_gauge"; EXPECT_CALL(*std::dynamic_pointer_cast>(writer_ptr), write("envoy.test_gauge:1|g")); sink.flushGauge(gauge, 1); - NiceMock timer; + NiceMock timer; timer.name_ = "test_timer"; EXPECT_CALL(*std::dynamic_pointer_cast>(writer_ptr), write("envoy.test_timer:5|ms")); @@ -137,22 +140,22 @@ TEST(UdpStatsdSinkWithTagsTest, CheckActualStats) { NiceMock tls_; UdpStatsdSink sink(tls_, writer_ptr, true); - std::vector tags = {Tag{"key1", "value1"}, Tag{"key2", "value2"}}; - NiceMock counter; + std::vector tags = {Stats::Tag{"key1", "value1"}, Stats::Tag{"key2", "value2"}}; + NiceMock counter; counter.name_ = "test_counter"; counter.tags_ = tags; EXPECT_CALL(*std::dynamic_pointer_cast>(writer_ptr), write("envoy.test_counter:1|c|#key1:value1,key2:value2")); sink.flushCounter(counter, 1); - NiceMock gauge; + NiceMock gauge; gauge.name_ = "test_gauge"; gauge.tags_ = tags; EXPECT_CALL(*std::dynamic_pointer_cast>(writer_ptr), write("envoy.test_gauge:1|g|#key1:value1,key2:value2")); sink.flushGauge(gauge, 1); - NiceMock timer; + NiceMock timer; timer.name_ = "test_timer"; timer.tags_ = tags; EXPECT_CALL(*std::dynamic_pointer_cast>(writer_ptr), @@ -163,5 +166,7 @@ TEST(UdpStatsdSinkWithTagsTest, CheckActualStats) { } } // namespace Statsd -} // namespace Stats +} // namespace Common +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/test/server/config/stats/BUILD b/test/extensions/stats_sinks/dog_statsd/BUILD similarity index 80% rename from test/server/config/stats/BUILD rename to test/extensions/stats_sinks/dog_statsd/BUILD index 2ae08d70a799..e162412d94d0 100644 --- a/test/server/config/stats/BUILD +++ b/test/extensions/stats_sinks/dog_statsd/BUILD @@ -15,9 +15,7 @@ envoy_cc_test( "//include/envoy/registry", "//source/common/config:well_known_names", "//source/common/protobuf:utility_lib", - "//source/common/stats:statsd_lib", - "//source/server/config/stats:dog_statsd_lib", - "//source/server/config/stats:statsd_lib", + "//source/extensions/stat_sinks/dog_statsd:config", "//test/mocks/server:server_mocks", "//test/test_common:environment_lib", "//test/test_common:network_utility_lib", diff --git a/test/extensions/stats_sinks/dog_statsd/config_test.cc b/test/extensions/stats_sinks/dog_statsd/config_test.cc new file mode 100644 index 000000000000..078137a20e58 --- /dev/null +++ b/test/extensions/stats_sinks/dog_statsd/config_test.cc @@ -0,0 +1,69 @@ +#include "envoy/config/bootstrap/v2/bootstrap.pb.h" +#include "envoy/registry/registry.h" + +#include "common/config/well_known_names.h" +#include "common/protobuf/utility.h" + +#include "extensions/stat_sinks/common/statsd/statsd.h" +#include "extensions/stat_sinks/dog_statsd/config.h" + +#include "test/mocks/server/mocks.h" +#include "test/test_common/environment.h" +#include "test/test_common/network_utility.h" +#include "test/test_common/utility.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using testing::NiceMock; +using testing::Return; +using testing::ReturnRef; +using testing::_; + +namespace Envoy { +namespace Extensions { +namespace StatSinks { +namespace DogStatsd { + +class DogStatsdConfigLoopbackTest : public testing::TestWithParam {}; +INSTANTIATE_TEST_CASE_P(IpVersions, DogStatsdConfigLoopbackTest, + testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), + TestUtility::ipTestParamsToString); + +TEST_P(DogStatsdConfigLoopbackTest, ValidUdpIp) { + const std::string name = Config::StatsSinkNames::get().DOG_STATSD; + + envoy::config::metrics::v2::DogStatsdSink sink_config; + envoy::api::v2::core::Address& address = *sink_config.mutable_address(); + envoy::api::v2::core::SocketAddress& socket_address = *address.mutable_socket_address(); + socket_address.set_protocol(envoy::api::v2::core::SocketAddress::UDP); + auto loopback_flavor = Network::Test::getCanonicalLoopbackAddress(GetParam()); + socket_address.set_address(loopback_flavor->ip()->addressAsString()); + socket_address.set_port_value(8125); + + Server::Configuration::StatsSinkFactory* factory = + Registry::FactoryRegistry::getFactory(name); + ASSERT_NE(factory, nullptr); + + ProtobufTypes::MessagePtr message = factory->createEmptyConfigProto(); + MessageUtil::jsonConvert(sink_config, *message); + + NiceMock server; + Stats::SinkPtr sink = factory->createStatsSink(*message, server); + EXPECT_NE(sink, nullptr); + EXPECT_NE(dynamic_cast(sink.get()), nullptr); + EXPECT_EQ(dynamic_cast(sink.get())->getUseTagForTest(), true); +} + +// Negative test for protoc-gen-validate constraints for dog_statsd. +TEST(DogStatsdConfigTest, ValidateFail) { + NiceMock server; + EXPECT_THROW( + DogStatsdSinkFactory().createStatsSink(envoy::config::metrics::v2::DogStatsdSink(), server), + ProtoValidationException); +} + +} // namespace DogStatsd +} // namespace StatSinks +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/stats_sinks/metrics_service/BUILD b/test/extensions/stats_sinks/metrics_service/BUILD new file mode 100644 index 000000000000..8bcc7c2c3f8a --- /dev/null +++ b/test/extensions/stats_sinks/metrics_service/BUILD @@ -0,0 +1,40 @@ +licenses(["notice"]) # Apache 2 + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_test", + "envoy_package", +) + +envoy_package() + +envoy_cc_test( + name = "metrics_service_test", + srcs = ["grpc_metrics_service_impl_test.cc"], + deps = [ + "//source/common/config:well_known_names", + "//source/common/event:dispatcher_lib", + "//source/common/upstream:upstream_includes", + "//source/common/upstream:upstream_lib", + "//source/extensions/stat_sinks/metrics_service:metrics_service_grpc_lib", + "//test/common/upstream:utility_lib", + "//test/mocks/grpc:grpc_mocks", + "//test/mocks/local_info:local_info_mocks", + "//test/mocks/thread_local:thread_local_mocks", + "//test/mocks/upstream:upstream_mocks", + ], +) + +envoy_cc_test( + name = "metrics_service_integration_test", + srcs = ["metrics_service_integration_test.cc"], + deps = [ + "//source/common/buffer:zero_copy_input_stream_lib", + "//source/common/grpc:codec_lib", + "//source/common/grpc:common_lib", + "//source/extensions/stat_sinks/metrics_service:config", + "//test/common/grpc:grpc_client_integration_lib", + "//test/integration:http_integration_lib", + "@envoy_api//envoy/service/metrics/v2:metrics_service_cc", + ], +) diff --git a/test/common/stats/grpc_metrics_service_impl_test.cc b/test/extensions/stats_sinks/metrics_service/grpc_metrics_service_impl_test.cc similarity index 91% rename from test/common/stats/grpc_metrics_service_impl_test.cc rename to test/extensions/stats_sinks/metrics_service/grpc_metrics_service_impl_test.cc index 67026f8724aa..b8adeb1700f6 100644 --- a/test/common/stats/grpc_metrics_service_impl_test.cc +++ b/test/extensions/stats_sinks/metrics_service/grpc_metrics_service_impl_test.cc @@ -1,4 +1,4 @@ -#include "common/stats/grpc_metrics_service_impl.h" +#include "extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h" #include "test/mocks/grpc/mocks.h" #include "test/mocks/local_info/mocks.h" @@ -13,8 +13,9 @@ using testing::Return; using testing::_; namespace Envoy { -namespace Stats { -namespace Metrics { +namespace Extensions { +namespace StatSinks { +namespace MetricsService { class GrpcMetricsStreamerImplTest : public testing::Test { public: @@ -102,11 +103,11 @@ TEST(MetricsServiceSinkTest, CheckSendCall) { sink.beginFlush(); - NiceMock counter; + NiceMock counter; counter.name_ = "test_counter"; sink.flushCounter(counter, 1); - NiceMock gauge; + NiceMock gauge; gauge.name_ = "test_gauge"; sink.flushGauge(gauge, 1); EXPECT_CALL(*streamer_, send(_)); @@ -121,11 +122,11 @@ TEST(MetricsServiceSinkTest, CheckStatsCount) { sink.beginFlush(); - NiceMock counter; + NiceMock counter; counter.name_ = "test_counter"; sink.flushCounter(counter, 1); - NiceMock gauge; + NiceMock gauge; gauge.name_ = "test_gauge"; sink.flushGauge(gauge, 1); @@ -135,7 +136,7 @@ TEST(MetricsServiceSinkTest, CheckStatsCount) { // Verify only newly added metrics come after endFlush call. sink.beginFlush(); - NiceMock counter1; + NiceMock counter1; counter1.name_ = "test_counter"; sink.flushCounter(counter1, 1); @@ -143,6 +144,7 @@ TEST(MetricsServiceSinkTest, CheckStatsCount) { EXPECT_EQ(1, (*streamer_).metric_count); } -} // namespace Metrics -} // namespace Stats +} // namespace MetricsService +} // namespace StatSinks +} // namespace Extensions } // namespace Envoy diff --git a/test/integration/metrics_service_integration_test.cc b/test/extensions/stats_sinks/metrics_service/metrics_service_integration_test.cc similarity index 98% rename from test/integration/metrics_service_integration_test.cc rename to test/extensions/stats_sinks/metrics_service/metrics_service_integration_test.cc index b7b7f3255432..c81aea51577c 100644 --- a/test/integration/metrics_service_integration_test.cc +++ b/test/extensions/stats_sinks/metrics_service/metrics_service_integration_test.cc @@ -11,8 +11,7 @@ #include "gtest/gtest.h" namespace Envoy { -namespace Stats { -namespace Metrics { +namespace { class MetricsServiceIntegrationTest : public HttpIntegrationTest, public Grpc::GrpcClientIntegrationParamTest { @@ -128,6 +127,5 @@ TEST_P(MetricsServiceIntegrationTest, BasicFlow) { cleanup(); } -} // namespace Metrics -} // namespace Stats +} // namespace } // namespace Envoy diff --git a/test/extensions/stats_sinks/statsd/BUILD b/test/extensions/stats_sinks/statsd/BUILD new file mode 100644 index 000000000000..b1078e5c2517 --- /dev/null +++ b/test/extensions/stats_sinks/statsd/BUILD @@ -0,0 +1,25 @@ +licenses(["notice"]) # Apache 2 + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_test", + "envoy_package", +) + +envoy_package() + +envoy_cc_test( + name = "config_test", + srcs = ["config_test.cc"], + deps = [ + "//include/envoy/registry", + "//source/common/config:well_known_names", + "//source/common/protobuf:utility_lib", + "//source/extensions/stat_sinks/statsd:config", + "//test/mocks/server:server_mocks", + "//test/test_common:environment_lib", + "//test/test_common:network_utility_lib", + "//test/test_common:utility_lib", + "@envoy_api//envoy/config/bootstrap/v2:bootstrap_cc", + ], +) diff --git a/test/extensions/stats_sinks/statsd/config_test.cc b/test/extensions/stats_sinks/statsd/config_test.cc new file mode 100644 index 000000000000..771c678cc900 --- /dev/null +++ b/test/extensions/stats_sinks/statsd/config_test.cc @@ -0,0 +1,88 @@ +#include "envoy/config/bootstrap/v2/bootstrap.pb.h" +#include "envoy/registry/registry.h" + +#include "common/config/well_known_names.h" +#include "common/protobuf/utility.h" + +#include "extensions/stat_sinks/common/statsd/statsd.h" +#include "extensions/stat_sinks/statsd/config.h" + +#include "test/mocks/server/mocks.h" +#include "test/test_common/environment.h" +#include "test/test_common/network_utility.h" +#include "test/test_common/utility.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using testing::NiceMock; +using testing::Return; +using testing::ReturnRef; +using testing::_; + +namespace Envoy { +namespace Extensions { +namespace StatSinks { +namespace Statsd { + +TEST(StatsConfigTest, ValidTcpStatsd) { + const std::string name = Config::StatsSinkNames::get().STATSD; + + envoy::config::metrics::v2::StatsdSink sink_config; + sink_config.set_tcp_cluster_name("fake_cluster"); + + Server::Configuration::StatsSinkFactory* factory = + Registry::FactoryRegistry::getFactory(name); + ASSERT_NE(factory, nullptr); + + ProtobufTypes::MessagePtr message = factory->createEmptyConfigProto(); + MessageUtil::jsonConvert(sink_config, *message); + + NiceMock server; + Stats::SinkPtr sink = factory->createStatsSink(*message, server); + EXPECT_NE(sink, nullptr); + EXPECT_NE(dynamic_cast(sink.get()), nullptr); +} + +class StatsConfigLoopbackTest : public testing::TestWithParam {}; +INSTANTIATE_TEST_CASE_P(IpVersions, StatsConfigLoopbackTest, + testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), + TestUtility::ipTestParamsToString); + +TEST_P(StatsConfigLoopbackTest, ValidUdpIpStatsd) { + const std::string name = Config::StatsSinkNames::get().STATSD; + + envoy::config::metrics::v2::StatsdSink sink_config; + envoy::api::v2::core::Address& address = *sink_config.mutable_address(); + envoy::api::v2::core::SocketAddress& socket_address = *address.mutable_socket_address(); + socket_address.set_protocol(envoy::api::v2::core::SocketAddress::UDP); + auto loopback_flavor = Network::Test::getCanonicalLoopbackAddress(GetParam()); + socket_address.set_address(loopback_flavor->ip()->addressAsString()); + socket_address.set_port_value(8125); + + Server::Configuration::StatsSinkFactory* factory = + Registry::FactoryRegistry::getFactory(name); + ASSERT_NE(factory, nullptr); + + ProtobufTypes::MessagePtr message = factory->createEmptyConfigProto(); + MessageUtil::jsonConvert(sink_config, *message); + + NiceMock server; + Stats::SinkPtr sink = factory->createStatsSink(*message, server); + EXPECT_NE(sink, nullptr); + EXPECT_NE(dynamic_cast(sink.get()), nullptr); + EXPECT_EQ(dynamic_cast(sink.get())->getUseTagForTest(), false); +} + +// Negative test for protoc-gen-validate constraints for statsd. +TEST(StatsdConfigTest, ValidateFail) { + NiceMock server; + EXPECT_THROW( + StatsdSinkFactory().createStatsSink(envoy::config::metrics::v2::StatsdSink(), server), + ProtoValidationException); +} + +} // namespace Statsd +} // namespace StatSinks +} // namespace Extensions +} // namespace Envoy diff --git a/test/integration/BUILD b/test/integration/BUILD index 07a45b86ac3b..83d9e739b674 100644 --- a/test/integration/BUILD +++ b/test/integration/BUILD @@ -320,7 +320,6 @@ envoy_cc_test_library( "//source/server/config/network:http_connection_manager_lib", "//source/server/config/network:raw_buffer_socket_lib", "//source/server/config/network:ssl_socket_lib", - "//source/server/config/stats:statsd_lib", "//source/server/http:health_check_lib", "//test/common/upstream:utility_lib", "//test/config:utility_lib", @@ -347,7 +346,6 @@ envoy_cc_test( ":http_integration_lib", "//source/common/http:header_map_lib", "//source/extensions/filters/http/dynamo:config", - "//source/server/config/stats:statsd_lib", "//test/test_common:utility_lib", ], ) @@ -404,20 +402,6 @@ envoy_cc_test( ], ) -envoy_cc_test( - name = "metrics_service_integration_test", - srcs = ["metrics_service_integration_test.cc"], - deps = [ - ":http_integration_lib", - "//source/common/buffer:zero_copy_input_stream_lib", - "//source/common/grpc:codec_lib", - "//source/common/grpc:common_lib", - "//source/server/config/stats:metrics_service_lib", - "//test/common/grpc:grpc_client_integration_lib", - "@envoy_api//envoy/service/metrics/v2:metrics_service_cc", - ], -) - envoy_cc_test( name = "proxy_proto_integration_test", srcs = [ diff --git a/test/server/BUILD b/test/server/BUILD index a53b389b60f7..c034679c3ab6 100644 --- a/test/server/BUILD +++ b/test/server/BUILD @@ -27,10 +27,10 @@ envoy_cc_test( "//source/common/config:well_known_names", "//source/common/event:dispatcher_lib", "//source/common/upstream:cluster_manager_lib", + "//source/extensions/stat_sinks/statsd:config", "//source/server:configuration_lib", "//source/server/config/http:dynamic_opentracing_lib", "//source/server/config/network:raw_buffer_socket_lib", - "//source/server/config/stats:statsd_lib", "//test/mocks:common_lib", "//test/mocks/network:network_mocks", "//test/mocks/server:server_mocks", @@ -158,8 +158,8 @@ envoy_cc_test( "//source/extensions/filters/http/dynamo:config", "//source/extensions/filters/http/ratelimit:config", "//source/extensions/filters/network/redis_proxy:config", + "//source/extensions/stat_sinks/statsd:config", "//source/server:server_lib", - "//source/server/config/stats:statsd_lib", "//test/integration:integration_lib", "//test/mocks/server:server_mocks", "//test/mocks/stats:stats_mocks", diff --git a/test/server/config/stats/config_test.cc b/test/server/config/stats/config_test.cc deleted file mode 100644 index 11601f6c045a..000000000000 --- a/test/server/config/stats/config_test.cc +++ /dev/null @@ -1,124 +0,0 @@ -#include - -#include "envoy/config/bootstrap/v2/bootstrap.pb.h" -#include "envoy/registry/registry.h" - -#include "common/config/well_known_names.h" -#include "common/protobuf/utility.h" -#include "common/stats/statsd.h" - -#include "server/config/stats/dog_statsd.h" -#include "server/config/stats/statsd.h" - -#include "test/mocks/server/mocks.h" -#include "test/test_common/environment.h" -#include "test/test_common/network_utility.h" -#include "test/test_common/utility.h" - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using testing::NiceMock; -using testing::Return; -using testing::ReturnRef; -using testing::_; - -namespace Envoy { -namespace Server { -namespace Configuration { - -TEST(StatsConfigTest, ValidTcpStatsd) { - const std::string name = Config::StatsSinkNames::get().STATSD; - - envoy::config::metrics::v2::StatsdSink sink_config; - sink_config.set_tcp_cluster_name("fake_cluster"); - - StatsSinkFactory* factory = Registry::FactoryRegistry::getFactory(name); - ASSERT_NE(factory, nullptr); - - ProtobufTypes::MessagePtr message = factory->createEmptyConfigProto(); - MessageUtil::jsonConvert(sink_config, *message); - - NiceMock server; - Stats::SinkPtr sink = factory->createStatsSink(*message, server); - EXPECT_NE(sink, nullptr); - EXPECT_NE(dynamic_cast(sink.get()), nullptr); -} - -class StatsConfigLoopbackTest : public testing::TestWithParam {}; -INSTANTIATE_TEST_CASE_P(IpVersions, StatsConfigLoopbackTest, - testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), - TestUtility::ipTestParamsToString); - -TEST_P(StatsConfigLoopbackTest, ValidUdpIpStatsd) { - const std::string name = Config::StatsSinkNames::get().STATSD; - - envoy::config::metrics::v2::StatsdSink sink_config; - envoy::api::v2::core::Address& address = *sink_config.mutable_address(); - envoy::api::v2::core::SocketAddress& socket_address = *address.mutable_socket_address(); - socket_address.set_protocol(envoy::api::v2::core::SocketAddress::UDP); - auto loopback_flavor = Network::Test::getCanonicalLoopbackAddress(GetParam()); - socket_address.set_address(loopback_flavor->ip()->addressAsString()); - socket_address.set_port_value(8125); - - StatsSinkFactory* factory = Registry::FactoryRegistry::getFactory(name); - ASSERT_NE(factory, nullptr); - - ProtobufTypes::MessagePtr message = factory->createEmptyConfigProto(); - MessageUtil::jsonConvert(sink_config, *message); - - NiceMock server; - Stats::SinkPtr sink = factory->createStatsSink(*message, server); - EXPECT_NE(sink, nullptr); - EXPECT_NE(dynamic_cast(sink.get()), nullptr); - EXPECT_EQ(dynamic_cast(sink.get())->getUseTagForTest(), false); -} - -// Negative test for protoc-gen-validate constraints for statsd. -TEST(StatsdConfigTest, ValidateFail) { - NiceMock server; - EXPECT_THROW( - StatsdSinkFactory().createStatsSink(envoy::config::metrics::v2::StatsdSink(), server), - ProtoValidationException); -} - -class DogStatsdConfigLoopbackTest : public testing::TestWithParam {}; -INSTANTIATE_TEST_CASE_P(IpVersions, DogStatsdConfigLoopbackTest, - testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), - TestUtility::ipTestParamsToString); - -TEST_P(DogStatsdConfigLoopbackTest, ValidUdpIp) { - const std::string name = Config::StatsSinkNames::get().DOG_STATSD; - - envoy::config::metrics::v2::DogStatsdSink sink_config; - envoy::api::v2::core::Address& address = *sink_config.mutable_address(); - envoy::api::v2::core::SocketAddress& socket_address = *address.mutable_socket_address(); - socket_address.set_protocol(envoy::api::v2::core::SocketAddress::UDP); - auto loopback_flavor = Network::Test::getCanonicalLoopbackAddress(GetParam()); - socket_address.set_address(loopback_flavor->ip()->addressAsString()); - socket_address.set_port_value(8125); - - StatsSinkFactory* factory = Registry::FactoryRegistry::getFactory(name); - ASSERT_NE(factory, nullptr); - - ProtobufTypes::MessagePtr message = factory->createEmptyConfigProto(); - MessageUtil::jsonConvert(sink_config, *message); - - NiceMock server; - Stats::SinkPtr sink = factory->createStatsSink(*message, server); - EXPECT_NE(sink, nullptr); - EXPECT_NE(dynamic_cast(sink.get()), nullptr); - EXPECT_EQ(dynamic_cast(sink.get())->getUseTagForTest(), true); -} - -// Negative test for protoc-gen-validate constraints for dog_statsd. -TEST(DogStatsdConfigTest, ValidateFail) { - NiceMock server; - EXPECT_THROW( - DogStatsdSinkFactory().createStatsSink(envoy::config::metrics::v2::DogStatsdSink(), server), - ProtoValidationException); -} - -} // namespace Configuration -} // namespace Server -} // namespace Envoy