3
3
package lib
4
4
5
5
import (
6
+ "context"
6
7
"encoding/json"
7
8
"errors"
8
9
"fmt"
@@ -18,6 +19,8 @@ import (
18
19
hostservices "github.com/synadia-io/nex/host-services"
19
20
"github.com/synadia-io/nex/host-services/builtins"
20
21
agentapi "github.com/synadia-io/nex/internal/agent-api"
22
+ "go.opentelemetry.io/otel"
23
+ "go.opentelemetry.io/otel/propagation"
21
24
v8 "rogchap.com/v8go"
22
25
)
23
26
@@ -96,20 +99,26 @@ func (v *V8) Deploy() error {
96
99
97
100
subject := fmt .Sprintf ("agentint.%s.trigger" , v .vmID )
98
101
_ , err := v .nc .Subscribe (subject , func (msg * nats.Msg ) {
102
+ ctx := context .WithValue (context .Background (), agentapi .NexTriggerSubject , msg .Header .Get (agentapi .NexTriggerSubject )) //nolint:all
103
+ ctx = otel .GetTextMapPropagator ().Extract (ctx , propagation .HeaderCarrier (msg .Header ))
104
+
99
105
startTime := time .Now ()
100
- val , err := v .Execute (msg . Header . Get ( agentapi . NexTriggerSubject ) , msg .Data )
106
+ val , err := v .Execute (ctx , msg .Data )
101
107
if err != nil {
102
108
_ , _ = v .stderr .Write ([]byte (fmt .Sprintf ("failed to execute function on trigger subject %s: %s" , subject , err .Error ())))
103
109
return
104
110
}
105
111
106
112
runtimeNanos := time .Since (startTime ).Nanoseconds ()
107
113
114
+ header := nats.Header {
115
+ agentapi .NexRuntimeNs : []string {strconv .FormatInt (runtimeNanos , 10 )},
116
+ }
117
+ otel .GetTextMapPropagator ().Inject (ctx , propagation .HeaderCarrier (header ))
118
+
108
119
err = msg .RespondMsg (& nats.Msg {
109
- Data : val ,
110
- Header : nats.Header {
111
- agentapi .NexRuntimeNs : []string {strconv .FormatInt (runtimeNanos , 10 )},
112
- },
120
+ Data : val ,
121
+ Header : header ,
113
122
})
114
123
if err != nil {
115
124
_ , _ = v .stderr .Write ([]byte (fmt .Sprintf ("failed to write %d-byte response: %s" , len (val ), err .Error ())))
@@ -127,12 +136,20 @@ func (v *V8) Deploy() error {
127
136
// Trigger execution of the deployed function; expects a `Validate` to have succeeded and `ubs` to be non-nil.
128
137
// The executed function can optionally return a value, in which case it will be deemed a reply and returned
129
138
// to the caller. In the case of a nil or empty value returned by the function, no reply will be sent.
130
- func (v * V8 ) Execute (subject string , payload []byte ) ([]byte , error ) {
139
+ func (v * V8 ) Execute (ctx context. Context , payload []byte ) ([]byte , error ) {
131
140
if v .ubs == nil {
132
141
return nil , fmt .Errorf ("invalid state for execution; no compiled code available for vm: %s" , v .name )
133
142
}
134
143
135
- ctx , err := v .newV8Context ()
144
+ var subject string
145
+ sub , ok := ctx .Value (agentapi .NexTriggerSubject ).(string )
146
+ if ok {
147
+ subject = sub
148
+ } else {
149
+ return nil , fmt .Errorf ("failed to initialize context in vm; no trigger subject provided in context: %s" , v .name )
150
+ }
151
+
152
+ v8ctx , err := v .newV8Context (ctx )
136
153
if err != nil {
137
154
return nil , fmt .Errorf ("failed to initialize context in vm: %s" , err .Error ())
138
155
}
@@ -141,7 +158,7 @@ func (v *V8) Execute(subject string, payload []byte) ([]byte, error) {
141
158
errs := make (chan error , 1 )
142
159
143
160
go func () {
144
- val , err := v .ubs .Run (ctx )
161
+ val , err := v .ubs .Run (v8ctx )
145
162
if err != nil {
146
163
errs <- err
147
164
return
@@ -153,7 +170,7 @@ func (v *V8) Execute(subject string, payload []byte) ([]byte, error) {
153
170
return
154
171
}
155
172
156
- argv1 , err := v8 .NewValue (ctx .Isolate (), subject )
173
+ argv1 , err := v8 .NewValue (v8ctx .Isolate (), subject )
157
174
if err != nil {
158
175
errs <- err
159
176
return
@@ -166,7 +183,7 @@ func (v *V8) Execute(subject string, payload []byte) ([]byte, error) {
166
183
return
167
184
}
168
185
169
- val , err = fn .Call (ctx .Global (), argv1 , argv2 )
186
+ val , err = fn .Call (v8ctx .Global (), argv1 , argv2 )
170
187
if err != nil {
171
188
errs <- err
172
189
return
@@ -270,10 +287,10 @@ func (v *V8) initUtils() {
270
287
v .utils [v8FunctionUInt8ArrayToString ] = uint8arrtostrfn
271
288
}
272
289
273
- func (v * V8 ) newV8Context () (* v8.Context , error ) {
290
+ func (v * V8 ) newV8Context (ctx context. Context ) (* v8.Context , error ) {
274
291
global := v8 .NewObjectTemplate (v .iso )
275
292
276
- hostServices , err := v .newHostServicesTemplate ()
293
+ hostServices , err := v .newHostServicesTemplate (ctx )
277
294
if err != nil {
278
295
return nil , err
279
296
}
@@ -286,63 +303,63 @@ func (v *V8) newV8Context() (*v8.Context, error) {
286
303
return v8 .NewContext (v .iso , global ), nil
287
304
}
288
305
289
- func (v * V8 ) newHostServicesTemplate () (* v8.ObjectTemplate , error ) {
306
+ func (v * V8 ) newHostServicesTemplate (ctx context. Context ) (* v8.ObjectTemplate , error ) {
290
307
hostServices := v8 .NewObjectTemplate (v .iso )
291
308
292
- err := hostServices .Set (hostServicesHTTPObjectName , v .newHTTPObjectTemplate ())
309
+ err := hostServices .Set (hostServicesHTTPObjectName , v .newHTTPObjectTemplate (ctx ))
293
310
if err != nil {
294
311
return nil , err
295
312
}
296
313
297
- err = hostServices .Set (hostServicesKVObjectName , v .newKeyValueObjectTemplate ())
314
+ err = hostServices .Set (hostServicesKVObjectName , v .newKeyValueObjectTemplate (ctx ))
298
315
if err != nil {
299
316
return nil , err
300
317
}
301
318
302
- err = hostServices .Set (hostServicesMessagingObjectName , v .newMessagingObjectTemplate ())
319
+ err = hostServices .Set (hostServicesMessagingObjectName , v .newMessagingObjectTemplate (ctx ))
303
320
if err != nil {
304
321
return nil , err
305
322
}
306
323
307
- err = hostServices .Set (hostServicesObjectStoreObjectName , v .newObjectStoreObjectTemplate ())
324
+ err = hostServices .Set (hostServicesObjectStoreObjectName , v .newObjectStoreObjectTemplate (ctx ))
308
325
if err != nil {
309
326
return nil , err
310
327
}
311
328
312
329
return hostServices , nil
313
330
}
314
331
315
- func (v * V8 ) newHTTPObjectTemplate () * v8.ObjectTemplate {
332
+ func (v * V8 ) newHTTPObjectTemplate (ctx context. Context ) * v8.ObjectTemplate {
316
333
http := v8 .NewObjectTemplate (v .iso )
317
334
318
335
_ = http .Set (hostServicesHTTPGetFunctionName , v8 .NewFunctionTemplate (
319
- v .iso , v .genHttpClientFunc (hostServicesHTTPGetFunctionName ),
336
+ v .iso , v .genHttpClientFunc (ctx , hostServicesHTTPGetFunctionName ),
320
337
))
321
338
322
339
_ = http .Set (hostServicesHTTPPostFunctionName , v8 .NewFunctionTemplate (
323
- v .iso , v .genHttpClientFunc (hostServicesHTTPPostFunctionName ),
340
+ v .iso , v .genHttpClientFunc (ctx , hostServicesHTTPPostFunctionName ),
324
341
))
325
342
326
343
_ = http .Set (hostServicesHTTPPutFunctionName , v8 .NewFunctionTemplate (
327
- v .iso , v .genHttpClientFunc (hostServicesHTTPPutFunctionName ),
344
+ v .iso , v .genHttpClientFunc (ctx , hostServicesHTTPPutFunctionName ),
328
345
))
329
346
330
347
_ = http .Set (hostServicesHTTPPatchFunctionName , v8 .NewFunctionTemplate (
331
- v .iso , v .genHttpClientFunc (hostServicesHTTPPatchFunctionName ),
348
+ v .iso , v .genHttpClientFunc (ctx , hostServicesHTTPPatchFunctionName ),
332
349
))
333
350
334
351
_ = http .Set (hostServicesHTTPDeleteFunctionName , v8 .NewFunctionTemplate (
335
- v .iso , v .genHttpClientFunc (hostServicesHTTPDeleteFunctionName ),
352
+ v .iso , v .genHttpClientFunc (ctx , hostServicesHTTPDeleteFunctionName ),
336
353
))
337
354
338
355
_ = http .Set (hostServicesHTTPHeadFunctionName , v8 .NewFunctionTemplate (
339
- v .iso , v .genHttpClientFunc (hostServicesHTTPHeadFunctionName ),
356
+ v .iso , v .genHttpClientFunc (ctx , hostServicesHTTPHeadFunctionName ),
340
357
))
341
358
342
359
return http
343
360
}
344
361
345
- func (v * V8 ) genHttpClientFunc (method string ) func (info * v8.FunctionCallbackInfo ) * v8.Value {
362
+ func (v * V8 ) genHttpClientFunc (ctx context. Context , method string ) func (info * v8.FunctionCallbackInfo ) * v8.Value {
346
363
return func (info * v8.FunctionCallbackInfo ) * v8.Value {
347
364
args := info .Args ()
348
365
if len (args ) == 0 {
@@ -367,7 +384,7 @@ func (v *V8) genHttpClientFunc(method string) func(info *v8.FunctionCallbackInfo
367
384
}
368
385
}
369
386
370
- httpresp , err := v .builtins .SimpleHttpRequest (method , url .String (), payload )
387
+ httpresp , err := v .builtins .SimpleHttpRequest (ctx , method , url .String (), payload )
371
388
if err != nil {
372
389
val , _ := v8 .NewValue (v .iso , err .Error ())
373
390
return v .iso .ThrowException (val )
@@ -419,7 +436,7 @@ func (v *V8) genHttpClientFunc(method string) func(info *v8.FunctionCallbackInfo
419
436
}
420
437
}
421
438
422
- func (v * V8 ) newKeyValueObjectTemplate () * v8.ObjectTemplate {
439
+ func (v * V8 ) newKeyValueObjectTemplate (ctx context. Context ) * v8.ObjectTemplate {
423
440
kv := v8 .NewObjectTemplate (v .iso )
424
441
425
442
_ = kv .Set (hostServicesKVGetFunctionName , v8 .NewFunctionTemplate (v .iso , func (info * v8.FunctionCallbackInfo ) * v8.Value {
@@ -431,7 +448,7 @@ func (v *V8) newKeyValueObjectTemplate() *v8.ObjectTemplate {
431
448
432
449
key := args [0 ].String ()
433
450
434
- resp , err := v .builtins .KVGet (key )
451
+ resp , err := v .builtins .KVGet (ctx , key )
435
452
if err != nil {
436
453
val , _ := v8 .NewValue (v .iso , err .Error ())
437
454
return v .iso .ThrowException (val )
@@ -462,7 +479,7 @@ func (v *V8) newKeyValueObjectTemplate() *v8.ObjectTemplate {
462
479
return v .iso .ThrowException (val )
463
480
}
464
481
465
- kvresp , err := v .builtins .KVSet (key , value )
482
+ kvresp , err := v .builtins .KVSet (ctx , key , value )
466
483
if err != nil {
467
484
val , _ := v8 .NewValue (v .iso , err .Error ())
468
485
return v .iso .ThrowException (val )
@@ -485,7 +502,7 @@ func (v *V8) newKeyValueObjectTemplate() *v8.ObjectTemplate {
485
502
486
503
key := args [0 ].String ()
487
504
488
- kvresp , err := v .builtins .KVDelete (key )
505
+ kvresp , err := v .builtins .KVDelete (ctx , key )
489
506
if err != nil {
490
507
val , _ := v8 .NewValue (v .iso , err .Error ())
491
508
return v .iso .ThrowException (val )
@@ -501,7 +518,7 @@ func (v *V8) newKeyValueObjectTemplate() *v8.ObjectTemplate {
501
518
502
519
_ = kv .Set (hostServicesKVKeysFunctionName , v8 .NewFunctionTemplate (v .iso , func (info * v8.FunctionCallbackInfo ) * v8.Value {
503
520
504
- resp , err := v .builtins .KVKeys ()
521
+ resp , err := v .builtins .KVKeys (ctx )
505
522
if err != nil {
506
523
val , _ := v8 .NewValue (v .iso , err .Error ())
507
524
return v .iso .ThrowException (val )
@@ -522,7 +539,7 @@ func (v *V8) newKeyValueObjectTemplate() *v8.ObjectTemplate {
522
539
return kv
523
540
}
524
541
525
- func (v * V8 ) newMessagingObjectTemplate () * v8.ObjectTemplate {
542
+ func (v * V8 ) newMessagingObjectTemplate (ctx context. Context ) * v8.ObjectTemplate {
526
543
messaging := v8 .NewObjectTemplate (v .iso )
527
544
528
545
_ = messaging .Set (hostServicesMessagingPublishFunctionName , v8 .NewFunctionTemplate (v .iso , func (info * v8.FunctionCallbackInfo ) * v8.Value {
@@ -539,7 +556,7 @@ func (v *V8) newMessagingObjectTemplate() *v8.ObjectTemplate {
539
556
return v .iso .ThrowException (val )
540
557
}
541
558
542
- err = v .builtins .MessagingPublish (subject , payload )
559
+ err = v .builtins .MessagingPublish (ctx , subject , payload )
543
560
if err != nil {
544
561
val , _ := v8 .NewValue (v .iso , err .Error ())
545
562
return v .iso .ThrowException (val )
@@ -562,7 +579,7 @@ func (v *V8) newMessagingObjectTemplate() *v8.ObjectTemplate {
562
579
return v .iso .ThrowException (val )
563
580
}
564
581
565
- resp , err := v .builtins .MessagingRequest (subject , payload )
582
+ resp , err := v .builtins .MessagingRequest (ctx , subject , payload )
566
583
if err != nil {
567
584
val , _ := v8 .NewValue (v .iso , err .Error ())
568
585
return v .iso .ThrowException (val )
@@ -664,7 +681,7 @@ func (v *V8) newMessagingObjectTemplate() *v8.ObjectTemplate {
664
681
return messaging
665
682
}
666
683
667
- func (v * V8 ) newObjectStoreObjectTemplate () * v8.ObjectTemplate {
684
+ func (v * V8 ) newObjectStoreObjectTemplate (ctx context. Context ) * v8.ObjectTemplate {
668
685
objectStore := v8 .NewObjectTemplate (v .iso )
669
686
670
687
_ = objectStore .Set (hostServicesObjectStoreGetFunctionName , v8 .NewFunctionTemplate (v .iso , func (info * v8.FunctionCallbackInfo ) * v8.Value {
@@ -675,7 +692,7 @@ func (v *V8) newObjectStoreObjectTemplate() *v8.ObjectTemplate {
675
692
}
676
693
677
694
name := args [0 ].String ()
678
- resp , err := v .builtins .ObjectGet (name )
695
+ resp , err := v .builtins .ObjectGet (ctx , name )
679
696
if err != nil {
680
697
val , _ := v8 .NewValue (v .iso , err .Error ())
681
698
return v .iso .ThrowException (val )
@@ -706,7 +723,7 @@ func (v *V8) newObjectStoreObjectTemplate() *v8.ObjectTemplate {
706
723
return v .iso .ThrowException (val )
707
724
}
708
725
709
- resp , err := v .builtins .ObjectPut (name , value )
726
+ resp , err := v .builtins .ObjectPut (ctx , name , value )
710
727
711
728
if err != nil {
712
729
val , _ := v8 .NewValue (v .iso , err .Error ())
@@ -733,7 +750,7 @@ func (v *V8) newObjectStoreObjectTemplate() *v8.ObjectTemplate {
733
750
}
734
751
735
752
name := args [0 ].String ()
736
- err := v .builtins .ObjectDelete (name )
753
+ err := v .builtins .ObjectDelete (ctx , name )
737
754
if err != nil {
738
755
val , _ := v8 .NewValue (v .iso , err .Error ())
739
756
return v .iso .ThrowException (val )
@@ -743,7 +760,7 @@ func (v *V8) newObjectStoreObjectTemplate() *v8.ObjectTemplate {
743
760
}))
744
761
745
762
_ = objectStore .Set (hostServicesObjectStoreListFunctionName , v8 .NewFunctionTemplate (v .iso , func (info * v8.FunctionCallbackInfo ) * v8.Value {
746
- resp , err := v .builtins .ObjectList ()
763
+ resp , err := v .builtins .ObjectList (ctx )
747
764
if err != nil {
748
765
val , _ := v8 .NewValue (v .iso , err .Error ())
749
766
return v .iso .ThrowException (val )
0 commit comments