You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.go 11 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. 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. func (t *trackingResponseWriter) end(tags *addedTags) {
  160. t.endOnce.Do(func() {
  161. if t.statusCode == 0 {
  162. t.statusCode = 200
  163. }
  164. span := trace.FromContext(t.ctx)
  165. span.SetStatus(TraceStatus(t.statusCode, t.statusLine))
  166. span.AddAttributes(trace.Int64Attribute(StatusCodeAttribute, int64(t.statusCode)))
  167. m := []stats.Measurement{
  168. ServerLatency.M(float64(time.Since(t.start)) / float64(time.Millisecond)),
  169. ServerResponseBytes.M(t.respSize),
  170. }
  171. if t.reqSize >= 0 {
  172. m = append(m, ServerRequestBytes.M(t.reqSize))
  173. }
  174. allTags := make([]tag.Mutator, len(tags.t)+1)
  175. allTags[0] = tag.Upsert(StatusCode, strconv.Itoa(t.statusCode))
  176. copy(allTags[1:], tags.t)
  177. stats.RecordWithTags(t.ctx, allTags, m...)
  178. })
  179. }
  180. func (t *trackingResponseWriter) Header() http.Header {
  181. return t.writer.Header()
  182. }
  183. func (t *trackingResponseWriter) Write(data []byte) (int, error) {
  184. n, err := t.writer.Write(data)
  185. t.respSize += int64(n)
  186. // Add message event for request bytes sent.
  187. span := trace.FromContext(t.ctx)
  188. span.AddMessageSendEvent(0 /* TODO: messageID */, int64(n), -1)
  189. return n, err
  190. }
  191. func (t *trackingResponseWriter) WriteHeader(statusCode int) {
  192. t.writer.WriteHeader(statusCode)
  193. t.statusCode = statusCode
  194. t.statusLine = http.StatusText(t.statusCode)
  195. }
  196. // wrappedResponseWriter returns a wrapped version of the original
  197. // ResponseWriter and only implements the same combination of additional
  198. // interfaces as the original.
  199. // This implementation is based on https://github.com/felixge/httpsnoop.
  200. func (t *trackingResponseWriter) wrappedResponseWriter() http.ResponseWriter {
  201. var (
  202. hj, i0 = t.writer.(http.Hijacker)
  203. cn, i1 = t.writer.(http.CloseNotifier)
  204. pu, i2 = t.writer.(http.Pusher)
  205. fl, i3 = t.writer.(http.Flusher)
  206. rf, i4 = t.writer.(io.ReaderFrom)
  207. )
  208. switch {
  209. case !i0 && !i1 && !i2 && !i3 && !i4:
  210. return struct {
  211. http.ResponseWriter
  212. }{t}
  213. case !i0 && !i1 && !i2 && !i3 && i4:
  214. return struct {
  215. http.ResponseWriter
  216. io.ReaderFrom
  217. }{t, rf}
  218. case !i0 && !i1 && !i2 && i3 && !i4:
  219. return struct {
  220. http.ResponseWriter
  221. http.Flusher
  222. }{t, fl}
  223. case !i0 && !i1 && !i2 && i3 && i4:
  224. return struct {
  225. http.ResponseWriter
  226. http.Flusher
  227. io.ReaderFrom
  228. }{t, fl, rf}
  229. case !i0 && !i1 && i2 && !i3 && !i4:
  230. return struct {
  231. http.ResponseWriter
  232. http.Pusher
  233. }{t, pu}
  234. case !i0 && !i1 && i2 && !i3 && i4:
  235. return struct {
  236. http.ResponseWriter
  237. http.Pusher
  238. io.ReaderFrom
  239. }{t, pu, rf}
  240. case !i0 && !i1 && i2 && i3 && !i4:
  241. return struct {
  242. http.ResponseWriter
  243. http.Pusher
  244. http.Flusher
  245. }{t, pu, fl}
  246. case !i0 && !i1 && i2 && i3 && i4:
  247. return struct {
  248. http.ResponseWriter
  249. http.Pusher
  250. http.Flusher
  251. io.ReaderFrom
  252. }{t, pu, fl, rf}
  253. case !i0 && i1 && !i2 && !i3 && !i4:
  254. return struct {
  255. http.ResponseWriter
  256. http.CloseNotifier
  257. }{t, cn}
  258. case !i0 && i1 && !i2 && !i3 && i4:
  259. return struct {
  260. http.ResponseWriter
  261. http.CloseNotifier
  262. io.ReaderFrom
  263. }{t, cn, rf}
  264. case !i0 && i1 && !i2 && i3 && !i4:
  265. return struct {
  266. http.ResponseWriter
  267. http.CloseNotifier
  268. http.Flusher
  269. }{t, cn, fl}
  270. case !i0 && i1 && !i2 && i3 && i4:
  271. return struct {
  272. http.ResponseWriter
  273. http.CloseNotifier
  274. http.Flusher
  275. io.ReaderFrom
  276. }{t, cn, fl, rf}
  277. case !i0 && i1 && i2 && !i3 && !i4:
  278. return struct {
  279. http.ResponseWriter
  280. http.CloseNotifier
  281. http.Pusher
  282. }{t, cn, pu}
  283. case !i0 && i1 && i2 && !i3 && i4:
  284. return struct {
  285. http.ResponseWriter
  286. http.CloseNotifier
  287. http.Pusher
  288. io.ReaderFrom
  289. }{t, cn, pu, rf}
  290. case !i0 && i1 && i2 && i3 && !i4:
  291. return struct {
  292. http.ResponseWriter
  293. http.CloseNotifier
  294. http.Pusher
  295. http.Flusher
  296. }{t, cn, pu, fl}
  297. case !i0 && i1 && i2 && i3 && i4:
  298. return struct {
  299. http.ResponseWriter
  300. http.CloseNotifier
  301. http.Pusher
  302. http.Flusher
  303. io.ReaderFrom
  304. }{t, cn, pu, fl, rf}
  305. case i0 && !i1 && !i2 && !i3 && !i4:
  306. return struct {
  307. http.ResponseWriter
  308. http.Hijacker
  309. }{t, hj}
  310. case i0 && !i1 && !i2 && !i3 && i4:
  311. return struct {
  312. http.ResponseWriter
  313. http.Hijacker
  314. io.ReaderFrom
  315. }{t, hj, rf}
  316. case i0 && !i1 && !i2 && i3 && !i4:
  317. return struct {
  318. http.ResponseWriter
  319. http.Hijacker
  320. http.Flusher
  321. }{t, hj, fl}
  322. case i0 && !i1 && !i2 && i3 && i4:
  323. return struct {
  324. http.ResponseWriter
  325. http.Hijacker
  326. http.Flusher
  327. io.ReaderFrom
  328. }{t, hj, fl, rf}
  329. case i0 && !i1 && i2 && !i3 && !i4:
  330. return struct {
  331. http.ResponseWriter
  332. http.Hijacker
  333. http.Pusher
  334. }{t, hj, pu}
  335. case i0 && !i1 && i2 && !i3 && i4:
  336. return struct {
  337. http.ResponseWriter
  338. http.Hijacker
  339. http.Pusher
  340. io.ReaderFrom
  341. }{t, hj, pu, rf}
  342. case i0 && !i1 && i2 && i3 && !i4:
  343. return struct {
  344. http.ResponseWriter
  345. http.Hijacker
  346. http.Pusher
  347. http.Flusher
  348. }{t, hj, pu, fl}
  349. case i0 && !i1 && i2 && i3 && i4:
  350. return struct {
  351. http.ResponseWriter
  352. http.Hijacker
  353. http.Pusher
  354. http.Flusher
  355. io.ReaderFrom
  356. }{t, hj, pu, fl, rf}
  357. case i0 && i1 && !i2 && !i3 && !i4:
  358. return struct {
  359. http.ResponseWriter
  360. http.Hijacker
  361. http.CloseNotifier
  362. }{t, hj, cn}
  363. case i0 && i1 && !i2 && !i3 && i4:
  364. return struct {
  365. http.ResponseWriter
  366. http.Hijacker
  367. http.CloseNotifier
  368. io.ReaderFrom
  369. }{t, hj, cn, rf}
  370. case i0 && i1 && !i2 && i3 && !i4:
  371. return struct {
  372. http.ResponseWriter
  373. http.Hijacker
  374. http.CloseNotifier
  375. http.Flusher
  376. }{t, hj, cn, fl}
  377. case i0 && i1 && !i2 && i3 && i4:
  378. return struct {
  379. http.ResponseWriter
  380. http.Hijacker
  381. http.CloseNotifier
  382. http.Flusher
  383. io.ReaderFrom
  384. }{t, hj, cn, fl, rf}
  385. case i0 && i1 && i2 && !i3 && !i4:
  386. return struct {
  387. http.ResponseWriter
  388. http.Hijacker
  389. http.CloseNotifier
  390. http.Pusher
  391. }{t, hj, cn, pu}
  392. case i0 && i1 && i2 && !i3 && i4:
  393. return struct {
  394. http.ResponseWriter
  395. http.Hijacker
  396. http.CloseNotifier
  397. http.Pusher
  398. io.ReaderFrom
  399. }{t, hj, cn, pu, rf}
  400. case i0 && i1 && i2 && i3 && !i4:
  401. return struct {
  402. http.ResponseWriter
  403. http.Hijacker
  404. http.CloseNotifier
  405. http.Pusher
  406. http.Flusher
  407. }{t, hj, cn, pu, fl}
  408. case i0 && i1 && i2 && i3 && i4:
  409. return struct {
  410. http.ResponseWriter
  411. http.Hijacker
  412. http.CloseNotifier
  413. http.Pusher
  414. http.Flusher
  415. io.ReaderFrom
  416. }{t, hj, cn, pu, fl, rf}
  417. default:
  418. return struct {
  419. http.ResponseWriter
  420. }{t}
  421. }
  422. }