Skip to content

Commit

Permalink
config: refactor tracers to use Config::Utility (#4984)
Browse files Browse the repository at this point in the history
Another PR for #4475. Refactor tracers config:

move interface to include/
Introduce FactoryBase to reduce boiler plate
Use Config::Utility to convert opaque config

Risk Level: Low
Testing: CI
Docs Changes: N/A
Release Notes: N/A

Signed-off-by: Lizan Zhou <[email protected]>
  • Loading branch information
lizan authored and htuch committed Nov 8, 2018
1 parent a196489 commit bacaa4c
Show file tree
Hide file tree
Showing 24 changed files with 209 additions and 171 deletions.
10 changes: 10 additions & 0 deletions include/envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,13 @@ envoy_cc_library(
"//source/common/singleton:const_singleton",
],
)

envoy_cc_library(
name = "tracer_config_interface",
hdrs = ["tracer_config.h"],
deps = [
":instance_interface",
"//source/common/protobuf",
"@envoy_api//envoy/config/trace/v2:trace_cc",
],
)
47 changes: 47 additions & 0 deletions include/envoy/server/tracer_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "envoy/common/pure.h"
#include "envoy/server/instance.h"
#include "envoy/tracing/http_tracer.h"

#include "common/protobuf/protobuf.h"

namespace Envoy {
namespace Server {
namespace Configuration {

/**
* Implemented by each Tracer and registered via Registry::registerFactory() or the convenience
* class RegisterFactory.
*/
class TracerFactory {
public:
virtual ~TracerFactory() {}

/**
* 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
* pointer should always be valid.
* @param config supplies the proto configuration for the HttpTracer
* @param server supplies the server instance
*/
virtual Tracing::HttpTracerPtr createHttpTracer(const Protobuf::Message& config,
Instance& server) PURE;

/**
* @return ProtobufTypes::MessagePtr create empty config proto message for v2. The tracing
* config, which arrives in an opaque message, will be parsed into this empty proto.
*/
virtual ProtobufTypes::MessagePtr createEmptyConfigProto() PURE;

/**
* Returns the identifying name for a particular implementation of tracer produced by the
* factory.
*/
virtual std::string name() PURE;
};

} // namespace Configuration
} // namespace Server
} // namespace Envoy
19 changes: 19 additions & 0 deletions source/extensions/tracers/common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
licenses(["notice"]) # Apache 2

load(
"//bazel:envoy_build_system.bzl",
"envoy_cc_library",
"envoy_package",
)

envoy_package()

envoy_cc_library(
name = "factory_base_lib",
hdrs = ["factory_base.h"],
deps = [
"//include/envoy/server:instance_interface",
"//include/envoy/server:tracer_config_interface",
"//source/common/config:utility_lib",
],
)
43 changes: 43 additions & 0 deletions source/extensions/tracers/common/factory_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "envoy/server/instance.h"
#include "envoy/server/tracer_config.h"

namespace Envoy {
namespace Extensions {
namespace Tracers {
namespace Common {

/**
* Common base class for tracer factory registrations. Removes a substantial amount of
* boilerplate.
*/
template <class ConfigProto> class FactoryBase : public Server::Configuration::TracerFactory {
public:
// Server::Configuration::TracerFactory
virtual Tracing::HttpTracerPtr createHttpTracer(const Protobuf::Message& config,
Server::Instance& server) override {
return createHttpTracerTyped(MessageUtil::downcastAndValidate<const ConfigProto&>(config),
server);
}

ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return std::make_unique<ConfigProto>();
}

std::string name() override { return name_; }

protected:
FactoryBase(const std::string& name) : name_(name) {}

private:
virtual Tracing::HttpTracerPtr createHttpTracerTyped(const ConfigProto& proto_config,
Server::Instance& server) PURE;

const std::string name_;
};

} // namespace Common
} // namespace Tracers
} // namespace Extensions
} // namespace Envoy
4 changes: 2 additions & 2 deletions source/extensions/tracers/datadog/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ envoy_cc_library(
],
external_deps = ["dd_opentracing_cpp"],
deps = [
"//source/common/config:utility_lib",
"//source/common/tracing:http_tracer_lib",
"//source/extensions/tracers:well_known_names",
"//source/extensions/tracers/common/ot:opentracing_driver_lib",
"//source/server:configuration_lib",
],
)

Expand All @@ -34,6 +34,6 @@ envoy_cc_library(
deps = [
":datadog_tracer_lib",
"//source/extensions/tracers:well_known_names",
"//source/server:configuration_lib",
"//source/extensions/tracers/common:factory_base_lib",
],
)
23 changes: 6 additions & 17 deletions source/extensions/tracers/datadog/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,16 @@ namespace Extensions {
namespace Tracers {
namespace Datadog {

Tracing::HttpTracerPtr
DatadogTracerFactory::createHttpTracer(const envoy::config::trace::v2::Tracing& configuration,
Server::Instance& server) {
DatadogTracerFactory::DatadogTracerFactory() : FactoryBase(TracerNames::get().Datadog) {}

ProtobufTypes::MessagePtr config_ptr = createEmptyConfigProto();

if (configuration.http().has_config()) {
MessageUtil::jsonConvert(configuration.http().config(), *config_ptr);
}

const auto& datadog_config =
dynamic_cast<const envoy::config::trace::v2::DatadogConfig&>(*config_ptr);

Tracing::DriverPtr datadog_driver{
std::make_unique<Driver>(datadog_config, server.clusterManager(), server.stats(),
server.threadLocal(), server.runtime())};
Tracing::HttpTracerPtr DatadogTracerFactory::createHttpTracerTyped(
const envoy::config::trace::v2::DatadogConfig& proto_config, Server::Instance& server) {
Tracing::DriverPtr datadog_driver =
std::make_unique<Driver>(proto_config, server.clusterManager(), server.stats(),
server.threadLocal(), server.runtime());
return std::make_unique<Tracing::HttpTracerImpl>(std::move(datadog_driver), server.localInfo());
}

std::string DatadogTracerFactory::name() { return TracerNames::get().Datadog; }

/**
* Static registration for the Datadog tracer. @see RegisterFactory.
*/
Expand Down
20 changes: 9 additions & 11 deletions source/extensions/tracers/datadog/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <string>

#include "envoy/server/instance.h"
#include "envoy/config/trace/v2/trace.pb.validate.h"

#include "server/configuration_impl.h"
#include "extensions/tracers/common/factory_base.h"

namespace Envoy {
namespace Extensions {
Expand All @@ -14,17 +14,15 @@ namespace Datadog {
/**
* Config registration for the Datadog tracer. @see TracerFactory.
*/
class DatadogTracerFactory : public Server::Configuration::TracerFactory {
class DatadogTracerFactory : public Common::FactoryBase<envoy::config::trace::v2::DatadogConfig> {
public:
// TracerFactory
Tracing::HttpTracerPtr createHttpTracer(const envoy::config::trace::v2::Tracing& configuration,
Server::Instance& server) override;
DatadogTracerFactory();

ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return std::make_unique<envoy::config::trace::v2::DatadogConfig>();
}

std::string name() override;
private:
// FactoryBase
Tracing::HttpTracerPtr
createHttpTracerTyped(const envoy::config::trace::v2::DatadogConfig& proto_config,
Server::Instance& server) override;
};

} // namespace Datadog
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/dynamic_ot/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ envoy_cc_library(
deps = [
":dynamic_opentracing_driver_lib",
"//source/extensions/tracers:well_known_names",
"//source/server:configuration_lib",
"//source/extensions/tracers/common:factory_base_lib",
],
)
26 changes: 9 additions & 17 deletions source/extensions/tracers/dynamic_ot/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,18 @@ namespace Extensions {
namespace Tracers {
namespace DynamicOt {

Tracing::HttpTracerPtr DynamicOpenTracingTracerFactory::createHttpTracer(
const envoy::config::trace::v2::Tracing& configuration, Server::Instance& server) {
ProtobufTypes::MessagePtr config_ptr = createEmptyConfigProto();

if (configuration.http().has_config()) {
MessageUtil::jsonConvert(configuration.http().config(), *config_ptr);
}

const auto& dynaot_config =
dynamic_cast<const envoy::config::trace::v2::DynamicOtConfig&>(*config_ptr);

const std::string library = dynaot_config.library();
const std::string config = MessageUtil::getJsonStringFromMessage(dynaot_config.config());
Tracing::DriverPtr dynamic_driver{
std::make_unique<DynamicOpenTracingDriver>(server.stats(), library, config)};
DynamicOpenTracingTracerFactory::DynamicOpenTracingTracerFactory()
: FactoryBase(TracerNames::get().DynamicOt) {}

Tracing::HttpTracerPtr DynamicOpenTracingTracerFactory::createHttpTracerTyped(
const envoy::config::trace::v2::DynamicOtConfig& proto_config, Server::Instance& server) {
const std::string library = proto_config.library();
const std::string config = MessageUtil::getJsonStringFromMessage(proto_config.config());
Tracing::DriverPtr dynamic_driver =
std::make_unique<DynamicOpenTracingDriver>(server.stats(), library, config);
return std::make_unique<Tracing::HttpTracerImpl>(std::move(dynamic_driver), server.localInfo());
}

std::string DynamicOpenTracingTracerFactory::name() { return TracerNames::get().DynamicOt; }

/**
* Static registration for the dynamic opentracing tracer. @see RegisterFactory.
*/
Expand Down
23 changes: 10 additions & 13 deletions source/extensions/tracers/dynamic_ot/config.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <string>
#include "envoy/config/trace/v2/trace.pb.validate.h"

#include "envoy/server/instance.h"

#include "server/configuration_impl.h"
#include "extensions/tracers/common/factory_base.h"

namespace Envoy {
namespace Extensions {
Expand All @@ -14,17 +12,16 @@ namespace DynamicOt {
/**
* Config registration for the dynamic opentracing tracer. @see TracerFactory.
*/
class DynamicOpenTracingTracerFactory : public Server::Configuration::TracerFactory {
class DynamicOpenTracingTracerFactory
: public Common::FactoryBase<envoy::config::trace::v2::DynamicOtConfig> {
public:
// TracerFactory
Tracing::HttpTracerPtr createHttpTracer(const envoy::config::trace::v2::Tracing& configuration,
Server::Instance& server) override;

ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return std::make_unique<envoy::config::trace::v2::DynamicOtConfig>();
}
DynamicOpenTracingTracerFactory();

std::string name() override;
private:
// FactoryBase
Tracing::HttpTracerPtr
createHttpTracerTyped(const envoy::config::trace::v2::DynamicOtConfig& configuration,
Server::Instance& server) override;
};

} // namespace DynamicOt
Expand Down
4 changes: 2 additions & 2 deletions source/extensions/tracers/lightstep/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ envoy_cc_library(
],
external_deps = ["lightstep"],
deps = [
"//source/common/config:utility_lib",
"//source/common/tracing:http_tracer_lib",
"//source/extensions/tracers:well_known_names",
"//source/extensions/tracers/common/ot:opentracing_driver_lib",
"//source/server:configuration_lib",
],
)

Expand All @@ -34,6 +34,6 @@ envoy_cc_library(
deps = [
":lightstep_tracer_lib",
"//source/extensions/tracers:well_known_names",
"//source/server:configuration_lib",
"//source/extensions/tracers/common:factory_base_lib",
],
)
27 changes: 8 additions & 19 deletions source/extensions/tracers/lightstep/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,22 @@ namespace Extensions {
namespace Tracers {
namespace Lightstep {

Tracing::HttpTracerPtr
LightstepTracerFactory::createHttpTracer(const envoy::config::trace::v2::Tracing& configuration,
Server::Instance& server) {
ProtobufTypes::MessagePtr config_ptr = createEmptyConfigProto();
LightstepTracerFactory::LightstepTracerFactory() : FactoryBase(TracerNames::get().Lightstep) {}

if (configuration.http().has_config()) {
MessageUtil::jsonConvert(configuration.http().config(), *config_ptr);
}

const auto& lightstep_config =
dynamic_cast<const envoy::config::trace::v2::LightstepConfig&>(*config_ptr);

std::unique_ptr<lightstep::LightStepTracerOptions> opts(new lightstep::LightStepTracerOptions());
const auto access_token_file = server.api().fileReadToEnd(lightstep_config.access_token_file());
Tracing::HttpTracerPtr LightstepTracerFactory::createHttpTracerTyped(
const envoy::config::trace::v2::LightstepConfig& proto_config, Server::Instance& server) {
auto opts = std::make_unique<lightstep::LightStepTracerOptions>();
const auto access_token_file = server.api().fileReadToEnd(proto_config.access_token_file());
const auto access_token_sv = StringUtil::rtrim(access_token_file);
opts->access_token.assign(access_token_sv.data(), access_token_sv.size());
opts->component_name = server.localInfo().clusterName();

Tracing::DriverPtr lightstep_driver{std::make_unique<LightStepDriver>(
lightstep_config, server.clusterManager(), server.stats(), server.threadLocal(),
server.runtime(), std::move(opts),
Common::Ot::OpenTracingDriver::PropagationMode::TracerNative)};
Tracing::DriverPtr lightstep_driver = std::make_unique<LightStepDriver>(
proto_config, server.clusterManager(), server.stats(), server.threadLocal(), server.runtime(),
std::move(opts), Common::Ot::OpenTracingDriver::PropagationMode::TracerNative);
return std::make_unique<Tracing::HttpTracerImpl>(std::move(lightstep_driver), server.localInfo());
}

std::string LightstepTracerFactory::name() { return TracerNames::get().Lightstep; }

/**
* Static registration for the lightstep tracer. @see RegisterFactory.
*/
Expand Down
Loading

0 comments on commit bacaa4c

Please sign in to comment.