tracing

package
v0.0.0-...-31e7f02 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Middleware = func(next http.Handler) http.Handler {
	return otelhttp.NewHandler(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				span := trace.SpanFromContext(r.Context())

				span.SetAttributes(attribute.KeyValue{
					Key:   attribute.Key("http.method"),
					Value: attribute.StringValue(r.Method),
				})
				span.SetAttributes(attribute.KeyValue{
					Key:   attribute.Key("http.url"),
					Value: attribute.StringValue(r.URL.String()),
				})
				span.SetAttributes(attribute.KeyValue{
					Key:   attribute.Key("http.protocol"),
					Value: attribute.StringValue(r.Proto),
				})
				span.SetAttributes(attribute.KeyValue{
					Key:   attribute.Key("http.remote_ip"),
					Value: attribute.StringValue(r.RemoteAddr),
				})
				span.SetAttributes(attribute.KeyValue{
					Key:   attribute.Key("http.user_agent"),
					Value: attribute.StringValue(r.UserAgent()),
				})
				span.SetAttributes(attribute.KeyValue{
					Key:   attribute.Key("http.referer"),
					Value: attribute.StringValue(r.Referer()),
				})
				headers := []string{}
				for k, v := range r.Header {
					value := strings.Join(v, ",")
					if k == "Authorization" || k == "Cookie" {
						value = scrub(value)
					}
					headers = append(headers, fmt.Sprintf("%s: %s", k, value))
				}
				span.SetAttributes(attribute.KeyValue{
					Key:   attribute.Key("http.headers"),
					Value: attribute.StringSliceValue(headers),
				})

				next.ServeHTTP(w, r)
			},
		),
		"http-request",
		otelhttp.WithSpanNameFormatter(
			func(_ string, r *http.Request) string {
				return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
			}),
	)
}

Functions

func Init

func Init(
	ctx context.Context,
	opts *Options,
) (
	func(),
	error,
)

Types

type Options

type Options struct {
	InitForGCP      bool
	GCPProjectID    string
	ServiceName     string
	ServiceVersion  string
	EnvironmentName string
}

Jump to

Keyboard shortcuts

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