nrseg

package module
v0.0.0-...-671eb86 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 17 Imported by: 0

README

nrseg

Go Reference MIT License test reviewdog

Background

https://docs.newrelic.com/docs/agents/go-agent/instrumentation/instrument-go-segments

NewRelic is excellent o11y service, but if we use Newrelic in Go app, we need to insert segment into every function/method to measure the time taken by functions and code blocks. For example, we can use with newrelic.FromContext and defer statement.

func SampleFunc(ctx context.Context) {
  defer newrelic.FromContext(ctx).StartSegment("sample_func").End()
  // do anything...
}

If there is your application in production already, you must add a segment into any function/method. It is a very time-consuming and tedious task.

Description

nrseg is cli tool for insert segment into all function/method in specified directory.

Before code is below,

package input

import (
  "context"
  "fmt"
  "net/http"
)

type FooBar struct{}

func (f *FooBar) SampleMethod(ctx context.Context) {
  fmt.Println("Hello, playground")
  fmt.Println("end function")
}

func SampleFunc(ctx context.Context) {
  fmt.Println("Hello, playground")
  fmt.Println("end function")
}

func SampleHandler(w http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(w, "Hello, %q", req.URL.Path)
}

// nrseg:ignore you can be ignored if you want to not insert segment.
func IgnoreHandler(w http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(w, "Hello, %q", req.URL.Path)
}

After execute nrseg, modified code is below.

package input

import (
  "context"
  "fmt"
  "net/http"

  "github.com/newrelic/go-agent/v3/newrelic"
)

type FooBar struct{}

func (f *FooBar) SampleMethod(ctx context.Context) {
  defer newrelic.FromContext(ctx).StartSegment("foo_bar_sample_method").End()
  fmt.Println("Hello, playground")
  fmt.Println("end function")
}

func SampleFunc(ctx context.Context) {
  defer newrelic.FromContext(ctx).StartSegment("sample_func").End()
  fmt.Println("Hello, playground")
  fmt.Println("end function")
}

func SampleHandler(w http.ResponseWriter, req *http.Request) {
  defer newrelic.FromContext(req.Context()).StartSegment("sample_handler").End()
  fmt.Fprintf(w, "Hello, %q", req.URL.Path)
}

// nrseg:ignore you can be ignored if you want to not insert segment.
func IgnoreHandler(w http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(w, "Hello, %q", req.URL.Path)
}
Features
  • Insert Function segments into the function with following arguments.
    • The function/method signature has context.Context.
      • defer newrelic.FromContext(ctx).StartSegment("func_name").End()
    • The function/method signature has *http.Request.
      • defer newrelic.FromContext(req.Context()).StartSegment("func_name").End()
  • Support any variable name of context.Context/*http.Request.
  • Support import alias of "context"/"net/http".
  • Use function/method name to segment name.
  • This processing is recursively repeated.
  • Able to ignore function/method by nrseg:ignore comment.
  • Ignore specified directories with cli option -i/-ignore.
  • Remove all Function segments
  • Add: dry-run option
  • Validate: Show a function that doesn't call the segment.
  • Support anonymous function

Synopsis

$ nrseg -i testuitl ./

Options

$ nrseg -h
Insert function segments into any function/method for Newrelic APM.

Usage of nrseg:
  -i string
        ignore directory names. ex: foo,bar,baz
        (testdata directory is always ignored.)
  -ignore string
        ignore directory names. ex: foo,bar,baz
        (testdata directory is always ignored.)
  -v    print version information and quit.
  -version
        print version information and quit.
exit status 1

Limitation

nrseg inserts only function segments, so we need the initialize of Newrelic manually.

If we want to adopt Newrelic to our application, , we write initialize, and newrelic.WrapHandleFunc manually before execute this tool.

app, err := newrelic.NewApplication(
		newrelic.ConfigAppName("my_application"),
		newrelic.ConfigLicense(newrelicLicenseKey),
		newrelic.ConfigDistributedTracerEnabled(true),
	)

Installation

$ go install github.com/yumemi-inc/nrseg/cmd/nrseg

Built binaries are available on github releases. https://github.com/yumemi-inc/nrseg/releases

MacOS

If you want to install on MacOS, you can use Homebrew.

brew install budougumi0617/tap/nrseg

Contribution

  1. Fork (https://github.com/yumemi-inc/nrseg/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

License

MIT

Author

budougumi0617

Documentation

Index

Constants

View Source
const (
	TypeContext     = "context.Context"
	TypeHttpRequest = "*http.Request"
	TypeUnknown     = "Unknown"
)
View Source
const NewRelicV3Pkg = "github.com/newrelic/go-agent/v3/newrelic"

Variables

View Source
var (
	// ErrShowVersion returns when set version flag.
	ErrShowVersion = errors.New("show version")
	ErrFlagTrue    = errors.New("find error")
)
View Source
var ErrNoImportNrPkg = errors.New("not import newrelic pkg")

Functions

func Process

func Process(filename string, src []byte) ([]byte, error)

func Run

func Run(args []string, outStream, errStream io.Writer, version, revision string) error

Run is entry point.

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