opencensus

package
v3.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 22 Imported by: 21

README

Opencensus

This module is deprecated and will likely be removed in future versions of flamingo. Please use the OpenTelemetry module instead.

General usage

First, if you're not sure what opencensus is, please visit https://opencensus.io/ to learn more about it.

Opencensus allows you to collect data for your application and process them via e.g. Prometheus, Jaeger, etc. The core package already collects metrics automatically for routers and prefixrouters rendering times out of the box. Also traces for request handling are done by default.

The metrics endpoint is provided under the systemendpoint. Once the module is activate you can access them via http://localhost:13210/metrics

Adding your own metrics

The most likely usecase is in a controller, but to have your own metric, please import the following packages:

  • "flamingo.me/flamingo/framework/opencensus"
  • "go.opencensus.io/stats"
  • "go.opencensus.io/stats/view"

Then, create a variable for your metric. See below example:

var stat = stats.Int64("flamingo/package/mystat", "my stat records 5 milliseconds per call", stats.UnitMilliseconds)

The Variable name of stat is totally arbitrary here. But "flamingo/package/mystat" is actually the metric id you're recording to. Normally, this should be a pattern of "yourproject/subpackage/yourstat". Followed up by a description of your metric and the Unit which is recorded (use stats.UnitDimensionless for simple counters).

To be able to transmit this metric, register it in an init method:

// register view in opencensus
func init() {
  opencensus.View("flamingo/package/mystat/sum", stat, view.Sum())
  opencensus.View("flamingo/package/mystat/count", stat, view.Count())
}

That's all that is needed as Setup. You can add to your metric in your code now. It's as simple as adding an increment:

func (c *Controller) Get(ctx context.Context, r *web.Request) web.Result {
  // ...

  // record 5ms per call
  stats.Record(ctx, stat.M(5))

  // ...
}

For a simple counter where stat is a stats.UnitDimensionless metric, you'd simply increment by one, i.e.

// record increment of 1 per call
stats.Record(ctx, stat.M(1))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// KeyArea is the key to represent the current flamingo area
	KeyArea, _ = tag.NewKey("area")
)

Functions

func URLPrefixSampler

func URLPrefixSampler(allowed, blocked []string, allowParentTrace bool) func(*http.Request) trace.StartOptions

URLPrefixSampler creates a sampling getter for ochttp.Server.

If the whitelist is empty it is treated as allowed, otherwise checked first. If the blacklist is set it will disable sampling again. If takeParentDecision is set we allow the decision to be taken from incoming tracing, otherwise we enforce our decision

func View

func View(name string, m stats.Measure, aggr *view.Aggregation, tagKeys ...tag.Key) error

View helps to register opencensus views with the default "area" tag

Types

type ConfiguredURLPrefixSampler

type ConfiguredURLPrefixSampler struct {
	Whitelist        config.Slice
	Blacklist        config.Slice
	AllowParentTrace bool
}

ConfiguredURLPrefixSampler constructs the prefix GetStartOptions getter with the default opencensus configuration

func (*ConfiguredURLPrefixSampler) GetStartOptions

func (c *ConfiguredURLPrefixSampler) GetStartOptions() func(*http.Request) trace.StartOptions

GetStartOptions constructor for ochttp.Server

func (*ConfiguredURLPrefixSampler) Inject added in v3.3.0

func (c *ConfiguredURLPrefixSampler) Inject(
	cfg *struct {
		Whitelist        config.Slice `inject:"config:flamingo.opencensus.tracing.sampler.whitelist,optional"`
		Blacklist        config.Slice `inject:"config:flamingo.opencensus.tracing.sampler.blacklist,optional"`
		AllowParentTrace bool         `inject:"config:flamingo.opencensus.tracing.sampler.allowParentTrace,optional"`
	},
) *ConfiguredURLPrefixSampler

Inject dependencies

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module registers the opencensus module which in turn enables jaeger & co. Deprecated: OpenCensus was discontinued in favor of OpenTelemetry, please use flamingo.me/opentelemetry instead.

func (*Module) Configure

func (m *Module) Configure(injector *dingo.Injector)

Configure the opencensus Module

func (*Module) CueConfig added in v3.1.0

func (m *Module) CueConfig() string

CueConfig defines the opencensus config scheme

func (*Module) Depends

func (m *Module) Depends() []dingo.Module

Depends on other modules

func (*Module) FlamingoLegacyConfigAlias added in v3.1.0

func (*Module) FlamingoLegacyConfigAlias() map[string]string

FlamingoLegacyConfigAlias maps legacy config to new

func (*Module) Inject added in v3.3.0

func (m *Module) Inject(
	cfg *struct {
		Endpoint       string `inject:"config:flamingo.opencensus.jaeger.endpoint"`
		ServiceName    string `inject:"config:flamingo.opencensus.serviceName"`
		ServiceAddr    string `inject:"config:flamingo.opencensus.serviceAddr"`
		JaegerEnable   bool   `inject:"config:flamingo.opencensus.jaeger.enable"`
		ZipkinEnable   bool   `inject:"config:flamingo.opencensus.zipkin.enable"`
		ZipkinEndpoint string `inject:"config:flamingo.opencensus.zipkin.endpoint"`
	},
) *Module

Inject dependencies

Jump to

Keyboard shortcuts

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