Debian Codegraph @

event/metric.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package event
6
7 import (
8 "context"
9 "time"
10 )
11
12 // A Unit is a unit of measurement for a metric.
13 type Unit string
14
15 const (
16 UnitDimensionless Unit = "1"
17 UnitBytes Unit = "By"
18 UnitMilliseconds Unit = "ms"
19 )
20
21 // A Metric represents a kind of recorded measurement.
22 type Metric interface {
23 Name() string
24 Options() MetricOptions
25 }
26
27 type MetricOptions struct {
28 // A string that should be common for all metrics of an application or
29 // service. Defaults to the import path of the package calling
30 // the metric construction function (NewCounter, etc.).
31 Namespace string
32
33 // Optional description of the metric.
34 Description string
35
36 // Optional unit for the metric. Defaults to UnitDimensionless.
37 Unit Unit
38 }
39
40 // A Counter is a metric that counts something cumulatively.
41 type Counter struct {
42 name string
43 opts MetricOptions
44 }
45
46 func initOpts(popts *MetricOptions) MetricOptions {
47 var opts MetricOptions
48 if popts != nil {
49 opts = *popts
50 }
51 if opts.Namespace == "" {
52 opts.Namespace = scanStack().Space
53 }
54 if opts.Unit == "" {
55 opts.Unit = UnitDimensionless
56 }
57 return opts
58 }
59
60 // NewCounter creates a counter with the given name.
61 func NewCounter(name string, opts *MetricOptions) *Counter {
62 return &Counter{name, initOpts(opts)}
63 }
64
65 func (c *Counter) Name() string { return c.name }
66 func (c *Counter) Options() MetricOptions { return c.opts }
67
68 // Record delivers a metric event with the given metric, value and labels to the
69 // exporter in the context.
70 func (c *Counter) Record(ctx context.Context, v int64, labels ...Label) {
71 ev := New(ctx, MetricKind)
72 if ev != nil {
73 record(ev, c, Int64(string(MetricVal), v))
74 ev.Labels = append(ev.Labels, labels...)
75 ev.Deliver()
76 }
77 }
78
79 // A FloatGauge records a single floating-point value that may go up or down.
80 // TODO(generics): Gauge[T]
81 type FloatGauge struct {
82 name string
83 opts MetricOptions
84 }
85
86 // NewFloatGauge creates a new FloatGauge with the given name.
87 func NewFloatGauge(name string, opts *MetricOptions) *FloatGauge {
88 return &FloatGauge{name, initOpts(opts)}
89 }
90
91 func (g *FloatGauge) Name() string { return g.name }
92 func (g *FloatGauge) Options() MetricOptions { return g.opts }
93
94 // Record converts its argument into a Value and returns a MetricValue with the
95 // receiver and the value.
96 func (g *FloatGauge) Record(ctx context.Context, v float64, labels ...Label) {
97 ev := New(ctx, MetricKind)
98 if ev != nil {
99 record(ev, g, Float64(string(MetricVal), v))
100 ev.Labels = append(ev.Labels, labels...)
101 ev.Deliver()
102 }
103 }
104
105 // A DurationDistribution records a distribution of durations.
106 // TODO(generics): Distribution[T]
107 type DurationDistribution struct {
108 name string
109 opts MetricOptions
110 }
111
112 // NewDuration creates a new Duration with the given name.
113 func NewDuration(name string, opts *MetricOptions) *DurationDistribution {
114 return &DurationDistribution{name, initOpts(opts)}
115 }
116
117 func (d *DurationDistribution) Name() string { return d.name }
118 func (d *DurationDistribution) Options() MetricOptions { return d.opts }
119
120 // Record converts its argument into a Value and returns a MetricValue with the
121 // receiver and the value.
122 func (d *DurationDistribution) Record(ctx context.Context, v time.Duration, labels ...Label) {
123 ev := New(ctx, MetricKind)
124 if ev != nil {
125 record(ev, d, Duration(string(MetricVal), v))
126 ev.Labels = append(ev.Labels, labels...)
127 ev.Deliver()
128 }
129 }
130
131 // An IntDistribution records a distribution of int64s.
132 type IntDistribution struct {
133 name string
134 opts MetricOptions
135 }
136
137 func (d *IntDistribution) Name() string { return d.name }
138 func (d *IntDistribution) Options() MetricOptions { return d.opts }
139
140 // NewIntDistribution creates a new IntDistribution with the given name.
141 func NewIntDistribution(name string, opts *MetricOptions) *IntDistribution {
142 return &IntDistribution{name, initOpts(opts)}
143 }
144
145 // Record converts its argument into a Value and returns a MetricValue with the
146 // receiver and the value.
147 func (d *IntDistribution) Record(ctx context.Context, v int64, labels ...Label) {
148 ev := New(ctx, MetricKind)
149 if ev != nil {
150 record(ev, d, Int64(string(MetricVal), v))
151 ev.Labels = append(ev.Labels, labels...)
152 ev.Deliver()
153 }
154 }
155
156 func record(ev *Event, m Metric, l Label) {
157 ev.Labels = append(ev.Labels, l, MetricKey.Of(m))
158 }