test/plugin/base_test.rb in fluent-plugin-google-cloud-0.10.4 vs test/plugin/base_test.rb in fluent-plugin-google-cloud-0.10.5

- old
+ new

@@ -941,26 +941,34 @@ d = create_driver(config + ENABLE_PROMETHEUS_CONFIG, 'test', true) log_entry_count.times do |i| d.emit("tag#{i}", 'message' => log_entry(i)) end d.run + @logs_sent.zip(request_log_names).each do |request, log_name| + assert_equal log_name, request['logName'] + end + verify_log_entries(log_entry_count, COMPUTE_PARAMS_NO_LOG_NAME, + 'textPayload') do |entry, entry_index| + verify_default_log_entry_text(entry['textPayload'], entry_index, + entry) + assert_equal entry_log_names[entry_index], entry['logName'] + end + # Verify the number of requests is different based on whether the + # 'split_logs_by_tag' flag is enabled. + assert_prometheus_metric_value( + :stackdriver_successful_requests_count, + request_count, + 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + :aggregate) + assert_prometheus_metric_value( + :stackdriver_ingested_entries_count, + log_entry_count, + 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + :aggregate) end - @logs_sent.zip(request_log_names).each do |request, log_name| - assert_equal log_name, request['logName'] - end - verify_log_entries(log_entry_count, COMPUTE_PARAMS_NO_LOG_NAME, - 'textPayload') do |entry, entry_index| - verify_default_log_entry_text(entry['textPayload'], entry_index, - entry) - assert_equal entry_log_names[entry_index], entry['logName'] - end - # Verify the number of requests is different based on whether the - # 'split_logs_by_tag' flag is enabled. - assert_prometheus_metric_value(:stackdriver_successful_requests_count, - request_count, :aggregate) - assert_prometheus_metric_value(:stackdriver_ingested_entries_count, - log_entry_count, :aggregate) end end def test_compute_timestamp setup_gce_metadata_stubs @@ -1966,18 +1974,116 @@ # Retry to protect from time races. retries ||= 0 expected = Time.now.to_i - start_time d.instance.update_uptime assert_metric_value.call( - :uptime, expected, version: Fluent::GoogleCloudOutput.version_string) + :uptime, expected, 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + version: Fluent::GoogleCloudOutput.version_string) rescue Test::Unit::AssertionFailedError retry if (retries += 1) < 3 end assert_not_equal 3, retries end end + def test_metrics + setup_gce_metadata_stubs + [ + [ENABLE_PROMETHEUS_CONFIG, method(:assert_prometheus_metric_value)], + [ENABLE_OPENCENSUS_CONFIG, method(:assert_opencensus_metric_value)] + ].each do |config, assert_metric_value| + [ + # Single successful request. + [ok_status_code, 1, 1, [0, 0, 0]], + # Several successful requests. + [ok_status_code, 2, 1, [0, 0, 0]], + # Single successful request with several entries. + [ok_status_code, 1, 2, [0, 0, 0]], + # Single failed request that causes logs to be dropped. + [client_error_status_code, 1, 1, [1, 1, 0]], + # Single failed request that escalates without logs being dropped with + # several entries. + [server_error_status_code, 1, 2, [0, 0, 2]] + ].each do |code, request_count, entry_count, metric_values| + clear_metrics + setup_logging_stubs(nil, code, 'SomeMessage') do + (1..request_count).each do |request_index| + d = create_driver(config) + (1..entry_count).each do |entry_index| + d.emit('message' => log_entry(entry_index.to_s)) + end + # rubocop:disable Lint/HandleExceptions + begin + d.run + rescue mock_error_type + end + # rubocop:enable Lint/HandleExceptions + failed_requests_count, dropped_entries_count, + retried_entries_count = metric_values + + successful_requests_count = \ + if code != ok_status_code + 0 + elsif config == ENABLE_OPENCENSUS_CONFIG + # TODO(b/173215689) Improve the Open Census side of testing. + # The test driver instance variables can not survive between + # test driver runs. So the open cencensus side counter gets + # reset as expected. + 1 + else + request_index + end + + ingested_entries_count = \ + if code != ok_status_code + 0 + elsif config == ENABLE_OPENCENSUS_CONFIG + # TODO(b/173215689) Improve the Open Census side of testing. + # The test driver instance variables can not survive between + # test driver runs. So the open cencensus side counter gets + # reset as expected. + entry_count + else + request_index * entry_count + end + + assert_metric_value.call(:stackdriver_successful_requests_count, + successful_requests_count, + 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + grpc: use_grpc, code: ok_status_code) + assert_metric_value.call(:stackdriver_ingested_entries_count, + ingested_entries_count, + 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + grpc: use_grpc, code: ok_status_code) + assert_metric_value.call(:stackdriver_retried_entries_count, + retried_entries_count, + 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + grpc: use_grpc, code: code) + # Skip failure assertions when code indicates success, because the + # assertion will fail in the case when a single metric contains time + # series with success and failure events. + next if code == ok_status_code + assert_metric_value.call(:stackdriver_failed_requests_count, + failed_requests_count, + 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + grpc: use_grpc, code: code) + assert_metric_value.call(:stackdriver_dropped_entries_count, + dropped_entries_count, + 'agent.googleapis.com/agent', + OpenCensus::Stats::Aggregation::Sum, d, + grpc: use_grpc, code: code) + end + end + end + end + end + private # Provide a stub context that initializes @logs_sent, executes the block and # resets WebMock at the end. def new_stub_context @@ -2454,10 +2560,35 @@ def create_driver(_conf = APPLICATION_DEFAULT_CONFIG, _tag = 'test') _undefined end # Set up http or grpc stubs to mock the external calls. - def setup_logging_stubs + def setup_logging_stubs(_error = nil, _code = nil, _message = nil) + _undefined + end + + # Whether this is the grpc path + def use_grpc + _undefined + end + + # The OK status code. + def ok_status_code + _undefined + end + + # A client side error status code. + def client_error_status_code + _undefined + end + + # A server side error status code. + def server_error_status_code + _undefined + end + + # The parent error type to expect in the mock + def mock_error_type _undefined end # Verify the number and the content of the log entries match the expectation. # The caller can optionally provide a block which is called for each entry.