ServeHTTP is the proxy http.Handler implementation
(w http.ResponseWriter, r *http.Request)
| 1752 | |
| 1753 | // ServeHTTP is the proxy http.Handler implementation |
| 1754 | func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 1755 | |
| 1756 | var requestElapsed, responseElapsed time.Duration |
| 1757 | requestStopWatch, responseStopWatch := newStopWatch(), newStopWatch() |
| 1758 | requestStopWatch.Start() |
| 1759 | |
| 1760 | lw := logging.NewLoggingWriter(w) |
| 1761 | |
| 1762 | p.metrics.IncCounter("incoming." + r.Proto) |
| 1763 | var ctx *context |
| 1764 | |
| 1765 | spanOpts := []ot.StartSpanOption{ot.Tags{ |
| 1766 | SpanKindTag: SpanKindServer, |
| 1767 | }} |
| 1768 | if wireContext, err := p.tracing.tracer.Extract(ot.HTTPHeaders, ot.HTTPHeadersCarrier(r.Header)); err == nil { |
| 1769 | spanOpts = append(spanOpts, ext.RPCServerOption(wireContext)) |
| 1770 | } |
| 1771 | span := p.tracing.tracer.StartSpan(p.tracing.initialOperationName, spanOpts...) |
| 1772 | |
| 1773 | defer func() { |
| 1774 | if ctx != nil && ctx.proxySpan != nil { |
| 1775 | ctx.proxySpan.Finish() |
| 1776 | } |
| 1777 | span.Finish() |
| 1778 | requestStopWatch.Stop() |
| 1779 | responseStopWatch.Stop() |
| 1780 | |
| 1781 | p.metrics.MeasureProxy(requestElapsed+requestStopWatch.Elapsed(), responseElapsed+responseStopWatch.Elapsed()) |
| 1782 | }() |
| 1783 | |
| 1784 | defer func() { |
| 1785 | accessLogEnabled, ok := ctx.stateBag[al.AccessLogEnabledKey].(*al.AccessLogFilter) |
| 1786 | if !ok { |
| 1787 | if p.accessLogDisabled { |
| 1788 | accessLogEnabled = &disabledAccessLog |
| 1789 | } else { |
| 1790 | accessLogEnabled = &enabledAccessLog |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | statusCode := lw.GetCode() |
| 1795 | |
| 1796 | if shouldLog(statusCode, accessLogEnabled) { |
| 1797 | authUser, _ := ctx.stateBag[filterslog.AuthUserKey].(string) |
| 1798 | entry := &logging.AccessEntry{ |
| 1799 | Request: r, |
| 1800 | ResponseSize: lw.GetBytes(), |
| 1801 | StatusCode: statusCode, |
| 1802 | RequestTime: ctx.startServe, |
| 1803 | Duration: time.Since(ctx.startServe), |
| 1804 | AuthUser: authUser, |
| 1805 | } |
| 1806 | |
| 1807 | additionalData, _ := ctx.stateBag[al.AccessLogAdditionalDataKey].(map[string]interface{}) |
| 1808 | if p.accessLogger != nil { |
| 1809 | p.accessLogger.LogAccess(entry, additionalData) |
| 1810 | } |
| 1811 | } |