1 // Copyright 2017, OpenCensus Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "golang.org/x/net/context"
23 "go.opencensus.io/tag"
24 "google.golang.org/grpc/grpclog"
25 "google.golang.org/grpc/stats"
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()
34 grpclog.Infof("opencensus: TagRPC called with nil info.")
40 method: info.FullMethodName,
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)
48 // extractPropagatedTags creates a new tag map containing the tags extracted from the
50 func (h *ServerHandler) extractPropagatedTags(ctx context.Context) *tag.Map {
51 buf := stats.Tags(ctx)
55 propagated, err := tag.Decode(buf)
58 grpclog.Warningf("opencensus: Failed to decode tags from gRPC metadata failed to decode: %v", err)