<commit_before>// +build go1.7

package tracerutil

import (
	"context"
	"io"
	"net/http"
	"net/http/httptrace"

	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/ext"
)

// Transport wraps a RoundTripper. If a request is being traced with
// HTTPTracer, Transport will inject the current span into the headers,
// set HTTP related tags on the span as well as finish the span after
// the response body is closed.
type Transport struct {
	http.RoundTripper
}

// TraceRequest adds a ClientTracer to req, tracing the request and
// all requests caused due to redirects. When tracing requests this
// way you must also use Transport.
//
// Example:
//
// 	http.DefaultClient.Transport = &tracerutil.Transport{http.DefaultTransport}
// 	req, err := http.NewRequest("GET", "http://google.com", nil)
// 	if err != nil {
// 		log.Fatal(err)
// 	}
// 	req, ht := tracerutil.TraceRequest(parentSpan, req)
// 	res, err := http.DefaultClient.Do(req)
// 	if err != nil {
// 		log.Println(err)
// 	}
// 	res.Body.Close()
// 	ht.Finish()
func TraceRequest(sp opentracing.Span, req *http.Request) (*http.Request, *HTTPTracer) {
	ht := newHTTPTrace(sp)
	ctx := httptrace.WithClientTrace(req.Context(), ht.clientTrace())
	req = req.WithContext(context.WithValue(ctx, "tracer", ht))
	return req, ht
}

type closeTracker struct {
	io.ReadCloser
	sp opentracing.Span
}

func (c closeTracker) Close() error {
	err := c.ReadCloser.Close()
	c.sp.LogEvent("Closed body")
	c.sp.Finish()
	return err
}

// RoundTrip implements the RoundTripper interface.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	tracer, ok := req.Context().Value("tracer").(*HTTPTracer)
	if !ok {
		return t.RoundTripper.RoundTrip(req)
	}

	tracer.start()

	ext.HTTPMethod.Set(tracer.sp, req.Method)
	ext.HTTPUrl.Set(tracer.sp, req.URL.String())

	carrier := opentracing.HTTPHeaderTextMapCarrier(req.Header)
	tracer.sp.Tracer().Inject(tracer.sp.Context(), opentracing.TextMap, carrier)
	resp, err := t.RoundTripper.RoundTrip(req)

	ext.HTTPStatusCode.Set(tracer.sp, uint16(resp.StatusCode))
	if err != nil && req.Method == "HEAD" {
		tracer.sp.Finish()
	} else {
		resp.Body = closeTracker{resp.Body, tracer.sp}
	}
	return resp, err
}

type HTTPTracer struct {<commit_msg>Use own type for context key<commit_after>// +build go1.7

package tracerutil

import (
	"context"
	"io"
	"net/http"
	"net/http/httptrace"

	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/ext"
)

type contextKey int

const (
	keyTracer contextKey = iota
)

// Transport wraps a RoundTripper. If a request is being traced with
// HTTPTracer, Transport will inject the current span into the headers,
// set HTTP related tags on the span as well as finish the span after
// the response body is closed.
type Transport struct {
	http.RoundTripper
}

// TraceRequest adds a ClientTracer to req, tracing the request and
// all requests caused due to redirects. When tracing requests this
// way you must also use Transport.
//
// Example:
//
// 	http.DefaultClient.Transport = &tracerutil.Transport{http.DefaultTransport}
// 	req, err := http.NewRequest("GET", "http://google.com", nil)
// 	if err != nil {
// 		log.Fatal(err)
// 	}
// 	req, ht := tracerutil.TraceRequest(parentSpan, req)
// 	res, err := http.DefaultClient.Do(req)
// 	if err != nil {
// 		log.Println(err)
// 	}
// 	res.Body.Close()
// 	ht.Finish()
func TraceRequest(sp opentracing.Span, req *http.Request) (*http.Request, *HTTPTracer) {
	ht := newHTTPTrace(sp)
	ctx := httptrace.WithClientTrace(req.Context(), ht.clientTrace())
	req = req.WithContext(context.WithValue(ctx, keyTracer, ht))
	return req, ht
}

type closeTracker struct {
	io.ReadCloser
	sp opentracing.Span
}

func (c closeTracker) Close() error {
	err := c.ReadCloser.Close()
	c.sp.LogEvent("Closed body")
	c.sp.Finish()
	return err
}

// RoundTrip implements the RoundTripper interface.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	tracer, ok := req.Context().Value(keyTracer).(*HTTPTracer)
	if !ok {
		return t.RoundTripper.RoundTrip(req)
	}

	tracer.start()

	ext.HTTPMethod.Set(tracer.sp, req.Method)
	ext.HTTPUrl.Set(tracer.sp, req.URL.String())

	carrier := opentracing.HTTPHeaderTextMapCarrier(req.Header)
	tracer.sp.Tracer().Inject(tracer.sp.Context(), opentracing.TextMap, carrier)
	resp, err := t.RoundTripper.RoundTrip(req)

	ext.HTTPStatusCode.Set(tracer.sp, uint16(resp.StatusCode))
	if err != nil && req.Method == "HEAD" {
		tracer.sp.Finish()
	} else {
		resp.Body = closeTracker{resp.Body, tracer.sp}
	}
	return resp, err
}

type HTTPTracer struct {
{'fraction_non_alphanumeric': 0.14467442863303148, 'fraction_numerical': 0.0017248814144027599, 'mean_word_length': 3.9828141783029003, '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': '4298078', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1666, 'n_words': 400}