Class: Google::Cloud::Logging::Middleware
- Inherits:
-
Object
- Object
- Google::Cloud::Logging::Middleware
- Defined in:
- lib/google/cloud/logging/middleware.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
The Google::Cloud::Logging::Logger instance.
Class Method Summary collapse
-
.build_monitored_resource(type = nil, labels = nil) ⇒ Google::Cloud::Logging::Resource
Construct a monitored resource based on the given type and label if both are provided.
Instance Method Summary collapse
-
#call(env) ⇒ Rack::Response
Rack middleware entry point.
-
#extract_trace_id(env) ⇒ String
Extract the trace_id from HTTP request header HTTP_X_CLOUD_TRACE_CONTEXT.
-
#initialize(app, logger: nil) ⇒ Google::Cloud::Logging::Middleware
constructor
Create a new AppEngine logging Middleware.
Constructor Details
#initialize(app, logger: nil) ⇒ Google::Cloud::Logging::Middleware
Create a new AppEngine logging Middleware.
36 37 38 39 |
# File 'lib/google/cloud/logging/middleware.rb', line 36 def initialize app, logger: nil @app = app @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
The Google::Cloud::Logging::Logger instance
22 23 24 |
# File 'lib/google/cloud/logging/middleware.rb', line 22 def logger @logger end |
Class Method Details
.build_monitored_resource(type = nil, labels = nil) ⇒ Google::Cloud::Logging::Resource
Construct a monitored resource based on the given type and label if both are provided. Otherwise, construct a default monitored resource based on the current environment.
Reference https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource for a full list of monitoring resources
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/google/cloud/logging/middleware.rb', line 119 def self.build_monitored_resource type = nil, labels = nil if type && labels Google::Cloud::Logging::Resource.new.tap do |r| r.type = type r.labels = labels end else default_monitored_resource end end |
Instance Method Details
#call(env) ⇒ Rack::Response
Rack middleware entry point. In most Rack based frameworks, a request is served by one thread. So entry point, we associate the GCP request trace_id with the current thread's object_id in logger. All the logs written by logger beyond this point will carry this request's trace_id. Untrack the trace_id with this thread upon exiting.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/google/cloud/logging/middleware.rb', line 52 def call env env["rack.logger"] = logger trace_id = extract_trace_id(env) logger.add_trace_id trace_id begin @app.call env ensure logger.delete_trace_id end end |
#extract_trace_id(env) ⇒ String
Extract the trace_id from HTTP request header HTTP_X_CLOUD_TRACE_CONTEXT.
69 70 71 72 73 |
# File 'lib/google/cloud/logging/middleware.rb', line 69 def extract_trace_id env trace_context = env["HTTP_X_CLOUD_TRACE_CONTEXT"].to_s return nil if trace_context.empty? trace_context.sub(%r{/.*}, "") end |