trace

package
v1.1.14 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 3 Imported by: 1

README

Example RPC Interceptor

Data flow:

Client add metadata
Server retrieves metadata
package trace

import (
	"context"
	"os"
	"path/filepath"

	"go-micro.dev/v4/server"
    "go-micro.dev/v4/metadata"

	"github.com/xhd2015/xgo/runtime/trace"
)

const X_XGO_TRACE_FILE = "X-Xgo-Trace-File"

func NewTraceInterceptor() server.HandlerWrapper {
	return func(hf server.HandlerFunc) server.HandlerFunc {
		return func(ctx context.Context, req server.Request, rsp interface{}) error {
            // traceFile example: /tmp/some_trace.json
            traceFile, _ := metadata.Get(ctx, X_XGO_TRACE_FILE)
			if traceFile == "" {
				return hf(ctx, req, rsp)
			}

			_, err = trace.Trace(trace.Config{
				OutputFile: traceFile,
			}, req.Body(), func() (interface{}, error) {
				err := hf(ctx, req, rsp)
				return rsp, err
			})
			return err
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Record added in v1.1.0

func Record(v interface{}, pre interface{}, post interface{}) func()

Record setup a recorder for v, with pre-hook and post-hook

func RecordCall added in v1.1.0

func RecordCall(v interface{}, pre interface{}) func()

RecordCall is a convenience function for recording a call to a function with a pre-hook. a practical example would be:

func SetupLog(t *testing.T) {
	trace.RecordCall(log.Errorf, func(ctx context.Context, format string, params []interface{}) {
		t.Logf("ERROR "+format, params...)
	})
}

func RecordResult added in v1.1.0

func RecordResult(v interface{}, post interface{}) func()

RecordResult is a convenience function for recording a result from a function

func Trace added in v1.1.0

func Trace(config Config, request interface{}, fn func() (interface{}, error)) (response interface{}, err error)

the `request` and `response` are only for recording purpose

Types

type Config added in v1.1.0

type Config struct {
	// OnFinish is called when the trace is finished
	OnFinish func(stack stack_model.IStack) `json:"-"`
	// OutputFile specifies the file to save the trace
	// in json format, which can be open by:
	//      xgo tool trace <OutputFile>
	OutputFile string `json:"OutputFile,omitempty"`

	// FilterTrace is called to filter the trace
	FilterTrace func(funcInfo *core.FuncInfo) bool `json:"-"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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