| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package event |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
type Unit string |
| 14 |
|
| 15 |
const ( |
| 16 |
UnitDimensionless Unit = "1" |
| 17 |
UnitBytes Unit = "By" |
| 18 |
UnitMilliseconds Unit = "ms" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
type Metric interface { |
| 23 |
Name() string |
| 24 |
Options() MetricOptions |
| 25 |
} |
| 26 |
|
| 27 |
type MetricOptions struct { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
Namespace string |
| 32 |
|
| 33 |
|
| 34 |
Description string |
| 35 |
|
| 36 |
|
| 37 |
Unit Unit |
| 38 |
} |
| 39 |
|
| 40 |
|
| 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 |
|
| 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 |
|
| 69 |
|
| 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 |
|
| 80 |
|
| 81 |
type FloatGauge struct { |
| 82 |
name string |
| 83 |
opts MetricOptions |
| 84 |
} |
| 85 |
|
| 86 |
|
| 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 |
|
| 95 |
|
| 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 |
|
| 106 |
|
| 107 |
type DurationDistribution struct { |
| 108 |
name string |
| 109 |
opts MetricOptions |
| 110 |
} |
| 111 |
|
| 112 |
|
| 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 |
|
| 121 |
|
| 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 |
|
| 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 |
|
| 141 |
func NewIntDistribution(name string, opts *MetricOptions) *IntDistribution { |
| 142 |
return &IntDistribution{name, initOpts(opts)} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
} |