<commit_before>package tracetransport

import (
	"context"
	"net/http"

	opentracing "github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/ext"
	"github.com/opentracing/opentracing-go/log"
	"github.com/pkg/errors"

	"github.com/paultyng/resttransport"
	"github.com/paultyng/resttransport/routename"
)

type tracingTransport struct {
	inner  resttransport.Transport
	namer  routename.Namer
	tracer opentracing.Tracer
}

// New returns a new instance of a resttransport that implements opentracing.
func New(tracer opentracing.Tracer, inner resttransport.Transport) resttransport.Transport {
	return &tracingTransport{
		inner:  inner,
		namer:  routename.New(),
		tracer: tracer,
	}
}

func (t *tracingTransport) RegisterHandler(httpMethod, path string, h resttransport.Handler) error {
	return t.inner.RegisterHandler(httpMethod, path, t.wrapHandler(httpMethod, path, h))
}

func (t *tracingTransport) RegisterAuthenticatedHandler(httpMethod, path string, h resttransport.Handler) error {
	return t.inner.RegisterAuthenticatedHandler(httpMethod, path, t.wrapHandler(httpMethod, path, h))
}

func (t *tracingTransport) wrapHandler(httpMethod, path string, inner resttransport.Handler) resttransport.Handler {
	return func(ctx context.Context, reqres resttransport.RequestResponse) error {
		wireContext, _ := t.tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(reqres.RequestHeader()))

		spanName := t.namer.Name(httpMethod, path)
		span := t.tracer.StartSpan(
			spanName,
			ext.RPCServerOption(wireContext),
		)
		defer span.Finish()

		ext.HTTPUrl.Set(span, path)
		ext.HTTPMethod.Set(span, httpMethod)

		ctx = opentracing.ContextWithSpan(ctx, span)

		err := inner(ctx, reqres)
		if err != nil {
			span.LogFields(
				log.String("err", err.Error()),
				log.String("cause", errors.Cause(err).Error()),
			)
		}

		return err
	}
}

// InjectHTTP propagates tracing headers to child HTTP requests.
func InjectHTTP(ctx context.Context, outbound *http.Request) {
	span := opentracing.SpanFromContext(ctx)
	if span != nil {
		span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(outbound.Header))
	}
}
<commit_msg>Fix issue with requiring entire http request<commit_after>package tracetransport

import (
	"context"
	"net/http"

	opentracing "github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/ext"
	"github.com/opentracing/opentracing-go/log"
	"github.com/pkg/errors"

	"github.com/paultyng/resttransport"
	"github.com/paultyng/resttransport/routename"
)

type tracingTransport struct {
	inner  resttransport.Transport
	namer  routename.Namer
	tracer opentracing.Tracer
}

// New returns a new instance of a resttransport that implements opentracing.
func New(tracer opentracing.Tracer, inner resttransport.Transport) resttransport.Transport {
	return &tracingTransport{
		inner:  inner,
		namer:  routename.New(),
		tracer: tracer,
	}
}

func (t *tracingTransport) RegisterHandler(httpMethod, path string, h resttransport.Handler) error {
	return t.inner.RegisterHandler(httpMethod, path, t.wrapHandler(httpMethod, path, h))
}

func (t *tracingTransport) RegisterAuthenticatedHandler(httpMethod, path string, h resttransport.Handler) error {
	return t.inner.RegisterAuthenticatedHandler(httpMethod, path, t.wrapHandler(httpMethod, path, h))
}

func (t *tracingTransport) wrapHandler(httpMethod, path string, inner resttransport.Handler) resttransport.Handler {
	return func(ctx context.Context, reqres resttransport.RequestResponse) error {
		wireContext, _ := t.tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(reqres.RequestHeader()))

		spanName := t.namer.Name(httpMethod, path)
		span := t.tracer.StartSpan(
			spanName,
			ext.RPCServerOption(wireContext),
		)
		defer span.Finish()

		ext.HTTPUrl.Set(span, path)
		ext.HTTPMethod.Set(span, httpMethod)

		ctx = opentracing.ContextWithSpan(ctx, span)

		err := inner(ctx, reqres)
		if err != nil {
			span.LogFields(
				log.String("err", err.Error()),
				log.String("cause", errors.Cause(err).Error()),
			)
		}

		return err
	}
}

// InjectHTTP propagates tracing headers to child HTTP requests.
func InjectHTTP(ctx context.Context, headers http.Header) {
	span := opentracing.SpanFromContext(ctx)
	if span != nil {
		span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(headers))
	}
}

{'fraction_non_alphanumeric': 0.11522819701762313, 'fraction_numerical': 0.0, 'mean_word_length': 5.2883522727272725, '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': '3914791', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1471, 'n_words': 294}