forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
initial version of a config fetcher. #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
285343b
initial version of a config fetcher.
samohte f3477d7
fix dynatrace sampler unit test
samohte 2497f1a
add SamplerConfig, add json parsing
samohte 133c5e4
* change expected json
samohte fbddf4b
add unit test for sampler_config_fetcher
samohte c0210b1
add tests, cleanup
samohte 52fdff5
use http_uri instead of http_service in dynatrace_sampler protobuf
samohte 9a36c83
Prefix token from config with "Api-Token" when creating authorization
samohte f636d1c
extended test to verify sent messages
samohte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #pragma once | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "source/common/json/json_loader.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Tracers { | ||
| namespace OpenTelemetry { | ||
|
|
||
| class SamplerConfig { | ||
| public: | ||
| static constexpr uint64_t ROOT_SPANS_PER_MINUTE_DEFAULT = 1000; | ||
|
|
||
| void parse(const std::string& json) { | ||
| root_spans_per_minute_.store(ROOT_SPANS_PER_MINUTE_DEFAULT); // reset to default | ||
| auto result = Envoy::Json::Factory::loadFromStringNoThrow(json); | ||
| if (result.ok()) { | ||
| auto obj = result.value(); | ||
| if (obj->hasObject("rootSpansPerMinute")) { | ||
| auto value = obj->getInteger("rootSpansPerMinute", ROOT_SPANS_PER_MINUTE_DEFAULT); | ||
| root_spans_per_minute_.store(value); | ||
| } | ||
| (void)obj; | ||
| } | ||
| } | ||
|
|
||
| uint64_t getRootSpansPerMinute() const { return root_spans_per_minute_.load(); } | ||
|
|
||
| private: | ||
| std::atomic<uint64_t> root_spans_per_minute_ = ROOT_SPANS_PER_MINUTE_DEFAULT; | ||
| }; | ||
|
|
||
| } // namespace OpenTelemetry | ||
| } // namespace Tracers | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
77 changes: 77 additions & 0 deletions
77
source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config_fetcher.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #include "source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config_fetcher.h" | ||
|
|
||
| #include "source/common/common/enum_to_int.h" | ||
| #include "source/common/http/utility.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Tracers { | ||
| namespace OpenTelemetry { | ||
|
|
||
| static constexpr std::chrono::seconds INITIAL_TIMER_DURATION{10}; | ||
| static constexpr std::chrono::minutes TIMER_INTERVAL{5}; | ||
|
|
||
| SamplerConfigFetcher::SamplerConfigFetcher(Server::Configuration::TracerFactoryContext& context, | ||
| const envoy::config::core::v3::HttpUri& http_uri, | ||
| const std::string& token) | ||
| : cluster_manager_(context.serverFactoryContext().clusterManager()), http_uri_(http_uri), | ||
| parsed_authorization_header_to_add_( | ||
| {Http::LowerCaseString("authorization"), absl::StrCat("Api-Token ", token)}), | ||
| sampler_config_() { | ||
|
|
||
| timer_ = context.serverFactoryContext().mainThreadDispatcher().createTimer([this]() -> void { | ||
| const auto thread_local_cluster = cluster_manager_.getThreadLocalCluster(http_uri_.cluster()); | ||
| if (thread_local_cluster == nullptr) { | ||
| ENVOY_LOG(error, "SamplerConfigFetcher failed: [cluster = {}] is not configured", | ||
| http_uri_.cluster()); | ||
| } else { | ||
| Http::RequestMessagePtr message = Http::Utility::prepareHeaders(http_uri_); | ||
| // TODO: set path once it is finalized in TI-8742 | ||
| // message->headers().setPath("path/to/sampler/service"); | ||
| message->headers().setReferenceMethod(Http::Headers::get().MethodValues.Get); | ||
| message->headers().setReference(parsed_authorization_header_to_add_.first, | ||
| parsed_authorization_header_to_add_.second); | ||
| active_request_ = thread_local_cluster->httpAsyncClient().send( | ||
| std::move(message), *this, | ||
| Http::AsyncClient::RequestOptions().setTimeout(std::chrono::milliseconds(6000))); | ||
| } | ||
| }); | ||
|
|
||
| timer_->enableTimer(std::chrono::seconds(INITIAL_TIMER_DURATION)); | ||
| } | ||
|
|
||
| SamplerConfigFetcher::~SamplerConfigFetcher() { | ||
| if (active_request_) { | ||
| active_request_->cancel(); | ||
| } | ||
| } | ||
|
|
||
| void SamplerConfigFetcher::onSuccess(const Http::AsyncClient::Request& /*request*/, | ||
| Http::ResponseMessagePtr&& http_response) { | ||
| onRequestDone(); | ||
| const auto response_code = Http::Utility::getResponseStatus(http_response->headers()); | ||
| if (response_code != enumToInt(Http::Code::OK)) { | ||
| ENVOY_LOG(error, "SamplerConfigFetcher received a non-success status code: {}", response_code); | ||
| } else { | ||
| ENVOY_LOG(info, "SamplerConfigFetcher received success status code: {}", response_code); | ||
| sampler_config_.parse(http_response->bodyAsString()); | ||
| } | ||
| } | ||
|
|
||
| void SamplerConfigFetcher::onFailure(const Http::AsyncClient::Request& /*request*/, | ||
| Http::AsyncClient::FailureReason reason) { | ||
| onRequestDone(); | ||
| ENVOY_LOG(info, "The OTLP export request failed. Reason {}", enumToInt(reason)); | ||
| } | ||
|
|
||
| void SamplerConfigFetcher::onRequestDone() { | ||
| // TODO: should we re-enable timer after send() to avoid having the request duration added to the | ||
| // timer? If so, we would need a list containing the active requests (not a single pointer) | ||
| active_request_ = nullptr; | ||
| timer_->enableTimer(std::chrono::seconds(TIMER_INTERVAL)); | ||
| } | ||
|
|
||
| } // namespace OpenTelemetry | ||
| } // namespace Tracers | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
57 changes: 57 additions & 0 deletions
57
source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config_fetcher.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #pragma once | ||
|
|
||
| #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" | ||
|
|
||
| #include "source/common/http/async_client_impl.h" | ||
| #include "source/common/http/async_client_utility.h" | ||
| #include "source/common/http/headers.h" | ||
| #include "source/common/http/message_impl.h" | ||
| #include "source/common/http/utility.h" | ||
| #include "source/extensions/tracers/opentelemetry/samplers/dynatrace/sampler_config.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace Tracers { | ||
| namespace OpenTelemetry { | ||
|
|
||
| class SamplerConfigFetcher : public Logger::Loggable<Logger::Id::tracing>, | ||
| public Http::AsyncClient::Callbacks { | ||
| public: | ||
| SamplerConfigFetcher(Server::Configuration::TracerFactoryContext& context, | ||
| const envoy::config::core::v3::HttpUri& http_uri, const std::string& token); | ||
|
|
||
| void onSuccess(const Http::AsyncClient::Request& request, | ||
| Http::ResponseMessagePtr&& response) override; | ||
|
|
||
| void onFailure(const Http::AsyncClient::Request& request, | ||
| Http::AsyncClient::FailureReason reason) override; | ||
| void onBeforeFinalizeUpstreamSpan(Envoy::Tracing::Span& /*span*/, | ||
| const Http::ResponseHeaderMap* /*response_headers*/) override{}; | ||
|
|
||
| const SamplerConfig& getSamplerConfig() const { return sampler_config_; } | ||
|
|
||
| ~SamplerConfigFetcher() override; | ||
|
|
||
| private: | ||
| Event::TimerPtr timer_; | ||
| Upstream::ClusterManager& cluster_manager_; | ||
| envoy::config::core::v3::HttpUri http_uri_; | ||
| std::pair<const Http::LowerCaseString, const std::string> parsed_authorization_header_to_add_; | ||
| Http::AsyncClient::Request* active_request_{}; | ||
| SamplerConfig sampler_config_; | ||
|
|
||
| void onRequestDone(); | ||
| }; | ||
|
|
||
| } // namespace OpenTelemetry | ||
| } // namespace Tracers | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.