Skip to content

Commit

Permalink
chore(jsonnetsecure): add tracing (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Aug 18, 2023
1 parent 3ec4565 commit 460bc74
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jsonnetsecure/jsonnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func JsonnetTestBinary(t testing.TB) string {
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil || stderr.Len() != 0 {
t.Fatalf("building the Go binary returned error: %v\n%s", err, string(stderr.String()))
t.Fatalf("building the Go binary returned error: %v\n%s", err, stderr.String())
}

return outPath
Expand Down
23 changes: 18 additions & 5 deletions jsonnetsecure/jsonnet_processvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"strings"
"time"

"go.opentelemetry.io/otel/attribute"

"github.com/ory/x/otelx"

"go.opentelemetry.io/otel/trace"

"github.com/cenkalti/backoff/v4"
"github.com/pkg/errors"
)
Expand All @@ -25,11 +31,18 @@ func NewProcessVM(opts *vmOptions) VM {
}
}

func (p *ProcessVM) EvaluateAnonymousSnippet(filename string, snippet string) (string, error) {
func (p *ProcessVM) EvaluateAnonymousSnippet(filename string, snippet string) (_ string, err error) {
tracer := trace.SpanFromContext(p.ctx).TracerProvider().Tracer("")
ctx, span := tracer.Start(p.ctx, "jsonnetsecure.ProcessVM.EvaluateAnonymousSnippet", trace.WithAttributes(attribute.String("filename", filename)))
defer otelx.End(span, &err)

// We retry the process creation, because it sometimes times out.
const processVMTimeout = 1 * time.Second
return backoff.RetryWithData(func() (string, error) {
ctx, cancel := context.WithTimeout(p.ctx, processVMTimeout)
return backoff.RetryWithData(func() (_ string, err error) {
ctx, span := tracer.Start(ctx, "jsonnetsecure.ProcessVM.EvaluateAnonymousSnippet.run")
defer otelx.End(span, &err)

ctx, cancel := context.WithTimeout(ctx, processVMTimeout)
defer cancel()

var (
Expand All @@ -49,7 +62,7 @@ func (p *ProcessVM) EvaluateAnonymousSnippet(filename string, snippet string) (s
cmd.Stderr = &stderr
cmd.Env = []string{"GOMAXPROCS=1"}

err := cmd.Run()
err = cmd.Run()
if stderr.Len() > 0 {
// If the process wrote to stderr, this means it started and we won't retry.
return "", backoff.Permanent(fmt.Errorf("jsonnetsecure: unexpected output on stderr: %q", stderr.String()))
Expand All @@ -59,7 +72,7 @@ func (p *ProcessVM) EvaluateAnonymousSnippet(filename string, snippet string) (s
}

return stdout.String(), nil
}, backoff.WithContext(backoff.NewExponentialBackOff(), p.ctx))
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
}

func (p *ProcessVM) ExtCode(key string, val string) {
Expand Down
9 changes: 6 additions & 3 deletions jsonnetsecure/jsonnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import (
)

func TestSecureVM(t *testing.T) {
testBinary := JsonnetTestBinary(t)

for _, optCase := range []struct {
name string
opts []Option
}{
{"none", []Option{}},
{"process vm", []Option{
WithProcessIsolatedVM(context.Background()),
WithJsonnetBinary(JsonnetTestBinary(t)),
WithJsonnetBinary(testBinary),
}},
} {
t.Run("options="+optCase.name, func(t *testing.T) {
Expand Down Expand Up @@ -114,7 +116,7 @@ func TestSecureVM(t *testing.T) {
defer cancel()
vm := MakeSecureVM(
WithProcessIsolatedVM(ctx),
WithJsonnetBinary(JsonnetTestBinary(t)),
WithJsonnetBinary(testBinary),
)
result, err := vm.EvaluateAnonymousSnippet("test", snippet)
require.Error(t, err)
Expand Down Expand Up @@ -161,12 +163,13 @@ func assertEqualVMOutput(t *testing.T, run func(factory func(t *testing.T) VM) s
func TestCreateMultipleProcessVMs(t *testing.T) {
ctx := context.Background()
wg := new(errgroup.Group)
testBinary := JsonnetTestBinary(t)

for i := 0; i < 100; i++ {
wg.Go(func() error {
vm := MakeSecureVM(
WithProcessIsolatedVM(ctx),
WithJsonnetBinary(JsonnetTestBinary(t)),
WithJsonnetBinary(testBinary),
)
_, err := vm.EvaluateAnonymousSnippet("test", "{a:1}")

Expand Down

0 comments on commit 460bc74

Please sign in to comment.