opentracing

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package opentracing is used to initialize the opentracing from the plugin, and supplies the OpenTracing http.RoundTripper and router middleware.

Index

Examples

Constants

This section is empty.

Variables

View Source
var PluginOptGroup = gconf.NewGroup("opentracing.plugin")

PluginOptGroup is the group of the OpenTracing plugin config options.

View Source
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

func HTTPHandler(handler http.Handler, opt *Option) http.Handler

HTTPHandler is the same as OpenTracing, but returns a http.Handler.

func InitOpenTracingFromPlugin

func InitOpenTracingFromPlugin(pluginPath string, config interface{}) (err error)

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:

  1. If config is empty, retry the env variable "OPENTRACING_PLUGIN_CONFIG".
  2. If pluginPath is empty, retry the env variable "OPENTRACING_PLUGIN_PATH".
  3. 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) RoundTrip

func (rt *HTTPRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the interface http.RounderTripper.

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

func (o *Option) GetComponentName(req *http.Request) string

GetComponentName returns ComponentName if it is not empty. Or ComponentNameFunc(req) instead.

func (*Option) GetTracer

func (o *Option) GetTracer() opentracing.Tracer

GetTracer returns the OpenTracing tracker.

func (*Option) Init

func (o *Option) Init()

Init initializes the OpenTracingOption.

Jump to

Keyboard shortcuts

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