orchestrion icon indicating copy to clipboard operation
orchestrion copied to clipboard

Print instrumented code

Open keisku opened this issue 8 months ago • 3 comments

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

Update this code

https://github.com/DataDog/orchestrion/blob/d1fa0407b5e462fb325eeceb8ebb00e3875b644b/internal/injector/write.go#L22-L60

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/[email protected]/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.....

keisku avatar Mar 12 '25 05:03 keisku

Having the ability to generate the instrumented code would be great for projects that don't rely on go build (i.e. Bazel's rules_go). That way, we can generate the instrumented source code in a separate build step, independently from invoking go tool ....

sluongng avatar Mar 21 '25 08:03 sluongng

Just an idea, how about adding ORCHESTRION_MODIFIED_FILE / --modified-file to specify the file path that orchestrion writes modified Go code? This is like the following option.

https://github.com/DataDog/orchestrion/blob/6f44b4466971e07f486aab5f449ec32d4f79701a/_docs/content/docs/troubleshooting.md?plain=1#L101-L117

keisku avatar Mar 24 '25 08:03 keisku

https://github.com/DataDog/orchestrion/pull/579 is trying to solve this issue.

keisku avatar Mar 24 '25 13:03 keisku