stackdriver

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

README

go-metrics-stackdriver

godoc

This library provides a stackdriver sink for applications instrumented with the go-metrics library.

Why did i fork this?

I want to experiment with client side windowed counters and delta type metrics

Details

stackdriver.NewSink's return value satisfies the go-metrics library's MetricSink interface. When providing a stackdriver.Sink to libraries and applications instrumented against MetricSink, the metrics will be aggregated within this library and written to stackdriver as Generic Task timeseries metrics.

Example

import "github.com/google/go-metrics-stackdriver"
...
client, _ := monitoring.NewMetricClient(context.Background())
ss := stackdriver.NewSink(client, &stackdriver.Config{
  ProjectID: projectID,
})
...
ss.SetGauge([]string{"foo"}, 42)
ss.IncrCounter([]string{"baz"}, 1)
ss.AddSample([]string{"method", "const"}, 200)

The full example can be run from a cloud shell console to test how metrics are collected and displayed.

You can also try out the example using Cloud Run!

Run on Google Cloud

Documentation

Overview

Package stackdriver provides a cloud monitoring sink for applications instrumented with the go-metrics library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultBucketer

func DefaultBucketer(key []string) []float64

DefaultBucketer is the default BucketFn used to determing bucketing values for metrics.

func DefaultLabelExtractor

func DefaultLabelExtractor(key []string, kind string) ([]string, []metrics.Label, error)

DefaultLabelExtractor is the default ExtractLabelsFn and is a direct passthrough. Counter and Gauge metrics are renamed to include the type in their name to avoid duplicate metrics with the same name but different types (which is allowed by go-metrics but not by Stackdriver).

Types

type BucketFn

type BucketFn func([]string) []float64

BucketFn returns the histogram bucket thresholds based on the given metric name.

type Config

type Config struct {
	// The Google Cloud Project ID to publish metrics to.
	// Optional. GCP instance metadata is used to determine the ProjectID if
	// not set.
	ProjectID string
	// The label extractor provides a way to rewrite metric key into a new key
	// with multiple labels. This is useful if the instrumented code includes
	// variable parameters within a metric name.
	// Optional. Defaults to DefaultLabelExtractor.
	LabelExtractor ExtractLabelsFn
	// Prefix of the metrics recorded. Defaults to "go-metrics/" so a metric
	// "foo" will be recorded as "custom.googleapis.com/go-metrics/foo".
	Prefix *string
	// The bucketer is used to determine histogram bucket boundaries
	// for the sampled metrics. This will execute before the LabelExtractor.
	// Optional. Defaults to DefaultBucketer.
	Bucketer BucketFn
	// The interval between sampled metric points. Must be > 1 minute.
	// https://cloud.google.com/monitoring/custom-metrics/creating-metrics#writing-ts
	// Optional. Defaults to 1 minute.
	ReportingInterval time.Duration

	// The location of the running task. See:
	// https://cloud.google.com/monitoring/api/resources#tag_generic_task
	// Optional. GCP instance metadata is used to determine the location,
	// otherwise it defaults to 'global'.
	Location string
	// The namespace for the running task. See:
	// https://cloud.google.com/monitoring/api/resources#tag_generic_task
	// Optional. Defaults to 'default'.
	Namespace string
	// The job name for the running task. See:
	// https://cloud.google.com/monitoring/api/resources#tag_generic_task
	// Optional. Defaults to the running program name.
	Job string
	// The task ID for the running task. See:
	// https://cloud.google.com/monitoring/api/resources#tag_generic_task
	// Optional. Defaults to a combination of hostname+pid.
	TaskID string

	// the amount of time that local counter will retain increments.
	// if 0 the window will be infinite
	CounterWindow time.Duration

	// Debug logging. Errors are always logged to stderr, but setting this to
	// true will log additional information that is helpful when debugging
	// errors.
	// Optional. Defaults to false.
	DebugLogs bool

	// MonitoredResource identifies the machine/service/resource that is
	// monitored. Different possible settings are defined here:
	// https://cloud.google.com/monitoring/api/resources
	//
	// Setting a nil MonitoredResource will run a defaultMonitoredResource
	// function.
	MonitoredResource *monitoredrespb.MonitoredResource

	// Logger that can be injected for custom log formatting.
	Logger Logger
}

Config options for the stackdriver Sink.

type ExtractLabelsFn

type ExtractLabelsFn func([]string, string) ([]string, []metrics.Label, error)

ExtractLabelsFn converts a given metric name and type into a new metric name and optionally additional labels. Errors will prevent the metric from writing to stackdriver.

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

Logger is the log interface used in go-metrics-stackdriver

type Sink

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

Sink conforms to the metrics.MetricSink interface and is used to transmit metrics information to stackdriver.

Sink performs in-process aggregation of metrics to limit calls to stackdriver.

func NewSink

func NewSink(client *monitoring.MetricClient, config *Config) *Sink

NewSink creates a Sink to flush metrics to stackdriver every interval. The interval should be greater than 1 minute.

func (*Sink) AddSample

func (s *Sink) AddSample(key []string, val float32)

AddSample adds a sample to a histogram metric.

func (*Sink) AddSampleWithLabels

func (s *Sink) AddSampleWithLabels(key []string, val float32, labels []metrics.Label)

AddSampleWithLabels adds a sample to a histogram metric.

func (*Sink) Close

func (s *Sink) Close(ctx context.Context) error

Close closes the sink and flushes any remaining data.

func (*Sink) EmitKey

func (s *Sink) EmitKey(key []string, val float32)

EmitKey is not implemented.

func (*Sink) IncrCounter

func (s *Sink) IncrCounter(key []string, val float32)

IncrCounter increments a counter by a value.

func (*Sink) IncrCounterWithLabels

func (s *Sink) IncrCounterWithLabels(key []string, val float32, labels []metrics.Label)

IncrCounterWithLabels increments a counter by a value.

func (*Sink) SetGauge

func (s *Sink) SetGauge(key []string, val float32)

SetGauge retains the last value it is set to.

func (*Sink) SetGaugeWithLabels

func (s *Sink) SetGaugeWithLabels(key []string, val float32, labels []metrics.Label)

SetGaugeWithLabels retains the last value it is set to.

func (*Sink) Shutdown

func (s *Sink) Shutdown()

Shutdown is a blocking function that flushes metrics in preparation of the application exiting.

Directories

Path Synopsis
Package vault provides helper functions to improve the go-metrics to stackdriver metric conversions specific to HashiCorp Vault.
Package vault provides helper functions to improve the go-metrics to stackdriver metric conversions specific to HashiCorp Vault.

Jump to

Keyboard shortcuts

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