在 GKE 中登录 Rails 应用程序的 Stackdriver 无法正常工作

Stackdriver logging in a Rails app in GKE is not working

Ruby 2.3.6,Rails 4.2.6,Stackdriver 0.15.0

按照 https://cloud.google.com/logging/docs/setup/ruby 的说明,我已将 stackdriver gem 添加到 Gemfile。据我所知,我不应该需要做任何其他事情。

但是,我没有在 Stackdriver 中看到 Rails 中的任何日志消息。我什至尝试 exec 在容器中 shell 运行 bundle exec rails console,并使用 Rails.logger.error "this is a log message".

显式登录

config.google_cloud.use_logging = trueconfig.log_level = :debug,FWIW。

UPDATE:我用 pp Rails.logger 仔细查看了记录器实例,它包含以下内容:

@resource=
  #<Google::Cloud::Logging::Resource:0x0000558b075d1f50
   @labels={:cluster_name=>"onehq-cluster", :namespace_id=>"default"},
   @type="container">,

我的容器是 staging 命名空间中的 运行,这可以解释为什么我没有看到日志消息。当我查看 default 命名空间中的 Stackdriver 日志查看器时,看,有日志。不幸的是,将所有消息发送到 default 对我没有任何好处,因为我在两个命名空间中有应用程序 运行,我需要能够区分它们。

我尝试添加额外的配置:

config.google_cloud.logging.monitored_resource.type = "container"
config.google_cloud.logging.monitored_resource.labels = { cluster_name: "my-cluster", namespace_id: "staging" }

现在 Rails.logger.error "this is a log message" 不会出现在 任何地方

更新 2:经过大量网络搜索,我设法找到了 https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/2025 which suggests that the namespace to which log messages are sent is determined by the value of an environment variable, and that said env var is not being set automatically in GKE containers. (It has supposedly been resolved by https://github.com/GoogleCloudPlatform/google-cloud-ruby/pull/2071,但我认为它还没有上线,至少不在我的集群中。 ) 我将尝试在我的部署文件中强制使用 env var,如果可行,我会将其最后一部分转换为答案。

经过大量网络搜索,我设法找到了 https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/2025 which suggests that the namespace to which log messages are sent is determined by the value of an environment variable, and that said env var is not being set automatically in GKE containers. (It has supposedly been resolved by https://github.com/GoogleCloudPlatform/google-cloud-ruby/pull/2071,但我认为它还没有上线,至少在我的集群中没有。)我在我的部署文件和 IIRC 中强制使用了 env var那行得通。 (我无法再访问该集群,因此无法验证。)

Google-Cloud-Env gem 今天早上发布了 the version (1.0.2) 你可以更新你的包。