wrap.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "github.com/acarl005/stripansi"
  17. "go.opentelemetry.io/otel/attribute"
  18. "go.opentelemetry.io/otel/codes"
  19. semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
  20. "go.opentelemetry.io/otel/trace"
  21. )
  22. // SpanWrapFunc wraps a function that takes a context with a trace.Span, marking the status as codes.Error if the
  23. // wrapped function returns an error.
  24. //
  25. // The context passed to the function is created from the span to ensure correct propagation.
  26. //
  27. // NOTE: This function is nearly identical to SpanWrapFuncForErrGroup, except the latter is designed specially for
  28. // convenience with errgroup.Group due to its prevalence throughout the codebase. The code is duplicated to avoid
  29. // adding even more levels of function wrapping/indirection.
  30. func SpanWrapFunc(spanName string, opts SpanOptions, fn func(ctx context.Context) error) func(context.Context) error {
  31. return func(ctx context.Context) error {
  32. ctx, span := Tracer.Start(ctx, spanName, opts.SpanStartOptions()...)
  33. defer span.End()
  34. if err := fn(ctx); err != nil {
  35. span.SetStatus(codes.Error, err.Error())
  36. return err
  37. }
  38. span.SetStatus(codes.Ok, "")
  39. return nil
  40. }
  41. }
  42. // SpanWrapFuncForErrGroup wraps a function that takes a context with a trace.Span, marking the status as codes.Error
  43. // if the wrapped function returns an error.
  44. //
  45. // The context passed to the function is created from the span to ensure correct propagation.
  46. //
  47. // NOTE: This function is nearly identical to SpanWrapFunc, except this function is designed specially for
  48. // convenience with errgroup.Group due to its prevalence throughout the codebase. The code is duplicated to avoid
  49. // adding even more levels of function wrapping/indirection.
  50. func SpanWrapFuncForErrGroup(ctx context.Context, spanName string, opts SpanOptions, fn func(ctx context.Context) error) func() error {
  51. return func() error {
  52. ctx, span := Tracer.Start(ctx, spanName, opts.SpanStartOptions()...)
  53. defer span.End()
  54. if err := fn(ctx); err != nil {
  55. span.SetStatus(codes.Error, err.Error())
  56. return err
  57. }
  58. span.SetStatus(codes.Ok, "")
  59. return nil
  60. }
  61. }
  62. // EventWrapFuncForErrGroup invokes a function and records an event, optionally including the returned
  63. // error as the "exception message" on the event.
  64. //
  65. // This is intended for lightweight usage to wrap errgroup.Group calls where a full span is not desired.
  66. func EventWrapFuncForErrGroup(ctx context.Context, eventName string, opts SpanOptions, fn func(ctx context.Context) error) func() error {
  67. return func() error {
  68. span := trace.SpanFromContext(ctx)
  69. eventOpts := opts.EventOptions()
  70. err := fn(ctx)
  71. if err != nil {
  72. eventOpts = append(eventOpts, trace.WithAttributes(semconv.ExceptionMessage(stripansi.Strip(err.Error()))))
  73. }
  74. span.AddEvent(eventName, eventOpts...)
  75. return err
  76. }
  77. }
  78. func AddAttributeToSpan(ctx context.Context, attr ...attribute.KeyValue) {
  79. span := trace.SpanFromContext(ctx)
  80. span.SetAttributes(attr...)
  81. }