Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENC-2031] Add new 'encore:trace' annotation + Other small changes #1022

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions cli/cmd/encore/app/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encr.dev/internal/conf"
"encr.dev/internal/env"
"encr.dev/internal/version"
"encr.dev/pkg/appfile"
"encr.dev/pkg/github"
"encr.dev/pkg/xos"
)
Expand Down Expand Up @@ -151,10 +152,12 @@ func createApp(ctx context.Context, name, template string) (err error) {
}
}

encoreAppPath := filepath.Join(name, "encore.app")
encoreAppPath := filepath.Join(name, appfile.Name)
appData, err := os.ReadFile(encoreAppPath)
if err != nil {
appData, err = []byte("{}"), nil
appData, err = []byte(`{
"$schema": "https://encore.dev/schemas/appfile.schema.json"
}`), nil
}

if app != nil {
Expand Down Expand Up @@ -504,7 +507,12 @@ func setEncoreAppID(data []byte, id string, commentLines []string) ([]byte, erro
Value: hujson.Literal(jsonValue),
}

schemaValue := hujson.Value{
Value: hujson.Literal(`"https://encore.dev/schemas/appfile.schema.json"`),
}

found := false
foundSchema := false
for i := range obj.Members {
m := &obj.Members[i]
if lit, ok := m.Name.Value.(hujson.Literal); ok && lit.String() == "id" {
Expand All @@ -515,6 +523,10 @@ func setEncoreAppID(data []byte, id string, commentLines []string) ([]byte, erro
found = true
break
}
if lit, ok := m.Name.Value.(hujson.Literal); ok && lit.String() == "$schema" {
foundSchema = true
m.Value = schemaValue
}
}

if !found {
Expand All @@ -527,6 +539,15 @@ func setEncoreAppID(data []byte, id string, commentLines []string) ([]byte, erro
}}, obj.Members...)
}

if !foundSchema {
obj.Members = append([]hujson.ObjectMember{{
Name: hujson.Value{
Value: hujson.Literal(`"$schema"`),
},
Value: schemaValue,
}}, obj.Members...)
}

root.Format()
return root.Pack(), nil
}
Expand Down
5 changes: 5 additions & 0 deletions cli/cmd/encore/app/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Test_setEncoreAppID(t *testing.T) {
id: "foo",
commentLines: []string{"bar"},
want: `{
"$schema": "https://encore.dev/schemas/appfile.schema.json",
// bar
"id": "foo",
}
Expand All @@ -27,6 +28,7 @@ func Test_setEncoreAppID(t *testing.T) {
id: "foo",
commentLines: []string{"bar"},
want: `{
"$schema": "https://encore.dev/schemas/appfile.schema.json",
// bar
"id": "foo",
}
Expand All @@ -40,6 +42,7 @@ func Test_setEncoreAppID(t *testing.T) {
id: "foo",
commentLines: []string{"bar", "baz"},
want: `{
"$schema": "https://encore.dev/schemas/appfile.schema.json",
// bar
// baz
"id": "foo",
Expand All @@ -48,13 +51,15 @@ func Test_setEncoreAppID(t *testing.T) {
},
{
data: []byte(`{
"$schema": "https://encore.dev/AN-OLD-SCHEMA",
"some_other_field": true,
// foo
"id": "test",
}`),
id: "foo",
commentLines: []string{"bar", "baz"},
want: `{
"$schema": "https://encore.dev/schemas/appfile.schema.json",
"some_other_field": true,
// bar
// baz
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/encore/app/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func initializeApp(name string) error {

// Create the encore.app file
encoreAppData := []byte(`{
"$schema": "https://encore.dev/schemas/appfile.schema.json",
"id": "` + app.Slug + `",
}
`)
Expand Down
9 changes: 9 additions & 0 deletions cli/daemon/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,16 @@ func (i *Instance) GlobalCORS() (appfile.CORS, error) {
}

return *cors, nil
}

// BuildConfig returns the build settings for this app.
func (i *Instance) BuildConfig() (appfile.BuildCfg, error) {
build, err := appfile.Build(i.root)
if err != nil {
return appfile.BuildCfg{}, err
}

return build, nil
}

func (i *Instance) Watch(fn WatchFunc) (WatchSubscriptionID, error) {
Expand Down
74 changes: 52 additions & 22 deletions cli/daemon/dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"slices"
"strings"
"sync"

"github.com/golang/protobuf/jsonpb"
"github.com/rs/zerolog/log"
Expand All @@ -26,6 +27,7 @@ import (
"encr.dev/internal/version"
"encr.dev/parser/encoding"
"encr.dev/pkg/editors"
"encr.dev/pkg/errinsrc"
"encr.dev/pkg/errlist"
tracepb2 "encr.dev/proto/encore/engine/trace2"
meta "encr.dev/proto/encore/parser/meta/v1"
Expand Down Expand Up @@ -378,6 +380,10 @@ func (s *Server) OnStart(r *run.Run) {
}

func (s *Server) OnCompileStart(r *run.Run) {
lastErrorMu.Lock()
lastError[r.App.PlatformOrLocalID()] = nil
lastErrorMu.Unlock()

status, err := buildAppStatus(r.App, r)
if err != nil {
log.Error().Err(err).Msg("dash: could not build app status")
Expand All @@ -394,6 +400,11 @@ func (s *Server) OnCompileStart(r *run.Run) {

// OnReload notifies active websocket clients about the reloaded run.
func (s *Server) OnReload(r *run.Run) {
// A reload means a successful compile, so clear any previous compile errors.
lastErrorMu.Lock()
lastError[r.App.PlatformOrLocalID()] = nil
lastErrorMu.Unlock()

status, err := buildAppStatus(r.App, r)
if err != nil {
log.Error().Err(err).Msg("dash: could not build app status")
Expand Down Expand Up @@ -430,8 +441,16 @@ func (s *Server) OnStderr(r *run.Run, out []byte) {
s.onOutput(r, out)
}

func (s *Server) OnError(r *run.Run, err *errlist.List) {
if err == nil {
var lastErrorMu sync.Mutex
var lastError = make(map[string]*errlist.List)

func (s *Server) OnError(r *run.Run, compileErr *errlist.List) {
lastErrorMu.Lock()
compileErr.MakeRelative(r.App.Root(), "")
lastError[r.App.PlatformOrLocalID()] = compileErr
lastErrorMu.Unlock()

if compileErr == nil {
return
}

Expand All @@ -441,10 +460,6 @@ func (s *Server) OnError(r *run.Run, err *errlist.List) {
return
}

err.MakeRelative(r.App.Root(), "")

status.CompileError = err.Error()

s.notify(&notification{
Method: "process/compile-error",
Params: status,
Expand Down Expand Up @@ -735,16 +750,17 @@ func makeProtoReplier(rep jsonrpc2.Replier) jsonrpc2.Replier {
//
// It is mirrored in the frontend at src/lib/client/dev-dash-client.ts as `AppStatus`.
type appStatus struct {
Running bool `json:"running"`
AppID string `json:"appID"`
PlatformID string `json:"platformID,omitempty"`
AppRoot string `json:"appRoot"`
PID string `json:"pid,omitempty"`
Meta json.RawMessage `json:"meta,omitempty"`
Addr string `json:"addr,omitempty"`
APIEncoding *encoding.APIEncoding `json:"apiEncoding,omitempty"`
Compiling bool `json:"compiling"`
CompileError string `json:"compileError,omitempty"`
Running bool `json:"running"`
AppID string `json:"appID"`
PlatformID string `json:"platformID,omitempty"`
AppRoot string `json:"appRoot"`
PID string `json:"pid,omitempty"`
Meta json.RawMessage `json:"meta,omitempty"`
Addr string `json:"addr,omitempty"`
APIEncoding *encoding.APIEncoding `json:"apiEncoding,omitempty"`
Compiling bool `json:"compiling"`
CompileError string `json:"compileError,omitempty"`
CompileErrorRaw *errlist.List `json:"compileErrorRaw,omitempty"`
}

func buildAppStatus(app *apps.Instance, runInstance *run.Run) (s appStatus, err error) {
Expand Down Expand Up @@ -778,14 +794,28 @@ func buildAppStatus(app *apps.Instance, runInstance *run.Run) (s appStatus, err
apiEnc = encoding.DescribeAPI(md)
}

lastErrorMu.Lock()
compileErr := lastError[app.PlatformOrLocalID()]
lastErrorMu.Unlock()

errStr := ""
if compileErr != nil && compileErr.List.Len() > 0 {
wasEnabled := errinsrc.ColoursEnabled()
errinsrc.ColoursInErrors(true)
errStr = compileErr.Error()
errinsrc.ColoursInErrors(wasEnabled)
}

// Build the response
resp := appStatus{
Running: false,
AppID: app.PlatformOrLocalID(),
PlatformID: app.PlatformID(),
Meta: json.RawMessage(mdStr),
AppRoot: app.Root(),
APIEncoding: apiEnc,
Running: false,
AppID: app.PlatformOrLocalID(),
PlatformID: app.PlatformID(),
Meta: json.RawMessage(mdStr),
AppRoot: app.Root(),
APIEncoding: apiEnc,
CompileError: errStr,
CompileErrorRaw: compileErr,
}
if runInstance != nil {
resp.Running = true
Expand Down
37 changes: 37 additions & 0 deletions cli/daemon/engine/trace2/sqlite/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ func (s *Store) updateSpanStartIndex(ctx context.Context, meta *trace2.Meta, ev
return nil
}

if span := start.GetGeneric(); span != nil {
_, err := s.db.ExecContext(ctx, `
INSERT INTO trace_span_index (
app_id, trace_id, span_id, span_type, started_at, is_root, service_name, endpoint_name, has_response, test_skipped
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, false, false)
ON CONFLICT (trace_id, span_id) DO UPDATE SET
is_root = excluded.is_root,
service_name = excluded.service_name,
endpoint_name = excluded.endpoint_name,
external_request_id = excluded.external_request_id
`, meta.AppID, encodeTraceID(ev.TraceId), encodeSpanID(ev.SpanId),
tracepbcli.SpanSummary_GENERIC_SPAN, ev.EventTime.AsTime().UnixNano(),
isRoot, "", span.SpanName)
if err != nil {
return errors.Wrap(err, "insert trace span event")
}
return nil
}

return nil
}

Expand Down Expand Up @@ -241,6 +260,24 @@ func (s *Store) updateSpanEndIndex(ctx context.Context, meta *trace2.Meta, ev *t
return nil
}

if req := end.GetGeneric(); req != nil {
_, err := s.db.ExecContext(ctx, `
INSERT INTO trace_span_index (
app_id, trace_id, span_id, span_type, has_response, is_error, duration_nanos
) VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (trace_id, span_id) DO UPDATE SET
has_response = excluded.has_response,
is_error = excluded.is_error,
duration_nanos = excluded.duration_nanos
`, meta.AppID, traceID, spanID,
tracepbcli.SpanSummary_GENERIC_SPAN, true,
end.Error != nil, end.DurationNanos)
if err != nil {
return errors.Wrap(err, "insert trace span event")
}
return nil
}

return nil
}

Expand Down
14 changes: 12 additions & 2 deletions cli/daemon/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,21 @@ func Docker(ctx context.Context, app *apps.Instance, req *daemonpb.ExportRequest
return false, errors.Wrap(err, "get experimental features")
}

buildConfig, err := app.BuildConfig()
if err != nil {
return false, errors.Wrap(err, "get build settings")
}
if req.CgoEnabled {
buildConfig.CgoEnabled = true
}
if params.BaseImageTag != "" {
buildConfig.Docker.BaseImage = params.BaseImageTag
}

vcsRevision := vcs.GetRevision(app.Root())
buildInfo := builder.BuildInfo{
BuildTags: []string{"timetzdata"},
CgoEnabled: req.CgoEnabled,
StaticLink: true,
BuildConfig: buildConfig,
Debug: false,
GOOS: req.Goos,
GOARCH: req.Goarch,
Expand Down
8 changes: 6 additions & 2 deletions cli/daemon/run/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ func (mgr *Manager) Check(ctx context.Context, p CheckParams) (buildDir string,

// TODO: We should check that all secret keys are defined as well.

buildConfig, err := p.App.BuildConfig()
if err != nil {
return "", errors.Wrap(err, "get build settings")
}

vcsRevision := vcs.GetRevision(p.App.Root())
buildInfo := builder.BuildInfo{
BuildTags: builder.LocalBuildTags,
CgoEnabled: true,
StaticLink: false,
BuildConfig: buildConfig,
Debug: false,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
Expand Down
8 changes: 6 additions & 2 deletions cli/daemon/run/exec_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (mgr *Manager) ExecScript(ctx context.Context, p ExecScriptParams) (err err
return err
}

buildConfig, err := p.App.BuildConfig()
if err != nil {
return errors.Wrap(err, "get build settings")
}

rm := infra.NewResourceManager(p.App, mgr.ClusterMgr, p.NS, p.Environ, mgr.DBProxyPort, false)
defer rm.StopAll()

Expand All @@ -77,8 +82,7 @@ func (mgr *Manager) ExecScript(ctx context.Context, p ExecScriptParams) (err err
vcsRevision := vcs.GetRevision(p.App.Root())
buildInfo := builder.BuildInfo{
BuildTags: builder.LocalBuildTags,
CgoEnabled: true,
StaticLink: false,
BuildConfig: buildConfig,
Debug: false,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
Expand Down
8 changes: 6 additions & 2 deletions cli/daemon/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,19 @@ func (r *Run) buildAndStart(ctx context.Context, tracker *optracker.OpTracker, i
return err
}

buildConfig, err := r.App.BuildConfig()
if err != nil {
return errors.Wrap(err, "get build settings")
}

if r.builder == nil {
r.builder = builderimpl.Resolve(expSet)
}

vcsRevision := vcs.GetRevision(r.App.Root())
buildInfo := builder.BuildInfo{
BuildTags: builder.LocalBuildTags,
CgoEnabled: true,
StaticLink: false,
BuildConfig: buildConfig,
Debug: r.Params.Debug,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
Expand Down