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.
Instance Method Summary collapse
-
#call(env) ⇒ Object
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.
37 38 39 40 |
# File 'lib/google/cloud/logging/middleware.rb', line 37 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 |
Instance Method Details
#call(env) ⇒ Object
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.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/google/cloud/logging/middleware.rb', line 53 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.
70 71 72 73 74 |
# File 'lib/google/cloud/logging/middleware.rb', line 70 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 |