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 ¶
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.
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:
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 ¶
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 ¶
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. |