httptrace

package module
v0.0.0-...-dfc379f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

Go Reference

This is a Go http-tracing library that will annotate OpenTelemetry spans.

You may want to use the official http-tracing support instead: go.opentelemetry.io/contrib/instrumentation/net/http/httptrace

This package differs from the official tracing solution in that it just adds "event" annotations to the span rather than creating many sub-spans as the official one does.

I found dealing with the sub-spans harder to follow when debugging processes with many http calls. This approach is similar to how the Zipkin tracing collects http trace data when tracing is enabled via the http.Transport.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestWithClientTrace

func RequestWithClientTrace(req *http.Request) *http.Request

RequestWithClientTrace adds httptrace.ClientTrace instrumentation into the request if there is an OpenTelemetry trace.Span previously registered in the request Context. This is modeled after the Zipkin transport tracing: https://github.com/openzipkin/zipkin-go/blob/v0.2.5/middleware/http/transport.go#L165

func Transport

func Transport(rt http.RoundTripper) http.RoundTripper

Transport creates an http transport that will automatically trace all requests if there is an OpenTelemetry trace.Span previously registered in the request Context.

Example
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/coryb/otelbundle/instrumentation/httptrace"
	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	"go.opentelemetry.io/otel/trace"
)

type spanProcessor struct {
	Ended chan sdktrace.ReadOnlySpan
}

var _ sdktrace.SpanProcessor = (*spanProcessor)(nil)

func (sp *spanProcessor) OnEnd(s sdktrace.ReadOnlySpan) {
	go func() {
		sp.Ended <- s
	}()
}
func (sp *spanProcessor) OnStart(parent context.Context, s sdktrace.ReadWriteSpan) {}
func (sp *spanProcessor) Shutdown(ctx context.Context) error                       { return nil }
func (sp *spanProcessor) ForceFlush(ctx context.Context) error                     { return nil }

var testProcessor = &spanProcessor{
	Ended: make(chan sdktrace.ReadOnlySpan),
}

type testIDGenerator struct{}

var _ sdktrace.IDGenerator = (*testIDGenerator)(nil)

func (g *testIDGenerator) NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) {
	traceID, _ := trace.TraceIDFromHex("60d19e9e9abf2197c1d6d8f93e28ee2a")
	spanID, _ := trace.SpanIDFromHex("a028bd951229a46f")
	return traceID, spanID
}

func (g *testIDGenerator) NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID {
	spanID, _ := trace.SpanIDFromHex("a028bd951229a46f")
	return spanID
}

func init() {
	otel.SetTracerProvider(
		sdktrace.NewTracerProvider(
			sdktrace.WithSampler(sdktrace.AlwaysSample()),
			sdktrace.WithIDGenerator(&testIDGenerator{}),
			sdktrace.WithSpanProcessor(testProcessor),
		),
	)
}

func main() {
	client := &http.Client{
		Transport: otelhttp.NewTransport(
			httptrace.Transport(http.DefaultTransport),
			otelhttp.WithSpanNameFormatter(func(_ string, req *http.Request) string {
				return fmt.Sprintf("%s %s", req.Method, req.URL.Path)
			}),
		),
	}
	resp, err := client.Get("https://example.com")
	if err != nil {
		panic(fmt.Sprintf("Failed to GET https://example.com %q", err))
	}
	resp.Body.Close()

	// global span processor for testing
	testSpan := <-testProcessor.Ended
	for _, ev := range testSpan.Events() {
		fmt.Printf("Got Event: %s\n", ev.Name)
	}

}
Output:

Got Event: Connecting
Got Event: DNS Start
Got Event: DNS Done
Got Event: Connect Start
Got Event: Connect Done
Got Event: TLS Handshake Start
Got Event: TLS Handshake Done
Got Event: Connected
Got Event: Wrote Headers
Got Event: Wrote Request
Got Event: First Response Byte

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL