doc.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. This package is currently in a Release Candidate phase. Backwards incompatible changes
  18. may be introduced prior to v1.0.0, but we believe the current API is ready to stabilize.
  19. To participate in distributed traces a Span needs to be created for the
  20. operation being performed as part of a traced workflow. It its simplest form:
  21. var tracer trace.Tracer
  22. func init() {
  23. tracer = otel.Tracer("instrumentation/package/name")
  24. }
  25. func operation(ctx context.Context) {
  26. var span trace.Span
  27. ctx, span = tracer.Start(ctx, "operation")
  28. defer span.End()
  29. // ...
  30. }
  31. A Tracer is unique to the instrumentation and is used to create Spans.
  32. Instrumentation should be designed to accept a TracerProvider from which it
  33. can create its own unique Tracer. Alternatively, the registered global
  34. TracerProvider from the go.opentelemetry.io/otel package can be used as
  35. a default.
  36. const (
  37. name = "instrumentation/package/name"
  38. version = "0.1.0"
  39. )
  40. type Instrumentation struct {
  41. tracer trace.Tracer
  42. }
  43. func NewInstrumentation(tp trace.TracerProvider) *Instrumentation {
  44. if tp == nil {
  45. tp = otel.TracerProvider()
  46. }
  47. return &Instrumentation{
  48. tracer: tp.Tracer(name, trace.WithInstrumentationVersion(version)),
  49. }
  50. }
  51. func operation(ctx context.Context, inst *Instrumentation) {
  52. var span trace.Span
  53. ctx, span = inst.tracer.Start(ctx, "operation")
  54. defer span.End()
  55. // ...
  56. }
  57. */
  58. package trace // import "go.opentelemetry.io/otel/trace"