2020-08-10 15:04:57 +00:00
|
|
|
// Copyright 2020 beego
|
2020-08-04 14:16:55 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package opentracing
|
|
|
|
|
|
|
|
import (
|
2020-08-09 14:59:08 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
logKit "github.com/go-kit/kit/log"
|
|
|
|
opentracingKit "github.com/go-kit/kit/tracing/opentracing"
|
2020-08-04 14:16:55 +00:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
|
|
|
|
beego "github.com/astaxie/beego/pkg"
|
2020-08-09 14:59:08 +00:00
|
|
|
beegoCtx "github.com/astaxie/beego/pkg/context"
|
2020-08-04 14:16:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FilterChainBuilder provides an extension point that we can support more configurations if necessary
|
|
|
|
type FilterChainBuilder struct {
|
|
|
|
// CustomSpanFunc makes users to custom the span.
|
2020-08-09 14:59:08 +00:00
|
|
|
CustomSpanFunc func(span opentracing.Span, ctx *beegoCtx.Context)
|
2020-08-04 14:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (builder *FilterChainBuilder) FilterChain(next beego.FilterFunc) beego.FilterFunc {
|
2020-08-09 14:59:08 +00:00
|
|
|
return func(ctx *beegoCtx.Context) {
|
|
|
|
var (
|
|
|
|
spanCtx context.Context
|
|
|
|
span opentracing.Span
|
|
|
|
)
|
2020-08-09 12:05:10 +00:00
|
|
|
operationName := builder.operationName(ctx)
|
2020-08-05 06:34:54 +00:00
|
|
|
|
2020-08-09 14:59:08 +00:00
|
|
|
if preSpan := opentracing.SpanFromContext(ctx.Request.Context()); preSpan == nil {
|
|
|
|
inject := opentracingKit.HTTPToContext(opentracing.GlobalTracer(), operationName, logKit.NewNopLogger())
|
|
|
|
spanCtx = inject(ctx.Request.Context(), ctx.Request)
|
|
|
|
span = opentracing.SpanFromContext(spanCtx)
|
|
|
|
} else {
|
|
|
|
span, spanCtx = opentracing.StartSpanFromContext(ctx.Request.Context(), operationName)
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:16:55 +00:00
|
|
|
defer span.Finish()
|
2020-08-09 12:05:10 +00:00
|
|
|
|
|
|
|
newReq := ctx.Request.Clone(spanCtx)
|
|
|
|
ctx.Reset(ctx.ResponseWriter.ResponseWriter, newReq)
|
|
|
|
|
2020-08-04 14:16:55 +00:00
|
|
|
next(ctx)
|
|
|
|
// if you think we need to do more things, feel free to create an issue to tell us
|
2020-08-10 15:04:57 +00:00
|
|
|
span.SetTag("http.status_code", ctx.ResponseWriter.Status)
|
|
|
|
span.SetTag("http.method", ctx.Input.Method())
|
|
|
|
span.SetTag("peer.hostname", ctx.Request.Host)
|
|
|
|
span.SetTag("http.url", ctx.Request.URL.String())
|
|
|
|
span.SetTag("http.scheme", ctx.Request.URL.Scheme)
|
|
|
|
span.SetTag("span.kind", "server")
|
|
|
|
span.SetTag("component", "beego")
|
|
|
|
if ctx.Output.IsServerError() || ctx.Output.IsClientError() {
|
|
|
|
span.SetTag("error", true)
|
|
|
|
}
|
|
|
|
span.SetTag("peer.address", ctx.Request.RemoteAddr)
|
|
|
|
span.SetTag("http.proto", ctx.Request.Proto)
|
|
|
|
|
|
|
|
span.SetTag("beego.route", ctx.Input.GetData("RouterPattern"))
|
|
|
|
|
2020-08-04 14:16:55 +00:00
|
|
|
if builder.CustomSpanFunc != nil {
|
|
|
|
builder.CustomSpanFunc(span, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-09 12:05:10 +00:00
|
|
|
|
2020-08-09 14:59:08 +00:00
|
|
|
func (builder *FilterChainBuilder) operationName(ctx *beegoCtx.Context) string {
|
2020-08-09 12:05:10 +00:00
|
|
|
operationName := ctx.Input.URL()
|
|
|
|
// it means that there is not any span, so we create a span as the root span.
|
|
|
|
// TODO, if we support multiple servers, this need to be changed
|
|
|
|
route, found := beego.BeeApp.Handlers.FindRouter(ctx)
|
|
|
|
if found {
|
2020-08-10 15:04:57 +00:00
|
|
|
operationName = ctx.Input.Method() + "#" + route.GetPattern()
|
2020-08-09 12:05:10 +00:00
|
|
|
}
|
|
|
|
return operationName
|
|
|
|
}
|