1// Copyright 2018, OpenCensus Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package ocgrpc
16
17import (
18	"go.opencensus.io/trace"
19	"golang.org/x/net/context"
20
21	"google.golang.org/grpc/stats"
22)
23
24// ClientHandler implements a gRPC stats.Handler for recording OpenCensus stats and
25// traces. Use with gRPC clients only.
26type ClientHandler struct {
27	// StartOptions allows configuring the StartOptions used to create new spans.
28	//
29	// StartOptions.SpanKind will always be set to trace.SpanKindClient
30	// for spans started by this handler.
31	StartOptions trace.StartOptions
32}
33
34// HandleConn exists to satisfy gRPC stats.Handler.
35func (c *ClientHandler) HandleConn(ctx context.Context, cs stats.ConnStats) {
36	// no-op
37}
38
39// TagConn exists to satisfy gRPC stats.Handler.
40func (c *ClientHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) context.Context {
41	// no-op
42	return ctx
43}
44
45// HandleRPC implements per-RPC tracing and stats instrumentation.
46func (c *ClientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
47	traceHandleRPC(ctx, rs)
48	statsHandleRPC(ctx, rs)
49}
50
51// TagRPC implements per-RPC context management.
52func (c *ClientHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
53	ctx = c.traceTagRPC(ctx, rti)
54	ctx = c.statsTagRPC(ctx, rti)
55	return ctx
56}
57