doc.go 4.2 KB

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