configdirector-server-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +2 -0
- data/CHANGELOG.md +254 -0
- data/LICENSE +21 -0
- data/README.md +230 -0
- data/lib/configdirector/client_hooks.rb +46 -0
- data/lib/configdirector/config_director_client.rb +322 -0
- data/lib/configdirector/config_evaluated_event.rb +10 -0
- data/lib/configdirector/config_evaluation.rb +51 -0
- data/lib/configdirector/config_state.rb +39 -0
- data/lib/configdirector/configs_updated_event.rb +23 -0
- data/lib/configdirector/connection_error_event.rb +11 -0
- data/lib/configdirector/connection_options.rb +60 -0
- data/lib/configdirector/context.rb +53 -0
- data/lib/configdirector/errors.rb +29 -0
- data/lib/configdirector/internal/client/client_connection.rb +119 -0
- data/lib/configdirector/internal/client/client_factory.rb +104 -0
- data/lib/configdirector/internal/client/client_options_validation.rb +53 -0
- data/lib/configdirector/internal/client/config_read.rb +31 -0
- data/lib/configdirector/internal/client/config_reader.rb +55 -0
- data/lib/configdirector/internal/client/connection_intake.rb +59 -0
- data/lib/configdirector/internal/client/connection_options_validation.rb +94 -0
- data/lib/configdirector/internal/client/context_admission.rb +48 -0
- data/lib/configdirector/internal/client/context_attributes.rb +78 -0
- data/lib/configdirector/internal/client/context_kind.rb +24 -0
- data/lib/configdirector/internal/client/evaluated_read.rb +9 -0
- data/lib/configdirector/internal/client/event_handlers.rb +114 -0
- data/lib/configdirector/internal/client/event_tracking.rb +39 -0
- data/lib/configdirector/internal/client/live_client_connections.rb +58 -0
- data/lib/configdirector/internal/client/number_of_seconds.rb +21 -0
- data/lib/configdirector/internal/client/registrations.rb +43 -0
- data/lib/configdirector/internal/client/restart_in_forked_processes.rb +25 -0
- data/lib/configdirector/internal/client/served_configs.rb +83 -0
- data/lib/configdirector/internal/client/serving_state.rb +102 -0
- data/lib/configdirector/internal/client/watches.rb +51 -0
- data/lib/configdirector/internal/client/what_follows_a_fork.rb +24 -0
- data/lib/configdirector/internal/connection/condition_parser.rb +74 -0
- data/lib/configdirector/internal/connection/config_definition.rb +37 -0
- data/lib/configdirector/internal/connection/http_session.rb +15 -0
- data/lib/configdirector/internal/connection/in_memory_connection.rb +134 -0
- data/lib/configdirector/internal/connection/json_post_request.rb +24 -0
- data/lib/configdirector/internal/connection/not_a_server_sdk_payload.rb +9 -0
- data/lib/configdirector/internal/connection/offline_mode_connection.rb +96 -0
- data/lib/configdirector/internal/connection/offline_values_file.rb +49 -0
- data/lib/configdirector/internal/connection/payloads_delivered_in_turn.rb +46 -0
- data/lib/configdirector/internal/connection/polling_interval.rb +21 -0
- data/lib/configdirector/internal/connection/request_deadline.rb +45 -0
- data/lib/configdirector/internal/connection/retry_after_header.rb +24 -0
- data/lib/configdirector/internal/connection/server_sdk_payload.rb +37 -0
- data/lib/configdirector/internal/connection/server_sdk_payload_parser.rb +89 -0
- data/lib/configdirector/internal/connection/targeting_rules_parser.rb +70 -0
- data/lib/configdirector/internal/connection/test_client_initializations.rb +111 -0
- data/lib/configdirector/internal/connection/test_client_values.rb +53 -0
- data/lib/configdirector/internal/connection/unreadable_definition.rb +9 -0
- data/lib/configdirector/internal/connection/wire_fields.rb +59 -0
- data/lib/configdirector/internal/evaluation/array_comparison.rb +25 -0
- data/lib/configdirector/internal/evaluation/assigned_percentage.rb +21 -0
- data/lib/configdirector/internal/evaluation/attribute_condition.rb +48 -0
- data/lib/configdirector/internal/evaluation/config_evaluator.rb +29 -0
- data/lib/configdirector/internal/evaluation/datetime_comparison.rb +93 -0
- data/lib/configdirector/internal/evaluation/evaluation_context.rb +17 -0
- data/lib/configdirector/internal/evaluation/number_comparison.rb +119 -0
- data/lib/configdirector/internal/evaluation/rapidhash.rb +124 -0
- data/lib/configdirector/internal/evaluation/rollout_share.rb +9 -0
- data/lib/configdirector/internal/evaluation/rule.rb +70 -0
- data/lib/configdirector/internal/evaluation/scalar_text.rb +70 -0
- data/lib/configdirector/internal/evaluation/segment.rb +17 -0
- data/lib/configdirector/internal/evaluation/segment_condition.rb +26 -0
- data/lib/configdirector/internal/evaluation/semver_comparison.rb +49 -0
- data/lib/configdirector/internal/evaluation/served_value.rb +9 -0
- data/lib/configdirector/internal/evaluation/share.rb +9 -0
- data/lib/configdirector/internal/evaluation/targeting_rules.rb +18 -0
- data/lib/configdirector/internal/evaluation/text_comparison.rb +32 -0
- data/lib/configdirector/internal/evaluation/trait_pointer.rb +42 -0
- data/lib/configdirector/internal/evaluation.rb +21 -0
- data/lib/configdirector/internal/identity/sdk_identity.rb +39 -0
- data/lib/configdirector/internal/identity/wrapper.rb +48 -0
- data/lib/configdirector/internal/logging/client_log.rb +37 -0
- data/lib/configdirector/internal/logging/default_logger.rb +34 -0
- data/lib/configdirector/internal/polling/poll_answer.rb +111 -0
- data/lib/configdirector/internal/polling/poll_request.rb +106 -0
- data/lib/configdirector/internal/polling/poll_timing.rb +25 -0
- data/lib/configdirector/internal/polling/polling_endpoint.rb +36 -0
- data/lib/configdirector/internal/polling/polling_mode_connection.rb +53 -0
- data/lib/configdirector/internal/polling/polling_session.rb +99 -0
- data/lib/configdirector/internal/polling/polling_transport.rb +74 -0
- data/lib/configdirector/internal/polling/refusal_detail.rb +36 -0
- data/lib/configdirector/internal/server_sent_events/event_stream_limit_exceeded.rb +9 -0
- data/lib/configdirector/internal/server_sent_events/event_stream_parser.rb +95 -0
- data/lib/configdirector/internal/server_sent_events/server_sent_event.rb +9 -0
- data/lib/configdirector/internal/stoppable_thread.rb +69 -0
- data/lib/configdirector/internal/streaming/heartbeats.rb +51 -0
- data/lib/configdirector/internal/streaming/reconnecting_stream.rb +82 -0
- data/lib/configdirector/internal/streaming/reconnection_delay.rb +29 -0
- data/lib/configdirector/internal/streaming/stream_connection.rb +86 -0
- data/lib/configdirector/internal/streaming/stream_end.rb +69 -0
- data/lib/configdirector/internal/streaming/stream_endpoint.rb +43 -0
- data/lib/configdirector/internal/streaming/stream_timing.rb +33 -0
- data/lib/configdirector/internal/streaming/streaming_mode_connection.rb +60 -0
- data/lib/configdirector/internal/streaming/streaming_transport.rb +42 -0
- data/lib/configdirector/internal/telemetry/aggregated_wire_entries.rb +26 -0
- data/lib/configdirector/internal/telemetry/collected_telemetry.rb +98 -0
- data/lib/configdirector/internal/telemetry/compact_json_text.rb +64 -0
- data/lib/configdirector/internal/telemetry/discarded_telemetry.rb +31 -0
- data/lib/configdirector/internal/telemetry/event_reporter.rb +47 -0
- data/lib/configdirector/internal/telemetry/invalid_context_submissions.rb +76 -0
- data/lib/configdirector/internal/telemetry/report_answer.rb +31 -0
- data/lib/configdirector/internal/telemetry/report_schedule.rb +66 -0
- data/lib/configdirector/internal/telemetry/report_timing.rb +20 -0
- data/lib/configdirector/internal/telemetry/reported_context.rb +28 -0
- data/lib/configdirector/internal/telemetry/reported_details.rb +71 -0
- data/lib/configdirector/internal/telemetry/reported_evaluation.rb +82 -0
- data/lib/configdirector/internal/telemetry/reported_telemetry.rb +127 -0
- data/lib/configdirector/internal/telemetry/reported_text.rb +13 -0
- data/lib/configdirector/internal/telemetry/reported_time.rb +13 -0
- data/lib/configdirector/internal/telemetry/reported_value.rb +35 -0
- data/lib/configdirector/internal/telemetry/telemetry_endpoint.rb +54 -0
- data/lib/configdirector/internal/telemetry/telemetry_report.rb +50 -0
- data/lib/configdirector/internal/telemetry/test_client_telemetry.rb +37 -0
- data/lib/configdirector/internal/telemetry/tracked_event_admission.rb +72 -0
- data/lib/configdirector/internal/telemetry/tracked_event_entry.rb +27 -0
- data/lib/configdirector/internal/telemetry/tracked_event_queue.rb +47 -0
- data/lib/configdirector/internal/telemetry/tracked_event_warning.rb +28 -0
- data/lib/configdirector/internal/telemetry/value_id.rb +23 -0
- data/lib/configdirector/internal/telemetry/written_json.rb +9 -0
- data/lib/configdirector/internal/unchangeable_copy.rb +38 -0
- data/lib/configdirector/internal/value/encoded_native_value.rb +45 -0
- data/lib/configdirector/internal/value/hash_keys.rb +34 -0
- data/lib/configdirector/internal/value/in_code_default_value.rb +33 -0
- data/lib/configdirector/internal/value/json_contents.rb +81 -0
- data/lib/configdirector/internal/value/json_position.rb +27 -0
- data/lib/configdirector/internal/value/native_values.rb +41 -0
- data/lib/configdirector/internal/value/parsed_json.rb +79 -0
- data/lib/configdirector/internal/value/plain_decimal_text.rb +36 -0
- data/lib/configdirector/internal/value/served_text.rb +76 -0
- data/lib/configdirector/internal/value/typed_value.rb +9 -0
- data/lib/configdirector/internal/value/unservable_value.rb +9 -0
- data/lib/configdirector/internal/value/utf8_text.rb +19 -0
- data/lib/configdirector/internal.rb +9 -0
- data/lib/configdirector/metadata.rb +26 -0
- data/lib/configdirector/subscription.rb +34 -0
- data/lib/configdirector/telemetry_options.rb +28 -0
- data/lib/configdirector/testing/test_client.rb +122 -0
- data/lib/configdirector/testing/tracked_event.rb +46 -0
- data/lib/configdirector/testing.rb +44 -0
- data/lib/configdirector/variation.rb +21 -0
- data/lib/configdirector/version.rb +7 -0
- data/lib/configdirector-server-sdk.rb +3 -0
- data/lib/configdirector.rb +164 -0
- data/sig/configdirector/internal/client.rbs +518 -0
- data/sig/configdirector/internal/connection.rbs +389 -0
- data/sig/configdirector/internal/evaluation.rbs +364 -0
- data/sig/configdirector/internal/identity.rbs +51 -0
- data/sig/configdirector/internal/logging.rbs +33 -0
- data/sig/configdirector/internal/polling.rbs +240 -0
- data/sig/configdirector/internal/server_sent_events.rbs +51 -0
- data/sig/configdirector/internal/streaming.rbs +266 -0
- data/sig/configdirector/internal/telemetry.rbs +556 -0
- data/sig/configdirector/internal/value.rbs +159 -0
- data/sig/configdirector/testing.rbs +70 -0
- data/sig/configdirector.rbs +465 -0
- metadata +231 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
class CollectedTelemetry
|
|
7
|
+
SHARE_OF_THE_EVENT_QUEUE_FOR_EVALUATIONS = 0.7
|
|
8
|
+
SHARE_OF_THE_EVENT_QUEUE_FOR_CAPTURED_CONTEXTS = 0.3
|
|
9
|
+
|
|
10
|
+
REPORTER_OF_WHAT_THE_PARENT_PROCESS_COLLECTED_BY_WHAT_FORKED = {
|
|
11
|
+
fork: :parent_process,
|
|
12
|
+
daemon: :forked_process
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
private_constant :SHARE_OF_THE_EVENT_QUEUE_FOR_EVALUATIONS, :SHARE_OF_THE_EVENT_QUEUE_FOR_CAPTURED_CONTEXTS,
|
|
16
|
+
:REPORTER_OF_WHAT_THE_PARENT_PROCESS_COLLECTED_BY_WHAT_FORKED
|
|
17
|
+
|
|
18
|
+
def initialize(event_queue_limit)
|
|
19
|
+
@evaluations_limit = (event_queue_limit * SHARE_OF_THE_EVENT_QUEUE_FOR_EVALUATIONS).ceil
|
|
20
|
+
@captured_contexts_limit = (event_queue_limit * SHARE_OF_THE_EVENT_QUEUE_FOR_CAPTURED_CONTEXTS).floor
|
|
21
|
+
@invalid_context_submissions = InvalidContextSubmissions.new
|
|
22
|
+
@tracked_event_queue = TrackedEventQueue.new
|
|
23
|
+
@collecting_process_id = Process.pid
|
|
24
|
+
discard_the_queued_events
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def add_evaluation(evaluated_read)
|
|
28
|
+
leave_to_the_parent_process_what_it_collected
|
|
29
|
+
reported_context = ReportedContext.of(evaluated_read.evaluation.context)
|
|
30
|
+
capture(reported_context) unless reported_context.nil?
|
|
31
|
+
@evaluations_collected_since ||= Time.now
|
|
32
|
+
@evaluations << ReportedEvaluation.of(evaluated_read)
|
|
33
|
+
return unless @evaluations.length > @evaluations_limit
|
|
34
|
+
|
|
35
|
+
@evaluations.shift
|
|
36
|
+
@evaluations_dropped += 1
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def add_tracked_event(admission)
|
|
40
|
+
leave_to_the_parent_process_what_it_collected
|
|
41
|
+
@tracked_event_queue.add(admission)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def count_invalid_context(kind_sample)
|
|
45
|
+
leave_to_the_parent_process_what_it_collected
|
|
46
|
+
@invalid_context_submissions.count(kind_sample, Time.now)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def choose_the_reporter_of_what_the_parent_process_collected(forked_through)
|
|
50
|
+
case REPORTER_OF_WHAT_THE_PARENT_PROCESS_COLLECTED_BY_WHAT_FORKED.fetch(forked_through)
|
|
51
|
+
when :parent_process then nil
|
|
52
|
+
when :forked_process then @collecting_process_id = Process.pid
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def taken_for_a_report
|
|
57
|
+
leave_to_the_parent_process_what_it_collected
|
|
58
|
+
taken_at = Time.now
|
|
59
|
+
|
|
60
|
+
TelemetryReport.new(
|
|
61
|
+
evaluations: @evaluations, evaluations_collected_since: @evaluations_collected_since,
|
|
62
|
+
evaluations_dropped: @evaluations_dropped, captured_contexts: @captured_contexts.values,
|
|
63
|
+
captured_contexts_dropped: @captured_contexts_dropped,
|
|
64
|
+
invalid_context_wire_entries: @invalid_context_submissions.wire_entries_taken_for_a_report(taken_at),
|
|
65
|
+
tracked_events: @tracked_event_queue.entries, taken_at: taken_at
|
|
66
|
+
).tap { discard_the_queued_events }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def discard_the_queued_events
|
|
70
|
+
@evaluations = []
|
|
71
|
+
@evaluations_collected_since = nil
|
|
72
|
+
@evaluations_dropped = 0
|
|
73
|
+
@captured_contexts = {}
|
|
74
|
+
@captured_contexts_dropped = 0
|
|
75
|
+
@tracked_event_queue.discard
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def leave_to_the_parent_process_what_it_collected
|
|
81
|
+
return if @collecting_process_id == Process.pid
|
|
82
|
+
|
|
83
|
+
@collecting_process_id = Process.pid
|
|
84
|
+
discard_the_queued_events
|
|
85
|
+
@invalid_context_submissions.discard
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def capture(reported_context)
|
|
89
|
+
@captured_contexts[[reported_context.kind, reported_context.identifier]] = reported_context
|
|
90
|
+
return unless @captured_contexts.size > @captured_contexts_limit
|
|
91
|
+
|
|
92
|
+
@captured_contexts.shift
|
|
93
|
+
@captured_contexts_dropped += 1
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ConfigDirector
|
|
6
|
+
module Internal
|
|
7
|
+
module Telemetry
|
|
8
|
+
module CompactJsonText
|
|
9
|
+
MOST_LEVELS_WRITTEN = 100
|
|
10
|
+
|
|
11
|
+
def self.of(value)
|
|
12
|
+
text_inside(value, [])
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.text_inside(value, containers_around_it)
|
|
16
|
+
case value
|
|
17
|
+
when nil then "null"
|
|
18
|
+
when true, false then value.to_s
|
|
19
|
+
when Integer, Float then number_text_of(value)
|
|
20
|
+
when Array, Hash then container_text_inside(value, containers_around_it)
|
|
21
|
+
when WrittenJson then value.text
|
|
22
|
+
else string_text_of(value.to_s)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.number_javascript_holds?(number)
|
|
27
|
+
number.abs <= Float::MAX
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.number_text_of(number)
|
|
31
|
+
number_javascript_holds?(number) ? Evaluation::ScalarText.of(number).to_s : "null"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.container_text_inside(container, containers_around_it)
|
|
35
|
+
return "null" if containers_around_it.length >= MOST_LEVELS_WRITTEN
|
|
36
|
+
return "null" if containers_around_it.any? { |container_around_it| container_around_it.equal?(container) }
|
|
37
|
+
|
|
38
|
+
containers_around_its_contents = containers_around_it + [container]
|
|
39
|
+
case container
|
|
40
|
+
when Array then "[#{element_texts_of(container, containers_around_its_contents).join(",")}]"
|
|
41
|
+
else "{#{member_texts_of(container, containers_around_its_contents).join(",")}}"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.element_texts_of(array, containers_around_its_contents)
|
|
46
|
+
array.dup.map { |element| text_inside(element, containers_around_its_contents) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.member_texts_of(hash, containers_around_its_contents)
|
|
50
|
+
hash.to_a.map do |key, member|
|
|
51
|
+
"#{string_text_of(key.to_s)}:#{text_inside(member, containers_around_its_contents)}"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.string_text_of(text)
|
|
56
|
+
JSON.generate(ReportedText.of(text))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private_class_method :text_inside, :number_text_of, :container_text_inside, :element_texts_of, :member_texts_of,
|
|
60
|
+
:string_text_of
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
class DiscardedTelemetry
|
|
7
|
+
def self.for_a_client_with(**)
|
|
8
|
+
new
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def record_evaluation(_evaluated_read); end
|
|
12
|
+
|
|
13
|
+
def record_tracked_event(_admission)
|
|
14
|
+
[]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def count_invalid_context(_kind_sample)
|
|
18
|
+
:new_kind_sample
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def start_reporting; end
|
|
22
|
+
|
|
23
|
+
def choose_the_reporter_of_what_the_parent_process_collected(_forked_through); end
|
|
24
|
+
|
|
25
|
+
def restart_in_a_forked_process; end
|
|
26
|
+
|
|
27
|
+
def close; end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
class EventReporter
|
|
7
|
+
WHAT_FOLLOWS_BY_ANSWER_KIND = {
|
|
8
|
+
accepted: :more_reports,
|
|
9
|
+
refused: :more_reports,
|
|
10
|
+
unanswered: :more_reports,
|
|
11
|
+
refused_unrecoverably: :no_more_reports
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
LOG_BY_ANSWER_KIND = {
|
|
15
|
+
accepted: ->(log, _answer) { log.debug { "Telemetry report successfully sent." } },
|
|
16
|
+
refused: lambda do |log, answer|
|
|
17
|
+
log.warn(
|
|
18
|
+
"The telemetry endpoint responded with status #{answer.status}. " \
|
|
19
|
+
"The events in this report were discarded."
|
|
20
|
+
)
|
|
21
|
+
end,
|
|
22
|
+
unanswered: ->(log, answer) { log.warn("Error attempting to send telemetry data: #{answer.detail}") },
|
|
23
|
+
refused_unrecoverably: lambda do |log, answer|
|
|
24
|
+
log.warn(
|
|
25
|
+
"Received a fatal status response (#{answer.status}) from the telemetry endpoint. " \
|
|
26
|
+
"No more telemetry data will be sent."
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
private_constant :WHAT_FOLLOWS_BY_ANSWER_KIND, :LOG_BY_ANSWER_KIND
|
|
32
|
+
|
|
33
|
+
def initialize(endpoint, logger)
|
|
34
|
+
@endpoint = endpoint
|
|
35
|
+
@log = Logging::ClientLog.new(logger, component: "EventReporter")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def what_follows_delivering(report)
|
|
39
|
+
answer = @endpoint.answer_to(report)
|
|
40
|
+
LOG_BY_ANSWER_KIND.fetch(answer.kind).call(@log, answer)
|
|
41
|
+
|
|
42
|
+
WHAT_FOLLOWS_BY_ANSWER_KIND.fetch(answer.kind)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
class InvalidContextSubmissions
|
|
7
|
+
DISTINCT_KIND_SAMPLE_LIMIT = 20
|
|
8
|
+
REASON = "invalid-context-kind"
|
|
9
|
+
|
|
10
|
+
private_constant :REASON
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@counts_by_kind_sample = {}
|
|
14
|
+
@count_beyond_the_sample_limit = 0
|
|
15
|
+
@counted_since = nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def count(kind_sample, now)
|
|
19
|
+
@counted_since ||= now
|
|
20
|
+
|
|
21
|
+
if @counts_by_kind_sample.key?(kind_sample) then count_known(kind_sample)
|
|
22
|
+
elsif @counts_by_kind_sample.size < DISTINCT_KIND_SAMPLE_LIMIT then count_new(kind_sample)
|
|
23
|
+
else count_beyond_the_sample_limit
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def wire_entries_taken_for_a_report(now)
|
|
28
|
+
wire_entries = AggregatedWireEntries.of(
|
|
29
|
+
counts_by_wire_event, counted_since: @counted_since, counted_until: now
|
|
30
|
+
)
|
|
31
|
+
discard
|
|
32
|
+
|
|
33
|
+
wire_entries
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def discard
|
|
37
|
+
@counts_by_kind_sample = {}
|
|
38
|
+
@count_beyond_the_sample_limit = 0
|
|
39
|
+
@counted_since = nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def count_known(kind_sample)
|
|
45
|
+
@counts_by_kind_sample[kind_sample] += 1
|
|
46
|
+
:known_kind_sample
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def count_new(kind_sample)
|
|
50
|
+
@counts_by_kind_sample[kind_sample] = 1
|
|
51
|
+
:new_kind_sample
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def count_beyond_the_sample_limit
|
|
55
|
+
@count_beyond_the_sample_limit += 1
|
|
56
|
+
@count_beyond_the_sample_limit == 1 ? :first_beyond_sample_limit : :beyond_sample_limit
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def counts_by_wire_event
|
|
60
|
+
counts_by_wire_event = @counts_by_kind_sample.to_h do |kind_sample, count|
|
|
61
|
+
[wire_event_of(kind_sample), count]
|
|
62
|
+
end
|
|
63
|
+
unless @count_beyond_the_sample_limit.zero?
|
|
64
|
+
counts_by_wire_event[wire_event_of(nil)] = @count_beyond_the_sample_limit
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
counts_by_wire_event
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def wire_event_of(kind_sample)
|
|
71
|
+
{ reason: REASON, kindSample: kind_sample }.compact
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
ReportAnswer = Data.define(:kind, :status, :detail)
|
|
7
|
+
|
|
8
|
+
class ReportAnswer
|
|
9
|
+
ACCEPTED_STATUSES = (200..299)
|
|
10
|
+
REPORT_TOO_LARGE_STATUS = 413
|
|
11
|
+
TOO_MANY_REQUESTS_STATUS = 429
|
|
12
|
+
STATUSES_THAT_END_TELEMETRY = ((400..499).to_a - [REPORT_TOO_LARGE_STATUS, TOO_MANY_REQUESTS_STATUS]).freeze
|
|
13
|
+
|
|
14
|
+
private_constant :ACCEPTED_STATUSES, :REPORT_TOO_LARGE_STATUS, :TOO_MANY_REQUESTS_STATUS,
|
|
15
|
+
:STATUSES_THAT_END_TELEMETRY
|
|
16
|
+
|
|
17
|
+
def self.with_status(status)
|
|
18
|
+
case status
|
|
19
|
+
when ACCEPTED_STATUSES then new(kind: :accepted, status: status, detail: nil)
|
|
20
|
+
when *STATUSES_THAT_END_TELEMETRY then new(kind: :refused_unrecoverably, status: status, detail: nil)
|
|
21
|
+
else new(kind: :refused, status: status, detail: nil)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.unanswered(detail)
|
|
26
|
+
new(kind: :unanswered, status: nil, detail: detail)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
class ReportSchedule
|
|
7
|
+
def initialize(timing)
|
|
8
|
+
@timing = timing
|
|
9
|
+
@lock = Mutex.new
|
|
10
|
+
@no_longer_waiting_for_the_next_report = ConditionVariable.new
|
|
11
|
+
@stopped = true
|
|
12
|
+
@thread = nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def start(&send_a_report)
|
|
16
|
+
@lock.synchronize do
|
|
17
|
+
return if @thread&.alive?
|
|
18
|
+
|
|
19
|
+
@stopped = false
|
|
20
|
+
thread = Thread.new { send_a_report_on_every_interval(send_a_report) }
|
|
21
|
+
thread.name = "configdirector-telemetry"
|
|
22
|
+
@thread = thread
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def stop
|
|
27
|
+
@lock.synchronize do
|
|
28
|
+
@stopped = true
|
|
29
|
+
@no_longer_waiting_for_the_next_report.broadcast
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def wait_for_a_report_being_sent
|
|
34
|
+
thread = @lock.synchronize { @thread }
|
|
35
|
+
thread&.join(@timing.seconds_a_report_may_take) unless thread.equal?(Thread.current)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def send_a_report_on_every_interval(send_a_report)
|
|
41
|
+
seconds_before_the_next_report = @timing.seconds_before_the_first_report
|
|
42
|
+
|
|
43
|
+
until stopped_within?(seconds_before_the_next_report)
|
|
44
|
+
send_a_report.call
|
|
45
|
+
seconds_before_the_next_report = @timing.seconds_between_reports
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def stopped_within?(seconds)
|
|
50
|
+
next_report_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds
|
|
51
|
+
|
|
52
|
+
@lock.synchronize do
|
|
53
|
+
loop do
|
|
54
|
+
return true if @stopped
|
|
55
|
+
|
|
56
|
+
seconds_left = next_report_at - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
57
|
+
return false unless seconds_left.positive?
|
|
58
|
+
|
|
59
|
+
@no_longer_waiting_for_the_next_report.wait(@lock, seconds_left)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
ReportTiming = Data.define(
|
|
7
|
+
:seconds_before_the_first_report, :seconds_between_reports, :seconds_a_report_may_take
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
class ReportTiming
|
|
11
|
+
def self.of_a_client_reporting_every(seconds_between_reports)
|
|
12
|
+
new(
|
|
13
|
+
seconds_before_the_first_report: 5, seconds_between_reports: seconds_between_reports,
|
|
14
|
+
seconds_a_report_may_take: 5
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
ReportedContext = Data.define(:kind, :identifier, :context)
|
|
7
|
+
|
|
8
|
+
class ReportedContext
|
|
9
|
+
KIND_OF_A_CONTEXT_WITHOUT_ONE = "user"
|
|
10
|
+
|
|
11
|
+
private_constant :KIND_OF_A_CONTEXT_WITHOUT_ONE
|
|
12
|
+
|
|
13
|
+
def self.of(context)
|
|
14
|
+
return nil if context.nil? || context.anonymous?
|
|
15
|
+
|
|
16
|
+
identifier = context.id
|
|
17
|
+
return nil if identifier.nil? || identifier.empty?
|
|
18
|
+
|
|
19
|
+
new(kind: context.kind || KIND_OF_A_CONTEXT_WITHOUT_ONE, identifier: identifier, context: context)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def wire_context
|
|
23
|
+
{ id: identifier, kind: kind, name: context.name, traits: context.traits }.compact
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
class ReportedDetails
|
|
7
|
+
MOST_TOP_LEVEL_KEYS = 20
|
|
8
|
+
MOST_JSON_BYTES = 1_024
|
|
9
|
+
MOST_VALUES_THE_JSON_BYTES_HOLD = MOST_JSON_BYTES
|
|
10
|
+
|
|
11
|
+
private_constant :MOST_VALUES_THE_JSON_BYTES_HOLD
|
|
12
|
+
|
|
13
|
+
def self.json_of(details)
|
|
14
|
+
return nil unless details.is_a?(Hash)
|
|
15
|
+
|
|
16
|
+
reported_form = new.reported_form_of(details)
|
|
17
|
+
return nil if reported_form.nil? || reported_form.length > MOST_TOP_LEVEL_KEYS
|
|
18
|
+
|
|
19
|
+
json_text = CompactJsonText.of(reported_form)
|
|
20
|
+
WrittenJson.new(text: json_text) if json_text.bytesize <= MOST_JSON_BYTES
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private_class_method :new
|
|
24
|
+
|
|
25
|
+
def initialize
|
|
26
|
+
@values_left = MOST_VALUES_THE_JSON_BYTES_HOLD
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def reported_form_of(details)
|
|
30
|
+
catch(:not_reportable) { form_of(details, 1) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def form_of(value, level)
|
|
36
|
+
count_one_more_value
|
|
37
|
+
|
|
38
|
+
case value
|
|
39
|
+
when Time then ReportedTime.of(value)
|
|
40
|
+
when String, Integer, Float, true, false, nil then value
|
|
41
|
+
when Symbol then value.name
|
|
42
|
+
when Array, Hash then container_form_of(value, level)
|
|
43
|
+
else throw :not_reportable
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def count_one_more_value
|
|
48
|
+
@values_left -= 1
|
|
49
|
+
throw :not_reportable if @values_left.negative?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def container_form_of(container, level)
|
|
53
|
+
throw :not_reportable if level > CompactJsonText::MOST_LEVELS_WRITTEN
|
|
54
|
+
|
|
55
|
+
case container
|
|
56
|
+
when Array then container.map { |element| form_of(element, level + 1) }
|
|
57
|
+
else container.to_h { |key, member| [name_of(key), form_of(member, level + 1)] }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def name_of(key)
|
|
62
|
+
case key
|
|
63
|
+
when String then key
|
|
64
|
+
when Symbol then key.name
|
|
65
|
+
else throw :not_reportable
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConfigDirector
|
|
4
|
+
module Internal
|
|
5
|
+
module Telemetry
|
|
6
|
+
ReportedEvaluation = Data.define(
|
|
7
|
+
:config_read, :config_state, :returned_the_in_code_default_value, :reason, :value_kept_for_the_report
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
class ReportedEvaluation
|
|
11
|
+
REQUESTED_TYPE_BY_IN_CODE_DEFAULT_VALUE_KIND = {
|
|
12
|
+
boolean: "Boolean",
|
|
13
|
+
string: "String",
|
|
14
|
+
integer: "Integer",
|
|
15
|
+
float: "Float",
|
|
16
|
+
json_object: "Hash",
|
|
17
|
+
json_array: "Array"
|
|
18
|
+
}.freeze
|
|
19
|
+
|
|
20
|
+
private_constant :REQUESTED_TYPE_BY_IN_CODE_DEFAULT_VALUE_KIND
|
|
21
|
+
|
|
22
|
+
def self.of(evaluated_read)
|
|
23
|
+
evaluation = evaluated_read.evaluation
|
|
24
|
+
config_state = evaluated_read.config_state
|
|
25
|
+
reported_by_the_value_id_sent_with_it = ReportedValue.reported_by_the_value_id_sent_with_it?(
|
|
26
|
+
evaluation.value, config_state&.type, evaluation.value_id
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
new(
|
|
30
|
+
config_read: evaluated_read.config_read, config_state: config_state,
|
|
31
|
+
returned_the_in_code_default_value: evaluation.default_value?, reason: evaluation.reason,
|
|
32
|
+
value_kept_for_the_report: reported_by_the_value_id_sent_with_it ? nil : evaluation.value
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def wire_event
|
|
37
|
+
wire_context.merge(wire_evaluation, wire_identifiers_of_what_was_served).compact
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def wire_context
|
|
43
|
+
reported_context = ReportedContext.of(config_read.context)
|
|
44
|
+
|
|
45
|
+
{ contextId: reported_context&.identifier, contextKind: reported_context&.kind }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def wire_evaluation
|
|
49
|
+
in_code_default_value = config_read.in_code_default_value
|
|
50
|
+
|
|
51
|
+
{
|
|
52
|
+
key: config_read.config_key.to_s, type: config_state&.type,
|
|
53
|
+
defaultValue: ReportedValue.of(in_code_default_value.value, config_state&.type),
|
|
54
|
+
requestedType: REQUESTED_TYPE_BY_IN_CODE_DEFAULT_VALUE_KIND.fetch(in_code_default_value.kind),
|
|
55
|
+
evaluatedValue: wire_evaluated_value, usedDefault: returned_the_in_code_default_value,
|
|
56
|
+
evaluationReason: reason
|
|
57
|
+
}
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def wire_evaluated_value
|
|
61
|
+
value_kept = value_kept_for_the_report
|
|
62
|
+
return { valueId: config_state_served&.value_id } if value_kept.nil?
|
|
63
|
+
|
|
64
|
+
ReportedValue.of(value_kept, config_state&.type)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def wire_identifiers_of_what_was_served
|
|
68
|
+
served = config_state_served
|
|
69
|
+
|
|
70
|
+
{
|
|
71
|
+
evaluatedValueId: served&.value_id, ruleId: served&.rule_id, shareId: served&.share_id,
|
|
72
|
+
variationId: served&.variation&.id
|
|
73
|
+
}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def config_state_served
|
|
77
|
+
returned_the_in_code_default_value ? nil : config_state
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|