client.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. package ocgrpc
  15. import (
  16. "context"
  17. "go.opencensus.io/trace"
  18. "google.golang.org/grpc/stats"
  19. )
  20. // ClientHandler implements a gRPC stats.Handler for recording OpenCensus stats and
  21. // traces. Use with gRPC clients only.
  22. type ClientHandler struct {
  23. // StartOptions allows configuring the StartOptions used to create new spans.
  24. //
  25. // StartOptions.SpanKind will always be set to trace.SpanKindClient
  26. // for spans started by this handler.
  27. StartOptions trace.StartOptions
  28. }
  29. // HandleConn exists to satisfy gRPC stats.Handler.
  30. func (c *ClientHandler) HandleConn(ctx context.Context, cs stats.ConnStats) {
  31. // no-op
  32. }
  33. // TagConn exists to satisfy gRPC stats.Handler.
  34. func (c *ClientHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) context.Context {
  35. // no-op
  36. return ctx
  37. }
  38. // HandleRPC implements per-RPC tracing and stats instrumentation.
  39. func (c *ClientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
  40. traceHandleRPC(ctx, rs)
  41. statsHandleRPC(ctx, rs)
  42. }
  43. // TagRPC implements per-RPC context management.
  44. func (c *ClientHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
  45. ctx = c.traceTagRPC(ctx, rti)
  46. ctx = c.statsTagRPC(ctx, rti)
  47. return ctx
  48. }