gootelautoinstrument

package module
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

moonjectlog

Inject fmt.Println at the beginning of each function call.

Usage
  1. Install moonjectlog with:
go install github.com/pijng/moonjectlog@latest
  1. Build your project with go build while specifying moonjectlog preprocessor:
go build -o output -a -toolexec="moonjectlog <absolute/path/to/project>" main.go

Important:

  • -a flag is required to recompile all your project, otherwise go compiler might do nothing and use cached build
  • <absolute/path/to/project> is and absolute path to the root of your project. If you run go build from the root – simply specify $PWD as an argument.
  1. Run the final binary:
./output
Demonstration

Suppose we have this code:

package main

import "fmt"

func main() {
	fmt.Println(someWork())
	fmt.Println(anotherWork())
	fmt.Println(finalWork())

	fmt.Println("End of main")
}

func someWork() int {
	return 1
}

func anotherWork() int {
	return 2
}

func finalWork() int {
	return 3
}

If we compile and run it, we get the expected output:

$ go build main.go
$ ./main
1
2
3
End of main
$

But if we apply moonjectlog as a preprocessor at compile time, we get the following result:

$ go build -a -toolexec="moonjectlog $PWD" main.go
$ ./main
Calling [main] func
Calling [someWork] func
1
Calling [anotherWork] func
2
Calling [finalWork] func
3
End of main
$

Thus, the preprocessor added a call to fmt.Println("Calling [%s] func") to the body of each function, while the source code remained unchanged.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartSpan

func StartSpan(name string) trace.Span

func StartSpanCtx

func StartSpanCtx(ctx context.Context, name string) trace.Span

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL