Package trace
Overview ▹
Index ▹
Constants
const ( // ScopeTraceAppend grants permissions to write trace data for a project. ScopeTraceAppend = "https://www.googleapis.com/auth/trace.append" // ScopeCloudPlatform grants permissions to view and manage your data // across Google Cloud Platform services. ScopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform" )
Variables
EnableGRPCTracing automatically traces all outgoing gRPC calls from cloud.google.com/go clients.
The functionality in gRPC that this relies on is currently experimental.
Deprecated: Use option.WithGRPCDialOption(grpc.WithUnaryInterceptor(GRPCClientInterceptor())) instead.
var EnableGRPCTracing option.ClientOption = option.WithGRPCDialOption(grpc.WithUnaryInterceptor(GRPCClientInterceptor()))
func GRPCClientInterceptor ¶
func GRPCClientInterceptor() grpc.UnaryClientInterceptor
GRPCClientInterceptor returns a grpc.UnaryClientInterceptor that traces all outgoing requests from a gRPC client. The calling context should already have a *trace.Span; a child span will be created for the outgoing gRPC call. If the calling context doesn't have a span, the call will not be traced.
The functionality in gRPC that this feature relies on is currently experimental.
func GRPCServerInterceptor ¶
func GRPCServerInterceptor(tc *Client) grpc.UnaryServerInterceptor
GRPCServerInterceptor returns a grpc.UnaryServerInterceptor that enables the tracing of the incoming gRPC calls. Incoming call's context can be used to extract the span on servers that enabled this option:
span := trace.FromContext(ctx)
The functionality in gRPC that this feature relies on is currently experimental.
func NewContext ¶
func NewContext(ctx context.Context, s *Span) context.Context
NewContext returns a derived context containing the span.
type Client ¶
Client is a client for uploading traces to the Google Stackdriver Trace server.
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error)
NewClient creates a new Google Stackdriver Trace client.
func (*Client) HTTPHandler ¶
func (c *Client) HTTPHandler(h http.Handler) http.Handler
HTTPHandler returns a http.Handler from the given handler that is aware of the incoming request's span. The span can be extracted from the incoming request in handler functions from incoming request's context:
span := trace.FromContext(r.Context())
The span will be auto finished by the handler.
▹ Example
func (*Client) NewHTTPClient ¶
func (c *Client) NewHTTPClient(orig *http.Client) *HTTPClient
NewHTTPClient creates a new HTTPClient that will trace the outgoing requests using tc. The attributes of this client are inherited from the given http.Client. If orig is nil, http.DefaultClient is used.
func (*Client) NewSpan ¶
func (c *Client) NewSpan(name string) *Span
NewSpan returns a new trace span with the given name.
A new trace and span ID is generated to trace the span. Returned span need to be finished by calling Finish or FinishWait.
func (*Client) SetSamplingPolicy ¶
func (c *Client) SetSamplingPolicy(p SamplingPolicy)
SetSamplingPolicy sets the SamplingPolicy that determines how often traces are initiated by this client.
func (*Client) SpanFromHeader ¶
func (c *Client) SpanFromHeader(name string, header string) *Span
SpanFromHeader returns a new trace span, based on a provided request header value. See https://cloud.google.com/trace/docs/faq.
It returns nil iff the client is nil.
The trace information and identifiers will be read from the header value. Otherwise, a new trace ID is made and the parent span ID is zero.
The name of the new span is provided as an argument.
If a non-nil sampling policy has been set in the client, it can override the options set in the header and choose whether to trace the request.
If the header doesn't have existing tracing information, then a *Span is returned anyway, but it will not be uploaded to the server, just as when calling SpanFromRequest on an untraced request.
Most users using HTTP should use SpanFromRequest, rather than SpanFromHeader, since it provides additional functionality for HTTP requests. In particular, it will set various pieces of request information as labels on the *Span, which is not available from the header alone.
func (*Client) SpanFromRequest ¶
func (c *Client) SpanFromRequest(r *http.Request) *Span
SpanFromRequest returns a new trace span for an HTTP request.
It returns nil iff the client is nil.
If the incoming HTTP request contains a trace context header, the trace ID, parent span ID, and tracing options will be read from that header. Otherwise, a new trace ID is made and the parent span ID is zero.
If a non-nil sampling policy has been set in the client, it can override the options set in the header and choose whether to trace the request.
If the request is not being traced, then a *Span is returned anyway, but it will not be uploaded to the server -- it is only useful for propagating trace context to child requests and for getting the TraceID. All its methods can still be called -- the Finish, FinishWait, and SetLabel methods do nothing. NewChild does nothing, and returns the same *Span. TraceID works as usual.
type Decision ¶
Decision is the value returned by a call to a SamplingPolicy's Sample method.
type Decision struct { Trace bool // Whether to trace the request. Sample bool // Whether the trace is included in the random sample. Policy string // Name of the sampling policy. Weight float64 // Sample weight to be used in statistical calculations. }
type FinishOption ¶
type FinishOption interface {
// contains filtered or unexported methods
}
func WithResponse ¶
func WithResponse(resp *http.Response) FinishOption
WithResponse returns an option that can be passed to Finish that indicates that some labels for the span should be set using the given *http.Response.
type HTTPClient ¶
HTTPClient is an HTTP client that enhances http.Client with automatic tracing support.
type HTTPClient struct {
http.Client
// contains filtered or unexported fields
}
func (*HTTPClient) Do ¶
func (c *HTTPClient) Do(req *http.Request) (*http.Response, error)
Do behaves like (*http.Client).Do but automatically traces outgoing requests if tracing is enabled for the current request.
If req.Context() contains a traced *Span, the outgoing request is traced with the existing span. If not, the request is not traced.
▹ Example
type Parameters ¶
Parameters contains the values passed to a SamplingPolicy's Sample method.
type Parameters struct {
HasTraceHeader bool // whether the incoming request has a valid X-Cloud-Trace-Context header.
}
type SamplingPolicy ¶
type SamplingPolicy interface { // Sample returns a Decision. // If Trace is false in the returned Decision, then the Decision should be // the zero value. Sample(p Parameters) Decision }
func NewLimitedSampler ¶
func NewLimitedSampler(fraction, maxqps float64) (SamplingPolicy, error)
NewLimitedSampler returns a sampling policy that randomly samples a given fraction of requests. It also enforces a limit on the number of traces per second. It tries to trace every request with a trace header, but will not exceed the qps limit to do it.
type Span ¶
Span contains information about one span of a trace.
type Span struct {
// contains filtered or unexported fields
}
func FromContext ¶
func FromContext(ctx context.Context) *Span
FromContext returns the span contained in the context, or nil.
func (*Span) Finish ¶
func (s *Span) Finish(opts ...FinishOption)
Finish declares that the span has finished.
If s is nil, Finish does nothing and returns nil.
If the option trace.WithResponse(resp) is passed, then some labels are set for s using information in the given *http.Response. This is useful when the span is for an outgoing http request; s will typically have been created by NewRemoteChild in this case.
If s is a root span (one created by SpanFromRequest) then s, and all its descendant spans that have finished, are uploaded to the Google Stackdriver Trace server asynchronously.
func (*Span) FinishWait ¶
func (s *Span) FinishWait(opts ...FinishOption) error
FinishWait is like Finish, but if s is a root span, it waits until uploading is finished, then returns an error if one occurred.
func (*Span) Header ¶
func (s *Span) Header() string
Header returns the value of the X-Cloud-Trace-Context header that should be used to propagate the span. This is the inverse of SpanFromHeader.
Most users should use NewRemoteChild unless they have specific propagation needs or want to control the naming of their span. Header() does not create a new span.
func (*Span) NewChild ¶
func (s *Span) NewChild(name string) *Span
NewChild creates a new span with the given name as a child of s. If s is nil, does nothing and returns nil.
func (*Span) NewRemoteChild ¶
func (s *Span) NewRemoteChild(r *http.Request) *Span
NewRemoteChild creates a new span as a child of s.
Some labels in the span are set from the outgoing *http.Request r.
A header is set in r so that the trace context is propagated to the destination. The parent span ID in that header is set as follows: - If the request is being traced, then the ID of s is used. - If the request is not being traced, but there was a trace context header
in the incoming request for this trace (the request passed to SpanFromRequest), the parent span ID in that header is used.
- Otherwise, the parent span ID is zero. The tracing bit in the options is set if tracing is enabled, or if it was set in the incoming request.
If s is nil, does nothing and returns nil.
func (*Span) SetLabel ¶
func (s *Span) SetLabel(key, value string)
SetLabel sets the label for the given key to the given value. If the value is empty, the label for that key is deleted. If a label is given a value automatically and by SetLabel, the automatically-set value is used. If s is nil, does nothing.
SetLabel shouldn't be called after Finish or FinishWait.
func (*Span) TraceID ¶
func (s *Span) TraceID() string
TraceID returns the ID of the trace to which s belongs.