Stackdriver logging in a Rails app in GKE is not working

5/29/2018

Ruby 2.3.6, Rails 4.2.6, Stackdriver 0.15.0

Following the directions at https://cloud.google.com/logging/docs/setup/ruby, I've added the stackdriver gem to Gemfile. From what I can tell, I shouldn't need to do anything else.

However, I'm not seeing any log messages from within Rails showing up in Stackdriver. I've even tried execing a shell in the container, running bundle exec rails console, and explicitly logging with Rails.logger.error "this is a log message".

config.google_cloud.use_logging = true and config.log_level = :debug, FWIW.

UPDATE: I took a closer look at the logger instance with pp Rails.logger, and it contains this:

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

My container is running in the staging namespace, which would explain why I'm not seeing log messages. When I look in the Stackdriver log viewer in the default namespace, lo, there are logs. Unfortunately, sending all messages to default won't do me any good, because I have apps running in two namespaces, and I need to be able to tell them apart.

I tried adding additional configuration:

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

And now Rails.logger.error "this is a log message" doesn't show up anywhere.

UPDATE 2: After much web searching, I managed to turn up 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 but I don't think that's live yet, at least not in my cluster.) I'll try forcing the env var in my deployment file, and if that works I'll convert the last part of this to an answer.

-- Craig S. Cottingham
google-cloud-logging
google-kubernetes-engine
ruby
ruby-on-rails

2 Answers

6/28/2018

Google-Cloud-Env gem has released this morning the version (1.0.2) you may update your bundle.

-- Alioua
Source: StackOverflow

6/28/2018

After much web searching, I managed to turn up 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 but I don't think that's live yet, at least not in my cluster.) I forced the env var in my deployment file, and IIRC that worked. (I no longer have access to that cluster, so I can't verify that.)

-- Craig S. Cottingham
Source: StackOverflow