@sentio/sdk 1.12.6 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/meter.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RecordMetaData } from './gen/processor/protos/processor'
1
+ import { MetricDescriptor, RecordMetaData } from './gen/processor/protos/processor'
2
2
  import { BaseContext, Context, SolanaContext, SuiContext } from './context'
3
3
  import { toMetricValue, Numberish } from './numberish'
4
4
  import Long from 'long'
@@ -27,8 +27,14 @@ export function normalizeLabels(labels: Labels): Labels {
27
27
  return normLabels
28
28
  }
29
29
 
30
- function GetRecordMetaData(ctx: BaseContext, name: string, labels: Labels): RecordMetaData {
31
- name = normalizeName(name)
30
+ function GetRecordMetaData(ctx: BaseContext, metric: Metric, labels: Labels): RecordMetaData {
31
+ let descriptor = metric.descriptor
32
+ if (metric.usage > 0) {
33
+ // Other setting don't need to be write multiple times
34
+ descriptor = MetricDescriptor.fromPartial({ name: descriptor.name })
35
+ }
36
+
37
+ descriptor.name = normalizeName(descriptor.name)
32
38
 
33
39
  if (ctx instanceof Context) {
34
40
  if (ctx.log) {
@@ -38,7 +44,8 @@ function GetRecordMetaData(ctx: BaseContext, name: string, labels: Labels): Reco
38
44
  transactionIndex: ctx.log.transactionIndex,
39
45
  logIndex: ctx.log.logIndex,
40
46
  chainId: ctx.chainId.toString(),
41
- name: name,
47
+ name: descriptor.name,
48
+ descriptor: descriptor,
42
49
  labels: normalizeLabels(labels),
43
50
  }
44
51
  }
@@ -49,7 +56,8 @@ function GetRecordMetaData(ctx: BaseContext, name: string, labels: Labels): Reco
49
56
  transactionIndex: -1,
50
57
  logIndex: -1,
51
58
  chainId: ctx.chainId.toString(),
52
- name: name,
59
+ name: descriptor.name,
60
+ descriptor: descriptor,
53
61
  labels: normalizeLabels(labels),
54
62
  }
55
63
  }
@@ -60,7 +68,8 @@ function GetRecordMetaData(ctx: BaseContext, name: string, labels: Labels): Reco
60
68
  transactionIndex: ctx.trace.transactionPosition, // TODO make sure if this is the right value to set
61
69
  logIndex: -1,
62
70
  chainId: ctx.chainId.toString(),
63
- name: name,
71
+ name: descriptor.name,
72
+ descriptor: descriptor,
64
73
  labels: normalizeLabels(labels),
65
74
  }
66
75
  }
@@ -71,7 +80,8 @@ function GetRecordMetaData(ctx: BaseContext, name: string, labels: Labels): Reco
71
80
  transactionIndex: 0,
72
81
  logIndex: 0,
73
82
  chainId: 'SOL_mainnet', // TODO set in context
74
- name: name,
83
+ name: descriptor.name,
84
+ descriptor: descriptor,
75
85
  labels: normalizeLabels(labels),
76
86
  }
77
87
  } else if (ctx instanceof SuiContext) {
@@ -81,7 +91,8 @@ function GetRecordMetaData(ctx: BaseContext, name: string, labels: Labels): Reco
81
91
  transactionIndex: 0,
82
92
  logIndex: 0,
83
93
  chainId: 'SUI_devnet', // TODO set in context
84
- name: name,
94
+ name: descriptor.name,
95
+ descriptor: descriptor,
85
96
  labels: normalizeLabels(labels),
86
97
  }
87
98
  }
@@ -90,80 +101,107 @@ function GetRecordMetaData(ctx: BaseContext, name: string, labels: Labels): Reco
90
101
 
91
102
  export type Labels = { [key: string]: string }
92
103
 
93
- export class Counter {
104
+ export class MetricDescriptorOption {
105
+ unit?: string
106
+ description?: string
107
+ sparse?: boolean
108
+ }
109
+
110
+ export class Metric {
111
+ descriptor: MetricDescriptor = MetricDescriptor.fromPartial({})
112
+ usage = 0
113
+
114
+ constructor(name: string, option?: MetricDescriptorOption) {
115
+ this.descriptor.name = name
116
+ if (option) {
117
+ if (option.unit) {
118
+ this.descriptor.unit = option.unit
119
+ }
120
+ if (option.description) {
121
+ this.descriptor.description = option.description
122
+ }
123
+ if (option.sparse) {
124
+ this.descriptor.sparse = option.sparse
125
+ }
126
+ }
127
+ }
128
+ }
129
+
130
+ export class Counter extends Metric {
131
+ add(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
132
+ this.record(ctx, value, labels, true)
133
+ }
134
+
135
+ sub(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
136
+ this.record(ctx, value, labels, false)
137
+ }
138
+
139
+ private record(ctx: BaseContext, value: Numberish, labels: Labels, add: boolean) {
140
+ ctx.counters.push({
141
+ metadata: GetRecordMetaData(ctx, this, labels),
142
+ metricValue: toMetricValue(value),
143
+ add: add,
144
+ runtimeInfo: undefined,
145
+ })
146
+ this.usage++
147
+ }
148
+ }
149
+
150
+ export class CounterBinding {
94
151
  private readonly ctx: BaseContext
95
- private readonly name: string
152
+ private readonly counter: Counter
96
153
 
97
154
  constructor(name: string, ctx: BaseContext) {
98
- this.name = name
155
+ this.counter = new Counter(name)
99
156
  this.ctx = ctx
100
157
  }
101
158
 
102
159
  add(value: Numberish, labels: Labels = {}) {
103
- this.record(value, labels, true)
160
+ this.counter.add(this.ctx, value, labels)
104
161
  }
105
162
 
106
163
  sub(value: Numberish, labels: Labels = {}) {
107
- this.record(value, labels, false)
164
+ this.counter.sub(this.ctx, value, labels)
108
165
  }
166
+ }
109
167
 
110
- private record(value: Numberish, labels: Labels, add: boolean) {
111
- this.ctx.counters.push({
112
- metadata: GetRecordMetaData(this.ctx, this.name, labels),
168
+ export class Gauge extends Metric {
169
+ record(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
170
+ ctx.gauges.push({
171
+ metadata: GetRecordMetaData(ctx, this, labels),
113
172
  metricValue: toMetricValue(value),
114
- add: add,
115
173
  runtimeInfo: undefined,
116
174
  })
175
+ this.usage++
117
176
  }
118
177
  }
119
178
 
120
- export class Gauge {
121
- private readonly name: string
179
+ export class GaugeBinding {
180
+ private readonly gauge: Gauge
122
181
  private readonly ctx: BaseContext
123
182
 
124
183
  constructor(name: string, ctx: BaseContext) {
125
- this.name = name
184
+ this.gauge = new Gauge(name)
126
185
  this.ctx = ctx
127
186
  }
128
187
 
129
188
  record(value: Numberish, labels: Labels = {}) {
130
- this.ctx.gauges.push({
131
- metadata: GetRecordMetaData(this.ctx, this.name, labels),
132
- metricValue: toMetricValue(value),
133
- runtimeInfo: undefined,
134
- })
189
+ this.gauge.record(this.ctx, value, labels)
135
190
  }
136
191
  }
137
192
 
138
193
  export class Meter {
139
194
  private readonly ctx: BaseContext
140
195
 
141
- // TODO is map necessary since we are sending request remotely?
142
- // counterMap = new Map<string, Counter>()
143
- // gaugeMap = new Map<string, Gauge>()
144
-
145
196
  constructor(ctx: BaseContext) {
146
197
  this.ctx = ctx
147
198
  }
148
199
 
149
- Counter(name: string): Counter {
150
- // let counter = this.counterMap.get(name)
151
-
152
- // if (!counter) {
153
- // counter = new Counter(name, this.ctx)
154
- // }
155
- // return counter
156
-
157
- return new Counter(name, this.ctx)
200
+ Counter(name: string): CounterBinding {
201
+ return new CounterBinding(name, this.ctx)
158
202
  }
159
203
 
160
- Gauge(name: string): Gauge {
161
- // let gauge = this.gaugeMap.get(name)
162
- //
163
- // if (!gauge) {
164
- // gauge = new Gauge(name, this.ctx)
165
- // }
166
- // return gauge
167
- return new Gauge(name, this.ctx)
204
+ Gauge(name: string): GaugeBinding {
205
+ return new GaugeBinding(name, this.ctx)
168
206
  }
169
207
  }