I'm using stackdriver logging (appengine gem) on App Engine Flexible Environment.
I enabled logging middleware on staging environment by config.google_cloud.use_logging = true, and I could get really good stackdriver UI. Thank you.
And I want to log user id, so I configured config.google_cloud.logging.labels, but any customized label was not logged.
labels: {
appengine.googleapis.com/instance_name: "aef-default-dev-g31m"
appengine.googleapis.com/trace_id: "22b9a172787112ed00442d075d6045dc"
compute.googleapis.com/resource_id: "3787020091829122899"
compute.googleapis.com/resource_name: "a7250b9c37c0"
compute.googleapis.com/zone: "asia-northeast1-b"
}
I tried to configure in config/environments/staging.rb pattern,
config.google_cloud.use_logging = true
config.google_cloud.logging.labels = {
"my-static-label" => "static-label-value",
}
and configure in config/initializers/google_cloud_logging.rb pattern,
Google::Cloud::Logging.configure do |config|
# occurs: undefined method `labels=' for nil:NilClass (NoMethodError)
config.google_cloud.labels = {
"my-static-label" => "static-label-value",
}
end
Google::Cloud::Logging.configure do |config|
config.labels = {
"my-static-label" => "static-label-value",
}
end
md5-68e8d6557714386a6dc3285b26425761
google-cloud-logging (1.5.7)
concurrent-ruby (~> 1.0)
google-cloud-core (~> 1.2)
google-gax (~> 1.3)
googleapis-common-protos-types (>= 1.0.2)
stackdriver-core (~> 1.3)
Hi @yossy-ok, thanks for the question. As I read the Stackdriver Instrumentation Configuration document, and as my testing has shown, the configuration can be added to either the config/application.rb file, or the config/environments/*.rb files. Any configuration added to config/initializers/*.rb files will be loaded too late in the Rails boot process, so I would avoid using those files for this purpose.
I made a simple temp app and verified the following behavior. Configuration in the config/environments/*.rb files will overwrite configuration in the config/application.rb file. Configuration added via Google::Cloud.configure will overwrite configuration added in Rails.application.configure. So I would choose either using Rails.application.configure or using Google::Cloud.configure, but not both.
One other thing I noticed, specifying labels in a config/environments/*.rb file will completely replace the labels specified in the config/application.rb file. One way to use both is to change the config/environments/*.rb file to add individual labels instead of overwriting them all.
config/application.rb:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module StackdriverTestApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
config.google_cloud.project_id = "my-project"
config.google_cloud.credentials = "/path/to/my/credentials.json"
config.google_cloud.use_logging = true
config.google_cloud.logging.labels = { foo: :application }
end
end
config/environments/development.rb:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# boilerplate configuration removed
config.google_cloud.logging.labels[:bar] = :development
end
Terminal:
$ bundle exec rails console
Loading development environment (Rails 5.2.2)
irb(main):001:0> Rails.logger.labels
=> {:foo=>:application, :bar=>:development}
Does this answer your question?
Google::Cloud::Logging.configure do |config|
# occurs: undefined method `labels=' for nil:NilClass (NoMethodError)
config.google_cloud.labels = {
"my-static-label" => "static-label-value",
}
end
This is a mistake in the guide. As you stated, it should be using config.labels, not config.google_cloud.labels. I will fix this in the guide.
One more thing. The original code you provided is not syntactically correct, and will raise an error when run.
labels: {
appengine.googleapis.com/instance_name: "aef-default-dev-g31m"
appengine.googleapis.com/trace_id: "22b9a172787112ed00442d075d6045dc"
compute.googleapis.com/resource_id: "3787020091829122899"
compute.googleapis.com/resource_name: "a7250b9c37c0"
compute.googleapis.com/zone: "asia-northeast1-b"
}
This will raise a SyntaxError. So if this is indeed the code that is being used it is possible that the configuration isn't loaded at all because of the error.
Thank you for your courteous reply. I understand config/application.rb, config/environments/*.rb and config/initializers/*.rb relationships.
And I was mistaken. I was seeing nginx.request (projects/(MyProject)/logs/appengine.googleapis.com%2Fnginx.request) labels field in Stackdriver UI, but I should see ruby_app_log (projects/fanc-dev/logs/ruby_app_log).
ruby_app_log logs have my defined labels!
I'm really sorry for taking up your time and really thank you.
P.S. I'm sorry to confuse you. To show that my defined labels are not exist, I copied labels from Stackdriver UI, so https://github.com/googleapis/google-cloud-ruby/issues/2717#issuecomment-446696919 is not ruby code, (perhaps screenshot was better). But really thank you.
Most helpful comment
This is a mistake in the guide. As you stated, it should be using
config.labels, notconfig.google_cloud.labels. I will fix this in the guide.