event.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package event
  5. import (
  6. "context"
  7. "golang.org/x/tools/internal/event/core"
  8. "golang.org/x/tools/internal/event/keys"
  9. "golang.org/x/tools/internal/event/label"
  10. )
  11. // Exporter is a function that handles events.
  12. // It may return a modified context and event.
  13. type Exporter func(context.Context, core.Event, label.Map) context.Context
  14. // SetExporter sets the global exporter function that handles all events.
  15. // The exporter is called synchronously from the event call site, so it should
  16. // return quickly so as not to hold up user code.
  17. func SetExporter(e Exporter) {
  18. core.SetExporter(core.Exporter(e))
  19. }
  20. // Log takes a message and a label list and combines them into a single event
  21. // before delivering them to the exporter.
  22. func Log(ctx context.Context, message string, labels ...label.Label) {
  23. core.Export(ctx, core.MakeEvent([3]label.Label{
  24. keys.Msg.Of(message),
  25. }, labels))
  26. }
  27. // IsLog returns true if the event was built by the Log function.
  28. // It is intended to be used in exporters to identify the semantics of the
  29. // event when deciding what to do with it.
  30. func IsLog(ev core.Event) bool {
  31. return ev.Label(0).Key() == keys.Msg
  32. }
  33. // Error takes a message and a label list and combines them into a single event
  34. // before delivering them to the exporter. It captures the error in the
  35. // delivered event.
  36. func Error(ctx context.Context, message string, err error, labels ...label.Label) {
  37. core.Export(ctx, core.MakeEvent([3]label.Label{
  38. keys.Msg.Of(message),
  39. keys.Err.Of(err),
  40. }, labels))
  41. }
  42. // IsError returns true if the event was built by the Error function.
  43. // It is intended to be used in exporters to identify the semantics of the
  44. // event when deciding what to do with it.
  45. func IsError(ev core.Event) bool {
  46. return ev.Label(0).Key() == keys.Msg &&
  47. ev.Label(1).Key() == keys.Err
  48. }
  49. // Metric sends a label event to the exporter with the supplied labels.
  50. func Metric(ctx context.Context, labels ...label.Label) {
  51. core.Export(ctx, core.MakeEvent([3]label.Label{
  52. keys.Metric.New(),
  53. }, labels))
  54. }
  55. // IsMetric returns true if the event was built by the Metric function.
  56. // It is intended to be used in exporters to identify the semantics of the
  57. // event when deciding what to do with it.
  58. func IsMetric(ev core.Event) bool {
  59. return ev.Label(0).Key() == keys.Metric
  60. }
  61. // Label sends a label event to the exporter with the supplied labels.
  62. func Label(ctx context.Context, labels ...label.Label) context.Context {
  63. return core.Export(ctx, core.MakeEvent([3]label.Label{
  64. keys.Label.New(),
  65. }, labels))
  66. }
  67. // IsLabel returns true if the event was built by the Label function.
  68. // It is intended to be used in exporters to identify the semantics of the
  69. // event when deciding what to do with it.
  70. func IsLabel(ev core.Event) bool {
  71. return ev.Label(0).Key() == keys.Label
  72. }
  73. // Start sends a span start event with the supplied label list to the exporter.
  74. // It also returns a function that will end the span, which should normally be
  75. // deferred.
  76. func Start(ctx context.Context, name string, labels ...label.Label) (context.Context, func()) {
  77. return core.ExportPair(ctx,
  78. core.MakeEvent([3]label.Label{
  79. keys.Start.Of(name),
  80. }, labels),
  81. core.MakeEvent([3]label.Label{
  82. keys.End.New(),
  83. }, nil))
  84. }
  85. // IsStart returns true if the event was built by the Start function.
  86. // It is intended to be used in exporters to identify the semantics of the
  87. // event when deciding what to do with it.
  88. func IsStart(ev core.Event) bool {
  89. return ev.Label(0).Key() == keys.Start
  90. }
  91. // IsEnd returns true if the event was built by the End function.
  92. // It is intended to be used in exporters to identify the semantics of the
  93. // event when deciding what to do with it.
  94. func IsEnd(ev core.Event) bool {
  95. return ev.Label(0).Key() == keys.End
  96. }
  97. // Detach returns a context without an associated span.
  98. // This allows the creation of spans that are not children of the current span.
  99. func Detach(ctx context.Context) context.Context {
  100. return core.Export(ctx, core.MakeEvent([3]label.Label{
  101. keys.Detach.New(),
  102. }, nil))
  103. }
  104. // IsDetach returns true if the event was built by the Detach function.
  105. // It is intended to be used in exporters to identify the semantics of the
  106. // event when deciding what to do with it.
  107. func IsDetach(ev core.Event) bool {
  108. return ev.Label(0).Key() == keys.Detach
  109. }