export.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2017, OpenCensus 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. package trace
  15. import (
  16. "sync"
  17. "sync/atomic"
  18. "time"
  19. )
  20. // Exporter is a type for functions that receive sampled trace spans.
  21. //
  22. // The ExportSpan method should be safe for concurrent use and should return
  23. // quickly; if an Exporter takes a significant amount of time to process a
  24. // SpanData, that work should be done on another goroutine.
  25. //
  26. // The SpanData should not be modified, but a pointer to it can be kept.
  27. type Exporter interface {
  28. ExportSpan(s *SpanData)
  29. }
  30. type exportersMap map[Exporter]struct{}
  31. var (
  32. exporterMu sync.Mutex
  33. exporters atomic.Value
  34. )
  35. // RegisterExporter adds to the list of Exporters that will receive sampled
  36. // trace spans.
  37. //
  38. // Binaries can register exporters, libraries shouldn't register exporters.
  39. func RegisterExporter(e Exporter) {
  40. exporterMu.Lock()
  41. new := make(exportersMap)
  42. if old, ok := exporters.Load().(exportersMap); ok {
  43. for k, v := range old {
  44. new[k] = v
  45. }
  46. }
  47. new[e] = struct{}{}
  48. exporters.Store(new)
  49. exporterMu.Unlock()
  50. }
  51. // UnregisterExporter removes from the list of Exporters the Exporter that was
  52. // registered with the given name.
  53. func UnregisterExporter(e Exporter) {
  54. exporterMu.Lock()
  55. new := make(exportersMap)
  56. if old, ok := exporters.Load().(exportersMap); ok {
  57. for k, v := range old {
  58. new[k] = v
  59. }
  60. }
  61. delete(new, e)
  62. exporters.Store(new)
  63. exporterMu.Unlock()
  64. }
  65. // SpanData contains all the information collected by a Span.
  66. type SpanData struct {
  67. SpanContext
  68. ParentSpanID SpanID
  69. SpanKind int
  70. Name string
  71. StartTime time.Time
  72. // The wall clock time of EndTime will be adjusted to always be offset
  73. // from StartTime by the duration of the span.
  74. EndTime time.Time
  75. // The values of Attributes each have type string, bool, or int64.
  76. Attributes map[string]interface{}
  77. Annotations []Annotation
  78. MessageEvents []MessageEvent
  79. Status
  80. Links []Link
  81. HasRemoteParent bool
  82. }