doc.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. /*
  4. Package trace provides an implementation of the tracing part of the
  5. OpenTelemetry API.
  6. To participate in distributed traces a Span needs to be created for the
  7. operation being performed as part of a traced workflow. In its simplest form:
  8. var tracer trace.Tracer
  9. func init() {
  10. tracer = otel.Tracer("instrumentation/package/name")
  11. }
  12. func operation(ctx context.Context) {
  13. var span trace.Span
  14. ctx, span = tracer.Start(ctx, "operation")
  15. defer span.End()
  16. // ...
  17. }
  18. A Tracer is unique to the instrumentation and is used to create Spans.
  19. Instrumentation should be designed to accept a TracerProvider from which it
  20. can create its own unique Tracer. Alternatively, the registered global
  21. TracerProvider from the go.opentelemetry.io/otel package can be used as
  22. a default.
  23. const (
  24. name = "instrumentation/package/name"
  25. version = "0.1.0"
  26. )
  27. type Instrumentation struct {
  28. tracer trace.Tracer
  29. }
  30. func NewInstrumentation(tp trace.TracerProvider) *Instrumentation {
  31. if tp == nil {
  32. tp = otel.TracerProvider()
  33. }
  34. return &Instrumentation{
  35. tracer: tp.Tracer(name, trace.WithInstrumentationVersion(version)),
  36. }
  37. }
  38. func operation(ctx context.Context, inst *Instrumentation) {
  39. var span trace.Span
  40. ctx, span = inst.tracer.Start(ctx, "operation")
  41. defer span.End()
  42. // ...
  43. }
  44. # API Implementations
  45. This package does not conform to the standard Go versioning policy; all of its
  46. interfaces may have methods added to them without a package major version bump.
  47. This non-standard API evolution could surprise an uninformed implementation
  48. author. They could unknowingly build their implementation in a way that would
  49. result in a runtime panic for their users that update to the new API.
  50. The API is designed to help inform an instrumentation author about this
  51. non-standard API evolution. It requires them to choose a default behavior for
  52. unimplemented interface methods. There are three behavior choices they can
  53. make:
  54. - Compilation failure
  55. - Panic
  56. - Default to another implementation
  57. All interfaces in this API embed a corresponding interface from
  58. [go.opentelemetry.io/otel/trace/embedded]. If an author wants the default
  59. behavior of their implementations to be a compilation failure, signaling to
  60. their users they need to update to the latest version of that implementation,
  61. they need to embed the corresponding interface from
  62. [go.opentelemetry.io/otel/trace/embedded] in their implementation. For
  63. example,
  64. import "go.opentelemetry.io/otel/trace/embedded"
  65. type TracerProvider struct {
  66. embedded.TracerProvider
  67. // ...
  68. }
  69. If an author wants the default behavior of their implementations to panic, they
  70. can embed the API interface directly.
  71. import "go.opentelemetry.io/otel/trace"
  72. type TracerProvider struct {
  73. trace.TracerProvider
  74. // ...
  75. }
  76. This option is not recommended. It will lead to publishing packages that
  77. contain runtime panics when users update to newer versions of
  78. [go.opentelemetry.io/otel/trace], which may be done with a transitive
  79. dependency.
  80. Finally, an author can embed another implementation in theirs. The embedded
  81. implementation will be used for methods not defined by the author. For example,
  82. an author who wants to default to silently dropping the call can use
  83. [go.opentelemetry.io/otel/trace/noop]:
  84. import "go.opentelemetry.io/otel/trace/noop"
  85. type TracerProvider struct {
  86. noop.TracerProvider
  87. // ...
  88. }
  89. It is strongly recommended that authors only embed
  90. [go.opentelemetry.io/otel/trace/noop] if they choose this default behavior.
  91. That implementation is the only one OpenTelemetry authors can guarantee will
  92. fully implement all the API interfaces when a user updates their API.
  93. */
  94. package trace // import "go.opentelemetry.io/otel/trace"