test/plugin/base_test.rb in fluent-plugin-google-cloud-0.7.2 vs test/plugin/base_test.rb in fluent-plugin-google-cloud-0.7.3

- old
+ new

@@ -610,10 +610,34 @@ assert_equal 5000, get_number(fields['data']), entry assert_equal null_value, fields['some_null_field'], entry end end + # Verify that when the log has only one effective field (named 'log', + # 'message', or 'msg') and the field is in JSON format, the field is parsed as + # JSON and sent as jsonPayload. + def test_detect_json_auto_triggered_with_one_field + setup_gce_metadata_stubs + json_string = '{"msg": "test log entry 0", "tag2": "test", ' \ + '"data": 5000, "some_null_field": null}' + setup_logging_stubs do + d = create_driver(DETECT_JSON_CONFIG) + %w(message log msg).each do |field| + d.emit(PRESERVED_KEYS_MAP.merge(field => json_string)) + end + d.run + end + verify_log_entries(3, COMPUTE_PARAMS, 'jsonPayload') do |entry| + fields = get_fields(entry['jsonPayload']) + assert_equal 4, fields.size, entry + assert_equal 'test log entry 0', get_string(fields['msg']), entry + assert_equal 'test', get_string(fields['tag2']), entry + assert_equal 5000, get_number(fields['data']), entry + assert_equal null_value, fields['some_null_field'], entry + end + end + # Verify that we drop the log entries when 'require_valid_tags' is true and # any non-string tags or tags with non-utf8 characters are detected. def test_reject_invalid_tags_with_require_valid_tags_true setup_gce_metadata_stubs INVALID_TAGS.keys.each do |tag| @@ -1217,10 +1241,12 @@ d.run end verify_log_entries(1, DATAFLOW_PARAMS) end + # Verify the subfields extraction of LogEntry fields. + def test_log_entry_http_request_field_from_record verify_subfields_from_record(DEFAULT_HTTP_REQUEST_KEY) end def test_log_entry_source_location_field_from_record @@ -1229,10 +1255,13 @@ def test_log_entry_operation_field_from_record verify_subfields_from_record(DEFAULT_OPERATION_KEY) end + # Verify the subfields extraction of LogEntry fields when there are other + # fields. + def test_log_entry_http_request_field_partial_from_record verify_subfields_partial_from_record(DEFAULT_HTTP_REQUEST_KEY) end def test_log_entry_source_location_field_partial_from_record @@ -1241,10 +1270,12 @@ def test_log_entry_operation_field_partial_from_record verify_subfields_partial_from_record(DEFAULT_OPERATION_KEY) end + # Verify the subfields extraction of LogEntry fields when they are not hashes. + def test_log_entry_http_request_field_when_not_hash verify_subfields_when_not_hash(DEFAULT_HTTP_REQUEST_KEY) end def test_log_entry_source_location_field_when_not_hash @@ -1253,10 +1284,12 @@ def test_log_entry_operation_field_when_not_hash verify_subfields_when_not_hash(DEFAULT_OPERATION_KEY) end + # Verify the subfields extraction of LogEntry fields when they are nil. + def test_log_entry_http_request_field_when_nil verify_subfields_when_nil(DEFAULT_HTTP_REQUEST_KEY) end def test_log_entry_source_location_field_when_nil @@ -1322,38 +1355,75 @@ assert_nil get_fields(entry['jsonPayload'])['httpRequest'], entry end end end - def test_log_entry_trace_field - verify_field_key('trace', DEFAULT_TRACE_KEY, 'custom_trace_key', - CONFIG_CUSTOM_TRACE_KEY_SPECIFIED, TRACE) + # Verify the default and customization of LogEntry field extraction key. + + def test_log_entry_insert_id_field + verify_field_key('insertId', DEFAULT_INSERT_ID_KEY, 'custom_insert_id_key', + CONFIG_CUSTOM_INSERT_ID_KEY_SPECIFIED, INSERT_ID) end + def test_log_entry_operation_field + verify_field_key('operation', DEFAULT_OPERATION_KEY, 'custom_operation_key', + CONFIG_CUSTOM_OPERATION_KEY_SPECIFIED, OPERATION_MESSAGE) + end + + def test_log_entry_source_location_field + verify_field_key('sourceLocation', DEFAULT_SOURCE_LOCATION_KEY, + 'custom_source_location_key', + CONFIG_CUSTOM_SOURCE_LOCATION_KEY_SPECIFIED, + source_location_message) + end + def test_log_entry_span_id_field verify_field_key('spanId', DEFAULT_SPAN_ID_KEY, 'custom_span_id_key', CONFIG_CUSTOM_SPAN_ID_KEY_SPECIFIED, SPAN_ID) end - def test_log_entry_insert_id_field - verify_field_key('insertId', DEFAULT_INSERT_ID_KEY, 'custom_insert_id_key', - CONFIG_CUSTOM_INSERT_ID_KEY_SPECIFIED, INSERT_ID) + def test_log_entry_trace_field + verify_field_key('trace', DEFAULT_TRACE_KEY, 'custom_trace_key', + CONFIG_CUSTOM_TRACE_KEY_SPECIFIED, TRACE) end - def test_cascading_json_detection_with_log_entry_trace_field + # Verify the cascading JSON detection of LogEntry fields. + + def test_cascading_json_detection_with_log_entry_insert_id_field verify_cascading_json_detection_with_log_entry_fields( - 'trace', DEFAULT_TRACE_KEY, TRACE, TRACE2) + 'insertId', DEFAULT_INSERT_ID_KEY, + root_level_value: INSERT_ID, + nested_level_value: INSERT_ID2) end + def test_cascading_json_detection_with_log_entry_operation_field + verify_cascading_json_detection_with_log_entry_fields( + 'operation', DEFAULT_OPERATION_KEY, + root_level_value: OPERATION_MESSAGE, + nested_level_value: OPERATION_MESSAGE2, + expected_value_from_nested: expected_operation_message2) + end + + def test_cascading_json_detection_with_log_entry_source_location_field + verify_cascading_json_detection_with_log_entry_fields( + 'sourceLocation', DEFAULT_SOURCE_LOCATION_KEY, + root_level_value: source_location_message, + nested_level_value: source_location_message2) + end + def test_cascading_json_detection_with_log_entry_span_id_field verify_cascading_json_detection_with_log_entry_fields( - 'spanId', DEFAULT_SPAN_ID_KEY, SPAN_ID, SPAN_ID2) + 'spanId', DEFAULT_SPAN_ID_KEY, + root_level_value: SPAN_ID, + nested_level_value: SPAN_ID2) end - def test_cascading_json_detection_with_log_entry_insert_id_field + def test_cascading_json_detection_with_log_entry_trace_field verify_cascading_json_detection_with_log_entry_fields( - 'insertId', DEFAULT_INSERT_ID_KEY, INSERT_ID, INSERT_ID2) + 'trace', DEFAULT_TRACE_KEY, + root_level_value: TRACE, + nested_level_value: TRACE2) end # Metadata Agent related tests. # Test enable_metadata_agent not set or set to false. @@ -2175,11 +2245,18 @@ # Cascading JSON detection is only triggered when the record has one field # left with name "log", "message" or "msg". This test verifies additional # LogEntry fields like spanId and traceId do not disable that by accident. def verify_cascading_json_detection_with_log_entry_fields( - log_entry_field, default_key, root_level_value, nested_level_value) + log_entry_field, default_key, expectation) + root_level_value = expectation[:root_level_value] + nested_level_value = expectation[:nested_level_value] + expected_value_from_root = expectation.fetch( + :expected_value_from_root, root_level_value) + expected_value_from_nested = expectation.fetch( + :expected_value_from_nested, nested_level_value) + setup_gce_metadata_stubs # { # "logging.googleapis.com/XXX' => 'sample value' # "msg": { @@ -2213,13 +2290,13 @@ # } log_entry_with_both_level_fields = log_entry_with_nested_level_field.merge( default_key => root_level_value) { - log_entry_with_root_level_field => root_level_value, - log_entry_with_nested_level_field => nested_level_value, - log_entry_with_both_level_fields => nested_level_value + log_entry_with_root_level_field => expected_value_from_root, + log_entry_with_nested_level_field => expected_value_from_nested, + log_entry_with_both_level_fields => expected_value_from_nested }.each_with_index do |(input_log_entry, expected_value), index| setup_logging_stubs do @logs_sent = [] d = create_driver(DETECT_JSON_CONFIG) d.emit(input_log_entry) @@ -2281,24 +2358,16 @@ verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry| assert_equal input[:expected_field_value], entry[log_entry_field], input payload_fields = get_fields(entry['jsonPayload']) assert_equal input[:expected_payload].size, payload_fields.size, input payload_fields.each do |key, value| - assert_equal input[:expected_payload][key], get_string(value), input + assert_hash_equal_json(input[:expected_payload][key], value) end end end end - def http_request_message - HTTP_REQUEST_MESSAGE - end - - def source_location_message - SOURCE_LOCATION_MESSAGE - end - # Replace the 'referer' field with nil. def http_request_message_with_nil_referer http_request_message.merge('referer' => nil) end @@ -2390,9 +2459,34 @@ _undefined end # The null value. def null_value(_field) + _undefined + end + + # Defined in specific gRPC or REST files. + def http_request_message + _undefined + end + + # Defined in specific gRPC or REST files. + def source_location_message + _undefined + end + + # Defined in specific gRPC or REST files. + def source_location_message2 + _undefined + end + + # Defined in specific gRPC or REST files. + def expected_operation_message2 + _undefined + end + + # Defined in specific gRPC or REST files. + def assert_hash_equal_json(_expected, _actual) _undefined end def _undefined raise "Method #{__callee__} is unimplemented and needs to be overridden."