opencensus

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2018 License: Apache-2.0 Imports: 0 Imported by: 9

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 can be 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. Currently, only keys of type string are supported. Other types will be supported in the future.

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

tag.Map is a map of tags. Package tags provide a builder to create tag maps.

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)
}

tagMap, err := tag.NewMap(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

To propagate a tag map to downstream methods and RPCs, add a tag map to the current context. NewContext will return a copy of the current context, and put the tag map into the returned one. If there is already a tag map in the current context, it will be replaced.

ctx = tag.NewContext(ctx, tagMap)

In order to update an existing tag map, get the tag map from the current context, use NewMap and put the new tag map back to the context.

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

Stats

Creating, retrieving and deleting a measure

Create and load measures with units:

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

Retrieve measure by name:

m := stats.FindMeasure("my.org/video_size")
if m == nil {
	log.Fatalln("measure not found")
}

Delete measure (this can be useful when replacing a measure by another measure with the same name):

if err := stats.DeleteMeasure(m); err != nil {
	log.Fatal(err)
}

However, it is an error to delete a Measure that's used by at least one View. The View using the Measure has to be unregistered first.

Creating an aggregation

Currently 4 types of aggregations are supported. The CountAggregation is used to count the number of times a sample was recorded. The DistributionAggregation is used to provide a histogram of the values of the samples. The SumAggregation is used to sum up all sample values. The MeanAggregation is used to calculate the mean of sample values.

distAgg := stats.DistributionAggregation([]float64{0, 1 << 32, 2 << 32, 3 << 32})
countAgg := stats.CountAggregation{}
sumAgg := stats.SumAggregation{}
meanAgg := stats.MeanAggregation{}
Create an aggregation window

Use Cumulative to continuously aggregate the recorded data.

cum := stats.Cumulative{}
Creating, registering and unregistering a view

Create and register a view:

view, err := stats.NewView(
	"my.org/video_size_distribution",
	"distribution of processed video size over time",
	nil,
	videoSize,
	distAgg,
	cum,
)
if err != nil {
	log.Fatalf("cannot create view: %v", err)
}
if err := stats.RegisterView(view); err != nil {
	log.Fatal(err)
}

Find view by name:

v := stats.FindView("my.org/video_size_distribution")
if v == nil {
	log.Fatalln("view not found")
}

Unregister view:

if err = stats.UnregisterView(v); err != nil {
	log.Fatal(err)
}

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. Trying to set an interval with a duration less than a certain minimum (maybe 1s) should have no effect.

stats.SetReportingPeriod(5 * time.Second)
Recording measurements

Recording usage can only be performed against already registered measure and their registered views. Measurements are implicitly tagged with the tags in the context:

stats.Record(ctx, videoSize.M(102478))
Retrieving collected data for a view

Users need to subscribe to a view in order to retrieve collected data.

if err := view.Subscribe(); err != nil {
	log.Fatal(err)
}

Subscribed views' data will be exported via the registered exporters.

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

An example logger exporter is below:


type exporter struct{}

func (e *exporter) ExportView(vd *stats.ViewData) {
	log.Println(vd)
}

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.

tagMap, err = tag.NewMap(ctx,
	tag.Insert(osKey, "macOS-10.12.5"),
	tag.Insert(userIDKey, "fff0989878"),
)
if err != nil {
	log.Fatal(err)
}
ctx = tag.NewContext(ctx, tagMap)
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.
stats/helloworld
Command helloworld is an example program that collects data for video size over a time window.
Command helloworld is an example program that collects data for video size over a time window.
stats/prometheus
Command prometheus is an example program that collects data for video size over a time window.
Command prometheus is an example program that collects data for video size over a time window.
stats/stackdriver
Command stackdriver is an example program that collects data for video size over a time window.
Command stackdriver is an example program that collects data for video size over a time window.
trace/helloworld
Command helloworld is an example program that creates spans.
Command helloworld is an example program that creates spans.
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 Tracing.
Package stackdriver contains the OpenCensus exporters for Stackdriver Monitoring and Tracing.
zipkin
Package zipkin contains an exporter for Zipkin.
Package zipkin contains an 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
grpc
Package grpc contains OpenCensus stats and trace integrations with gRPC.
Package grpc contains OpenCensus stats and trace integrations with gRPC.
grpc/grpcstats
Package grpcstats provides OpenCensus stats support for gRPC clients and servers.
Package grpcstats provides OpenCensus stats support for gRPC clients and servers.
grpc/grpctrace
Package grpctrace is a package to assist with tracing incoming and outgoing gRPC requests.
Package grpctrace is a package to assist with tracing incoming and outgoing gRPC requests.
http/httptrace
Package httptrace contains OpenCensus tracing integrations with net/http.
Package httptrace contains OpenCensus tracing integrations with net/http.
http/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.
Package stats contains support for OpenCensus stats collection.
Package stats contains support for OpenCensus stats collection.
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