fluent-plugin-google-cloud 0.6.4.pre.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,6 +16,7 @@ require 'google/apis'
16
16
  require 'helper'
17
17
  require 'mocha/test_unit'
18
18
  require 'webmock/test_unit'
19
+ require 'prometheus/client'
19
20
 
20
21
  require_relative 'constants'
21
22
 
@@ -99,7 +100,7 @@ module BaseTest
99
100
  assert_equal PROJECT_ID, d.instance.project_id
100
101
  assert_equal ZONE, d.instance.zone
101
102
  assert_equal VM_ID, d.instance.vm_id
102
- assert_equal COMPUTE_CONSTANTS[:resource_type], d.instance.resource.type
103
+ assert_equal false, d.instance.running_on_managed_vm
103
104
  end
104
105
 
105
106
  def test_managed_vm_metadata_loading
@@ -110,11 +111,9 @@ module BaseTest
110
111
  assert_equal PROJECT_ID, d.instance.project_id
111
112
  assert_equal ZONE, d.instance.zone
112
113
  assert_equal VM_ID, d.instance.vm_id
113
- assert_equal APPENGINE_CONSTANTS[:resource_type], d.instance.resource.type
114
- assert_equal MANAGED_VM_BACKEND_NAME,
115
- d.instance.resource.labels['module_id']
116
- assert_equal MANAGED_VM_BACKEND_VERSION,
117
- d.instance.resource.labels['version_id']
114
+ assert_equal true, d.instance.running_on_managed_vm
115
+ assert_equal MANAGED_VM_BACKEND_NAME, d.instance.gae_backend_name
116
+ assert_equal MANAGED_VM_BACKEND_VERSION, d.instance.gae_backend_version
118
117
  end
119
118
 
120
119
  def test_gce_metadata_does_not_load_when_use_metadata_service_is_false
@@ -124,7 +123,7 @@ module BaseTest
124
123
  assert_equal CUSTOM_PROJECT_ID, d.instance.project_id
125
124
  assert_equal CUSTOM_ZONE, d.instance.zone
126
125
  assert_equal CUSTOM_VM_ID, d.instance.vm_id
127
- assert_equal COMPUTE_CONSTANTS[:resource_type], d.instance.resource.type
126
+ assert_equal false, d.instance.running_on_managed_vm
128
127
  end
129
128
 
130
129
  def test_gce_used_when_detect_subservice_is_false
@@ -158,6 +157,8 @@ module BaseTest
158
157
  assert_equal parts[1], d.instance.project_id, "Index #{index} failed."
159
158
  assert_equal parts[2], d.instance.zone, "Index #{index} failed."
160
159
  assert_equal parts[3], d.instance.vm_id, "Index #{index} failed."
160
+ assert_equal false, d.instance.running_on_managed_vm,
161
+ "Index #{index} failed."
161
162
  end
162
163
  end
163
164
 
@@ -701,17 +702,78 @@ module BaseTest
701
702
  end
702
703
  end
703
704
 
704
- # Test container logs when metadata is extracted from the 'kubernetes' field
705
- # in the log record.
706
- def test_container_logs_metadata_from_record
707
- verify_container_logs(method(:container_log_entry_with_metadata),
708
- CONTAINER_FROM_METADATA_PARAMS)
705
+ def test_one_container_log_metadata_from_plugin
706
+ setup_gce_metadata_stubs
707
+ setup_container_metadata_stubs
708
+ setup_logging_stubs do
709
+ d = create_driver(APPLICATION_DEFAULT_CONFIG, CONTAINER_TAG)
710
+ d.emit(container_log_entry_with_metadata(log_entry(0)))
711
+ d.run
712
+ end
713
+ verify_log_entries(1, CONTAINER_FROM_METADATA_PARAMS) do |entry|
714
+ assert_equal CONTAINER_SECONDS_EPOCH, entry['timestamp']['seconds'], entry
715
+ assert_equal CONTAINER_NANOS, entry['timestamp']['nanos'], entry
716
+ assert_equal CONTAINER_SEVERITY, entry['severity'], entry
717
+ end
718
+ end
719
+
720
+ def test_multiple_container_logs_metadata_from_plugin
721
+ setup_gce_metadata_stubs
722
+ setup_container_metadata_stubs
723
+ [2, 3, 5, 11, 50].each do |n|
724
+ @logs_sent = []
725
+ setup_logging_stubs do
726
+ d = create_driver(APPLICATION_DEFAULT_CONFIG, CONTAINER_TAG)
727
+ # The test driver doesn't clear its buffer of entries after running, so
728
+ # do it manually here.
729
+ d.instance_variable_get('@entries').clear
730
+ n.times { |i| d.emit(container_log_entry_with_metadata(log_entry(i))) }
731
+ d.run
732
+ end
733
+ verify_log_entries(n, CONTAINER_FROM_METADATA_PARAMS) do |entry|
734
+ assert_equal CONTAINER_SECONDS_EPOCH, entry['timestamp']['seconds'],
735
+ entry
736
+ assert_equal CONTAINER_NANOS, entry['timestamp']['nanos'], entry
737
+ assert_equal CONTAINER_SEVERITY, entry['severity'], entry
738
+ end
739
+ end
740
+ end
741
+
742
+ def test_multiple_container_logs_metadata_from_tag
743
+ setup_gce_metadata_stubs
744
+ setup_container_metadata_stubs
745
+ [2, 3, 5, 11, 50].each do |n|
746
+ @logs_sent = []
747
+ setup_logging_stubs do
748
+ d = create_driver(APPLICATION_DEFAULT_CONFIG, CONTAINER_TAG)
749
+ # The test driver doesn't clear its buffer of entries after running, so
750
+ # do it manually here.
751
+ d.instance_variable_get('@entries').clear
752
+ n.times { |i| d.emit(container_log_entry(log_entry(i))) }
753
+ d.run
754
+ end
755
+ verify_log_entries(n, CONTAINER_FROM_TAG_PARAMS) do |entry|
756
+ assert_equal CONTAINER_SECONDS_EPOCH, entry['timestamp']['seconds'],
757
+ entry
758
+ assert_equal CONTAINER_NANOS, entry['timestamp']['nanos'], entry
759
+ assert_equal CONTAINER_SEVERITY, entry['severity'], entry
760
+ end
761
+ end
709
762
  end
710
763
 
711
- # Test container logs when metadata is extracted from the tag.
712
- def test_container_logs_metadata_from_tag
713
- verify_container_logs(method(:container_log_entry),
714
- CONTAINER_FROM_TAG_PARAMS)
764
+ def test_one_container_log_metadata_from_tag
765
+ setup_gce_metadata_stubs
766
+ setup_container_metadata_stubs
767
+ setup_logging_stubs do
768
+ d = create_driver(APPLICATION_DEFAULT_CONFIG, CONTAINER_TAG)
769
+ d.emit(container_log_entry(log_entry(0)))
770
+ d.run
771
+ end
772
+ verify_log_entries(1, CONTAINER_FROM_TAG_PARAMS) do |entry|
773
+ assert_equal CONTAINER_SECONDS_EPOCH, entry['timestamp']['seconds'], entry
774
+ assert_equal CONTAINER_NANOS, entry['timestamp']['nanos'], entry
775
+ assert_equal CONTAINER_SEVERITY, entry['severity'], entry
776
+ end
715
777
  end
716
778
 
717
779
  def test_one_container_log_from_tag_stderr
@@ -778,134 +840,6 @@ module BaseTest
778
840
  end
779
841
  end
780
842
 
781
- # Docker Container.
782
-
783
- # Test textPayload logs from Docker container stderr / stdout.
784
- def test_text_payload_docker_container_logs
785
- setup_gce_metadata_stubs
786
- [1, 2, 3, 5, 11, 50].each do |n|
787
- @logs_sent = []
788
- setup_logging_stubs do
789
- d = create_driver(APPLICATION_DEFAULT_CONFIG, DOCKER_CONTAINER_TAG)
790
- # Generate log entries with 'log' (simply a string) and 'time' fields.
791
- n.times { |i| d.emit(docker_container_log_entry(log_entry(i))) }
792
- d.run
793
- end
794
- # 'container_id' and 'container_name' should have been extracted from tag
795
- # and properly set in resource.labels and common metadata labels as in
796
- # DOCKER_CONTAINER_PARAMS.
797
- verify_log_entries(n, DOCKER_CONTAINER_PARAMS) do |entry|
798
- # Timestamp in 'time' field from log entry should be set properly.
799
- assert_equal DOCKER_CONTAINER_SECONDS_EPOCH,
800
- entry['timestamp']['seconds'], entry
801
- assert_equal DOCKER_CONTAINER_NANOS, entry['timestamp']['nanos'],
802
- entry
803
- # Severity is 'DEFAULT' because 'stream' info is absent from log entry.
804
- assert_equal_with_default entry['severity'], 'DEFAULT', 'DEFAULT', entry
805
- end
806
- end
807
- end
808
-
809
- # Test jsonPayload logs from Docker container stderr / stdout.
810
- def test_json_payload_docker_container_logs
811
- setup_gce_metadata_stubs
812
- [1, 2, 3, 5, 11, 50].each do |n|
813
- @logs_sent = []
814
- setup_logging_stubs do
815
- d = create_driver(APPLICATION_DEFAULT_CONFIG, DOCKER_CONTAINER_TAG)
816
- n.times do
817
- # Generate log entries with 'log' (json) and 'time' fields.
818
- d.emit(docker_container_log_entry('{"msg": "test log entry ' \
819
- "#{n}" \
820
- '", "tag2": "test", "data": ' \
821
- '5000, "severity": "WARNING"}'))
822
- end
823
- d.run
824
- end
825
- # 'container_id' and 'container_name' should have been extracted from tag
826
- # and properly set in resource.labels and common metadata labels as in
827
- # DOCKER_CONTAINER_PARAMS.
828
- verify_log_entries(n, DOCKER_CONTAINER_PARAMS, 'jsonPayload') do |entry|
829
- # 'log' field should be detected as json and parsed properly.
830
- fields = get_fields(entry['jsonPayload'])
831
- assert_equal 3, fields.size, entry
832
- assert_equal "test log entry #{n}", get_string(fields['msg']), entry
833
- assert_equal 'test', get_string(fields['tag2']), entry
834
- assert_equal 5000, get_number(fields['data']), entry
835
- # Timestamp in 'time' field from log entry should be set properly.
836
- assert_equal DOCKER_CONTAINER_SECONDS_EPOCH,
837
- entry['timestamp']['seconds'], entry
838
- assert_equal DOCKER_CONTAINER_NANOS, entry['timestamp']['nanos'], entry
839
- # Severity in 'severity' field from log entry should be set properly.
840
- assert_equal 'WARNING', entry['severity'], entry
841
- end
842
- end
843
- end
844
-
845
- # Test the following metadata is properly extracted from json record:
846
- # * container_id
847
- # * container_name
848
- # * source (this field maps to the 'stream' metadata label.
849
- def test_docker_container_logs_metadata_from_json_record
850
- setup_gce_metadata_stubs
851
- {
852
- # 'stream' label should be extracted from json record when present.
853
- docker_container_log_entry_with_metadata(
854
- log_entry(0)
855
- ) => {
856
- # When the 'source' field from the json record has value 'stdout',
857
- # 'severity' should be 'INFO'.
858
- 'params' => DOCKER_CONTAINER_PARAMS_WITH_STREAM_STDOUT,
859
- 'severity' => 'INFO'
860
- },
861
- # 'container_id' and 'container_name' can be extracted from tag or
862
- # json record. If present in both cases, values in the json record will
863
- # overwrite values in tags. For example:
864
- # DOCKER_CONTAINER_PARAMS_WITH_METADATA_OVERWRITTEN has different
865
- # 'container_id' and 'container_name' values from DOCKER_CONTAINER_TAG,
866
- # and we need to verify these two fields get overwritten as expected.
867
- docker_container_log_entry_with_metadata(
868
- log_entry(0), DOCKER_CONTAINER_ID2, DOCKER_CONTAINER_NAME2,
869
- DOCKER_CONTAINER_STREAM_STDERR
870
- ) => {
871
- # When the 'source' field from the json record has value 'stderr',
872
- # 'severity' should be 'ERROR'.
873
- 'params' => DOCKER_CONTAINER_PARAMS_WITH_METADATA_OVERWRITTEN,
874
- 'severity' => 'ERROR'
875
- }
876
- }.each do |log_entry, expected|
877
- @logs_sent = []
878
- setup_logging_stubs do
879
- d = create_driver(APPLICATION_DEFAULT_CONFIG, DOCKER_CONTAINER_TAG)
880
- d.emit(log_entry)
881
- d.run
882
- end
883
- verify_log_entries(1, expected['params']) do |entry|
884
- assert_equal DOCKER_CONTAINER_SECONDS_EPOCH,
885
- entry['timestamp']['seconds'], entry
886
- assert_equal DOCKER_CONTAINER_NANOS, entry['timestamp']['nanos'],
887
- entry
888
- assert_equal_with_default entry['severity'], expected['severity'],
889
- 'DEFAULT', entry
890
- end
891
- end
892
- end
893
-
894
- # Test logs from applications running in Docker containers.
895
- def test_docker_container_application_logs
896
- setup_gce_metadata_stubs
897
- setup_docker_remote_api_stubs
898
- setup_logging_stubs do
899
- # Metadata Agent is not enabled. Will call Docker Remote API for
900
- # container info.
901
- d = create_driver(APPLICATION_DEFAULT_CONFIG,
902
- DOCKER_CONTAINER_TAG_WITH_APPLICATION)
903
- d.emit('message' => log_entry(0))
904
- d.run
905
- end
906
- verify_log_entries(1, DOCKER_CONTAINER_WITH_APPLICATION_PARAMS)
907
- end
908
-
909
843
  def test_cloudfunctions_log
910
844
  setup_gce_metadata_stubs
911
845
  setup_cloudfunctions_metadata_stubs
@@ -1081,72 +1015,6 @@ module BaseTest
1081
1015
  end
1082
1016
  end
1083
1017
 
1084
- # Metadata Agent related tests.
1085
- def test_configure_enable_metadata_agent_default_and_false
1086
- setup_gce_metadata_stubs
1087
- [create_driver, create_driver(DISABLE_METADATA_AGENT_CONFIG)].each do |d|
1088
- assert_false d.instance.instance_variable_get(:@enable_metadata_agent)
1089
- end
1090
- end
1091
-
1092
- def test_configure_enable_metadata_agent_true
1093
- setup_gce_metadata_stubs
1094
- setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
1095
- d = create_driver(ENABLE_METADATA_AGENT_CONFIG)
1096
- assert_true d.instance.instance_variable_get(:@enable_metadata_agent)
1097
- end
1098
-
1099
- # Test an implicit monitored resource can be retrieved from Metadata Agent
1100
- # with an empty string as the locally-unique id.
1101
- def test_retrieve_implicit_monitored_resource
1102
- # Minimum set up for gce metadata stubs. Stubs for 'vm_id' and 'location'
1103
- # info are deliberately not set up.
1104
- setup_gce_metadata_stubs_minimum
1105
- setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
1106
- setup_logging_stubs do
1107
- d = create_driver(ENABLE_METADATA_AGENT_CONFIG)
1108
- d.emit('message' => log_entry(0))
1109
- d.run
1110
- end
1111
- verify_log_entries(1, COMPUTE_PARAMS)
1112
- end
1113
-
1114
- # Test a docker container monitored resource can be retrieved from Metadata
1115
- # Agent with "container.<container_id>" as the locally-unique id.
1116
- def test_retrieve_docker_container_monitored_resource
1117
- setup_gce_metadata_stubs_minimum
1118
- setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
1119
- setup_metadata_agent_stubs(DOCKER_CONSTANTS[:resource_type])
1120
- setup_logging_stubs do
1121
- # Tag format here: "container.<container_id>.<container_name>"
1122
- d = create_driver(ENABLE_METADATA_AGENT_CONFIG, DOCKER_CONTAINER_TAG)
1123
- d.emit('message' => log_entry(0))
1124
- d.run
1125
- end
1126
- # gce_metadata_stubs has ZONE1, while metadata agent stub has ZONE2. Here we
1127
- # need to verify ZONE2 overwrites ZONE1 as expected.
1128
- verify_log_entries(1, DOCKER_CONTAINER_PARAMS_WITH_ZONE2)
1129
- end
1130
-
1131
- # Test a docker container monitored resource can be retrieved from Metadata
1132
- # Agent with "container.<container_name>" as the locally-unique id when the
1133
- # log entry comes from an application running in the docker container.
1134
- def test_retrieve_docker_container_monitored_resource_with_application
1135
- setup_gce_metadata_stubs_minimum
1136
- setup_metadata_agent_stubs(IMPLICIT_MONITORED_RESOURCE_UNIQUE_KEY)
1137
- setup_metadata_agent_stubs(
1138
- "#{DOCKER_CONSTANTS[:resource_type]}_application")
1139
- setup_logging_stubs do
1140
- # Tag format here: "application-container.<container_name>.
1141
- # <additional_tag>"
1142
- d = create_driver(ENABLE_METADATA_AGENT_CONFIG,
1143
- DOCKER_CONTAINER_TAG_WITH_APPLICATION)
1144
- d.emit('message' => log_entry(0))
1145
- d.run
1146
- end
1147
- verify_log_entries(1, DOCKER_CONTAINER_WITH_APPLICATION_PARAMS)
1148
- end
1149
-
1150
1018
  private
1151
1019
 
1152
1020
  def stub_metadata_request(metadata_path, response_body)
@@ -1183,26 +1051,6 @@ module BaseTest
1183
1051
  'Content-Type' => 'application/json' })
1184
1052
  end
1185
1053
 
1186
- # The minimum stubs needed for infomation that Metadata Agent can not provide.
1187
- def setup_gce_metadata_stubs_minimum
1188
- # Stub the root, used for platform detection by the plugin and 'googleauth'.
1189
- stub_request(:get, 'http://169.254.169.254')
1190
- .to_return(status: 200, headers: { 'Metadata-Flavor' => 'Google' })
1191
-
1192
- # Create stubs for all the GCE metadata lookups the agent needs to make.
1193
- stub_metadata_request('project/project-id', PROJECT_ID)
1194
- stub_metadata_request('instance/attributes/',
1195
- "attribute1\nattribute2\nattribute3")
1196
-
1197
- # Used by 'googleauth' to fetch the default service account credentials.
1198
- stub_request(:get, 'http://169.254.169.254/computeMetadata/v1/' \
1199
- 'instance/service-accounts/default/token')
1200
- .to_return(body: %({"access_token": "#{FAKE_AUTH_TOKEN}"}),
1201
- status: 200,
1202
- headers: { 'Content-Length' => FAKE_AUTH_TOKEN.length,
1203
- 'Content-Type' => 'application/json' })
1204
- end
1205
-
1206
1054
  def setup_ec2_metadata_stubs
1207
1055
  # Stub the root, used for platform detection.
1208
1056
  stub_request(:get, 'http://169.254.169.254')
@@ -1253,11 +1101,6 @@ module BaseTest
1253
1101
  'KUBE_BEARER_TOKEN: AoQiMuwkNP2BMT0S')
1254
1102
  end
1255
1103
 
1256
- def setup_docker_remote_api_stubs
1257
- stub_request(:get, "http://unix/containers/#{DOCKER_CONTAINER_NAME}/json")
1258
- .to_return(status: 200, body: "{\"Id\":\"#{DOCKER_CONTAINER_ID}\"}")
1259
- end
1260
-
1261
1104
  def setup_cloudfunctions_metadata_stubs
1262
1105
  stub_metadata_request(
1263
1106
  'instance/attributes/',
@@ -1283,12 +1126,8 @@ module BaseTest
1283
1126
  DATAPROC_REGION)
1284
1127
  end
1285
1128
 
1286
- def setup_metadata_agent_stubs(resource_type)
1287
- unique_id = MONITORED_RESOURCE_STUBS[resource_type]['unique_id']
1288
- resource = MONITORED_RESOURCE_STUBS[resource_type]['monitored_resource']
1289
- stub_request(:get, 'http://local-metadata-agent.stackdriver.com:8000' \
1290
- "/monitoredResource/#{unique_id}")
1291
- .to_return(status: 200, body: resource)
1129
+ def setup_prometheus
1130
+ Prometheus::Client.registry.instance_variable_set('@metrics', {})
1292
1131
  end
1293
1132
 
1294
1133
  def container_tag_with_container_name(container_name)
@@ -1296,7 +1135,6 @@ module BaseTest
1296
1135
  "#{container_name}"
1297
1136
  end
1298
1137
 
1299
- # GKE Container
1300
1138
  def container_log_entry_with_metadata(
1301
1139
  log, container_name = CONTAINER_CONTAINER_NAME)
1302
1140
  {
@@ -1324,27 +1162,6 @@ module BaseTest
1324
1162
  }
1325
1163
  end
1326
1164
 
1327
- # Docker Container
1328
- def docker_container_log_entry_with_metadata(
1329
- log, container_id = DOCKER_CONTAINER_ID,
1330
- container_name = DOCKER_CONTAINER_NAME,
1331
- stream = DOCKER_CONTAINER_STREAM_STDOUT)
1332
- {
1333
- log: log,
1334
- container_id: container_id,
1335
- container_name: container_name,
1336
- time: DOCKER_CONTAINER_TIMESTAMP,
1337
- source: stream
1338
- }
1339
- end
1340
-
1341
- def docker_container_log_entry(log)
1342
- {
1343
- log: log,
1344
- time: CONTAINER_TIMESTAMP
1345
- }
1346
- end
1347
-
1348
1165
  def cloudfunctions_log_entry(i)
1349
1166
  {
1350
1167
  stream: 'stdout',
@@ -1435,25 +1252,6 @@ module BaseTest
1435
1252
  assert i == n, "Number of entries #{i} does not match expected number #{n}"
1436
1253
  end
1437
1254
 
1438
- def verify_container_logs(log_entry_method, expected_params)
1439
- setup_gce_metadata_stubs
1440
- setup_container_metadata_stubs
1441
- [1, 2, 3, 5, 11, 50].each do |n|
1442
- @logs_sent = []
1443
- setup_logging_stubs do
1444
- d = create_driver(APPLICATION_DEFAULT_CONFIG, CONTAINER_TAG)
1445
- n.times { |i| d.emit(log_entry_method.call(log_entry(i))) }
1446
- d.run
1447
- end
1448
- verify_log_entries(n, expected_params) do |entry|
1449
- assert_equal CONTAINER_SECONDS_EPOCH, entry['timestamp']['seconds'],
1450
- entry
1451
- assert_equal CONTAINER_NANOS, entry['timestamp']['nanos'], entry
1452
- assert_equal CONTAINER_SEVERITY, entry['severity'], entry
1453
- end
1454
- end
1455
- end
1456
-
1457
1255
  # Replace the 'referer' field with nil.
1458
1256
  def http_request_message_with_nil_referer
1459
1257
  HTTP_REQUEST_MESSAGE.merge('referer' => nil)
@@ -1515,6 +1313,12 @@ module BaseTest
1515
1313
  _undefined
1516
1314
  end
1517
1315
 
1316
+ def assert_prometheus_metric_value(metric_name, expected_value, labels = {})
1317
+ metric = Prometheus::Client.registry.get(metric_name)
1318
+ assert_not_nil(metric)
1319
+ assert_equal(expected_value, metric.get(labels))
1320
+ end
1321
+
1518
1322
  # Get the fields of the payload.
1519
1323
  def get_fields(_payload)
1520
1324
  _undefined