opencensus

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2018 License: Apache-2.0 Imports: 0 Imported by: 0

README

OpenCensus Libraries for Go

Build Status Windows Build Status GoDoc Gitter chat

OpenCensus Go is a Go implementation of OpenCensus, a toolkit for collecting application performance and behavior monitoring data. Currently it consists of three major components: tags, stats, and tracing.

This project is still at a very early stage of development. The API is changing rapidly, vendoring is recommended.

Installation

$ go get -u go.opencensus.io/...

Prerequisites

OpenCensus Go libraries require Go 1.8 or later.

Exporters

OpenCensus can export instrumentation data to various backends. Currently, OpenCensus supports:

Tags

Tags represent propagated key-value pairs. They are propagated using context.Context in the same process or can be encoded to be transmitted on the wire and decoded back to a tag.Map at the destination.

Getting a key by a name

A key is defined by its name. To use a key, a user needs to know its name and type.

// Get a key to represent user OS.
key, err := tag.NewKey("my.org/keys/user-os")
if err != nil {
	log.Fatal(err)
}
Creating tags and propagating them

Package tag provides a builder to create tag maps and put it into the current context. To propagate a tag map to downstream methods and RPCs, New will add the produced tag map to the current context. If there is already a tag map in the current context, it will be replaced.

osKey, err := tag.NewKey("my.org/keys/user-os")
if err != nil {
	log.Fatal(err)
}
userIDKey, err := tag.NewKey("my.org/keys/user-id")
if err != nil {
	log.Fatal(err)
}

ctx, err = tag.New(ctx,
	tag.Insert(osKey, "macOS-10.12.5"),
	tag.Upsert(userIDKey, "cde36753ed"),
)
if err != nil {
	log.Fatal(err)
}
Propagating a tag map in a context

If you have access to a tag.Map, you can also propagate it in the current context:

m := tag.FromContext(ctx)

In order to update existing tags from the current context, use New and pass the returned context.

ctx, err = tag.New(ctx,
	tag.Insert(osKey, "macOS-10.12.5"),
	tag.Upsert(userIDKey, "fff0989878"),
)
if err != nil {
	log.Fatal(err)
}

Stats

Measures

Measures are used for recording data points with associated units. Creating a Measure:

videoSize, err := stats.Int64("my.org/video_size", "processed video size", "MB")
if err != nil {
	log.Fatal(err)
}
Recording Measurements

Measurements are data points associated with Measures. Recording implicitly tags the set of Measurements with the tags from the provided context:

stats.Record(ctx, videoSize.M(102478))
Views

Views are how Measures are aggregated. You can think of them as queries over the set of recorded data points (Measurements).

Views have two parts: the tags to group by and the aggregation type used.

Currently four types of aggregations are supported:

  • CountAggregation is used to count the number of times a sample was recorded.
  • DistributionAggregation is used to provide a histogram of the values of the samples.
  • SumAggregation is used to sum up all sample values.
  • MeanAggregation is used to calculate the mean of sample values.
distAgg := view.DistributionAggregation([]float64{0, 1 << 32, 2 << 32, 3 << 32})
countAgg := view.CountAggregation{}
sumAgg := view.SumAggregation{}
meanAgg := view.MeanAggregation{}

Here we create a view with the DistributionAggregation over our Measure. All Measurements will be aggregated together irrespective of their tags, i.e. no grouping by tag.

err = view.Subscribe(&view.View{
	Name:        "my.org/video_size_distribution",
	Description: "distribution of processed video size over time",
	Measure:     videoSize,
	Aggregation: view.DistributionAggregation([]float64{0, 1 << 32, 2 << 32, 3 << 32}),
})
if err != nil {
	log.Fatal(err)
}

Subscribe begins collecting data for the view. Subscribed views' data will be exported via the registered exporters.

// Register an exporter to be able to retrieve
// the data from the subscribed views.
view.RegisterExporter(&exporter{})

An example logger exporter is below:


type exporter struct{}

func (e *exporter) ExportView(vd *view.Data) {
	log.Println(vd)
}

Configure the default interval between reports of collected data. This is a system wide interval and impacts all views. The default interval duration is 10 seconds.

view.SetReportingPeriod(5 * time.Second)

Traces

Starting and ending a span
ctx, span := trace.StartSpan(ctx, "your choice of name")
defer span.End()

More tracing examples are coming soon...

Profiles

OpenCensus tags can be applied as profiler labels for users who are on Go 1.9 and above.

ctx, err = tag.New(ctx,
	tag.Insert(osKey, "macOS-10.12.5"),
	tag.Insert(userIDKey, "fff0989878"),
)
if err != nil {
	log.Fatal(err)
}
tag.Do(ctx, func(ctx context.Context) {
	// Do work.
	// When profiling is on, samples will be
	// recorded with the key/values from the tag map.
})

A screenshot of the CPU profile from the program above:

CPU profile

Documentation

Overview

Package opencensus contains Go support for OpenCensus.

Directories

Path Synopsis
examples
grpc/proto
Package helloworld is a generated protocol buffer package.
Package helloworld is a generated protocol buffer package.
helloworld
Command helloworld is an example program that collects data for video size.
Command helloworld is an example program that collects data for video size.
stats/prometheus
Command prometheus is an example program that collects data for video size.
Command prometheus is an example program that collects data for video size.
stats/stackdriver
Command stackdriver is an example program that collects data for video size.
Command stackdriver is an example program that collects data for video size.
trace/jaeger
Command jaeger is an example program that creates spans and uploads to Jaeger.
Command jaeger is an example program that creates spans and uploads to Jaeger.
exporter
jaeger
Package jaeger contains an OpenCensus tracing exporter for Jaeger.
Package jaeger contains an OpenCensus tracing exporter for Jaeger.
prometheus
Package prometheus contains the Prometheus exporters for Stackdriver Monitoring.
Package prometheus contains the Prometheus exporters for Stackdriver Monitoring.
stackdriver
Package stackdriver contains the OpenCensus exporters for Stackdriver Monitoring and Stackdriver Tracing.
Package stackdriver contains the OpenCensus exporters for Stackdriver Monitoring and Stackdriver Tracing.
zipkin
Package zipkin contains an trace exporter for Zipkin.
Package zipkin contains an trace exporter for Zipkin.
readme
Package readme generates the README.
Package readme generates the README.
tagencoding
Package tagencoding contains the tag encoding used interally by the stats collector.
Package tagencoding contains the tag encoding used interally by the stats collector.
plugin
ocgrpc
Package ocgrpc contains OpenCensus stats and trace integrations for gRPC.
Package ocgrpc contains OpenCensus stats and trace integrations for gRPC.
ochttp
Package ochttp provides OpenCensus instrumentation for net/http package.
Package ochttp provides OpenCensus instrumentation for net/http package.
ochttp/propagation/b3
Package b3 contains a propagation.HTTPFormat implementation for B3 propagation.
Package b3 contains a propagation.HTTPFormat implementation for B3 propagation.
ochttp/propagation/google
Package google contains a propagation.HTTPFormat implementation for Google Cloud Trace and Stackdriver.
Package google contains a propagation.HTTPFormat implementation for Google Cloud Trace and Stackdriver.
ochttp/propagation/tracecontext
Package tracecontext contains HTTP propagator for TraceContext standard.
Package tracecontext contains HTTP propagator for TraceContext standard.
Package stats contains support for OpenCensus stats recording.
Package stats contains support for OpenCensus stats recording.
view
Package view contains support for collecting and exposing aggregates over stats.
Package view contains support for collecting and exposing aggregates over stats.
Package tag contains OpenCensus tags.
Package tag contains OpenCensus tags.
Package trace contains types for representing trace information, and functions for global configuration of tracing.
Package trace contains types for representing trace information, and functions for global configuration of tracing.
propagation
Package propagation implements the binary trace context format.
Package propagation implements the binary trace context format.
Package zpages implements a collection of HTML pages that display RPC stats and trace data, and also functions to write that same data in plain text to an io.Writer.
Package zpages implements a collection of HTML pages that display RPC stats and trace data, and also functions to write that same data in plain text to an io.Writer.

Jump to

Keyboard shortcuts

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