trace

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Trace

type Trace struct {
	Propagation      propagation.HTTPFormat
	IsPublicEndpoint bool
	FormatSpanName   func(r *http.Request) string
	StartOptions     trace.StartOptions
	GetStartOptions  func(r *http.Request) trace.StartOptions
}

Trace middleware

Example

Sample only a fraction of requests so a high-traffic edge does not export a span for every request, and label spans with the path instead of the full URL.

package main

import (
	"net/http"

	octrace "go.opencensus.io/trace"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/trace"
)

func main() {
	m := trace.New()
	m.StartOptions.Sampler = octrace.ProbabilitySampler(0.05) // ~5% of traces
	m.FormatSpanName = func(r *http.Request) string {
		return r.Method + " " + r.URL.Path
	}

	s := parapet.New()
	s.Use(m)
}
Example (GetStartOptions)

Decide sampling per request: always sample internal/debug traffic, fall back to a low probability for everything else.

package main

import (
	"net/http"
	"strings"

	octrace "go.opencensus.io/trace"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/trace"
)

func main() {
	always := octrace.StartOptions{Sampler: octrace.AlwaysSample()}
	sampled := octrace.StartOptions{Sampler: octrace.ProbabilitySampler(0.01)}

	m := trace.New()
	m.GetStartOptions = func(r *http.Request) octrace.StartOptions {
		if strings.HasPrefix(r.URL.Path, "/internal/") {
			return always
		}
		return sampled
	}

	s := parapet.New()
	s.Use(m)
}
Example (Propagation)

Read incoming trace context using the B3 headers (X-B3-TraceId, ...) emitted by Zipkin-style clients instead of the default format, so spans join the caller's trace. Mark this hop as a public endpoint so an untrusted client's span context starts a fresh, linked trace rather than being trusted as parent.

package main

import (
	"go.opencensus.io/plugin/ochttp/propagation/b3"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/trace"
)

func main() {
	m := trace.New()
	m.Propagation = &b3.HTTPFormat{}
	m.IsPublicEndpoint = true

	s := parapet.New()
	s.Use(m)
}

func New

func New() *Trace

New creates new trace middleware

Example

Add OpenCensus server-side tracing to the proxy. Each request becomes a span; an exporter registered elsewhere (Stackdriver, Zipkin, ...) ships them.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/trace"
)

func main() {
	s := parapet.New()
	s.Use(trace.New())
	// s.Use(upstream.SingleHost("10.0.0.1:8080")) — the proxied handler runs inside the span.
}

func (Trace) ServeHandler

func (m Trace) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

Jump to

Keyboard shortcuts

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