client_stats_handler.go 1.4 KB

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