metrics

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package metrics defines a common metric interface that can be implemented by different metric clients. By default metrics use the local implementation, which log the results of the metrics when Closed. To use a different implementation call that specific init method ex InitAndExportGCP. Call Close after all counters have been recorded.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAll

func CloseAll() error

CloseAll should be called only after all metrics have been recorded. It will call the correct close method on all created metrics based on which implementation was used. If the local implementation was used, the metric results will be logged.

func GetResults

func GetResults() (map[string]CounterResult, map[string]LatencyResult, error)

GetResults returns the results from all metrics. This should only be called in tests.

func InitAndExportGCP

func InitAndExportGCP(projectID string) error

InitAndExportGCP starts exporting metrics to GCP on a 60 second interval. Metrics can be created with NewCounter before calling InitAndExportGCP, but no callers should call Record() on any metric until InitAndExportGCP is called.

func InitLocal

func InitLocal()

InitLocal is optional and does nothing, but does make it clearer to the code reader that we are using the local implementation of metrics. The local implementation is used by default and logs all metrics upon call to CloseAll.

func InitNoOp

func InitNoOp()

InitNoOp initializes all metrics to have no-op behavior on all calls. Since many metrics may be globals or package-specific globals, this makes it easier to run t.Parallel tests where metric results are not checked or inconsequential.

func ResetAll

func ResetAll()

ResetAll resets the count/dist of all created metrics. It should only be called in tests. Metric results cannot be asserted in tests run in t.Parallel(). If using t.Parallel() consider InitNoOp instead.

Types

type Counter

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

Counter holds an implementation of the counterInterface. Call NewCounter() to get a Counter.

func NewCounter

func NewCounter(name, description, unit string, aggregation aggregation.Aggregation, tagKeys ...string) *Counter

NewCounter creates a counter. Name should be unique between all metrics. Subsequent calls to Record() should provide the TagValues to the TagKeys in the same order specified in NewCounter. TagKeys should be a closed set of values, for example FHIR Resource type. InitAndExportGCP and NewCounter can be called in any order. Subsequent calls to NewCounter with the same name will be ignored and log a warning. Counters should not store any PHI.

Example
// Init functions should only be called once.
// By default the metrics use a local implementation which logs the results of
// the metrics upon call to CloseAll().
// InitAndExportGCP() will write the metrics to GCP.

// Counters with aggregation type Count keep a count for a particular set of tag values.
c := NewCounter("ExampleCounterName", "Counter Description", "1", aggregation.Count, "FHIRResource", "FHIRVersion")

// NewCounter and Init (ex InitAndExportGCP) can be called in any order.
// Init must be called before the first call to Record unless the default local
// implementation is being used.
c.Record(context.Background(), 1, "OBSERVATION", "STU3")

// The tagValues "OBSERVATION", "STU3" must be passed in the same order as the
// tagKeys "FHIRResource", "FHIRVersion".
c.Record(context.Background(), 1, "OBSERVATION", "STU4")
c.Record(context.Background(), 3, "ENCOUNTER", "STU3")

// CloseAll should be called once at the end of the program. For the local
// implementation it will log all metrics. For GCP implementation it will
// flush and close the exporter to GCP.
CloseAll()
Output:

func (*Counter) Record

func (c *Counter) Record(ctx context.Context, val int64, tagValues ...string) error

Record adds val to the counter. The tagValues must match the tagKeys provided in the call to NewCounter. InitAndExportGCP MUST be called before the first call to Record unless the default local counter implementation is used. Counters should not store any PHI.

type CounterResult

type CounterResult struct {
	// Count maps a concatenation of the tagValues to count for those tagValues.
	// If no tags are used then Count will map the name to the count.
	Count       map[string]int64
	Name        string
	Description string
	Unit        string
	Aggregation aggregation.Aggregation
	TagKeys     []string
}

CounterResult holds the results for the counter. It can be printed using String().

func (*CounterResult) String

func (c *CounterResult) String() string

String returns a printable result. The result string is stable. There is no randomness in the ordering the results are printed.

type Latency

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

Latency holds an implementation of the latencyInterface. Call NewLatency() to get a Latency.

func NewLatency

func NewLatency(name, description, unit string, buckets []float64, tagKeys ...string) *Latency

NewLatency creates a Latency. Subsequent calls to Record() should provide the TagValues to the TagKeys in the same order specified in NewLatency. TagKeys should be a closed set of values, for example FHIR Resource type. The distribution is defined by the Buckets. For example, Buckets: [0, 3, 5] will create a distribution with 4 buckets where the last bucket is anything > 5. Dist: <0, >=0 <3, >=3 <5, >=5. InitAndExportGCP and NewLatency can be called in any order. Subsequent calls to NewLatency with the same name will be ignored and log a warning. Latency should not store any PHI.

Example
// Init functions should only be called once.
// By default the metrics use a local implementation which logs the results of
// the metrics upon call to CloseAll().
// InitAndExportGCP() will write the metrics to GCP.

// For Latency the distribution is defined by the Buckets. For example,
// Buckets: [0, 3, 5] will create a distribution with 4 buckets where the last
// bucket is anything > 5. Dist: <0, >=0 <3, >=3 <5, >=5. Recording a value of
// 3.5 will increment the >=3 <5 bucket.
l := NewLatency("ExampleLatencyName", "Latency Description", "ms", []float64{0, 3, 5}, "FHIRResource", "FHIRVersion")

// NewLatency and Init (ex InitAndExportGCP) can be called in any order. Init
// must be called before the first call to Record unless the default local
// implementation is being used.
l.Record(context.Background(), 1, "OBSERVATION", "STU3")

// The tagValues "OBSERVATION", "STU3" must be passed in the same order as the
// tagKeys "FHIRResource", "FHIRVersion".
l.Record(context.Background(), 1, "OBSERVATION", "STU4")
l.Record(context.Background(), 3, "ENCOUNTER", "STU3")

// CloseAll should be called once at the end of the program. For the local
// implementation it will log all metrics. For GCP implementation it will
// flush and close the exporter to GCP.
CloseAll()
Output:

func (*Latency) Record

func (l *Latency) Record(ctx context.Context, val float64, tagValues ...string) error

Record adds 1 to the correct bucket in the distribution. The tagValues must match the tagKeys provided in the call to NewLatency. InitAndExportGCP MUST be called before the first call to Record unless the default local counter implementation is used. Latency should not store any PHI.

type LatencyResult

type LatencyResult struct {
	// Dist maps a concatenation of the tagValues to distribution for those
	// tagValues. If no tags are used then Dist will map the name to the
	// distribution. The distribution is defined by the Buckets. For example,
	// Buckets: [0, 3, 5] will create a distribution with 4 buckets where the last
	// bucket is anything > 5. Dist: <0, >=0 <3, >=3 <5, >=5.
	Dist        map[string][]int
	Name        string
	Description string
	Unit        string
	Buckets     []float64
	TagKeys     []string
}

LatencyResult holds the results of the latency. It can be printed using String().

func (*LatencyResult) String

func (l *LatencyResult) String() string

String returns a printable result. The result string is stable. There is no randomness in the ordering the results are printed.

Directories

Path Synopsis
Package aggregation holds an enum of different aggregation types for Counters.
Package aggregation holds an enum of different aggregation types for Counters.
Package fake implements metrics that can be used in tests that are run with t.Parallel().
Package fake implements metrics that can be used in tests that are run with t.Parallel().
Package local contains a simple often non-blocking (on Increment) thread-safe counters that can be used across multiple goroutines, with results collected at the end.
Package local contains a simple often non-blocking (on Increment) thread-safe counters that can be used across multiple goroutines, with results collected at the end.
Package opencensus wraps the opencensus client to implement the interface found in metrics.go.
Package opencensus wraps the opencensus client to implement the interface found in metrics.go.

Jump to

Keyboard shortcuts

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