fluent-plugin-google-cloud 0.6.5.pre.1 → 0.6.5
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 +4 -4
- data/Gemfile.lock +8 -10
- data/fluent-plugin-google-cloud.gemspec +1 -2
- data/lib/fluent/plugin/out_google_cloud.rb +405 -614
- data/test/plugin/base_test.rb +134 -348
- data/test/plugin/constants.rb +24 -159
- metadata +4 -18
data/test/plugin/base_test.rb
CHANGED
@@ -516,6 +516,11 @@ module BaseTest
|
|
516
516
|
'timestampSeconds' => ts.tv_sec, 'timestampNanos' => ts.tv_nsec)
|
517
517
|
expected_ts.push(ts)
|
518
518
|
emit_index += 1
|
519
|
+
d.emit('message' => log_entry(emit_index),
|
520
|
+
'timestampSeconds' => "#{ts.tv_sec}",
|
521
|
+
'timestampNanos' => "#{ts.tv_nsec}")
|
522
|
+
expected_ts.push(ts)
|
523
|
+
emit_index += 1
|
519
524
|
d.run
|
520
525
|
end
|
521
526
|
end
|
@@ -779,134 +784,6 @@ module BaseTest
|
|
779
784
|
end
|
780
785
|
end
|
781
786
|
|
782
|
-
# Docker Container.
|
783
|
-
|
784
|
-
# Test textPayload logs from Docker container stderr / stdout.
|
785
|
-
def test_text_payload_docker_container_logs
|
786
|
-
setup_gce_metadata_stubs
|
787
|
-
[1, 2, 3, 5, 11, 50].each do |n|
|
788
|
-
@logs_sent = []
|
789
|
-
setup_logging_stubs do
|
790
|
-
d = create_driver(APPLICATION_DEFAULT_CONFIG, DOCKER_CONTAINER_TAG)
|
791
|
-
# Generate log entries with 'log' (simply a string) and 'time' fields.
|
792
|
-
n.times { |i| d.emit(docker_container_log_entry(log_entry(i))) }
|
793
|
-
d.run
|
794
|
-
end
|
795
|
-
# 'container_id' and 'container_name' should have been extracted from tag
|
796
|
-
# and properly set in resource.labels and common metadata labels as in
|
797
|
-
# DOCKER_CONTAINER_PARAMS.
|
798
|
-
verify_log_entries(n, DOCKER_CONTAINER_PARAMS) do |entry|
|
799
|
-
# Timestamp in 'time' field from log entry should be set properly.
|
800
|
-
assert_equal DOCKER_CONTAINER_SECONDS_EPOCH,
|
801
|
-
entry['timestamp']['seconds'], entry
|
802
|
-
assert_equal DOCKER_CONTAINER_NANOS, entry['timestamp']['nanos'],
|
803
|
-
entry
|
804
|
-
# Severity is 'DEFAULT' because 'stream' info is absent from log entry.
|
805
|
-
assert_equal_with_default entry['severity'], 'DEFAULT', 'DEFAULT', entry
|
806
|
-
end
|
807
|
-
end
|
808
|
-
end
|
809
|
-
|
810
|
-
# Test jsonPayload logs from Docker container stderr / stdout.
|
811
|
-
def test_json_payload_docker_container_logs
|
812
|
-
setup_gce_metadata_stubs
|
813
|
-
[1, 2, 3, 5, 11, 50].each do |n|
|
814
|
-
@logs_sent = []
|
815
|
-
setup_logging_stubs do
|
816
|
-
d = create_driver(APPLICATION_DEFAULT_CONFIG, DOCKER_CONTAINER_TAG)
|
817
|
-
n.times do
|
818
|
-
# Generate log entries with 'log' (json) and 'time' fields.
|
819
|
-
d.emit(docker_container_log_entry('{"msg": "test log entry ' \
|
820
|
-
"#{n}" \
|
821
|
-
'", "tag2": "test", "data": ' \
|
822
|
-
'5000, "severity": "WARNING"}'))
|
823
|
-
end
|
824
|
-
d.run
|
825
|
-
end
|
826
|
-
# 'container_id' and 'container_name' should have been extracted from tag
|
827
|
-
# and properly set in resource.labels and common metadata labels as in
|
828
|
-
# DOCKER_CONTAINER_PARAMS.
|
829
|
-
verify_log_entries(n, DOCKER_CONTAINER_PARAMS, 'jsonPayload') do |entry|
|
830
|
-
# 'log' field should be detected as json and parsed properly.
|
831
|
-
fields = get_fields(entry['jsonPayload'])
|
832
|
-
assert_equal 3, fields.size, entry
|
833
|
-
assert_equal "test log entry #{n}", get_string(fields['msg']), entry
|
834
|
-
assert_equal 'test', get_string(fields['tag2']), entry
|
835
|
-
assert_equal 5000, get_number(fields['data']), entry
|
836
|
-
# Timestamp in 'time' field from log entry should be set properly.
|
837
|
-
assert_equal DOCKER_CONTAINER_SECONDS_EPOCH,
|
838
|
-
entry['timestamp']['seconds'], entry
|
839
|
-
assert_equal DOCKER_CONTAINER_NANOS, entry['timestamp']['nanos'], entry
|
840
|
-
# Severity in 'severity' field from log entry should be set properly.
|
841
|
-
assert_equal 'WARNING', entry['severity'], entry
|
842
|
-
end
|
843
|
-
end
|
844
|
-
end
|
845
|
-
|
846
|
-
# Test the following metadata is properly extracted from json record:
|
847
|
-
# * container_id
|
848
|
-
# * container_name
|
849
|
-
# * source (this field maps to the 'stream' metadata label.
|
850
|
-
def test_docker_container_logs_metadata_from_json_record
|
851
|
-
setup_gce_metadata_stubs
|
852
|
-
{
|
853
|
-
# 'stream' label should be extracted from json record when present.
|
854
|
-
docker_container_log_entry_with_metadata(
|
855
|
-
log_entry(0)
|
856
|
-
) => {
|
857
|
-
# When the 'source' field from the json record has value 'stdout',
|
858
|
-
# 'severity' should be 'INFO'.
|
859
|
-
'params' => DOCKER_CONTAINER_PARAMS_WITH_STREAM_STDOUT,
|
860
|
-
'severity' => 'INFO'
|
861
|
-
},
|
862
|
-
# 'container_id' and 'container_name' can be extracted from tag or
|
863
|
-
# json record. If present in both cases, values in the json record will
|
864
|
-
# overwrite values in tags. For example:
|
865
|
-
# DOCKER_CONTAINER_PARAMS_WITH_METADATA_OVERWRITTEN has different
|
866
|
-
# 'container_id' and 'container_name' values from DOCKER_CONTAINER_TAG,
|
867
|
-
# and we need to verify these two fields get overwritten as expected.
|
868
|
-
docker_container_log_entry_with_metadata(
|
869
|
-
log_entry(0), DOCKER_CONTAINER_ID2, DOCKER_CONTAINER_NAME2,
|
870
|
-
DOCKER_CONTAINER_STREAM_STDERR
|
871
|
-
) => {
|
872
|
-
# When the 'source' field from the json record has value 'stderr',
|
873
|
-
# 'severity' should be 'ERROR'.
|
874
|
-
'params' => DOCKER_CONTAINER_PARAMS_WITH_METADATA_OVERWRITTEN,
|
875
|
-
'severity' => 'ERROR'
|
876
|
-
}
|
877
|
-
}.each do |log_entry, expected|
|
878
|
-
@logs_sent = []
|
879
|
-
setup_logging_stubs do
|
880
|
-
d = create_driver(APPLICATION_DEFAULT_CONFIG, DOCKER_CONTAINER_TAG)
|
881
|
-
d.emit(log_entry)
|
882
|
-
d.run
|
883
|
-
end
|
884
|
-
verify_log_entries(1, expected['params']) do |entry|
|
885
|
-
assert_equal DOCKER_CONTAINER_SECONDS_EPOCH,
|
886
|
-
entry['timestamp']['seconds'], entry
|
887
|
-
assert_equal DOCKER_CONTAINER_NANOS, entry['timestamp']['nanos'],
|
888
|
-
entry
|
889
|
-
assert_equal_with_default entry['severity'], expected['severity'],
|
890
|
-
'DEFAULT', entry
|
891
|
-
end
|
892
|
-
end
|
893
|
-
end
|
894
|
-
|
895
|
-
# Test logs from applications running in Docker containers.
|
896
|
-
def test_docker_container_application_logs
|
897
|
-
setup_gce_metadata_stubs
|
898
|
-
setup_docker_remote_api_stubs
|
899
|
-
setup_logging_stubs do
|
900
|
-
# Metadata Agent is not enabled. Will call Docker Remote API for
|
901
|
-
# container info.
|
902
|
-
d = create_driver(APPLICATION_DEFAULT_CONFIG,
|
903
|
-
DOCKER_CONTAINER_TAG_WITH_APPLICATION)
|
904
|
-
d.emit('message' => log_entry(0))
|
905
|
-
d.run
|
906
|
-
end
|
907
|
-
verify_log_entries(1, DOCKER_CONTAINER_WITH_APPLICATION_PARAMS)
|
908
|
-
end
|
909
|
-
|
910
787
|
def test_cloudfunctions_log
|
911
788
|
setup_gce_metadata_stubs
|
912
789
|
setup_cloudfunctions_metadata_stubs
|
@@ -982,47 +859,40 @@ module BaseTest
|
|
982
859
|
verify_log_entries(1, DATAPROC_PARAMS, 'jsonPayload')
|
983
860
|
end
|
984
861
|
|
985
|
-
def
|
986
|
-
|
987
|
-
setup_logging_stubs do
|
988
|
-
d = create_driver
|
989
|
-
d.emit('httpRequest' => HTTP_REQUEST_MESSAGE)
|
990
|
-
d.run
|
991
|
-
end
|
992
|
-
verify_log_entries(1, COMPUTE_PARAMS, 'httpRequest') do |entry|
|
993
|
-
assert_equal HTTP_REQUEST_MESSAGE, entry['httpRequest'], entry
|
994
|
-
assert_nil get_fields(entry['jsonPayload'])['httpRequest'], entry
|
995
|
-
end
|
862
|
+
def test_log_entry_http_request_field_from_record
|
863
|
+
verify_subfields_from_record(DEFAULT_HTTP_REQUEST_KEY)
|
996
864
|
end
|
997
865
|
|
998
|
-
def
|
999
|
-
|
1000
|
-
setup_logging_stubs do
|
1001
|
-
d = create_driver
|
1002
|
-
d.emit('httpRequest' => HTTP_REQUEST_MESSAGE.merge(
|
1003
|
-
'otherKey' => 'value'))
|
1004
|
-
d.run
|
1005
|
-
end
|
1006
|
-
verify_log_entries(1, COMPUTE_PARAMS, 'httpRequest') do |entry|
|
1007
|
-
assert_equal HTTP_REQUEST_MESSAGE, entry['httpRequest'], entry
|
1008
|
-
fields = get_fields(entry['jsonPayload'])
|
1009
|
-
request = get_fields(get_struct(fields['httpRequest']))
|
1010
|
-
assert_equal 'value', get_string(request['otherKey']), entry
|
1011
|
-
end
|
866
|
+
def test_log_entry_source_location_field_from_record
|
867
|
+
verify_subfields_from_record(DEFAULT_SOURCE_LOCATION_KEY)
|
1012
868
|
end
|
1013
869
|
|
1014
|
-
def
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
870
|
+
def test_log_entry_operation_field_from_record
|
871
|
+
verify_subfields_from_record(DEFAULT_OPERATION_KEY)
|
872
|
+
end
|
873
|
+
|
874
|
+
def test_log_entry_http_request_field_partial_from_record
|
875
|
+
verify_subfields_partial_from_record(DEFAULT_HTTP_REQUEST_KEY)
|
876
|
+
end
|
877
|
+
|
878
|
+
def test_log_entry_source_location_field_partial_from_record
|
879
|
+
verify_subfields_partial_from_record(DEFAULT_SOURCE_LOCATION_KEY)
|
880
|
+
end
|
881
|
+
|
882
|
+
def test_log_entry_operation_field_partial_from_record
|
883
|
+
verify_subfields_partial_from_record(DEFAULT_OPERATION_KEY)
|
884
|
+
end
|
885
|
+
|
886
|
+
def test_log_entry_http_request_field_when_not_hash
|
887
|
+
verify_subfields_when_not_hash(DEFAULT_HTTP_REQUEST_KEY)
|
888
|
+
end
|
889
|
+
|
890
|
+
def test_log_entry_source_location_field_when_not_hash
|
891
|
+
verify_subfields_when_not_hash(DEFAULT_SOURCE_LOCATION_KEY)
|
892
|
+
end
|
893
|
+
|
894
|
+
def test_log_entry_operation_field_when_not_hash
|
895
|
+
verify_subfields_when_not_hash(DEFAULT_OPERATION_KEY)
|
1026
896
|
end
|
1027
897
|
|
1028
898
|
def test_http_request_from_record_with_referer_nil_or_absent
|
@@ -1082,7 +952,48 @@ module BaseTest
|
|
1082
952
|
end
|
1083
953
|
end
|
1084
954
|
|
1085
|
-
def
|
955
|
+
def test_labels_from_record
|
956
|
+
setup_gce_metadata_stubs
|
957
|
+
setup_logging_stubs do
|
958
|
+
d = create_driver
|
959
|
+
d.emit(DEFAULT_LABELS_KEY => CUSTOM_LABELS_MESSAGE)
|
960
|
+
d.run
|
961
|
+
end
|
962
|
+
labels = COMPUTE_PARAMS[:labels].merge(CUSTOM_LABELS_MESSAGE)
|
963
|
+
params = COMPUTE_PARAMS.merge(labels: labels)
|
964
|
+
verify_log_entries(1, params, 'labels') do |entry|
|
965
|
+
assert_nil get_fields(entry['jsonPayload'])[DEFAULT_LABELS_KEY], entry
|
966
|
+
end
|
967
|
+
end
|
968
|
+
|
969
|
+
def test_labels_from_record_conflict
|
970
|
+
setup_gce_metadata_stubs
|
971
|
+
setup_logging_stubs do
|
972
|
+
d = create_driver
|
973
|
+
d.emit(DEFAULT_LABELS_KEY => { CONFLICTING_LABEL_KEY => 'a_string' })
|
974
|
+
d.run
|
975
|
+
end
|
976
|
+
verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
|
977
|
+
fields = get_fields(entry['jsonPayload'])
|
978
|
+
labels = get_fields(get_struct(fields[DEFAULT_LABELS_KEY]))
|
979
|
+
assert_equal('a_string', get_string(labels[CONFLICTING_LABEL_KEY]), entry)
|
980
|
+
end
|
981
|
+
end
|
982
|
+
|
983
|
+
def test_labels_from_record_when_not_hash
|
984
|
+
setup_gce_metadata_stubs
|
985
|
+
setup_logging_stubs do
|
986
|
+
d = create_driver
|
987
|
+
d.emit(DEFAULT_LABELS_KEY => 'a_string')
|
988
|
+
d.run
|
989
|
+
end
|
990
|
+
verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
|
991
|
+
fields = get_fields(entry['jsonPayload'])
|
992
|
+
assert_equal 'a_string', get_string(fields[DEFAULT_LABELS_KEY]), entry
|
993
|
+
end
|
994
|
+
end
|
995
|
+
|
996
|
+
def test_log_entry_trace_field
|
1086
997
|
setup_gce_metadata_stubs
|
1087
998
|
message = log_entry(0)
|
1088
999
|
trace = 'projects/project-1/traces/1234567890abcdef1234567890abcdef'
|
@@ -1091,24 +1002,28 @@ module BaseTest
|
|
1091
1002
|
# It leaves trace entry field nil if no trace value sent.
|
1092
1003
|
driver_config: APPLICATION_DEFAULT_CONFIG,
|
1093
1004
|
emitted_log: { 'msg' => message },
|
1005
|
+
expected_fields: { 'msg' => message },
|
1094
1006
|
expected_trace_value: nil
|
1095
1007
|
},
|
1096
1008
|
{
|
1097
1009
|
# By default, it sets trace via Google-specific key.
|
1098
1010
|
driver_config: APPLICATION_DEFAULT_CONFIG,
|
1099
1011
|
emitted_log: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
|
1012
|
+
expected_fields: { 'msg' => message },
|
1100
1013
|
expected_trace_value: trace
|
1101
1014
|
},
|
1102
1015
|
{
|
1103
1016
|
# It allows setting the trace via a custom configured key.
|
1104
1017
|
driver_config: CONFIG_CUSTOM_TRACE_KEY_SPECIFIED,
|
1105
1018
|
emitted_log: { 'msg' => message, 'custom_trace_key' => trace },
|
1019
|
+
expected_fields: { 'msg' => message },
|
1106
1020
|
expected_trace_value: trace
|
1107
1021
|
},
|
1108
1022
|
{
|
1109
1023
|
# It no longer sets trace by the default key if custom key specified.
|
1110
1024
|
driver_config: CONFIG_CUSTOM_TRACE_KEY_SPECIFIED,
|
1111
1025
|
emitted_log: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
|
1026
|
+
expected_fields: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
|
1112
1027
|
expected_trace_value: nil
|
1113
1028
|
}
|
1114
1029
|
].each do |input|
|
@@ -1120,129 +1035,16 @@ module BaseTest
|
|
1120
1035
|
end
|
1121
1036
|
verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
|
1122
1037
|
assert_equal input[:expected_trace_value], entry['trace'], input
|
1123
|
-
end
|
1124
|
-
end
|
1125
|
-
end
|
1126
1038
|
|
1127
|
-
def test_trace_removal_from_json_payload
|
1128
|
-
setup_gce_metadata_stubs
|
1129
|
-
message = log_entry(0)
|
1130
|
-
trace = 'projects/project-1/traces/1234567890abcdef1234567890abcdef'
|
1131
|
-
[
|
1132
|
-
{
|
1133
|
-
# By default, it removes trace value from jsonPayload.
|
1134
|
-
driver_config: APPLICATION_DEFAULT_CONFIG,
|
1135
|
-
emitted_log: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
|
1136
|
-
expected_json_payload: { 'msg' => message }
|
1137
|
-
},
|
1138
|
-
{
|
1139
|
-
# It keeps the trace value in jsonPayload if keep_trace_key set to true.
|
1140
|
-
driver_config: CONFIG_KEEP_TRACE_KEY_TRUE,
|
1141
|
-
emitted_log: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
|
1142
|
-
expected_json_payload: { 'msg' => message, DEFAULT_TRACE_KEY => trace }
|
1143
|
-
}
|
1144
|
-
].each do |input|
|
1145
|
-
setup_logging_stubs do
|
1146
|
-
@logs_sent = []
|
1147
|
-
d = create_driver(input[:driver_config])
|
1148
|
-
d.emit(input[:emitted_log])
|
1149
|
-
d.run
|
1150
|
-
end
|
1151
|
-
verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
|
1152
1039
|
fields = get_fields(entry['jsonPayload'])
|
1153
|
-
assert_equal input[:
|
1040
|
+
assert_equal input[:expected_fields].size, fields.size, input
|
1154
1041
|
fields.each do |key, value|
|
1155
|
-
assert_equal
|
1156
|
-
get_string(value), input)
|
1042
|
+
assert_equal input[:expected_fields][key], get_string(value), input
|
1157
1043
|
end
|
1158
1044
|
end
|
1159
1045
|
end
|
1160
1046
|
end
|
1161
1047
|
|
1162
|
-
# Metadata Agent related tests.
|
1163
|
-
def test_configure_enable_metadata_agent_default_and_false
|
1164
|
-
setup_gce_metadata_stubs
|
1165
|
-
[create_driver, create_driver(DISABLE_METADATA_AGENT_CONFIG)].each do |d|
|
1166
|
-
assert_false d.instance.instance_variable_get(:@enable_metadata_agent)
|
1167
|
-
end
|
1168
|
-
end
|
1169
|
-
|
1170
|
-
def test_configure_enable_metadata_agent_true
|
1171
|
-
setup_gce_metadata_stubs
|
1172
|
-
setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
|
1173
|
-
d = create_driver(ENABLE_METADATA_AGENT_CONFIG)
|
1174
|
-
assert_true d.instance.instance_variable_get(:@enable_metadata_agent)
|
1175
|
-
end
|
1176
|
-
|
1177
|
-
# Test an implicit monitored resource can be retrieved from Metadata Agent
|
1178
|
-
# with an empty string as the locally-unique id.
|
1179
|
-
def test_retrieve_implicit_monitored_resource
|
1180
|
-
# Minimum set up for gce metadata stubs. Stubs for 'vm_id' and 'location'
|
1181
|
-
# info are deliberately not set up.
|
1182
|
-
setup_gce_metadata_stubs_minimum
|
1183
|
-
setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
|
1184
|
-
setup_logging_stubs do
|
1185
|
-
d = create_driver(ENABLE_METADATA_AGENT_CONFIG)
|
1186
|
-
d.emit('message' => log_entry(0))
|
1187
|
-
d.run
|
1188
|
-
end
|
1189
|
-
verify_log_entries(1, COMPUTE_PARAMS)
|
1190
|
-
end
|
1191
|
-
|
1192
|
-
# Test a docker container monitored resource can be retrieved from Metadata
|
1193
|
-
# Agent with "container.<container_id>" as the locally-unique id.
|
1194
|
-
def test_retrieve_docker_container_monitored_resource
|
1195
|
-
setup_gce_metadata_stubs_minimum
|
1196
|
-
setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
|
1197
|
-
setup_metadata_agent_stubs(DOCKER_CONSTANTS[:resource_type])
|
1198
|
-
setup_logging_stubs do
|
1199
|
-
# Tag format here: "container.<container_id>.<container_name>"
|
1200
|
-
d = create_driver(ENABLE_METADATA_AGENT_CONFIG, DOCKER_CONTAINER_TAG)
|
1201
|
-
d.emit('message' => log_entry(0))
|
1202
|
-
d.run
|
1203
|
-
end
|
1204
|
-
# gce_metadata_stubs has ZONE1, while metadata agent stub has ZONE2. Here we
|
1205
|
-
# need to verify ZONE2 overwrites ZONE1 as expected.
|
1206
|
-
verify_log_entries(1, DOCKER_CONTAINER_PARAMS_WITH_ZONE2)
|
1207
|
-
end
|
1208
|
-
|
1209
|
-
# Test a docker container monitored resource can be retrieved from Metadata
|
1210
|
-
# Agent with "container.<container_name>" as the locally-unique id when the
|
1211
|
-
# log entry comes from an application running in the docker container.
|
1212
|
-
def test_retrieve_docker_container_monitored_resource_with_application
|
1213
|
-
setup_gce_metadata_stubs_minimum
|
1214
|
-
setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
|
1215
|
-
setup_metadata_agent_stubs(
|
1216
|
-
"#{DOCKER_CONSTANTS[:resource_type]}_application")
|
1217
|
-
setup_logging_stubs do
|
1218
|
-
# Tag format here: "application-container.<container_name>.
|
1219
|
-
# <additional_tag>"
|
1220
|
-
d = create_driver(ENABLE_METADATA_AGENT_CONFIG,
|
1221
|
-
DOCKER_CONTAINER_TAG_WITH_APPLICATION)
|
1222
|
-
d.emit('message' => log_entry(0))
|
1223
|
-
d.run
|
1224
|
-
end
|
1225
|
-
verify_log_entries(1, DOCKER_CONTAINER_WITH_APPLICATION_PARAMS)
|
1226
|
-
end
|
1227
|
-
|
1228
|
-
# Test a GKE container monitored resource can be retrieved from Metadata
|
1229
|
-
# Agent with "gke_containerName.<namespace_id>.<pod_name>.<container_name>" as
|
1230
|
-
# the locally-unique id when the log entry comes from an application running
|
1231
|
-
# in Kubenetes.
|
1232
|
-
def test_retrieve_gke_application_container_monitored_resource
|
1233
|
-
setup_gce_metadata_stubs_minimum
|
1234
|
-
setup_container_metadata_stubs
|
1235
|
-
setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
|
1236
|
-
setup_metadata_agent_stubs(
|
1237
|
-
"#{CONTAINER_CONSTANTS[:resource_type]}_application")
|
1238
|
-
setup_logging_stubs do
|
1239
|
-
d = create_driver(ENABLE_METADATA_AGENT_CONFIG)
|
1240
|
-
d.emit(gke_application_container_log_entry(log_entry(0)))
|
1241
|
-
d.run
|
1242
|
-
end
|
1243
|
-
verify_log_entries(1, CONTAINER_FROM_APPLICATION_PARAMS)
|
1244
|
-
end
|
1245
|
-
|
1246
1048
|
private
|
1247
1049
|
|
1248
1050
|
def stub_metadata_request(metadata_path, response_body)
|
@@ -1279,26 +1081,6 @@ module BaseTest
|
|
1279
1081
|
'Content-Type' => 'application/json' })
|
1280
1082
|
end
|
1281
1083
|
|
1282
|
-
# The minimum stubs needed for infomation that Metadata Agent can not provide.
|
1283
|
-
def setup_gce_metadata_stubs_minimum
|
1284
|
-
# Stub the root, used for platform detection by the plugin and 'googleauth'.
|
1285
|
-
stub_request(:get, 'http://169.254.169.254')
|
1286
|
-
.to_return(status: 200, headers: { 'Metadata-Flavor' => 'Google' })
|
1287
|
-
|
1288
|
-
# Create stubs for all the GCE metadata lookups the agent needs to make.
|
1289
|
-
stub_metadata_request('project/project-id', PROJECT_ID)
|
1290
|
-
stub_metadata_request('instance/attributes/',
|
1291
|
-
"attribute1\nattribute2\nattribute3")
|
1292
|
-
|
1293
|
-
# Used by 'googleauth' to fetch the default service account credentials.
|
1294
|
-
stub_request(:get, 'http://169.254.169.254/computeMetadata/v1/' \
|
1295
|
-
'instance/service-accounts/default/token')
|
1296
|
-
.to_return(body: %({"access_token": "#{FAKE_AUTH_TOKEN}"}),
|
1297
|
-
status: 200,
|
1298
|
-
headers: { 'Content-Length' => FAKE_AUTH_TOKEN.length,
|
1299
|
-
'Content-Type' => 'application/json' })
|
1300
|
-
end
|
1301
|
-
|
1302
1084
|
def setup_ec2_metadata_stubs
|
1303
1085
|
# Stub the root, used for platform detection.
|
1304
1086
|
stub_request(:get, 'http://169.254.169.254')
|
@@ -1349,11 +1131,6 @@ module BaseTest
|
|
1349
1131
|
'KUBE_BEARER_TOKEN: AoQiMuwkNP2BMT0S')
|
1350
1132
|
end
|
1351
1133
|
|
1352
|
-
def setup_docker_remote_api_stubs
|
1353
|
-
stub_request(:get, "http://unix/containers/#{DOCKER_CONTAINER_NAME}/json")
|
1354
|
-
.to_return(status: 200, body: "{\"Id\":\"#{DOCKER_CONTAINER_ID}\"}")
|
1355
|
-
end
|
1356
|
-
|
1357
1134
|
def setup_cloudfunctions_metadata_stubs
|
1358
1135
|
stub_metadata_request(
|
1359
1136
|
'instance/attributes/',
|
@@ -1383,20 +1160,11 @@ module BaseTest
|
|
1383
1160
|
Prometheus::Client.registry.instance_variable_set('@metrics', {})
|
1384
1161
|
end
|
1385
1162
|
|
1386
|
-
def setup_metadata_agent_stubs(resource_type)
|
1387
|
-
unique_id = MONITORED_RESOURCE_STUBS[resource_type]['unique_id']
|
1388
|
-
resource = MONITORED_RESOURCE_STUBS[resource_type]['monitored_resource']
|
1389
|
-
stub_request(:get, 'http://local-metadata-agent.stackdriver.com:8000' \
|
1390
|
-
"/monitoredResource/#{unique_id}")
|
1391
|
-
.to_return(status: 200, body: resource)
|
1392
|
-
end
|
1393
|
-
|
1394
1163
|
def container_tag_with_container_name(container_name)
|
1395
1164
|
"kubernetes.#{CONTAINER_POD_NAME}_#{CONTAINER_NAMESPACE_NAME}_" \
|
1396
1165
|
"#{container_name}"
|
1397
1166
|
end
|
1398
1167
|
|
1399
|
-
# GKE Container
|
1400
1168
|
def container_log_entry_with_metadata(
|
1401
1169
|
log, container_name = CONTAINER_CONTAINER_NAME)
|
1402
1170
|
{
|
@@ -1424,37 +1192,6 @@ module BaseTest
|
|
1424
1192
|
}
|
1425
1193
|
end
|
1426
1194
|
|
1427
|
-
def gke_application_container_log_entry(log)
|
1428
|
-
{
|
1429
|
-
log: log,
|
1430
|
-
LOCALLY_UNIQUE_ID_LABEL_NAME => 'gke_containerName' \
|
1431
|
-
".#{CONTAINER_NAMESPACE_ID}" \
|
1432
|
-
".#{CONTAINER_POD_NAME}" \
|
1433
|
-
".#{CONTAINER_CONTAINER_NAME}"
|
1434
|
-
}
|
1435
|
-
end
|
1436
|
-
|
1437
|
-
# Docker Container
|
1438
|
-
def docker_container_log_entry_with_metadata(
|
1439
|
-
log, container_id = DOCKER_CONTAINER_ID,
|
1440
|
-
container_name = DOCKER_CONTAINER_NAME,
|
1441
|
-
stream = DOCKER_CONTAINER_STREAM_STDOUT)
|
1442
|
-
{
|
1443
|
-
log: log,
|
1444
|
-
container_id: container_id,
|
1445
|
-
container_name: container_name,
|
1446
|
-
time: DOCKER_CONTAINER_TIMESTAMP,
|
1447
|
-
source: stream
|
1448
|
-
}
|
1449
|
-
end
|
1450
|
-
|
1451
|
-
def docker_container_log_entry(log)
|
1452
|
-
{
|
1453
|
-
log: log,
|
1454
|
-
time: CONTAINER_TIMESTAMP
|
1455
|
-
}
|
1456
|
-
end
|
1457
|
-
|
1458
1195
|
def cloudfunctions_log_entry(i)
|
1459
1196
|
{
|
1460
1197
|
stream: 'stdout',
|
@@ -1545,14 +1282,14 @@ module BaseTest
|
|
1545
1282
|
assert i == n, "Number of entries #{i} does not match expected number #{n}"
|
1546
1283
|
end
|
1547
1284
|
|
1548
|
-
def verify_container_logs(
|
1285
|
+
def verify_container_logs(log_entry_factory, expected_params)
|
1549
1286
|
setup_gce_metadata_stubs
|
1550
1287
|
setup_container_metadata_stubs
|
1551
1288
|
[1, 2, 3, 5, 11, 50].each do |n|
|
1552
1289
|
@logs_sent = []
|
1553
1290
|
setup_logging_stubs do
|
1554
1291
|
d = create_driver(APPLICATION_DEFAULT_CONFIG, CONTAINER_TAG)
|
1555
|
-
n.times { |i| d.emit(
|
1292
|
+
n.times { |i| d.emit(log_entry_factory.call(log_entry(i))) }
|
1556
1293
|
d.run
|
1557
1294
|
end
|
1558
1295
|
verify_log_entries(n, expected_params) do |entry|
|
@@ -1564,6 +1301,55 @@ module BaseTest
|
|
1564
1301
|
end
|
1565
1302
|
end
|
1566
1303
|
|
1304
|
+
def verify_subfields_from_record(payload_key)
|
1305
|
+
destination_key, payload_value = LOG_ENTRY_SUBFIELDS_PARAMS[payload_key]
|
1306
|
+
@logs_sent = []
|
1307
|
+
setup_gce_metadata_stubs
|
1308
|
+
setup_logging_stubs do
|
1309
|
+
d = create_driver
|
1310
|
+
d.emit(payload_key => payload_value)
|
1311
|
+
d.run
|
1312
|
+
end
|
1313
|
+
verify_log_entries(1, COMPUTE_PARAMS, destination_key) do |entry|
|
1314
|
+
assert_equal payload_value, entry[destination_key], entry
|
1315
|
+
fields = get_fields(entry['jsonPayload'])
|
1316
|
+
assert_nil fields[payload_key], entry
|
1317
|
+
end
|
1318
|
+
end
|
1319
|
+
|
1320
|
+
def verify_subfields_partial_from_record(payload_key)
|
1321
|
+
destination_key, payload_value = LOG_ENTRY_SUBFIELDS_PARAMS[payload_key]
|
1322
|
+
@logs_sent = []
|
1323
|
+
setup_gce_metadata_stubs
|
1324
|
+
setup_logging_stubs do
|
1325
|
+
d = create_driver
|
1326
|
+
d.emit(payload_key => payload_value.merge('otherKey' => 'value'))
|
1327
|
+
d.run
|
1328
|
+
end
|
1329
|
+
verify_log_entries(1, COMPUTE_PARAMS, destination_key) do |entry|
|
1330
|
+
assert_equal payload_value, entry[destination_key], entry
|
1331
|
+
fields = get_fields(entry['jsonPayload'])
|
1332
|
+
request = get_fields(get_struct(fields[payload_key]))
|
1333
|
+
assert_equal 'value', get_string(request['otherKey']), entry
|
1334
|
+
end
|
1335
|
+
end
|
1336
|
+
|
1337
|
+
def verify_subfields_when_not_hash(payload_key)
|
1338
|
+
destination_key = LOG_ENTRY_SUBFIELDS_PARAMS[payload_key][0]
|
1339
|
+
@logs_sent = []
|
1340
|
+
setup_gce_metadata_stubs
|
1341
|
+
setup_logging_stubs do
|
1342
|
+
d = create_driver
|
1343
|
+
d.emit(payload_key => 'a_string')
|
1344
|
+
d.run
|
1345
|
+
end
|
1346
|
+
verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
|
1347
|
+
field = get_fields(entry['jsonPayload'])[payload_key]
|
1348
|
+
assert_equal 'a_string', get_string(field), entry
|
1349
|
+
assert_nil entry[destination_key], entry
|
1350
|
+
end
|
1351
|
+
end
|
1352
|
+
|
1567
1353
|
# Replace the 'referer' field with nil.
|
1568
1354
|
def http_request_message_with_nil_referer
|
1569
1355
|
HTTP_REQUEST_MESSAGE.merge('referer' => nil)
|