export.go 2.2 KB

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