# Cloud Trace API - Class Google::Cloud::Trace::Middleware (v0.46.0)

0.46.0 (latest) 0.45.0 0.44.1 0.43.0 0.42.2 0.41.4 Reference documentation and code samples for the Cloud Trace API class Google::Cloud::Trace::Middleware.

## Trace Middleware

A Rack middleware that manages trace context and captures a trace of
the request. Specifically, it:

<br />

- Reads the trace context from the request headers, if present. Otherwise, generates a new trace context.
- Makes a sampling decision if one is not already specified.
- Records a span measuring the entire handling of the request, annotated with a set of standard request data.
- Makes the trace context available so downstream middlewares and the app can add further spans to the trace.
- Sends the completed trace to the Stackdriver service.

### Installing

<br />

To use this middleware, simply install it in your middleware stack.
Here is an example Sinatra application that includes the Trace
middleware:

\`\`\`ruby

## Simple sinatra application

require "sinatra"
require "google/cloud/trace"

use Google::Cloud::Trace::Middleware

get "/" do
"Hello World!"
end
\`\`\`

Here is an example `config.ru` file for a web application that uses
the standard Rack configuration mechanism.

\`\`\`ruby

## config.ru for simple Rack application

require "google/cloud/trace"
use Google::Cloud::Trace::Middleware

run MyApp
\`\`\`

If your application uses Ruby On Rails, you may also use the provided
[Railtie](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-trace/latest/Google-Cloud-Trace-Railtie "Google::Cloud::Trace::Railtie (class)") for close integration with Rails and
ActiveRecord.

### Custom measurements

By default, this middleware creates traces that measure just the http
request handling as a whole. If you want to provide more detailed
measurements of smaller processes, use the classes provided in this
library. Below is a Sinatra example to get you started.

\`\`\`ruby

## Simple sinatra application

require "sinatra"
require "google/cloud/trace"

use Google::Cloud::Trace::Middleware

get "/" do
Google::Cloud::Trace.in_span "Sleeping on the job!" do
sleep rand
end
"Hello World!"
end
\`\`\`

### Error handling

An error encountered during the reporting of traces by the middleware
can be handled using a Proc set in the `on_error` configuration. (See
[configure](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-trace/latest/Google-Cloud-Trace#Google__Cloud__Trace_configure_class_ "Google::Cloud::Trace.configure (method)").) The Proc must take the error object
as the single argument.

\`\`\`ruby

## Configure error handling

require "sinatra"
require "google/cloud/trace"
require "google/cloud/error_reporting"

Google::Cloud::Trace.configure do \|config\|
config.on_error = lambda do \|error\|
Google::Cloud::ErrorReporting.report error
end
end

use Google::Cloud::Trace::Middleware

get "/" do
Google::Cloud::Trace.in_span "Sleeping on the job!" do
sleep rand
end
"Hello World!"
end
\`\`\`

### Sampling and blacklisting

A sampler makes the decision whether to record a trace for each
request (if the decision was not made by the context, e.g. by providing
a request header). By default, this sampler is the default
[TimeSampler](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-trace/latest/Google-Cloud-Trace-TimeSampler "Google::Cloud::Trace::TimeSampler (class)"), which enforces a maximum QPS per
process, and blacklists a small number of request paths such as
health checks sent by Google App Engine. You may adjust this behavior
by providing an alternate sampler. See
[TimeSampler](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-trace/latest/Google-Cloud-Trace-TimeSampler "Google::Cloud::Trace::TimeSampler (class)").

## Inherits

- Object

## Methods

### #call

    def call(env) -> Rack::Response

Implementation of the trace middleware. Creates a trace for this
request, populates it with a root span for the entire request, and
ensures it is reported back to Stackdriver.
**Parameter**

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

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

### #initialize

    def initialize(app, service: nil, **kwargs) -> Middleware

Create a new Middleware for traces
**Parameters**

- **app** (Rack Application) --- Rack application
- **service** (Google::Cloud::Trace::Service, AsyncReporter) *(defaults to: nil)* --- The service object to update traces. Optional if running on GCE.
- **kwargs** (Hash) --- Hash of configuration settings. Used for backward API compatibility. See the [Instrumentation Guide](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-trace/latest/INSTRUMENTATION "Instrumentation Guide") and [Configuration
  Guide](https://googleapis.dev/ruby/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION.html) for the prefered way to set configuration parameters.
**Returns**

- ([Middleware](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-trace/latest/Google-Cloud-Trace-Middleware)) --- a new instance of Middleware

## Constants

### AGENT_NAME

**value:** "ruby #[VERSION](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-trace/latest/Google-Cloud-Trace#Google__Cloud__Trace__VERSION "Google::Cloud::Trace::VERSION (constant)")".freeze  
The name of this trace agent as reported to the Stackdriver backend.