lib/google/cloud/logging/middleware.rb in google-cloud-logging-1.5.7 vs lib/google/cloud/logging/middleware.rb in google-cloud-logging-1.6.0

- old
+ new

@@ -26,11 +26,12 @@ ## # A default value for the log_name_map argument. Directs health check # logs to a separate log name so they don't spam the main log. DEFAULT_LOG_NAME_MAP = - { "/_ah/health" => "ruby_health_check_log" }.freeze + { "/_ah/health" => "ruby_health_check_log", + "/healthz" => "ruby_health_check_log" }.freeze ## # The Google::Cloud::Logging::Logger instance attr_reader :logger @@ -41,35 +42,40 @@ # @param [Google::Cloud::Logging::Logger] logger A logger to be used by # this middleware. The middleware will be interacting with the logger # to track Stackdriver request trace ID. It also properly sets # env["rack.logger"] to this assigned logger for accessing. If not # specified, a default logger with be used. + # @param [Proc] on_init A callback to be invoked when the middleware is + # initialized. The callback takes no arguments. Optional. # @param [Hash] kwargs Hash of configuration settings. Used for # backward API compatibility. See the [Configuration # Guide](https://googleapis.github.io/google-cloud-ruby/docs/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION) # for the prefered way to set configuration parameters. # # @return [Google::Cloud::Logging::Middleware] A new # Google::Cloud::Logging::Middleware instance # - def initialize app, logger: nil, **kwargs + def initialize app, logger: nil, on_init: nil, **kwargs @app = app load_config kwargs - if logger - @logger = logger - else + logger ||= Middleware.logger + logger ||= begin log_name = configuration.log_name - logging = Logging.new project_id: configuration.project_id, + logging = Logging.new project_id: configuration.project_id, credentials: configuration.credentials resource = Middleware.build_monitored_resource( configuration.monitored_resource.type, configuration.monitored_resource.labels ) - @logger = logging.logger log_name, resource + Middleware.logger = logging.logger log_name, resource end + + on_init.call if on_init + + @logger = logger end ## # Rack middleware entry point. In most Rack based frameworks, a request # is served by one thread. So entry point, we associate the GCP request @@ -183,10 +189,16 @@ else default_monitored_resource end end + class << self + ## + # @private Global logger object + attr_accessor :logger + end + ## # @private Extract information from current environment and construct # the correct monitoring resource types and labels. # # @return [Google::Cloud::Logging::Resource] An Resource object with @@ -224,22 +236,23 @@ # def self.default_monitored_resource type, labels = if Google::Cloud.env.app_engine? ["gae_app", { - module_id: Google::Cloud.env.app_engine_service_id, + module_id: Google::Cloud.env.app_engine_service_id, version_id: Google::Cloud.env.app_engine_service_version }] elsif Google::Cloud.env.container_engine? + namespace_id = Google::Cloud.env.container_engine_namespace_id + namespace_id ||= "default" ["container", { cluster_name: Google::Cloud.env.container_engine_cluster_name, - namespace_id: \ - Google::Cloud.env.container_engine_namespace_id || "default" + namespace_id: namespace_id }] elsif Google::Cloud.env.compute_engine? ["gce_instance", { instance_id: Google::Cloud.env.instance_name, - zone: Google::Cloud.env.instance_zone + zone: Google::Cloud.env.instance_zone }] else ["global", {}] end