Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / go.opencensus.io / plugin / ocgrpc / server_stats_handler.go
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
16 package ocgrpc
17
18 import (
19         "time"
20
21         "golang.org/x/net/context"
22
23         "go.opencensus.io/tag"
24         "google.golang.org/grpc/grpclog"
25         "google.golang.org/grpc/stats"
26 )
27
28 // statsTagRPC gets the metadata from gRPC context, extracts the encoded tags from
29 // it and creates a new tag.Map and puts them into the returned context.
30 func (h *ServerHandler) statsTagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
31         startTime := time.Now()
32         if info == nil {
33                 if grpclog.V(2) {
34                         grpclog.Infof("opencensus: TagRPC called with nil info.")
35                 }
36                 return ctx
37         }
38         d := &rpcData{
39                 startTime: startTime,
40                 method:    info.FullMethodName,
41         }
42         propagated := h.extractPropagatedTags(ctx)
43         ctx = tag.NewContext(ctx, propagated)
44         ctx, _ = tag.New(ctx, tag.Upsert(KeyServerMethod, methodName(info.FullMethodName)))
45         return context.WithValue(ctx, rpcDataKey, d)
46 }
47
48 // extractPropagatedTags creates a new tag map containing the tags extracted from the
49 // gRPC metadata.
50 func (h *ServerHandler) extractPropagatedTags(ctx context.Context) *tag.Map {
51         buf := stats.Tags(ctx)
52         if buf == nil {
53                 return nil
54         }
55         propagated, err := tag.Decode(buf)
56         if err != nil {
57                 if grpclog.V(2) {
58                         grpclog.Warningf("opencensus: Failed to decode tags from gRPC metadata failed to decode: %v", err)
59                 }
60                 return nil
61         }
62         return propagated
63 }