Cloud Trace API - Module Google::Cloud::Trace (v0.43.0)

Methods

.configure

defself.configure()->Google::Cloud::Config

Configure the Stackdriver Trace instrumentation Middleware.

The following Stackdriver Trace configuration parameters aresupported:

  • project_id - (String) Project identifier for the StackdriverTrace service you are connecting to. (The parameterproject isconsidered deprecated, but may also be used.)
  • credentials - (String, Hash, Google::Auth::Credentials) The path tothe keyfile as a String, the contents of the keyfile as a Hash, or aGoogle::Auth::Credentials object. (SeeCredentials) (Theparameterkeyfile is considered deprecated, but may also be used.)
  • scope - (String, Array
  • timeout - (Integer) Default timeout to use in requests.
  • endpoint - (String) Override of the endpoint host name, ornilto use the default endpoint.
  • capture_stack - (Boolean) Whether to capture stack traces for eachspan. Default:false
  • sampler - (Proc) A sampler Proc makes the decision whether to recorda trace for each request. Default:Google::Cloud::Trace::TimeSampler
  • span_id_generator - (Proc) A generator Proc that generates the nameString for new TraceRecord. Default:random numbers
  • notifications - (Array) An array of ActiveSupport notification typesto include in traces. Rails-only option. Default:Google::Cloud::Trace::Railtie::DEFAULT_NOTIFICATIONS
  • max_data_length - (Integer) The maximum length of span propertiesrecorded with ActiveSupport notification events. Rails-only option.Default:Google::Cloud::Trace::Notifications::DEFAULT_MAX_DATA_LENGTH
  • on_error - (Proc) A Proc to be run when an error is encounteredduring the reporting of traces by the middleware. The Proc must takethe error object as the single argument.

See theConfiguration Guide for fullconfiguration parameters.

Yields
  • (Google::Cloud.configure.trace)
Returns
  • (Google::Cloud::Config) — The configuration objectthe Google::Cloud::Trace module uses.

.get

defself.get()->Google::Cloud::Trace::TraceSpan,Google::Cloud::Trace::TraceRecord,nil

Retrieve the current trace span or trace object for the current thread.This data should previously have been set usingTrace.set.

Returns
Example
require"google/cloud/trace"trace_client=Google::Cloud::Trace.newtrace=trace_client.new_traceGoogle::Cloud::Trace.settrace# Later...Google::Cloud::Trace.get.create_span"my_span"

.in_span

defself.in_span(name,kind:Google::Cloud::Trace::SpanKind::UNSPECIFIED,labels:{})

Open a new span for the current thread, instrumenting the given block.The span is created within the current thread's trace context as set byTrace.set. The context is updated so any further callswithin the block will create subspans. The new span is also yielded tothe block.

Does nothing if there is no trace context for the current thread.

Parameters
  • name (String) — Name of the span to create
  • kind (Google::Cloud::Trace::SpanKind)(defaults to: Google::Cloud::Trace::SpanKind::UNSPECIFIED) — Kind of span to create.Optional.
  • labels (Hash{String => String})(defaults to: {}) — Labels for the span
Example
require"google/cloud/trace"trace_client=Google::Cloud::Trace.newtrace=trace_client.new_traceGoogle::Cloud::Trace.settraceGoogle::Cloud::Trace.in_span"my_span"do|span|span.labels["foo"]="bar"# Do stuff...Google::Cloud::Trace.in_span"my_subspan"do|subspan|subspan.labels["foo"]="sub-bar"# Do other stuff...endend

.new

defself.new(project_id:nil,credentials:nil,scope:nil,timeout:nil,endpoint:nil,project:nil,keyfile:nil)->Google::Cloud::Trace::Project

Creates a new object for connecting to the Stackdriver Trace service.Each call creates a new connection.

For more information on connecting to Google Cloud see theAuthentication Guide.

Parameters
  • project_id (String)(defaults to: nil) — Project identifier for the Stackdriver Traceservice you are connecting to. If not present, the default project forthe credentials is used.
  • credentials (String, Hash, Google::Auth::Credentials)(defaults to: nil) — The path tothe keyfile as a String, the contents of the keyfile as a Hash, or aGoogle::Auth::Credentials object. (SeeCredentials)
  • scope (String, Array<String>)(defaults to: nil)

    The OAuth 2.0 scopes controllingthe set of resources and operations that the connection can access.SeeUsing OAuth 2.0 to Access GoogleAPIs.

    The default scope is:

    • https://www.googleapis.com/auth/cloud-platform
  • timeout (Integer)(defaults to: nil) — Default timeout to use in requests. Optional.
  • endpoint (String)(defaults to: nil) — Override of the endpoint host name. Optional.If the param is nil, uses the default endpoint.
  • project (String)(defaults to: nil) — Alias for theproject_id argument. Deprecated.
  • keyfile (String)(defaults to: nil) — Alias for thecredentials argument.Deprecated.
Raises
  • (ArgumentError)
Example
require"google/cloud/trace"trace_client=Google::Cloud::Trace.newtraces=trace_client.list_tracesTime.now-3600,Time.nowtraces.eachdo|trace|puts"Retrieved trace ID:#{trace.trace_id}"end

.set

defself.set(trace)

Set the current trace span being measured for the current thread, orthe current trace if no span is currently open. This may be used withweb frameworks that assign a thread to each request, to track thetrace instrumentation state for the request being handled. You may useTrace.get to retrieve the data.

Parameter
Example
require"google/cloud/trace"trace_client=Google::Cloud::Trace.newtrace=trace_client.new_traceGoogle::Cloud::Trace.settrace# Later...Google::Cloud::Trace.get.create_span"my_span"

Constants

THREAD_KEY

value::stackdriver_trace_span

VERSION

value:"0.43.0".freeze

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.