Skip to content

Commit

Permalink
Change constructor parameter type, pass config to SamplerConfigFetcher
Browse files Browse the repository at this point in the history
Signed-off-by: thomas.ebner <[email protected]>
  • Loading branch information
samohte committed Feb 12, 2024
1 parent af65744 commit e4a5974
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ envoy_cc_library(
"//source/extensions/tracers/opentelemetry:opentelemetry_tracer_lib",
"//source/extensions/tracers/opentelemetry:trace_state_lib",
"//source/extensions/tracers/opentelemetry/samplers:sampler_lib",
"@envoy_api//envoy/config/core/v3:pkg_cc_proto",
"@envoy_api//envoy/extensions/tracers/opentelemetry/samplers/v3:pkg_cc_proto",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ DynatraceSamplerFactory::createSampler(const Protobuf::Message& config,
const envoy::extensions::tracers::opentelemetry::samplers::v3::DynatraceSamplerConfig&>(
*mptr, context.messageValidationVisitor());

SamplerConfigFetcherPtr cf = std::make_unique<SamplerConfigFetcherImpl>(
context, proto_config.http_uri(), proto_config.token(), proto_config.root_spans_per_minute());
SamplerConfigFetcherPtr cf = std::make_unique<SamplerConfigFetcherImpl>(context, proto_config);
return std::make_shared<DynatraceSampler>(proto_config, context, std::move(cf));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ static constexpr std::chrono::minutes TIMER_INTERVAL{5};

SamplerConfigFetcherImpl::SamplerConfigFetcherImpl(
Server::Configuration::TracerFactoryContext& context,
const envoy::config::core::v3::HttpUri& http_uri, const std::string& token,
uint32_t default_root_spans_per_minute)
: cluster_manager_(context.serverFactoryContext().clusterManager()), http_uri_(http_uri),
const envoy::extensions::tracers::opentelemetry::samplers::v3::DynatraceSamplerConfig& config)
: cluster_manager_(context.serverFactoryContext().clusterManager()),
http_uri_(config.http_uri()),
parsed_authorization_header_to_add_(
{Http::LowerCaseString("authorization"), absl::StrCat("Api-Token ", token)}),
sampler_config_(default_root_spans_per_minute) {
{Http::LowerCaseString("authorization"), absl::StrCat("Api-Token ", config.token())}),
sampler_config_(config.root_spans_per_minute()) {

timer_ = context.serverFactoryContext().mainThreadDispatcher().createTimer([this]() -> void {
const auto thread_local_cluster = cluster_manager_.getThreadLocalCluster(http_uri_.cluster());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include <utility>
#include <vector>

#include "envoy/config/core/v3/http_service.pb.h"
#include "envoy/config/core/v3/http_uri.pb.h"
#include "envoy/extensions/tracers/opentelemetry/samplers/v3/dynatrace_sampler.pb.h"
#include "envoy/http/async_client.h"
#include "envoy/http/message.h"
#include "envoy/server/tracer_config.h"
Expand Down Expand Up @@ -33,9 +32,10 @@ class SamplerConfigFetcherImpl : public SamplerConfigFetcher,
public Logger::Loggable<Logger::Id::tracing>,
public Http::AsyncClient::Callbacks {
public:
SamplerConfigFetcherImpl(Server::Configuration::TracerFactoryContext& context,
const envoy::config::core::v3::HttpUri& http_uri,
const std::string& token, uint32_t default_root_spans_per_minute);
SamplerConfigFetcherImpl(
Server::Configuration::TracerFactoryContext& context,
const envoy::extensions::tracers::opentelemetry::samplers::v3::DynatraceSamplerConfig&
config);

void onSuccess(const Http::AsyncClient::Request& request,
Http::ResponseMessagePtr&& response) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ class SamplerConfigFetcherTest : public testing::Test {
SamplerConfigFetcherTest()
: request_(&tracer_factory_context_.server_factory_context_.cluster_manager_
.thread_local_cluster_.async_client_) {

const std::string yaml_string = R"EOF(
cluster: "cluster_name"
uri: "https://testhost.com/otlp/v1/traces"
timeout: 0.250s
tenant: "abc12345"
cluster_id: "980df25c"
token: "tokenval"
http_uri:
cluster: "cluster_name"
uri: "https://testhost.com/otlp/v1/traces"
timeout: 0.250s
root_spans_per_minute: 1000
)EOF";
TestUtility::loadFromYaml(yaml_string, http_uri_);
TestUtility::loadFromYaml(yaml_string, proto_config_);

ON_CALL(tracer_factory_context_.server_factory_context_.cluster_manager_,
getThreadLocalCluster(_))
Expand All @@ -47,7 +54,7 @@ class SamplerConfigFetcherTest : public testing::Test {

protected:
NiceMock<Envoy::Server::Configuration::MockTracerFactoryContext> tracer_factory_context_;
envoy::config::core::v3::HttpUri http_uri_;
envoy::extensions::tracers::opentelemetry::samplers::v3::DynatraceSamplerConfig proto_config_;
NiceMock<Event::MockTimer>* timer_;
Http::MockAsyncClientRequest request_;
};
Expand All @@ -68,15 +75,13 @@ TEST_F(SamplerConfigFetcherTest, TestRequestIsSent) {
EXPECT_CALL(tracer_factory_context_.server_factory_context_.cluster_manager_.thread_local_cluster_
.async_client_,
send_(MessageMatcher("unused-arg"), _, _));
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, http_uri_, "tokenval",
SamplerConfig::ROOT_SPANS_PER_MINUTE_DEFAULT);
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, proto_config_);
timer_->invokeCallback();
}

// Test receiving http response code 200 and valid json
TEST_F(SamplerConfigFetcherTest, TestResponseOkValidJson) {
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, http_uri_, "tokenXASSD",
SamplerConfig::ROOT_SPANS_PER_MINUTE_DEFAULT);
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, proto_config_);
timer_->invokeCallback();

Http::ResponseMessagePtr message(new Http::ResponseMessageImpl(
Expand All @@ -89,8 +94,7 @@ TEST_F(SamplerConfigFetcherTest, TestResponseOkValidJson) {

// Test receiving http response code 200 and invalid json
TEST_F(SamplerConfigFetcherTest, TestResponseOkInvalidJson) {
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, http_uri_, "tokenXASSD",
SamplerConfig::ROOT_SPANS_PER_MINUTE_DEFAULT);
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, proto_config_);
timer_->invokeCallback();

Http::ResponseMessagePtr message(new Http::ResponseMessageImpl(
Expand All @@ -104,8 +108,7 @@ TEST_F(SamplerConfigFetcherTest, TestResponseOkInvalidJson) {

// Test receiving http response code != 200
TEST_F(SamplerConfigFetcherTest, TestResponseErrorCode) {
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, http_uri_, "tokenXASSD",
SamplerConfig::ROOT_SPANS_PER_MINUTE_DEFAULT);
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, proto_config_);
timer_->invokeCallback();

Http::ResponseMessagePtr message(new Http::ResponseMessageImpl(
Expand All @@ -119,8 +122,7 @@ TEST_F(SamplerConfigFetcherTest, TestResponseErrorCode) {

// Test sending failed
TEST_F(SamplerConfigFetcherTest, TestOnFailure) {
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, http_uri_, "tokenXASSD",
SamplerConfig::ROOT_SPANS_PER_MINUTE_DEFAULT);
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, proto_config_);
timer_->invokeCallback();
config_fetcher.onFailure(request_, Http::AsyncClient::FailureReason::Reset);
EXPECT_EQ(config_fetcher.getSamplerConfig().getRootSpansPerMinute(),
Expand All @@ -131,8 +133,7 @@ TEST_F(SamplerConfigFetcherTest, TestOnFailure) {
// Test calling onBeforeFinalizeUpstreamSpan
TEST_F(SamplerConfigFetcherTest, TestOnBeforeFinalizeUpstreamSpan) {
Tracing::MockSpan child_span_;
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, http_uri_, "tokenXASSD",
SamplerConfig::ROOT_SPANS_PER_MINUTE_DEFAULT);
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, proto_config_);
// onBeforeFinalizeUpstreamSpan() is an empty method, nothing to ASSERT, nothing should happen
config_fetcher.onBeforeFinalizeUpstreamSpan(child_span_, nullptr);
}
Expand All @@ -143,8 +144,7 @@ TEST_F(SamplerConfigFetcherTest, TestNoCluster) {
ON_CALL(tracer_factory_context_.server_factory_context_.cluster_manager_,
getThreadLocalCluster(_))
.WillByDefault(Return(nullptr));
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, http_uri_, "tokenXASSD",
SamplerConfig::ROOT_SPANS_PER_MINUTE_DEFAULT);
SamplerConfigFetcherImpl config_fetcher(tracer_factory_context_, proto_config_);
timer_->invokeCallback();
// nothing to assert, should not crash or throw.
}
Expand Down

0 comments on commit e4a5974

Please sign in to comment.