Skip to content

Commit

Permalink
feat: Provide a query for whether a logging agent is expected in the …
Browse files Browse the repository at this point in the history
…current environment
  • Loading branch information
dazuma committed Aug 22, 2024
1 parent e82f121 commit 7e43e14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/google/cloud/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,19 @@ def kubernetes_engine_namespace_id
end
alias container_engine_namespace_id kubernetes_engine_namespace_id

##
# Determine whether the application is running in an environment where a
# Google Cloud logging agent is expected to be running. In such an
# environment, we expect that the standard output and error streams are
# likely to be parsed by the logging agent and log entries are written to
# the Google Cloud Logging service.
#
# @return [boolean]
#
def logging_agent_expected?
compute_engine? && !cloud_shell? && (app_engine? || knative? || kubernetes_engine?)
end

##
# Returns the global instance of {Google::Cloud::Env}.
#
Expand Down
9 changes: 8 additions & 1 deletion test/google/cloud/env_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def ext_conn failure_count: 0
_(env.cloud_shell?).must_equal false
_(env.compute_engine?).must_equal true
_(env.raw_compute_engine?).must_equal false
_(env.logging_agent_expected?).must_equal true

_(env.project_id).must_equal project_id
_(env.numeric_project_id).must_equal numeric_project_id
Expand Down Expand Up @@ -189,6 +190,7 @@ def ext_conn failure_count: 0
_(env.cloud_shell?).must_equal false
_(env.compute_engine?).must_equal true
_(env.raw_compute_engine?).must_equal false
_(env.logging_agent_expected?).must_equal true

_(env.project_id).must_equal project_id
_(env.numeric_project_id).must_equal numeric_project_id
Expand Down Expand Up @@ -218,6 +220,7 @@ def ext_conn failure_count: 0
_(env.cloud_shell?).must_equal false
_(env.compute_engine?).must_equal true
_(env.raw_compute_engine?).must_equal false
_(env.logging_agent_expected?).must_equal true

_(env.project_id).must_equal project_id
_(env.numeric_project_id).must_equal numeric_project_id
Expand Down Expand Up @@ -247,6 +250,7 @@ def ext_conn failure_count: 0
_(env.cloud_shell?).must_equal false
_(env.compute_engine?).must_equal true
_(env.raw_compute_engine?).must_equal false
_(env.logging_agent_expected?).must_equal true

_(env.project_id).must_equal project_id
_(env.numeric_project_id).must_equal numeric_project_id
Expand Down Expand Up @@ -276,6 +280,7 @@ def ext_conn failure_count: 0
_(env.cloud_shell?).must_equal true
_(env.compute_engine?).must_equal true
_(env.raw_compute_engine?).must_equal false
_(env.logging_agent_expected?).must_equal false

_(env.project_id).must_equal project_id
_(env.numeric_project_id).must_be_nil
Expand All @@ -292,7 +297,7 @@ def ext_conn failure_count: 0
_(env.kubernetes_engine_namespace_id).must_be_nil
end

it "returns correct values when running on compute engine" do
it "returns correct values when running on raw compute engine" do
env.variables.backing_data = gce_variables
env.compute_smbios.override_product_name = "Google"
env.compute_metadata.overrides = gce_metadata_overrides
Expand All @@ -305,6 +310,7 @@ def ext_conn failure_count: 0
_(env.cloud_shell?).must_equal false
_(env.compute_engine?).must_equal true
_(env.raw_compute_engine?).must_equal true
_(env.logging_agent_expected?).must_equal false

_(env.project_id).must_equal project_id
_(env.numeric_project_id).must_equal numeric_project_id
Expand Down Expand Up @@ -334,6 +340,7 @@ def ext_conn failure_count: 0
_(env.cloud_shell?).must_equal false
_(env.compute_engine?).must_equal false
_(env.raw_compute_engine?).must_equal false
_(env.logging_agent_expected?).must_equal false

_(env.project_id).must_be_nil
_(env.numeric_project_id).must_be_nil
Expand Down

0 comments on commit 7e43e14

Please sign in to comment.