-
Notifications
You must be signed in to change notification settings - Fork 4
/
meter.ts
192 lines (163 loc) · 5.16 KB
/
meter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { BaseContext } from './base-context.js'
import { Numberish, toMetricValue } from './numberish.js'
import { NamedResultDescriptor } from './metadata.js'
import { AggregationConfig, AggregationType, MetricConfig, MetricType } from '@sentio/protos'
import { MapStateStorage } from '@sentio/runtime'
export type Labels = { [key: string]: string }
export class MetricOptions {
unit?: string
description?: string
sparse?: boolean
// persistentBetweenVersion?: boolean
aggregationConfig?: Partial<AggregationConfig>
}
export class CounterOptions {
unit?: string
description?: string
sparse?: boolean
resolutionConfig?: {
intervalInMinutes: number
}
}
// enum MetricType {
// Counter = 0,
// Gauge = 1,
// }
export class Metric extends NamedResultDescriptor {
config: MetricConfig
constructor(type: MetricType, name: string, option?: MetricConfig) {
super(name)
this.config = MetricConfig.fromPartial({ ...option, name: this.name, type })
const aggregationConfig = this.config.aggregationConfig
if (aggregationConfig && aggregationConfig.intervalInMinutes.length) {
if (aggregationConfig.intervalInMinutes.length > 1) {
console.error('current only support one intervalInMinutes, only first interval will be used for', name)
}
if (aggregationConfig.intervalInMinutes[0] > 0 && aggregationConfig.types.length === 0) {
aggregationConfig.types = [AggregationType.SUM, AggregationType.COUNT]
}
}
}
}
export class MetricState extends MapStateStorage<Metric> {
static INSTANCE = new MetricState()
getOrRegisterMetric(type: MetricType, name: string, option?: CounterOptions | MetricOptions): Metric {
const metricMap = this.getOrRegister()
let metric = metricMap.get(name)
if (metric && metric.config.type !== type) {
throw Error(`redefine ${name} of metric type ${type} that is previously ${metric.config.type}`)
}
if (!metric) {
if (type === MetricType.COUNTER) {
metric = Counter._create(name, option)
} else {
metric = Gauge._create(name, option)
}
}
metricMap.set(name, metric)
return metric
}
}
export class Counter extends Metric {
static register(name: string, option?: CounterOptions): Counter {
return MetricState.INSTANCE.getOrRegisterMetric(MetricType.COUNTER, name, option) as Counter
}
/**
* internal use only, to create a metric use {@link register} instead
*/
static _create(name: string, option?: CounterOptions): Counter {
return new Counter(name, option)
}
private constructor(name: string, option?: CounterOptions) {
super(
MetricType.COUNTER,
name,
MetricConfig.fromPartial({
...option,
aggregationConfig: {
intervalInMinutes: option?.resolutionConfig ? [option?.resolutionConfig?.intervalInMinutes] : []
}
})
)
}
add(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
this.record(ctx, value, labels, true)
}
sub(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
this.record(ctx, value, labels, false)
}
private record(ctx: BaseContext, value: Numberish, labels: Labels, add: boolean) {
ctx.update({
counters: [
{
metadata: ctx.getMetaData(this.name, labels),
metricValue: toMetricValue(value),
add: add,
runtimeInfo: undefined
}
]
})
}
}
export class CounterBinding {
private readonly ctx: BaseContext
private readonly counter: Counter
constructor(name: string, ctx: BaseContext) {
this.counter = Counter._create(name)
this.ctx = ctx
}
add(value: Numberish, labels: Labels = {}) {
this.counter.add(this.ctx, value, labels)
}
sub(value: Numberish, labels: Labels = {}) {
this.counter.sub(this.ctx, value, labels)
}
}
export class Gauge extends Metric {
static register(name: string, option?: MetricOptions): Gauge {
return MetricState.INSTANCE.getOrRegisterMetric(MetricType.GAUGE, name, option) as Gauge
}
/**
* internal use only, to create a metric use {@link register} instead
*/
static _create(name: string, option?: MetricOptions): Gauge {
return new Gauge(name, option)
}
private constructor(name: string, option?: MetricOptions) {
super(MetricType.GAUGE, name, MetricConfig.fromPartial({ ...option }))
}
record(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
ctx.update({
gauges: [
{
metadata: ctx.getMetaData(this.config.name, labels),
metricValue: toMetricValue(value),
runtimeInfo: undefined
}
]
})
}
}
export class GaugeBinding {
private readonly gauge: Gauge
private readonly ctx: BaseContext
constructor(name: string, ctx: BaseContext) {
this.gauge = Gauge._create(name)
this.ctx = ctx
}
record(value: Numberish, labels: Labels = {}) {
this.gauge.record(this.ctx, value, labels)
}
}
export class Meter {
private readonly ctx: BaseContext
constructor(ctx: BaseContext) {
this.ctx = ctx
}
Counter(name: string): CounterBinding {
return new CounterBinding(name, this.ctx)
}
Gauge(name: string): GaugeBinding {
return new GaugeBinding(name, this.ctx)
}
}