perf

package
v0.1.47 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2023 License: Apache-2.0 Imports: 28 Imported by: 1

Documentation

Overview

Package perf exposes application performance utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGrpcStatsHandler added in v0.0.5

func NewGrpcStatsHandler() stats.Handler

NewGrpcStatsHandler creates a new stats handler writing to opencensus.

Example:

grpcServer, err := rpc.NewServer(logger, rpc.WithStatsHandler(perf.NewGrpcStatsHandler()))

See further documentation here: https://opencensus.io/guides/grpc/go/

Example

ExampleNewGrpcStatsHandler shows how to create a new gRPC server with intrumentation for metrics/spans.

logger := golog.NewDevelopmentLogger("perf-example")

// Create a new perf.Exporter that collects metrics/spans and exports them to the correct backend.
// For development we log to console but in production use `perf.NewCloudExporter()` to send them
// to GCP Cloud Monitoring (Stackdriver).
exporter := NewDevelopmentExporter()

// Create a gRPC server that is instrumented with metrics/spans collection.
serverOpts := []rpc.ServerOption{
	rpc.WithDebug(),
	// Add the stats handler to the gRPC server. Will capture rpc request counts, latency, bytes received.
	// See further documentation here: https://opencensus.io/guides/grpc/go/
	rpc.WithStatsHandler(NewGrpcStatsHandler()),
}
grpcServer, err := rpc.NewServer(logger, serverOpts...)
if err != nil {
	logger.Panic("failed to start grpc server")
}

// Start the exporting of metrics and spans.
exporter.Start()
// Don't forget to stop the collection once finished. Stop() will flush any pending metrics/spans.
defer exporter.Stop()

grpcServer.Stop()
Output:

func NewMongoDBMonitor added in v0.1.14

func NewMongoDBMonitor(opts ...MongoDBMonitorOption) *event.CommandMonitor

NewMongoDBMonitor creates a new mongodb event CommandMonitor.

func NewMongoDBPoolMonitor added in v0.1.14

func NewMongoDBPoolMonitor() *event.PoolMonitor

NewMongoDBPoolMonitor creates a new mongodb pool event PoolMonitor.

func NewRoundTripperWithStats added in v0.0.5

func NewRoundTripperWithStats() http.RoundTripper

NewRoundTripperWithStats creates a new RoundTripper with stats collecting writing to opencensus.

Example:

client := &http.Client{Transport: perf.NewRoundTripperWithStats()},
Example

ExampleNewRoundTripperWithStats shows how to instrument a new HTTP client with metrics.

logger := golog.NewDevelopmentLogger("perf-example")
client := &http.Client{
	// Instrument the HTTP client with a new RoundTripper Transport that records the metrics/spans
	Transport: NewRoundTripperWithStats(),
}

res, err := client.Get("http://localhost:1234")
if err != nil {
	logger.Panic("failed to start grpc server")
}
defer res.Body.Close()
Output:

func WrapHTTPHandlerForStats added in v0.0.5

func WrapHTTPHandlerForStats(h http.Handler) http.Handler

WrapHTTPHandlerForStats wraps a http handler with stats collection writing to opencensus.

Example:

httpHandler := goji.NewMux()
hanlderWithStats = perf.WrapHTTPHandlerForStats(httpHandler)
Example

ExampleWrapHTTPHandlerForStats shows how to create a new HTTP server with intrumentation for metrics/spans.

ctx := context.Background()

// Create a new perf.Exporter that collects metrics/spans and exports them to the correct backend.
// For development we log to console but in production use `perf.NewCloudExporter()` to send them
// to GCP Cloud Monitoring (Stackdriver).
exporter := NewDevelopmentExporter()

// Create a HTTP server that is instrumented with metrics/spans collection.
httpServerExitDone := &sync.WaitGroup{}
mux := goji.NewMux()
mux.HandleFunc(pat.Get("/hello/:name"), func(w http.ResponseWriter, r *http.Request) {
	name := pat.Param(r, "name")
	fmt.Fprintf(w, "Hello, %s!", name)
})
srv := &http.Server{
	Addr: "localhost:0",
	// To instrument the HTTP server wrap the handler with `perf.WrapHTTPHandlerForStats()`.
	Handler: WrapHTTPHandlerForStats(mux),
}
go func() {
	defer httpServerExitDone.Done() // let main know we are done cleaning up

	// always returns error. ErrServerClosed on graceful close
	if err := srv.ListenAndServe(); err != http.ErrServerClosed {
		// unexpected error. port in use?
		log.Fatalf("ListenAndServe(): %v", err)
	}
}()

if err := srv.Shutdown(ctx); err != nil {
	panic(err) // failure/timeout shutting down the server gracefully
}
// wait for goroutine started in startHttpServer() to stop
httpServerExitDone.Wait()

// Start the exporting of metrics and spans.
exporter.Start()
// Don't forget to stop the collection once finished. Stop() will flush any pending metrics/spans.
defer exporter.Stop()
Output:

Types

type CloudOptions added in v0.0.5

type CloudOptions struct {
	Context      context.Context
	Logger       golog.Logger
	MetricPrefix string // Optional metric prefix.
}

CloudOptions are options for the production cloud exporter to Stackdriver (Cloud Monitoring).

type DevelopmentExporterOptions added in v0.1.14

type DevelopmentExporterOptions struct {
	// ReportingInterval is a time interval between two successive metrics
	// export.
	ReportingInterval time.Duration

	// MetricsDisabled determines if metrics reporting is disabled or not.
	MetricsDisabled bool

	// TracesDisabled determines if trace reporting is disabled or not.
	TracesDisabled bool
}

DevelopmentExporterOptions provides options for DevelopmentExporter.

type Exporter added in v0.0.5

type Exporter interface {
	// Start will start the exporting of metrics and return any errors if failed to start.
	Start() error
	// Stop will stop all exporting and flush remaining metrics.
	Stop()
}

Exporter wrapper around Trace and Metric exporter for OpenCensus.

func NewCloudExporter added in v0.0.5

func NewCloudExporter(opts CloudOptions) (Exporter, error)

NewCloudExporter creates a new Stackdriver (Cloud Monitoring) OpenCensus exporter with all options setup views registered..

func NewDevelopmentExporter added in v0.0.5

func NewDevelopmentExporter() Exporter

NewDevelopmentExporter creates a new log exporter.

func NewDevelopmentExporterWithOptions added in v0.1.14

func NewDevelopmentExporterWithOptions(options DevelopmentExporterOptions) Exporter

NewDevelopmentExporterWithOptions creates a new log exporter with the given options.

type MongoDBMonitorOption added in v0.1.14

type MongoDBMonitorOption func(*config)

MongoDBMonitorOption represents an option that can be passed to NewMongoDBMonitor.

func WithMongoDBMonitorSampler added in v0.1.14

func WithMongoDBMonitorSampler(sampler trace.Sampler) MongoDBMonitorOption

WithMongoDBMonitorSampler set a sampler for all started spans.

Directories

Path Synopsis
Package statz used to wrap OpenCensus metric collection
Package statz used to wrap OpenCensus metric collection
example
Package main is an example of application stats collection
Package main is an example of application stats collection
internal
Package internal holds the global state for Statz
Package internal holds the global state for Statz
units
Package units contains unit definitions for statz
Package units contains unit definitions for statz

Jump to

Keyboard shortcuts

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