Skip to content

Print instrumented code #571

Description

@keisku

It's really useful to make sure how orchestrion instrument the code.

Update this code

// writeModifiedFile writes the modified file to disk after having restored it to Go source code,
// and returns the path to the modified file.
func (i *Injector) writeModifiedFile(ctx context.Context, decorator *decorator.Decorator, file *dst.File) (string, error) {
log := zerolog.Ctx(ctx)
filename := decorator.Filenames[file]
log.Trace().Str("path", filename).Msg("Writing modified file")
canonicalizeImports(ctx, file)
if err := lineinfo.AnnotateMovedNodes(decorator, file, i.newRestorer); err != nil {
return filename, fmt.Errorf("annotating moved nodes in %q: %w", filename, err)
}
restorer := i.newRestorer(filename)
astFile, err := restorer.RestoreFile(file)
if err != nil {
return filename, fmt.Errorf("restoring %q: %w", filename, err)
}
var buf bytes.Buffer
if err := format.Node(&buf, restorer.Fset, astFile); err != nil {
return filename, fmt.Errorf("formatting %q: %w", filename, err)
}
if i.ModifiedFile != nil {
filename = i.ModifiedFile(filename)
dir := filepath.Dir(filename)
if err := os.MkdirAll(dir, 0o755); err != nil {
return filename, fmt.Errorf("mkdir %q: %w", dir, err)
}
}
if err := os.WriteFile(filename, postProcess(buf.Bytes()), 0o644); err != nil {
return filename, fmt.Errorf("writing %q: %w", filename, err)
}
return filename, nil
}

like this code.

	var buf bytes.Buffer
	if err := format.Node(&buf, restorer.Fset, astFile); err != nil {
		return filename, fmt.Errorf("formatting %q: %w", filename, err)
	}
	fmt.Printf("%s:\n%s\n", filename, buf.String())

Example output:

// ....skip.....

/home/ubuntu/go/pkg/mod/github.com/aws/aws-sdk-go-v2/config@v1.22.1/resolve.go:
//line /home/ubuntu/go/pkg/mod/github.com/aws/aws-sdk-go-v2/[email protected]/resolve.go:1:1
package config

import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"

	"github.com/aws/aws-sdk-go-v2/aws"
	awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
	"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
	"github.com/aws/smithy-go/logging"
	//line <generated>:1
	__orchestrion_awstrace "github.com/DataDog/dd-trace-go/contrib/aws/aws-sdk-go-v2/v2/aws"
)

// resolveDefaultAWSConfig will write default configuration values into the cfg
// value. It will write the default values, overwriting any previous value.
//
// This should be used as the first resolver in the slice of resolvers when
// resolving external configuration.
//
//line /home/ubuntu/go/pkg/mod/github.com/aws/aws-sdk-go-v2/[email protected]/resolve.go:23
func resolveDefaultAWSConfig(ctx context.Context, cfg *aws.Config, cfgs configs) error {
	var sources []interface{}
	for _, s := range cfgs {
		sources = append(sources, s)
	}

	*cfg =
		//line <generated>:1
		func(cfg aws.Config) aws.Config {
			__orchestrion_awstrace.AppendMiddleware(&cfg)
			return cfg
		}(
			//line /home/ubuntu/go/pkg/mod/github.com/aws/aws-sdk-go-v2/[email protected]/resolve.go:29
			aws.Config{
				Credentials:   aws.AnonymousCredentials{},
				Logger:        logging.NewStandardLogger(os.Stderr),
				ConfigSources: sources,
			})
	return nil
}

// ....skip.....

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions