Cloud Trace API - Module Google::Cloud::Trace (v0.43.0) Stay organized with collections Save and categorize content based on your preferences.
Reference documentation and code samples for the Cloud Trace API module Google::Cloud::Trace.
Stackdriver Trace
The Stackdriver Trace service collects and stores latency data from yourapplication and displays it in the Google Cloud Platform Console, givingyou detailed near-real-time insight into application performance.
Methods
.configure
defself.configure()->Google::Cloud::ConfigConfigure 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 parameterprojectisconsidered 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) (Theparameterkeyfileis considered deprecated, but may also be used.)scope- (String, Arraytimeout- (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:falsesampler- (Proc) A sampler Proc makes the decision whether to recorda trace for each request. Default:Google::Cloud::Trace::TimeSamplerspan_id_generator- (Proc) A generator Proc that generates the nameString for new TraceRecord. Default:random numbersnotifications- (Array) An array of ActiveSupport notification typesto include in traces. Rails-only option. Default:Google::Cloud::Trace::Railtie::DEFAULT_NOTIFICATIONSmax_data_length- (Integer) The maximum length of span propertiesrecorded with ActiveSupport notification events. Rails-only option.Default:Google::Cloud::Trace::Notifications::DEFAULT_MAX_DATA_LENGTHon_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.
- (Google::Cloud.configure.trace)
- (Google::Cloud::Config) — The configuration objectthe Google::Cloud::Trace module uses.
.get
defself.get()->Google::Cloud::Trace::TraceSpan,Google::Cloud::Trace::TraceRecord,nilRetrieve the current trace span or trace object for the current thread.This data should previously have been set usingTrace.set.
- (Google::Cloud::Trace::TraceSpan,Google::Cloud::Trace::TraceRecord, nil) — The span or trace object,or
nil.
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.
- 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
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::ProjectCreates 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.
- 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 the
project_idargument. Deprecated. - keyfile (String)(defaults to: nil) — Alias for the
credentialsargument.Deprecated.
- (ArgumentError)
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.
- trace (Google::Cloud::Trace::TraceSpan,Google::Cloud::Trace::TraceRecord, nil) — The current spanbeing measured, the current trace object, or
nilif none.
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.