doc.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. */
  56. package trace // import "go.opentelemetry.io/otel/trace"