-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindings.go
More file actions
472 lines (396 loc) · 11.9 KB
/
bindings.go
File metadata and controls
472 lines (396 loc) · 11.9 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package jsmachine
import (
"fmt"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/robertkrimen/otto"
"go.uber.org/zap"
)
// Bindings represents all Go functions exposed to JavaScript
type Bindings struct {
log *LogBinding
metrics *MetricsBinding
}
// newBindings creates a new bindings instance
func newBindings(logger *zap.Logger, plugin *Plugin) *Bindings {
return &Bindings{
log: newLogBinding(logger),
metrics: newMetricsBinding(plugin),
}
}
// injectIntoVM injects all bindings into the Otto VM
func (b *Bindings) injectIntoVM(vm *otto.Otto) error {
// Inject log binding
if err := b.log.inject(vm); err != nil {
return fmt.Errorf("failed to inject log binding: %w", err)
}
// Inject metrics binding
if err := b.metrics.inject(vm); err != nil {
return fmt.Errorf("failed to inject metrics binding: %w", err)
}
return nil
}
// LogBinding provides logging functions to JavaScript
type LogBinding struct {
logger *zap.Logger
}
// newLogBinding creates a new log binding
func newLogBinding(logger *zap.Logger) *LogBinding {
return &LogBinding{
logger: logger,
}
}
// inject injects the log object into the VM
func (l *LogBinding) inject(vm *otto.Otto) error {
logObj, err := vm.Object(`({})`)
if err != nil {
return err
}
// log.info(message, fields)
if err := logObj.Set("info", l.info); err != nil {
return err
}
// log.error(message, fields)
if err := logObj.Set("error", l.error); err != nil {
return err
}
// log.warn(message, fields)
if err := logObj.Set("warn", l.warn); err != nil {
return err
}
// log.debug(message, fields)
if err := logObj.Set("debug", l.debug); err != nil {
return err
}
return vm.Set("log", logObj)
}
// info logs an info message
func (l *LogBinding) info(call otto.FunctionCall) otto.Value {
message := l.getMessage(call)
fields := l.getFields(call)
l.logger.Info(message, fields...)
return otto.UndefinedValue()
}
// error logs an error message
func (l *LogBinding) error(call otto.FunctionCall) otto.Value {
message := l.getMessage(call)
fields := l.getFields(call)
l.logger.Error(message, fields...)
return otto.UndefinedValue()
}
// warn logs a warning message
func (l *LogBinding) warn(call otto.FunctionCall) otto.Value {
message := l.getMessage(call)
fields := l.getFields(call)
l.logger.Warn(message, fields...)
return otto.UndefinedValue()
}
// debug logs a debug message
func (l *LogBinding) debug(call otto.FunctionCall) otto.Value {
message := l.getMessage(call)
fields := l.getFields(call)
l.logger.Debug(message, fields...)
return otto.UndefinedValue()
}
// getMessage extracts the message from the function call
func (l *LogBinding) getMessage(call otto.FunctionCall) string {
if len(call.ArgumentList) == 0 {
return ""
}
return call.Argument(0).String()
}
// getFields extracts structured fields from the function call
func (l *LogBinding) getFields(call otto.FunctionCall) []zap.Field {
if len(call.ArgumentList) < 2 {
return nil
}
// Second argument should be an object with fields
fieldsValue := call.Argument(1)
if !fieldsValue.IsObject() {
return nil
}
fieldsObj := fieldsValue.Object()
keys := fieldsObj.Keys()
fields := make([]zap.Field, 0, len(keys))
for _, key := range keys {
value, err := fieldsObj.Get(key)
if err != nil {
continue
}
// Convert value to appropriate zap field
exported, err := value.Export()
if err != nil {
continue
}
fields = append(fields, zap.Any(key, exported))
}
return fields
}
// MetricsBinding provides metrics functions to JavaScript
// Following the metrics plugin pattern: metrics must be pre-registered via metrics plugin
// JavaScript code can only manipulate existing metrics through the metrics plugin's collectors sync.Map
type MetricsBinding struct {
plugin *Plugin
mu sync.RWMutex
// Cache of collectors loaded from metrics plugin
// These are fetched from the metrics plugin's collectors sync.Map
cachedCollectors sync.Map // map[string]prometheus.Collector
}
// newMetricsBinding creates a new metrics binding
func newMetricsBinding(plugin *Plugin) *MetricsBinding {
return &MetricsBinding{
plugin: plugin,
}
}
// inject injects the metrics object into the VM
func (m *MetricsBinding) inject(vm *otto.Otto) error {
metricsObj, err := vm.Object(`({})`)
if err != nil {
return err
}
// metrics.add(name, value, labels) - for counters and gauges
if err := metricsObj.Set("add", m.add); err != nil {
return err
}
// metrics.set(name, value, labels) - for gauges only
if err := metricsObj.Set("set", m.set); err != nil {
return err
}
// metrics.observe(name, value, labels) - for histograms
if err := metricsObj.Set("observe", m.observe); err != nil {
return err
}
return vm.Set("metrics", metricsObj)
}
// getCollector retrieves a collector from the metrics plugin
// This follows the same pattern as metrics plugin's rpc.go: c, exist := r.p.collectors.Load(m.Name)
func (m *MetricsBinding) getCollector(name string) (prometheus.Collector, bool) {
// Check cache first
if cached, exists := m.cachedCollectors.Load(name); exists {
return cached.(prometheus.Collector), true
}
// Get from metrics plugin
if m.plugin.metricsPlugin == nil {
m.plugin.log.Warn("metrics plugin not available", zap.String("metric", name))
return nil, false
}
// Load from metrics plugin's collectors sync.Map
collector, exists := m.plugin.metricsPlugin.collectors.Load(name)
if !exists {
return nil, false
}
// Extract the actual collector from the wrapper
col := collector.(*metricsCollector)
actualCollector := col.col
// Cache it for future use
m.cachedCollectors.Store(name, actualCollector)
return actualCollector, true
}
// add adds value to a counter or gauge (follows metrics plugin rpc.go pattern)
func (m *MetricsBinding) add(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) < 2 {
return otto.UndefinedValue()
}
name := call.Argument(0).String()
value, err := call.Argument(1).ToFloat()
if err != nil {
return otto.UndefinedValue()
}
// Extract labels if provided
var labelValues []string
if len(call.ArgumentList) > 2 {
labelValues = m.extractLabelValues(call, 2)
}
// Get collector from metrics plugin (same pattern as rpc.go)
collector, exists := m.getCollector(name)
if !exists {
m.plugin.log.Warn("metric not found in metrics plugin",
zap.String("name", name))
return otto.UndefinedValue()
}
// Handle different collector types (exact pattern from metrics plugin rpc.go)
switch c := collector.(type) {
case prometheus.Counter:
c.Add(value)
case *prometheus.CounterVec:
if len(labelValues) == 0 {
m.plugin.log.Warn("required labels for collector",
zap.String("name", name))
return otto.UndefinedValue()
}
counter, err := c.GetMetricWithLabelValues(labelValues...)
if err != nil {
m.plugin.log.Error("failed to get metric with labels",
zap.String("name", name),
zap.Strings("labels", labelValues),
zap.Error(err))
return otto.UndefinedValue()
}
counter.Add(value)
case prometheus.Gauge:
c.Add(value)
case *prometheus.GaugeVec:
if len(labelValues) == 0 {
m.plugin.log.Warn("required labels for collector",
zap.String("name", name))
return otto.UndefinedValue()
}
gauge, err := c.GetMetricWithLabelValues(labelValues...)
if err != nil {
m.plugin.log.Error("failed to get metric with labels",
zap.String("name", name),
zap.Strings("labels", labelValues),
zap.Error(err))
return otto.UndefinedValue()
}
gauge.Add(value)
default:
m.plugin.log.Warn("collector does not support add operation",
zap.String("name", name))
}
return otto.UndefinedValue()
}
// set sets a gauge value (follows metrics plugin rpc.go pattern)
func (m *MetricsBinding) set(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) < 2 {
return otto.UndefinedValue()
}
name := call.Argument(0).String()
value, err := call.Argument(1).ToFloat()
if err != nil {
return otto.UndefinedValue()
}
// Extract labels if provided
var labelValues []string
if len(call.ArgumentList) > 2 {
labelValues = m.extractLabelValues(call, 2)
}
// Get collector from metrics plugin
collector, exists := m.getCollector(name)
if !exists {
m.plugin.log.Warn("metric not found in metrics plugin",
zap.String("name", name))
return otto.UndefinedValue()
}
// Handle different gauge types (exact pattern from metrics plugin rpc.go)
switch c := collector.(type) {
case prometheus.Gauge:
c.Set(value)
case *prometheus.GaugeVec:
if len(labelValues) == 0 {
m.plugin.log.Warn("required labels for collector",
zap.String("name", name))
return otto.UndefinedValue()
}
gauge, err := c.GetMetricWithLabelValues(labelValues...)
if err != nil {
m.plugin.log.Error("failed to get metric with labels",
zap.String("name", name),
zap.Strings("labels", labelValues),
zap.Error(err))
return otto.UndefinedValue()
}
gauge.Set(value)
default:
m.plugin.log.Warn("collector does not support set operation (only gauges)",
zap.String("name", name))
}
return otto.UndefinedValue()
}
// observe records a histogram observation (follows metrics plugin rpc.go pattern)
func (m *MetricsBinding) observe(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) < 2 {
return otto.UndefinedValue()
}
name := call.Argument(0).String()
value, err := call.Argument(1).ToFloat()
if err != nil {
return otto.UndefinedValue()
}
// Extract labels if provided
var labelValues []string
if len(call.ArgumentList) > 2 {
labelValues = m.extractLabelValues(call, 2)
}
// Get collector from metrics plugin
collector, exists := m.getCollector(name)
if !exists {
m.plugin.log.Warn("metric not found in metrics plugin",
zap.String("name", name))
return otto.UndefinedValue()
}
// Handle different histogram types (exact pattern from metrics plugin rpc.go)
switch c := collector.(type) {
case prometheus.Histogram:
c.Observe(value)
case *prometheus.HistogramVec:
if len(labelValues) == 0 {
m.plugin.log.Warn("required labels for collector",
zap.String("name", name))
return otto.UndefinedValue()
}
observer, err := c.GetMetricWithLabelValues(labelValues...)
if err != nil {
m.plugin.log.Error("failed to get metric with labels",
zap.String("name", name),
zap.Strings("labels", labelValues),
zap.Error(err))
return otto.UndefinedValue()
}
observer.Observe(value)
default:
m.plugin.log.Warn("collector does not support observe operation (only histograms)",
zap.String("name", name))
}
return otto.UndefinedValue()
}
// extractLabelValues extracts label values as string slice (for GetMetricWithLabelValues)
// This accepts either an array of label values or an object with label key-value pairs
func (m *MetricsBinding) extractLabelValues(call otto.FunctionCall, argIndex int) []string {
if len(call.ArgumentList) <= argIndex {
return nil
}
labelsValue := call.Argument(argIndex)
// Handle array of label values: ["value1", "value2"]
if labelsValue.Class() == "Array" {
length, err := labelsValue.Object().Get("length")
if err != nil {
return nil
}
lengthInt, err := length.ToInteger()
if err != nil {
return nil
}
values := make([]string, lengthInt)
for i := int64(0); i < lengthInt; i++ {
item, err := labelsValue.Object().Get(fmt.Sprintf("%d", i))
if err != nil {
continue
}
values[i] = item.String()
}
return values
}
// Handle object with label key-value pairs: {method: "GET", status: "200"}
// Convert to array of values (order matters for GetMetricWithLabelValues!)
if labelsValue.IsObject() {
labelsObj := labelsValue.Object()
keys := labelsObj.Keys()
values := make([]string, len(keys))
for i, key := range keys {
value, err := labelsObj.Get(key)
if err != nil {
continue
}
values[i] = value.String()
}
return values
}
return nil
}
// metricsCollector is the internal collector wrapper used by metrics plugin
// This mirrors the structure in metrics/plugin.go
type metricsCollector struct {
col prometheus.Collector
registered bool
}