otelhttptrace

package module
v0.0.0-...-905a04c Latest Latest
Warning

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

Go to latest
Published: Jul 1, 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 (
	"fmt"
	"net/http"

	"github.com/coryb/otelhttptrace"
	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func main() {
	client := &http.Client{
		Transport: otelhttp.NewTransport(
			otelhttptrace.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://github.com")
	if err != nil {
		panic(fmt.Sprintf("Failed to GET https://github.com %q", err))
	}
	defer resp.Body.Close()
	fmt.Printf("Got %s", resp.Status)

}
Output:

Got 200 OK

Types

This section is empty.

Jump to

Keyboard shortcuts

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