github.com/xmidt-org/webpa-common@v1.11.9/tracing/tracinghttp/http.go (about) 1 package tracinghttp 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 "time" 8 9 gokithttp "github.com/go-kit/kit/transport/http" 10 "github.com/xmidt-org/webpa-common/tracing" 11 ) 12 13 const ( 14 SpanHeader = "X-Xmidt-Span" 15 ErrorHeader = "X-Xmidt-Error" 16 ) 17 18 // HeadersForSpans emits header information for each Span. The timeLayout may be empty, in which case time.RFC3339 is used. 19 // All times are converted to UTC prior to formatting. 20 func HeadersForSpans(timeLayout string, h http.Header, spans ...tracing.Span) { 21 if len(timeLayout) == 0 { 22 timeLayout = time.RFC3339 23 } 24 25 output := new(bytes.Buffer) 26 for _, s := range spans { 27 output.Reset() 28 fmt.Fprintf(output, `"%s","%s","%s"`, s.Name(), s.Start().UTC().Format(timeLayout), s.Duration()) 29 h.Add(SpanHeader, output.String()) 30 31 if err := s.Error(); err != nil { 32 output.Reset() 33 if coder, ok := err.(gokithttp.StatusCoder); ok { 34 fmt.Fprintf(output, `"%s",%d,"%s"`, s.Name(), coder.StatusCode(), err.Error()) 35 } else { 36 fmt.Fprintf(output, `"%s",,"%s"`, s.Name(), err.Error()) 37 } 38 39 h.Add(ErrorHeader, output.String()) 40 } 41 } 42 }