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