wrap.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2020 Docker Compose CLI 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
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package tracing
  14. import (
  15. "context"
  16. "go.opentelemetry.io/otel/codes"
  17. semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
  18. "go.opentelemetry.io/otel/trace"
  19. )
  20. // SpanWrapFunc wraps a function that takes a context with a trace.Span, marking the status as codes.Error if the
  21. // wrapped function returns an error.
  22. //
  23. // The context passed to the function is created from the span to ensure correct propagation.
  24. //
  25. // NOTE: This function is nearly identical to SpanWrapFuncForErrGroup, except the latter is designed specially for
  26. // convenience with errgroup.Group due to its prevalence throughout the codebase. The code is duplicated to avoid
  27. // adding even more levels of function wrapping/indirection.
  28. func SpanWrapFunc(spanName string, opts SpanOptions, fn func(ctx context.Context) error) func(context.Context) error {
  29. return func(ctx context.Context) error {
  30. ctx, span := Tracer.Start(ctx, spanName, opts.SpanStartOptions()...)
  31. defer span.End()
  32. if err := fn(ctx); err != nil {
  33. span.SetStatus(codes.Error, err.Error())
  34. return err
  35. }
  36. span.SetStatus(codes.Ok, "")
  37. return nil
  38. }
  39. }
  40. // SpanWrapFuncForErrGroup wraps a function that takes a context with a trace.Span, marking the status as codes.Error
  41. // if the wrapped function returns an error.
  42. //
  43. // The context passed to the function is created from the span to ensure correct propagation.
  44. //
  45. // NOTE: This function is nearly identical to SpanWrapFunc, except this function is designed specially for
  46. // convenience with errgroup.Group due to its prevalence throughout the codebase. The code is duplicated to avoid
  47. // adding even more levels of function wrapping/indirection.
  48. func SpanWrapFuncForErrGroup(ctx context.Context, spanName string, opts SpanOptions, fn func(ctx context.Context) error) func() error {
  49. return func() error {
  50. ctx, span := Tracer.Start(ctx, spanName, opts.SpanStartOptions()...)
  51. defer span.End()
  52. if err := fn(ctx); err != nil {
  53. span.SetStatus(codes.Error, err.Error())
  54. return err
  55. }
  56. span.SetStatus(codes.Ok, "")
  57. return nil
  58. }
  59. }
  60. // EventWrapFuncForErrGroup invokes a function and records an event, optionally including the returned
  61. // error as the "exception message" on the event.
  62. //
  63. // This is intended for lightweight usage to wrap errgroup.Group calls where a full span is not desired.
  64. func EventWrapFuncForErrGroup(ctx context.Context, eventName string, opts SpanOptions, fn func(ctx context.Context) error) func() error {
  65. return func() error {
  66. span := trace.SpanFromContext(ctx)
  67. eventOpts := opts.EventOptions()
  68. err := fn(ctx)
  69. if err != nil {
  70. eventOpts = append(eventOpts, trace.WithAttributes(semconv.ExceptionMessage(err.Error())))
  71. }
  72. span.AddEvent(eventName, eventOpts...)
  73. return err
  74. }
  75. }