stackdriver

package module
v0.2.1-0...-3aca219 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2020 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.

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

This is not an officially supported Google product.

Documentation

Overview

Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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 your 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

	// Debug logging. Errors will be 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
}

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

Samples are for timing information, where quantiles are used.

func (*Sink) AddSampleWithLabels

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

Samples are for timing information, where quantiles are used.

func (*Sink) EmitKey

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

Should emit a Key/Value pair for each call.

func (*Sink) IncrCounter

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

Counters should accumulate values.

func (*Sink) IncrCounterWithLabels

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

Counters should accumulate values.

func (*Sink) SetGauge

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

A Gauge should retain the last value it is set to.

func (*Sink) SetGaugeWithLabels

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

A Gauge should retain the last value it is set to.

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