options.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package otgrpc
  2. import "github.com/opentracing/opentracing-go"
  3. // Option instances may be used in OpenTracing(Server|Client)Interceptor
  4. // initialization.
  5. //
  6. // See this post about the "functional options" pattern:
  7. // http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
  8. type Option func(o *options)
  9. // LogPayloads returns an Option that tells the OpenTracing instrumentation to
  10. // try to log application payloads in both directions.
  11. func LogPayloads() Option {
  12. return func(o *options) {
  13. o.logPayloads = true
  14. }
  15. }
  16. // SpanInclusionFunc provides an optional mechanism to decide whether or not
  17. // to trace a given gRPC call. Return true to create a Span and initiate
  18. // tracing, false to not create a Span and not trace.
  19. //
  20. // parentSpanCtx may be nil if no parent could be extraction from either the Go
  21. // context.Context (on the client) or the RPC (on the server).
  22. type SpanInclusionFunc func(
  23. parentSpanCtx opentracing.SpanContext,
  24. method string,
  25. req, resp interface{}) bool
  26. // IncludingSpans binds a IncludeSpanFunc to the options
  27. func IncludingSpans(inclusionFunc SpanInclusionFunc) Option {
  28. return func(o *options) {
  29. o.inclusionFunc = inclusionFunc
  30. }
  31. }
  32. // SpanDecoratorFunc provides an (optional) mechanism for otgrpc users to add
  33. // arbitrary tags/logs/etc to the opentracing.Span associated with client
  34. // and/or server RPCs.
  35. type SpanDecoratorFunc func(
  36. span opentracing.Span,
  37. method string,
  38. req, resp interface{},
  39. grpcError error)
  40. // SpanDecorator binds a function that decorates gRPC Spans.
  41. func SpanDecorator(decorator SpanDecoratorFunc) Option {
  42. return func(o *options) {
  43. o.decorator = decorator
  44. }
  45. }
  46. // The internal-only options struct. Obviously overkill at the moment; but will
  47. // scale well as production use dictates other configuration and tuning
  48. // parameters.
  49. type options struct {
  50. logPayloads bool
  51. decorator SpanDecoratorFunc
  52. // May be nil.
  53. inclusionFunc SpanInclusionFunc
  54. }
  55. // newOptions returns the default options.
  56. func newOptions() *options {
  57. return &options{
  58. logPayloads: false,
  59. inclusionFunc: nil,
  60. }
  61. }
  62. func (o *options) apply(opts ...Option) {
  63. for _, opt := range opts {
  64. opt(o)
  65. }
  66. }