1// Copyright 2017, 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//
15
16package ocgrpc
17
18import (
19	"context"
20	"time"
21
22	"go.opencensus.io/tag"
23	"google.golang.org/grpc/grpclog"
24	"google.golang.org/grpc/stats"
25)
26
27// statsTagRPC gets the tag.Map populated by the application code, serializes
28// its tags into the GRPC metadata in order to be sent to the server.
29func (h *ClientHandler) statsTagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
30	startTime := time.Now()
31	if info == nil {
32		if grpclog.V(2) {
33			grpclog.Info("clientHandler.TagRPC called with nil info.")
34		}
35		return ctx
36	}
37
38	d := &rpcData{
39		startTime: startTime,
40		method:    info.FullMethodName,
41	}
42	ts := tag.FromContext(ctx)
43	if ts != nil {
44		encoded := tag.Encode(ts)
45		ctx = stats.SetTags(ctx, encoded)
46	}
47
48	return context.WithValue(ctx, rpcDataKey, d)
49}
50