<commit_before>package httphandlers

import (
	"bufio"
	"net"
	"net/http"
	"net/url"
	"time"

	"github.com/Financial-Times/transactionid-utils-go"
	log "github.com/Sirupsen/logrus"
	"github.com/rcrowley/go-metrics"
)

// HTTPMetricsHandler records metrics for each request
func HTTPMetricsHandler(registry metrics.Registry, h http.Handler) http.Handler {
	return httpMetricsHandler{registry, h}
}

type httpMetricsHandler struct {
	registry metrics.Registry
	handler  http.Handler
}

func (h httpMetricsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	t := metrics.GetOrRegisterTimer(req.Method, h.registry)
	t.Time(func() { h.handler.ServeHTTP(w, req) })
}

// TransactionAwareRequestLoggingHandler finds a transactionID on a request header or generates one, and makes sure it gets logged
// using the supplied logrus.Logger
func TransactionAwareRequestLoggingHandler(logger *log.Logger, h http.Handler) http.Handler {
	return transactionAwareRequestLoggingHandler{logger, h}
}

type transactionAwareRequestLoggingHandler struct {
	logger  *log.Logger
	handler http.Handler
}

func (h transactionAwareRequestLoggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	transactionID := transactionidutils.GetTransactionIDFromRequest(req)
	t := time.Now()
	loggingResponseWriter := wrapWriter(w)
	url := *req.URL
	h.handler.ServeHTTP(loggingResponseWriter, req)
	duration := time.Since(t)
	writeRequestLog(h.logger, req, transactionID, url, duration, loggingResponseWriter.Status(), loggingResponseWriter.Size())
}<commit_msg>added transactionId header to response<commit_after>package httphandlers

import (
	"bufio"
	"net"
	"net/http"
	"net/url"
	"time"

	"github.com/Financial-Times/transactionid-utils-go"
	log "github.com/Sirupsen/logrus"
	"github.com/rcrowley/go-metrics"
)

// HTTPMetricsHandler records metrics for each request
func HTTPMetricsHandler(registry metrics.Registry, h http.Handler) http.Handler {
	return httpMetricsHandler{registry, h}
}

type httpMetricsHandler struct {
	registry metrics.Registry
	handler  http.Handler
}

func (h httpMetricsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	t := metrics.GetOrRegisterTimer(req.Method, h.registry)
	t.Time(func() { h.handler.ServeHTTP(w, req) })
}

// TransactionAwareRequestLoggingHandler finds a transactionID on a request header or generates one, and makes sure it gets logged
// using the supplied logrus.Logger
func TransactionAwareRequestLoggingHandler(logger *log.Logger, h http.Handler) http.Handler {
	return transactionAwareRequestLoggingHandler{logger, h}
}

type transactionAwareRequestLoggingHandler struct {
	logger  *log.Logger
	handler http.Handler
}

func (h transactionAwareRequestLoggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	transactionID := transactionidutils.GetTransactionIDFromRequest(req)
	w.Header().Set(transactionidutils.TransactionIDHeader, transactionID)
	t := time.Now()
	loggingResponseWriter := wrapWriter(w)
	url := *req.URL
	h.handler.ServeHTTP(loggingResponseWriter, req)
	duration := time.Since(t)
	writeRequestLog(h.logger, req, transactionID, url, duration, loggingResponseWriter.Status(), loggingResponseWriter.Size())
}
{'fraction_non_alphanumeric': 0.10193871169480925, 'fraction_numerical': 0.0, 'mean_word_length': 6.156599552572707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1253713', 'n_tokens_mistral': 975, 'n_tokens_neox': 977, 'n_words': 226}