Codebase list golang-github-opentracing-contrib-go-stdlib / 8e85eba
allow user to set component name mqliang 6 years ago
2 changed file(s) with 43 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
1717 const (
1818 keyTracer contextKey = iota
1919 )
20
21 const defaultComponentName = "net/http"
2022
2123 // Transport wraps a RoundTripper. If a request is being traced with
2224 // Tracer, Transport will inject the current span into the headers,
2830 }
2931
3032 type clientOptions struct {
31 opName string
33 operationName string
34 componentName string
3235 disableClientTrace bool
3336 }
3437
3740
3841 // OperationName returns a ClientOption that sets the operation
3942 // name for the client-side span.
40 func OperationName(opName string) ClientOption {
43 func OperationName(operationName string) ClientOption {
4144 return func(options *clientOptions) {
42 options.opName = opName
45 options.operationName = operationName
46 }
47 }
48
49 // ComponentName returns a ClientOption that sets the component
50 // name for the client-side span.
51 func ComponentName(componentName string) ClientOption {
52 return func(options *clientOptions) {
53 options.componentName = componentName
4354 }
4455 }
4556
149160 if parent != nil {
150161 spanctx = parent.Context()
151162 }
152 opName := h.opts.opName
153 if opName == "" {
154 opName = "HTTP Client"
163 operationName := h.opts.operationName
164 if operationName == "" {
165 operationName = "HTTP Client"
155166 }
156 root := h.tr.StartSpan(opName, opentracing.ChildOf(spanctx))
167 root := h.tr.StartSpan(operationName, opentracing.ChildOf(spanctx))
157168 h.root = root
158169 }
159170
160171 ctx := h.root.Context()
161172 h.sp = h.tr.StartSpan("HTTP "+req.Method, opentracing.ChildOf(ctx))
162173 ext.SpanKindRPCClient.Set(h.sp)
163 ext.Component.Set(h.sp, "net/http")
174
175 componentName := h.opts.componentName
176 if componentName == "" {
177 componentName = defaultComponentName
178 }
179 ext.Component.Set(h.sp, componentName)
164180
165181 return h.sp
166182 }
1919 }
2020
2121 type mwOptions struct {
22 opNameFunc func(r *http.Request) string
22 opNameFunc func(r *http.Request) string
23 componentName string
2324 }
2425
25 // MWOption contols the behavior of the Middleware.
26 // MWOption controls the behavior of the Middleware.
2627 type MWOption func(*mwOptions)
2728
2829 // OperationNameFunc returns a MWOption that uses given function f
3031 func OperationNameFunc(f func(r *http.Request) string) MWOption {
3132 return func(options *mwOptions) {
3233 options.opNameFunc = f
34 }
35 }
36
37 // MWComponentName returns a MWOption that sets the component name
38 // name for the server-side span.
39 func MWComponentName(componentName string) MWOption {
40 return func(options *mwOptions) {
41 options.componentName = componentName
3342 }
3443 }
3544
6675 sp := tr.StartSpan(opts.opNameFunc(r), ext.RPCServerOption(ctx))
6776 ext.HTTPMethod.Set(sp, r.Method)
6877 ext.HTTPUrl.Set(sp, r.URL.String())
69 ext.Component.Set(sp, "net/http")
78
79 // set component name, use "net/http" if caller does not specify
80 componentName := opts.componentName
81 if componentName == "" {
82 componentName = defaultComponentName
83 }
84 ext.Component.Set(sp, componentName)
85
7086 w = &statusCodeTracker{w, 200}
7187 r = r.WithContext(opentracing.ContextWithSpan(r.Context(), sp))
7288