-
Notifications
You must be signed in to change notification settings - Fork 138
Support Central Config #464
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'elastic_apm/central_config/cache_control' | ||
|
|
||
| module ElasticAPM | ||
| # @api private | ||
| class CentralConfig | ||
| include Logging | ||
|
|
||
| # @api private | ||
| class ResponseError < InternalError | ||
| def initialize(response) | ||
| @response = response | ||
| end | ||
|
|
||
| attr_reader :response | ||
| end | ||
| class ClientError < ResponseError; end | ||
| class ServerError < ResponseError; end | ||
|
|
||
| DEFAULT_MAX_AGE = 300 | ||
|
|
||
| def initialize(config) | ||
| @config = config | ||
| @modified_options = {} | ||
| @service_info = { | ||
| 'service.name': config.service_name, | ||
| 'service.environment': config.environment | ||
| }.to_json | ||
| end | ||
|
|
||
| attr_reader :config, :task | ||
|
|
||
| def start | ||
| return unless config.central_config? | ||
|
|
||
| fetch_and_apply_config | ||
| end | ||
|
|
||
| def fetch_and_apply_config | ||
| Concurrent::Promise | ||
| .execute(&method(:fetch_config)) | ||
| .on_success(&method(:handle_success)) | ||
| .rescue(&method(:handle_error)) | ||
| end | ||
|
|
||
| def stop | ||
| @task&.cancel | ||
| end | ||
|
|
||
| # rubocop:disable Metrics/MethodLength | ||
| def fetch_config | ||
| resp = perform_request | ||
|
|
||
| case resp.status | ||
| when 200..299 | ||
| resp | ||
| when 300..399 | ||
| resp | ||
| when 400..499 | ||
| raise ClientError, resp | ||
| when 500..599 | ||
| raise ServerError, resp | ||
mikker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
| # rubocop:enable Metrics/MethodLength | ||
|
|
||
| def assign(update) | ||
| # For each updated option, store the original value, | ||
| # unless already stored | ||
| update.each_key do |key| | ||
mikker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @modified_options[key] ||= config.get(key.to_sym)&.value | ||
| end | ||
|
|
||
| # If the new update doesn't set a previously modified option, | ||
| # revert it to the original | ||
| @modified_options.each_key do |key| | ||
| next if update.key?(key) | ||
| update[key] = @modified_options.delete(key) | ||
| end | ||
|
|
||
| config.assign(update) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # rubocop:disable Metrics/MethodLength | ||
| def handle_success(resp) | ||
| if resp.status == 304 | ||
| info 'Received 304 Not Modified' | ||
| else | ||
| update = JSON.parse(resp.body.to_s) | ||
| assign(update) | ||
|
|
||
| info 'Updated config from Kibana' | ||
| end | ||
|
|
||
| schedule_next_fetch(resp) | ||
|
|
||
| true | ||
| rescue Exception => e | ||
| error 'Failed to apply remote config, %s', e.inspect | ||
| debug { e.backtrace.join('\n') } | ||
| end | ||
| # rubocop:enable Metrics/MethodLength | ||
|
|
||
| def handle_error(error) | ||
| error( | ||
| 'Failed fetching config: %s, trying again in %d seconds', | ||
| error.response.body, DEFAULT_MAX_AGE | ||
mikker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| assign({}) | ||
|
|
||
| schedule_next_fetch(error.response) | ||
| end | ||
|
|
||
| def perform_request | ||
| Http.post( | ||
| config.server_url + '/agent/v1/config/', | ||
| body: @service_info, | ||
| headers: { etag: 1, content_type: 'application/json' } | ||
| ) | ||
| end | ||
|
|
||
| def schedule_next_fetch(resp) | ||
| seconds = | ||
| if (cache_header = resp.headers['Cache-Control']) | ||
| CacheControl.new(cache_header).max_age | ||
| else | ||
| DEFAULT_MAX_AGE | ||
| end | ||
|
|
||
| @task = | ||
| Concurrent::ScheduledTask | ||
| .execute(seconds, &method(:fetch_and_apply_config)) | ||
| end | ||
| end | ||
| end | ||
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,34 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ElasticAPM | ||
| class CentralConfig | ||
| # @api private | ||
| class CacheControl | ||
| def initialize(value) | ||
| @header = value | ||
| parse!(value) | ||
| end | ||
|
|
||
| attr_reader( | ||
| :must_revalidate, | ||
| :no_cache, | ||
| :no_store, | ||
| :no_transform, | ||
| :public, | ||
| :private, | ||
| :proxy_revalidate, | ||
| :max_age, | ||
| :s_maxage | ||
| ) | ||
|
|
||
| private | ||
|
|
||
| def parse!(value) | ||
| value.split(',').each do |token| | ||
| k, v = token.split('=').map(&:strip) | ||
| instance_variable_set(:"@#{k.gsub('-', '_')}", v ? v.to_i : true) | ||
mikker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ElasticAPM | ||
| RSpec.describe CentralConfig::CacheControl do | ||
| let(:header) { nil } | ||
| subject { described_class.new(header) } | ||
|
|
||
| context 'with max-age' do | ||
| let(:header) { 'max-age=300' } | ||
| its(:max_age) { should be 300 } | ||
| its(:must_revalidate) { should be nil } | ||
| end | ||
|
|
||
| context 'with must-revalidate' do | ||
| let(:header) { 'must-revalidate' } | ||
| its(:max_age) { should be nil } | ||
| its(:must_revalidate) { should be true } | ||
| end | ||
|
|
||
| context 'with multiple values' do | ||
| let(:header) { 'must-revalidate, public, max-age=300' } | ||
| its(:max_age) { should be 300 } | ||
| its(:must_revalidate) { should be true } | ||
| its(:public) { should be true } | ||
| its(:private) { should be nil } | ||
| end | ||
| end | ||
| end |
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.