opencensus

package module
Version: v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2021 License: Apache-2.0 Imports: 0 Imported by: 8

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.

OpenCensus and OpenTracing have merged to form OpenTelemetry, which serves as the next major version of OpenCensus and OpenTracing. OpenTelemetry will offer backwards compatibility with existing OpenCensus integrations, and we will continue to make security patches to existing OpenCensus libraries for two years. Read more about the merger here.

Installation

$ go get -u go.opencensus.io

The API of this project is still evolving, see: Deprecation Policy. The use of vendoring or a dependency management tool is recommended.

Prerequisites

OpenCensus Go libraries require Go 1.8 or later.

Getting Started

The easiest way to get started using OpenCensus in your application is to use an existing integration with your RPC framework:

If you're using a framework not listed here, you could either implement your own middleware for your framework or use custom stats and spans directly in your application.

Exporters

OpenCensus can export instrumentation data to various backends. OpenCensus has exporter implementations for the following, users can implement their own exporters by implementing the exporter interfaces (stats, trace):

Overview

OpenCensus Overview

In a microservices environment, a user request may go through multiple services until there is a response. OpenCensus allows you to instrument your services and collect diagnostics data all through your services end-to-end.

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. Usually, this will be handled by an integration plugin, e.g. ocgrpc.ServerHandler and ocgrpc.ClientHandler for gRPC.

Package tag allows adding or modifying tags in the current context.

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

Stats

OpenCensus is a low-overhead framework even if instrumentation is always enabled. In order to be so, it is optimized to make recording of data points fast and separate from the data aggregation.

OpenCensus stats collection happens in two stages:

  • Definition of measures and recording of data points
  • Definition of views and aggregation of the recorded data
Recording

Measurements are data points associated with a measure. 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 three 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.
distAgg := view.Distribution(1<<32, 2<<32, 3<<32)
countAgg := view.Count()
sumAgg := view.Sum()

Here we create a view with the DistributionAggregation over our measure.

if err := view.Register(&view.View{
	Name:        "example.com/video_size_distribution",
	Description: "distribution of processed video size over time",
	Measure:     videoSize,
	Aggregation: view.Distribution(1<<32, 2<<32, 3<<32),
}); err != nil {
	log.Fatalf("Failed to register view: %v", err)
}

Register begins collecting data for the view. Registered views' data will be exported via the registered exporters.

Traces

A distributed trace tracks the progression of a single user request as it is handled by the services and processes that make up an application. Each step is called a span in the trace. Spans include metadata about the step, including especially the time spent in the step, called the span’s latency.

Below you see a trace and several spans underneath it.

Traces and spans

Spans

Span is the unit step in a trace. Each span has a name, latency, status and additional metadata.

Below we are starting a span for a cache read and ending it when we are done:

ctx, span := trace.StartSpan(ctx, "cache.Get")
defer span.End()

// Do work to get from cache.
Propagation

Spans can have parents or can be root spans if they don't have any parents. The current span is propagated in-process and across the network to allow associating new child spans with the parent.

In the same process, context.Context is used to propagate spans. trace.StartSpan creates a new span as a root if the current context doesn't contain a span. Or, it creates a child of the span that is already in current context. The returned context can be used to keep propagating the newly created span in the current context.

ctx, span := trace.StartSpan(ctx, "cache.Get")
defer span.End()

// Do work to get from cache.

Across the network, OpenCensus provides different propagation methods for different protocols.

Execution Tracer

With Go 1.11, OpenCensus Go will support integration with the Go execution tracer. See Debugging Latency in Go for an example of their mutual use.

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

Deprecation Policy

Before version 1.0.0, the following deprecation policy will be observed:

No backwards-incompatible changes will be made except for the removal of symbols that have been marked as Deprecated for at least one minor release (e.g. 0.9.0 to 0.10.0). A release removing the Deprecated functionality will be made no sooner than 28 days after the first release in which the functionality was marked Deprecated.

Documentation

Overview

Package opencensus contains Go support for OpenCensus.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version is the current release version of OpenCensus in use.

Types

This section is empty.

Source Files

Directories

Path Synopsis
examples
derived_gauges
This example demonstrates the use of derived gauges.
This example demonstrates the use of derived gauges.
exporter
Package exporter contains a log exporter that supports exporting OpenCensus metrics and spans to a logging framework.
Package exporter contains a log exporter that supports exporting OpenCensus metrics and spans to a logging framework.
gauges
This example shows how to use gauge metrics.
This example shows how to use gauge metrics.
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.
quickstart
Command stats implements the stats Quick Start example from: https://opencensus.io/quickstart/go/metrics/
Command stats implements the stats Quick Start example from: https://opencensus.io/quickstart/go/metrics/
exporter
stackdriver/propagation
Package propagation implement X-Cloud-Trace-Context header propagation used by Google Cloud products.
Package propagation implement X-Cloud-Trace-Context header propagation used by Google Cloud products.
Package metric support for gauge and cumulative metrics.
Package metric support for gauge and cumulative metrics.
metricdata
Package metricdata contains the metrics data model.
Package metricdata contains the metrics data model.
metricexport
Package metricexport contains support for exporting metric data.
Package metricexport contains support for exporting metric data.
test
Package test for testing code instrumented with the metric and stats packages.
Package test for testing code instrumented with the metric and stats packages.
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/tracecontext
Package tracecontext contains HTTP propagator for TraceContext standard.
Package tracecontext contains HTTP propagator for TraceContext standard.
runmetrics
Package runmetrics contains support for runtime metrics.
Package runmetrics contains support for runtime metrics.
Package resource provides functionality for resource, which capture identifying information about the entities for which signals are exported.
Package resource provides functionality for resource, which capture identifying information about the entities for which signals are exported.
resourcekeys
Package resourcekeys contains well known type and label keys for resources.
Package resourcekeys contains well known type and label keys for resources.
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 support for OpenCensus distributed tracing.
Package trace contains support for OpenCensus distributed tracing.
propagation
Package propagation implements the binary trace context format.
Package propagation implements the binary trace context format.
tracestate
Package tracestate implements support for the Tracestate header of the W3C TraceContext propagation format.
Package tracestate implements support for the Tracestate header of the W3C TraceContext propagation 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.
check
Command version checks that the version string matches the latest Git tag.
Command version checks that the version string matches the latest Git tag.
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.
testpb
Package testpb is a generated protocol buffer package.
Package testpb is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL