Documentation ¶
Overview ¶
Package opentracing is used to initialize the opentracing from the plugin, and supplies the OpenTracing http.RoundTripper and router middleware.
Index ¶
- Variables
- func HTTPHandler(handler http.Handler, opt *Option) http.Handler
- func InitOpenTracingFromPlugin(pluginPath string, config interface{}) (err error)
- func MustInitOpenTracingFromPlugin(pluginPath string, config interface{})
- func OpenTracing(opt *Option) ship.Middleware
- func RegisterPluginOpts()
- type HTTPRoundTripper
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var PluginOptGroup = gconf.NewGroup("opentracing.plugin")
PluginOptGroup is the group of the OpenTracing plugin config options.
var PluginOpts = []gconf.Opt{
gconf.StrOpt("path", "The path of the plugin implementing the OpenTracing tracer."),
gconf.StrOpt("config", "The configuration information of the plugin."),
}
PluginOpts collects the options of the OpenTracing Plugin.
Functions ¶
func HTTPHandler ¶ added in v0.16.0
HTTPHandler is the same as OpenTracing, but returns a http.Handler.
func InitOpenTracingFromPlugin ¶
InitOpenTracingFromPlugin initializes the OpenTracing implementation, which will load the implementation plugin and call the function InitOpenTracing with config.
The plugin must contain the function
func InitOpenTracing(config interface{}) error
Notice:
- If config is empty, retry the env variable "OPENTRACING_PLUGIN_CONFIG".
- If pluginPath is empty, retry the env variable "OPENTRACING_PLUGIN_PATH".
- If pluginPath is empty, it returns nil and does nothing.
func MustInitOpenTracingFromPlugin ¶
func MustInitOpenTracingFromPlugin(pluginPath string, config interface{})
MustInitOpenTracingFromPlugin is the same as InitOpenTracing, but logs the error and exits the program when an error occurs.
func OpenTracing ¶
func OpenTracing(opt *Option) ship.Middleware
OpenTracing is a middleware to support OpenTracing, which extracts the span context from the http request header, creates a new span as the server span from the span context, and put it into the request context.
Example ¶
package main import ( "context" "io/ioutil" "net/http" // tr "github.com/opentracing/opentracing-go" // "github.com/uber/jaeger-client-go/config" "github.com/xgfone/goapp/opentracing" "github.com/xgfone/ship/v3" ) func initOpenTracing() { // cfg, err := config.FromEnv() // if err != nil { // panic(err) // } // cfg.ServiceName = "app1" // tracer, _, err := cfg.NewTracer() // if err != nil { // panic(err) // } // tr.SetGlobalTracer(tracer) } func init() { // Initialize the Jaeger Tracer implementation. initOpenTracing() // HTTPRoundTripper will extract the parent span from the context // of the sent http.Request, then create a new span for the current request. http.DefaultTransport = opentracing.NewHTTPRoundTripper(http.DefaultTransport, nil) } func main() { // Main function app := ship.Default() // OpenTracing middleware extracts the span context from the http request // header, creates a new span as the server parent span from span context // and put it into the request context. app.Use(opentracing.OpenTracing(nil)) app.Route("/app1").GET(func(c *ship.Context) (err error) { // ctx contains the parent span, which is extracted by OpenTracing middleware // from the HTTP request header. ctx := c.Request().Context() data1, err := request(ctx, "http://127.0.0.1:8002/app2") if err != nil { return } data2, err := request(ctx, "http://127.0.0.1:8003/app3") if err != nil { return } return c.Text(200, "app1:%s,%s", data1, data2) }) app.Start(":8001").Wait() } func request(ctx context.Context, url string) (data string, err error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return } // Pass the parent span to the new http.Request. req = req.WithContext(ctx) // http.DefaultTransport will extract the parent span from ctx, // then create a child span for the current request. resp, err := http.DefaultClient.Do(req) if err != nil { return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) return string(body), err }
Output:
func RegisterPluginOpts ¶
func RegisterPluginOpts()
RegisterPluginOpts registers the options of opentracing plugin.
Types ¶
type HTTPRoundTripper ¶
type HTTPRoundTripper struct { http.RoundTripper Option }
HTTPRoundTripper is a RoundTripper to support OpenTracing, which extracts the parent span from the context of the sent http.Request, then creates a new span by the context of the parent span for http.Request.
func NewHTTPRoundTripper ¶
func NewHTTPRoundTripper(rt http.RoundTripper, opt *Option) *HTTPRoundTripper
NewHTTPRoundTripper returns a new HTTPRoundTripper.
func (*HTTPRoundTripper) WrappedRoundTripper ¶
func (rt *HTTPRoundTripper) WrappedRoundTripper() http.RoundTripper
WrappedRoundTripper returns the wrapped http.RoundTripper.
type Option ¶
type Option struct { Tracer opentracing.Tracer // Default: opentracing.GlobalTracer() ComponentName string // Default: use ComponentNameFunc(req) // ComponentNameFunc is used to get the component name if ComponentName // is empty. // // Default: "net/http" ComponentNameFunc func(*http.Request) string // URLTagFunc is used to get the value of the tag "http.url". // // Default: url.String() URLTagFunc func(*url.URL) string // SpanFilter is used to filter the span if returning true. // // Default: return false SpanFilter func(*http.Request) bool // OperationNameFunc is used to the operation name. // // Default: fmt.Sprintf("HTTP %s %s", r.Method, r.URL.Path) OperationNameFunc func(*http.Request) string // SpanObserver is used to do extra things of the span for the request. // // For example, // OpenTracingOption { // SpanObserver: func(*http.Request, opentracing.Span) { // ext.PeerHostname.Set(span, req.Host) // }, // } // // Default: Do nothing. SpanObserver func(*http.Request, opentracing.Span) }
Option is used to configure the OpenTracing middleware and RoundTripper.
func (*Option) GetComponentName ¶
GetComponentName returns ComponentName if it is not empty. Or ComponentNameFunc(req) instead.