<commit_before>package handlers

import (
	"net/http"

	"code.cloudfoundry.org/auction/auctiontypes"
	"code.cloudfoundry.org/auctioneer"
	"code.cloudfoundry.org/bbs/handlers/middleware"
	"code.cloudfoundry.org/lager"
	"github.com/tedsuo/rata"
)

func New(runner auctiontypes.AuctionRunner, logger lager.Logger) http.Handler {
	taskAuctionHandler := logWrap(NewTaskAuctionHandler(runner).Create, logger)
	lrpAuctionHandler := logWrap(NewLRPAuctionHandler(runner).Create, logger)

	emitter := middleware.NewLatencyEmitter(logger)
	actions := rata.Handlers{
		auctioneer.CreateTaskAuctionsRoute: emitter.EmitLatency(taskAuctionHandler),
		auctioneer.CreateLRPAuctionsRoute:  emitter.EmitLatency(lrpAuctionHandler),
	}

	handler, err := rata.NewRouter(auctioneer.Routes, actions)
	if err != nil {
		panic("unable to create router: " + err.Error())
	}

	return middleware.RequestCountWrap(handler)
}

func logWrap(loggable func(http.ResponseWriter, *http.Request, lager.Logger), logger lager.Logger) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		requestLog := logger.Session("request", lager.Data{
			"method":  r.Method,
			"request": r.URL.String(),
		})

		requestLog.Info("serving")
		loggable(w, r, requestLog)
		requestLog.Info("done")
	}
}<commit_msg>Create eager latency emitter to match changes to middleware.<commit_after>package handlers

import (
	"net/http"
	"time"

	"code.cloudfoundry.org/auction/auctiontypes"
	"code.cloudfoundry.org/auctioneer"
	"code.cloudfoundry.org/bbs/handlers/middleware"
	"code.cloudfoundry.org/lager"
	"github.com/tedsuo/rata"
)

func New(runner auctiontypes.AuctionRunner, logger lager.Logger) http.Handler {
	taskAuctionHandler := logWrap(NewTaskAuctionHandler(runner).Create, logger)
	lrpAuctionHandler := logWrap(NewLRPAuctionHandler(runner).Create, logger)

	emitter := middleware.NewLatencyEmitterWrapper(&auctioneerEmitter{logger: logger})

	actions := rata.Handlers{
		auctioneer.CreateTaskAuctionsRoute: emitter.RecordLatency(taskAuctionHandler),
		auctioneer.CreateLRPAuctionsRoute:  emitter.RecordLatency(lrpAuctionHandler),
	}

	handler, err := rata.NewRouter(auctioneer.Routes, actions)
	if err != nil {
		panic("unable to create router: " + err.Error())
	}

	return middleware.RequestCountWrap(handler)
}

func logWrap(loggable func(http.ResponseWriter, *http.Request, lager.Logger), logger lager.Logger) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		requestLog := logger.Session("request", lager.Data{
			"method":  r.Method,
			"request": r.URL.String(),
		})

		requestLog.Info("serving")
		loggable(w, r, requestLog)
		requestLog.Info("done")
	}
}

type auctioneerEmitter struct {
	logger lager.Logger
}

func (e *auctioneerEmitter) IncrementCounter(delta int) {
	middleware.RequestCount.Increment()
}

func (e *auctioneerEmitter) UpdateLatency(latency time.Duration) {
	err := middleware.RequestLatency.Send(latency)
	if err != nil {
		e.logger.Error("failed-to-send-latency", err)
	}
}
{'fraction_non_alphanumeric': 0.12762412529156947, 'fraction_numerical': 0.0, 'mean_word_length': 5.853881278538813, 'pattern_counts': {'":': 4, '<': 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': '2807543', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1027, 'n_words': 169}