server.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Copyright 2018, 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 ochttp
  15. import (
  16. "context"
  17. "io"
  18. "net/http"
  19. "strconv"
  20. "sync"
  21. "time"
  22. "go.opencensus.io/stats"
  23. "go.opencensus.io/tag"
  24. "go.opencensus.io/trace"
  25. "go.opencensus.io/trace/propagation"
  26. )
  27. // Handler is an http.Handler wrapper to instrument your HTTP server with
  28. // OpenCensus. It supports both stats and tracing.
  29. //
  30. // Tracing
  31. //
  32. // This handler is aware of the incoming request's span, reading it from request
  33. // headers as configured using the Propagation field.
  34. // The extracted span can be accessed from the incoming request's
  35. // context.
  36. //
  37. // span := trace.FromContext(r.Context())
  38. //
  39. // The server span will be automatically ended at the end of ServeHTTP.
  40. type Handler struct {
  41. // Propagation defines how traces are propagated. If unspecified,
  42. // B3 propagation will be used.
  43. Propagation propagation.HTTPFormat
  44. // Handler is the handler used to handle the incoming request.
  45. Handler http.Handler
  46. // StartOptions are applied to the span started by this Handler around each
  47. // request.
  48. //
  49. // StartOptions.SpanKind will always be set to trace.SpanKindServer
  50. // for spans started by this transport.
  51. StartOptions trace.StartOptions
  52. // GetStartOptions allows to set start options per request. If set,
  53. // StartOptions is going to be ignored.
  54. GetStartOptions func(*http.Request) trace.StartOptions
  55. // IsPublicEndpoint should be set to true for publicly accessible HTTP(S)
  56. // servers. If true, any trace metadata set on the incoming request will
  57. // be added as a linked trace instead of being added as a parent of the
  58. // current trace.
  59. IsPublicEndpoint bool
  60. // FormatSpanName holds the function to use for generating the span name
  61. // from the information found in the incoming HTTP Request. By default the
  62. // name equals the URL Path.
  63. FormatSpanName func(*http.Request) string
  64. }
  65. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  66. var tags addedTags
  67. r, traceEnd := h.startTrace(w, r)
  68. defer traceEnd()
  69. w, statsEnd := h.startStats(w, r)
  70. defer statsEnd(&tags)
  71. handler := h.Handler
  72. if handler == nil {
  73. handler = http.DefaultServeMux
  74. }
  75. r = r.WithContext(context.WithValue(r.Context(), addedTagsKey{}, &tags))
  76. handler.ServeHTTP(w, r)
  77. }
  78. func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Request, func()) {
  79. if isHealthEndpoint(r.URL.Path) {
  80. return r, func() {}
  81. }
  82. var name string
  83. if h.FormatSpanName == nil {
  84. name = spanNameFromURL(r)
  85. } else {
  86. name = h.FormatSpanName(r)
  87. }
  88. ctx := r.Context()
  89. startOpts := h.StartOptions
  90. if h.GetStartOptions != nil {
  91. startOpts = h.GetStartOptions(r)
  92. }
  93. var span *trace.Span
  94. sc, ok := h.extractSpanContext(r)
  95. if ok && !h.IsPublicEndpoint {
  96. ctx, span = trace.StartSpanWithRemoteParent(ctx, name, sc,
  97. trace.WithSampler(startOpts.Sampler),
  98. trace.WithSpanKind(trace.SpanKindServer))
  99. } else {
  100. ctx, span = trace.StartSpan(ctx, name,
  101. trace.WithSampler(startOpts.Sampler),
  102. trace.WithSpanKind(trace.SpanKindServer),
  103. )
  104. if ok {
  105. span.AddLink(trace.Link{
  106. TraceID: sc.TraceID,
  107. SpanID: sc.SpanID,
  108. Type: trace.LinkTypeParent,
  109. Attributes: nil,
  110. })
  111. }
  112. }
  113. span.AddAttributes(requestAttrs(r)...)
  114. if r.Body == nil {
  115. // TODO: Handle cases where ContentLength is not set.
  116. } else if r.ContentLength > 0 {
  117. span.AddMessageReceiveEvent(0, /* TODO: messageID */
  118. int64(r.ContentLength), -1)
  119. }
  120. return r.WithContext(ctx), span.End
  121. }
  122. func (h *Handler) extractSpanContext(r *http.Request) (trace.SpanContext, bool) {
  123. if h.Propagation == nil {
  124. return defaultFormat.SpanContextFromRequest(r)
  125. }
  126. return h.Propagation.SpanContextFromRequest(r)
  127. }
  128. func (h *Handler) startStats(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, func(tags *addedTags)) {
  129. ctx, _ := tag.New(r.Context(),
  130. tag.Upsert(Host, r.Host),
  131. tag.Upsert(Path, r.URL.Path),
  132. tag.Upsert(Method, r.Method))
  133. track := &trackingResponseWriter{
  134. start: time.Now(),
  135. ctx: ctx,
  136. writer: w,
  137. }
  138. if r.Body == nil {
  139. // TODO: Handle cases where ContentLength is not set.
  140. track.reqSize = -1
  141. } else if r.ContentLength > 0 {
  142. track.reqSize = r.ContentLength
  143. }
  144. stats.Record(ctx, ServerRequestCount.M(1))
  145. return track.wrappedResponseWriter(), track.end
  146. }
  147. type trackingResponseWriter struct {
  148. ctx context.Context
  149. reqSize int64
  150. respSize int64
  151. start time.Time
  152. statusCode int
  153. statusLine string
  154. endOnce sync.Once
  155. writer http.ResponseWriter
  156. }
  157. // Compile time assertion for ResponseWriter interface
  158. var _ http.ResponseWriter = (*trackingResponseWriter)(nil)
  159. var logTagsErrorOnce sync.Once
  160. func (t *trackingResponseWriter) end(tags *addedTags) {
  161. t.endOnce.Do(func() {
  162. if t.statusCode == 0 {
  163. t.statusCode = 200
  164. }
  165. span := trace.FromContext(t.ctx)
  166. span.SetStatus(TraceStatus(t.statusCode, t.statusLine))
  167. span.AddAttributes(trace.Int64Attribute(StatusCodeAttribute, int64(t.statusCode)))
  168. m := []stats.Measurement{
  169. ServerLatency.M(float64(time.Since(t.start)) / float64(time.Millisecond)),
  170. ServerResponseBytes.M(t.respSize),
  171. }
  172. if t.reqSize >= 0 {
  173. m = append(m, ServerRequestBytes.M(t.reqSize))
  174. }
  175. allTags := make([]tag.Mutator, len(tags.t)+1)
  176. allTags[0] = tag.Upsert(StatusCode, strconv.Itoa(t.statusCode))
  177. copy(allTags[1:], tags.t)
  178. stats.RecordWithTags(t.ctx, allTags, m...)
  179. })
  180. }
  181. func (t *trackingResponseWriter) Header() http.Header {
  182. return t.writer.Header()
  183. }
  184. func (t *trackingResponseWriter) Write(data []byte) (int, error) {
  185. n, err := t.writer.Write(data)
  186. t.respSize += int64(n)
  187. // Add message event for request bytes sent.
  188. span := trace.FromContext(t.ctx)
  189. span.AddMessageSendEvent(0 /* TODO: messageID */, int64(n), -1)
  190. return n, err
  191. }
  192. func (t *trackingResponseWriter) WriteHeader(statusCode int) {
  193. t.writer.WriteHeader(statusCode)
  194. t.statusCode = statusCode
  195. t.statusLine = http.StatusText(t.statusCode)
  196. }
  197. // wrappedResponseWriter returns a wrapped version of the original
  198. // ResponseWriter and only implements the same combination of additional
  199. // interfaces as the original.
  200. // This implementation is based on https://github.com/felixge/httpsnoop.
  201. func (t *trackingResponseWriter) wrappedResponseWriter() http.ResponseWriter {
  202. var (
  203. hj, i0 = t.writer.(http.Hijacker)
  204. cn, i1 = t.writer.(http.CloseNotifier)
  205. pu, i2 = t.writer.(http.Pusher)
  206. fl, i3 = t.writer.(http.Flusher)
  207. rf, i4 = t.writer.(io.ReaderFrom)
  208. )
  209. switch {
  210. case !i0 && !i1 && !i2 && !i3 && !i4:
  211. return struct {
  212. http.ResponseWriter
  213. }{t}
  214. case !i0 && !i1 && !i2 && !i3 && i4:
  215. return struct {
  216. http.ResponseWriter
  217. io.ReaderFrom
  218. }{t, rf}
  219. case !i0 && !i1 && !i2 && i3 && !i4:
  220. return struct {
  221. http.ResponseWriter
  222. http.Flusher
  223. }{t, fl}
  224. case !i0 && !i1 && !i2 && i3 && i4:
  225. return struct {
  226. http.ResponseWriter
  227. http.Flusher
  228. io.ReaderFrom
  229. }{t, fl, rf}
  230. case !i0 && !i1 && i2 && !i3 && !i4:
  231. return struct {
  232. http.ResponseWriter
  233. http.Pusher
  234. }{t, pu}
  235. case !i0 && !i1 && i2 && !i3 && i4:
  236. return struct {
  237. http.ResponseWriter
  238. http.Pusher
  239. io.ReaderFrom
  240. }{t, pu, rf}
  241. case !i0 && !i1 && i2 && i3 && !i4:
  242. return struct {
  243. http.ResponseWriter
  244. http.Pusher
  245. http.Flusher
  246. }{t, pu, fl}
  247. case !i0 && !i1 && i2 && i3 && i4:
  248. return struct {
  249. http.ResponseWriter
  250. http.Pusher
  251. http.Flusher
  252. io.ReaderFrom
  253. }{t, pu, fl, rf}
  254. case !i0 && i1 && !i2 && !i3 && !i4:
  255. return struct {
  256. http.ResponseWriter
  257. http.CloseNotifier
  258. }{t, cn}
  259. case !i0 && i1 && !i2 && !i3 && i4:
  260. return struct {
  261. http.ResponseWriter
  262. http.CloseNotifier
  263. io.ReaderFrom
  264. }{t, cn, rf}
  265. case !i0 && i1 && !i2 && i3 && !i4:
  266. return struct {
  267. http.ResponseWriter
  268. http.CloseNotifier
  269. http.Flusher
  270. }{t, cn, fl}
  271. case !i0 && i1 && !i2 && i3 && i4:
  272. return struct {
  273. http.ResponseWriter
  274. http.CloseNotifier
  275. http.Flusher
  276. io.ReaderFrom
  277. }{t, cn, fl, rf}
  278. case !i0 && i1 && i2 && !i3 && !i4:
  279. return struct {
  280. http.ResponseWriter
  281. http.CloseNotifier
  282. http.Pusher
  283. }{t, cn, pu}
  284. case !i0 && i1 && i2 && !i3 && i4:
  285. return struct {
  286. http.ResponseWriter
  287. http.CloseNotifier
  288. http.Pusher
  289. io.ReaderFrom
  290. }{t, cn, pu, rf}
  291. case !i0 && i1 && i2 && i3 && !i4:
  292. return struct {
  293. http.ResponseWriter
  294. http.CloseNotifier
  295. http.Pusher
  296. http.Flusher
  297. }{t, cn, pu, fl}
  298. case !i0 && i1 && i2 && i3 && i4:
  299. return struct {
  300. http.ResponseWriter
  301. http.CloseNotifier
  302. http.Pusher
  303. http.Flusher
  304. io.ReaderFrom
  305. }{t, cn, pu, fl, rf}
  306. case i0 && !i1 && !i2 && !i3 && !i4:
  307. return struct {
  308. http.ResponseWriter
  309. http.Hijacker
  310. }{t, hj}
  311. case i0 && !i1 && !i2 && !i3 && i4:
  312. return struct {
  313. http.ResponseWriter
  314. http.Hijacker
  315. io.ReaderFrom
  316. }{t, hj, rf}
  317. case i0 && !i1 && !i2 && i3 && !i4:
  318. return struct {
  319. http.ResponseWriter
  320. http.Hijacker
  321. http.Flusher
  322. }{t, hj, fl}
  323. case i0 && !i1 && !i2 && i3 && i4:
  324. return struct {
  325. http.ResponseWriter
  326. http.Hijacker
  327. http.Flusher
  328. io.ReaderFrom
  329. }{t, hj, fl, rf}
  330. case i0 && !i1 && i2 && !i3 && !i4:
  331. return struct {
  332. http.ResponseWriter
  333. http.Hijacker
  334. http.Pusher
  335. }{t, hj, pu}
  336. case i0 && !i1 && i2 && !i3 && i4:
  337. return struct {
  338. http.ResponseWriter
  339. http.Hijacker
  340. http.Pusher
  341. io.ReaderFrom
  342. }{t, hj, pu, rf}
  343. case i0 && !i1 && i2 && i3 && !i4:
  344. return struct {
  345. http.ResponseWriter
  346. http.Hijacker
  347. http.Pusher
  348. http.Flusher
  349. }{t, hj, pu, fl}
  350. case i0 && !i1 && i2 && i3 && i4:
  351. return struct {
  352. http.ResponseWriter
  353. http.Hijacker
  354. http.Pusher
  355. http.Flusher
  356. io.ReaderFrom
  357. }{t, hj, pu, fl, rf}
  358. case i0 && i1 && !i2 && !i3 && !i4:
  359. return struct {
  360. http.ResponseWriter
  361. http.Hijacker
  362. http.CloseNotifier
  363. }{t, hj, cn}
  364. case i0 && i1 && !i2 && !i3 && i4:
  365. return struct {
  366. http.ResponseWriter
  367. http.Hijacker
  368. http.CloseNotifier
  369. io.ReaderFrom
  370. }{t, hj, cn, rf}
  371. case i0 && i1 && !i2 && i3 && !i4:
  372. return struct {
  373. http.ResponseWriter
  374. http.Hijacker
  375. http.CloseNotifier
  376. http.Flusher
  377. }{t, hj, cn, fl}
  378. case i0 && i1 && !i2 && i3 && i4:
  379. return struct {
  380. http.ResponseWriter
  381. http.Hijacker
  382. http.CloseNotifier
  383. http.Flusher
  384. io.ReaderFrom
  385. }{t, hj, cn, fl, rf}
  386. case i0 && i1 && i2 && !i3 && !i4:
  387. return struct {
  388. http.ResponseWriter
  389. http.Hijacker
  390. http.CloseNotifier
  391. http.Pusher
  392. }{t, hj, cn, pu}
  393. case i0 && i1 && i2 && !i3 && i4:
  394. return struct {
  395. http.ResponseWriter
  396. http.Hijacker
  397. http.CloseNotifier
  398. http.Pusher
  399. io.ReaderFrom
  400. }{t, hj, cn, pu, rf}
  401. case i0 && i1 && i2 && i3 && !i4:
  402. return struct {
  403. http.ResponseWriter
  404. http.Hijacker
  405. http.CloseNotifier
  406. http.Pusher
  407. http.Flusher
  408. }{t, hj, cn, pu, fl}
  409. case i0 && i1 && i2 && i3 && i4:
  410. return struct {
  411. http.ResponseWriter
  412. http.Hijacker
  413. http.CloseNotifier
  414. http.Pusher
  415. http.Flusher
  416. io.ReaderFrom
  417. }{t, hj, cn, pu, fl, rf}
  418. default:
  419. return struct {
  420. http.ResponseWriter
  421. }{t}
  422. }
  423. }