<commit_before>package middleware

import (
	"fmt"
	"github.com/remind101/nra"
	"github.com/remind101/pkg/httpx"
	"golang.org/x/net/context"
	"net/http"
)

type NewRelicTracer struct {
	handler  httpx.Handler
	tracer   nra.TxTracer
	router   *httpx.Router
	createTx func(string, string, nra.TxTracer) nra.Tx
}

func NewRelicTracing(h httpx.Handler, router *httpx.Router, tracer nra.TxTracer) *NewRelicTracer {
	return &NewRelicTracer{h, tracer, router, createTx}
}

func (h *NewRelicTracer) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	templatePath := h.templatePath(r)
	transactionName := fmt.Sprintf("%s (%s)", templatePath, r.Method)

	tx := h.createTx(transactionName, r.URL.String(), h.tracer)
	ctx = nra.WithTx(ctx, tx)

	tx.Start()
	defer tx.End()

	return h.handler.ServeHTTPContext(ctx, w, r)
}

func (h *NewRelicTracer) templatePath(r *http.Request) string {
	route, _, vars := h.router.Handler(r)
	var templatePath string

	if route != nil {
		var pairs []string
		for k, _ := range vars {
			pairs = append(pairs, k, fmt.Sprintf(":%s", k))<commit_msg>Make templatePath a func<commit_after>package middleware

import (
	"fmt"
	"net/http"

	"github.com/remind101/nra"
	"github.com/remind101/pkg/httpx"
	"golang.org/x/net/context"
)

type NewRelicTracer struct {
	handler  httpx.Handler
	tracer   nra.TxTracer
	router   *httpx.Router
	createTx func(string, string, nra.TxTracer) nra.Tx
}

func NewRelicTracing(h httpx.Handler, router *httpx.Router, tracer nra.TxTracer) *NewRelicTracer {
	return &NewRelicTracer{h, tracer, router, createTx}
}

func (h *NewRelicTracer) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	path := templatePath(h.router, r)
	txName := fmt.Sprintf("%s (%s)", path, r.Method)

	tx := h.createTx(txName, r.URL.String(), h.tracer)
	ctx = nra.WithTx(ctx, tx)

	tx.Start()
	defer tx.End()

	return h.handler.ServeHTTPContext(ctx, w, r)
}

func templatePath(router *httpx.Router, r *http.Request) string {
	route, _, vars := router.Handler(r)
	var templatePath string

	if route != nil {
		var pairs []string
		for k, _ := range vars {
			pairs = append(pairs, k, fmt.Sprintf(":%s", k))
{'fraction_non_alphanumeric': 0.14116575591985428, 'fraction_numerical': 0.00546448087431694, 'mean_word_length': 4.604591836734694, 'pattern_counts': {'":': 2, '<': 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': '4720926', 'n_tokens_mistral': 868, 'n_tokens_neox': 834, 'n_words': 189}