選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

81 行
2.9 KiB

  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. "go.opencensus.io/trace"
  17. "golang.org/x/net/context"
  18. "google.golang.org/grpc/stats"
  19. )
  20. // ServerHandler implements gRPC stats.Handler recording OpenCensus stats and
  21. // traces. Use with gRPC servers.
  22. //
  23. // When installed (see Example), tracing metadata is read from inbound RPCs
  24. // by default. If no tracing metadata is present, or if the tracing metadata is
  25. // present but the SpanContext isn't sampled, then a new trace may be started
  26. // (as determined by Sampler).
  27. type ServerHandler struct {
  28. // IsPublicEndpoint may be set to true to always start a new trace around
  29. // each RPC. Any SpanContext in the RPC metadata will be added as a linked
  30. // span instead of making it the parent of the span created around the
  31. // server RPC.
  32. //
  33. // Be aware that if you leave this false (the default) on a public-facing
  34. // server, callers will be able to send tracing metadata in gRPC headers
  35. // and trigger traces in your backend.
  36. IsPublicEndpoint bool
  37. // StartOptions to use for to spans started around RPCs handled by this server.
  38. //
  39. // These will apply even if there is tracing metadata already
  40. // present on the inbound RPC but the SpanContext is not sampled. This
  41. // ensures that each service has some opportunity to be traced. If you would
  42. // like to not add any additional traces for this gRPC service, set:
  43. //
  44. // StartOptions.Sampler = trace.ProbabilitySampler(0.0)
  45. //
  46. // StartOptions.SpanKind will always be set to trace.SpanKindServer
  47. // for spans started by this handler.
  48. StartOptions trace.StartOptions
  49. }
  50. var _ stats.Handler = (*ServerHandler)(nil)
  51. // HandleConn exists to satisfy gRPC stats.Handler.
  52. func (s *ServerHandler) HandleConn(ctx context.Context, cs stats.ConnStats) {
  53. // no-op
  54. }
  55. // TagConn exists to satisfy gRPC stats.Handler.
  56. func (s *ServerHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) context.Context {
  57. // no-op
  58. return ctx
  59. }
  60. // HandleRPC implements per-RPC tracing and stats instrumentation.
  61. func (s *ServerHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
  62. traceHandleRPC(ctx, rs)
  63. statsHandleRPC(ctx, rs)
  64. }
  65. // TagRPC implements per-RPC context management.
  66. func (s *ServerHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
  67. ctx = s.traceTagRPC(ctx, rti)
  68. ctx = s.statsTagRPC(ctx, rti)
  69. return ctx
  70. }