# google-cloud-debugger - Class Google::Cloud::Debugger::Middleware (v0.42.2)

Reference documentation and code samples for the google-cloud-debugger class Google::Cloud::Debugger::Middleware.

Rack Middleware implementation that supports Stackdriver Debugger Agent
in Rack-based Ruby frameworks. It instantiates a new debugger agent if
one isn't given already. It helps optimize Debugger Agent Tracer
performance by suspending and resuming the tracer between each request.


To use this middleware, simply install it in your Rack configuration.
The middleware will take care of registering itself with the
Stackdriver Debugger and activating the debugger agent. The location
of the middleware in the middleware stack matters: breakpoints will be
detected in middleware appearing after but not before this middleware.

<br />

For best results, you should also call [Middleware.start_agents](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-debugger/latest/Google-Cloud-Debugger-Middleware#Google__Cloud__Debugger__Middleware_start_agents_class_ "Google::Cloud::Debugger::Middleware.start_agents (method)")
during application initialization. See its documentation for details.

## Inherits

- Object

## Methods

### .start_agents

    def self.start_agents()

This should be called once the application determines that it is safe
to start background threads and open gRPC connections. It informs
the middleware system that it can start debugger agents.


Generally, this matters if the application forks worker processes;
this method should be called only after workers are forked, since
threads and network connections interact badly with fork. For
example, when running Puma in
[clustered mode](https://github.com/puma/puma#clustered-mode), this
method should be called in an `on_worker_boot` block.

If the application does no forking, this method can be called any
time early in the application initialization process.

<br />

If [Middleware.start_agents](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-debugger/latest/Google-Cloud-Debugger-Middleware#Google__Cloud__Debugger__Middleware_start_agents_class_ "Google::Cloud::Debugger::Middleware.start_agents (method)") is never called, the debugger agent will
be started when the first request is received. This should be safe,
but it will probably mean breakpoints will not be recognized during
that first request. For best results, an application should call this
method at the appropriate time.

### #call

    def call(env) -> Rack::Response

Rack middleware entry point. In most Rack based frameworks, a request
is served by one thread. It enables/resume the debugger breakpoints
tracing and stops/pauses the tracing afterwards to help improve
debugger performance.
**Parameter**

- **env** (Hash) --- Rack environment hash
**Returns**

- (Rack::Response) --- The response from downstream Rack app

### #initialize

    def initialize(app, debugger: nil, **kwargs) -> Google::Cloud::Debugger::Middleware

Create a new Debugger Middleware.
**Parameters**

- **app** (Rack Application) --- Rack application
- **debugger** ([Google::Cloud::Debugger::Project](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-debugger/latest/Google-Cloud-Debugger-Project)) *(defaults to: nil)* --- A debugger to be used by this middleware. If not given, will construct a new one using the other parameters.
- **kwargs** (Hash) --- Hash of configuration settings. Used for backward API compatibility. See the [Configuration
  Guide](https://googleapis.dev/ruby/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION.html) for the prefered way to set configuration parameters.
**Returns**

- ([Google::Cloud::Debugger::Middleware](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-debugger/latest/Google-Cloud-Debugger-Middleware)) --- A new Google::Cloud::Debugger::Middleware instance