Skip to content

Commit

Permalink
change README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
icowan committed Oct 27, 2021
1 parent 51c9c9c commit 67ba8a9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func Run() {
ctx = context.WithValue(ctx, "token-context", token)
return ctx
}),
//kithttp.ServerBefore(middleware.TracingServerBefore(tracer)),
kithttp.ServerBefore(middleware.TracingServerBefore(tracer)),
}

ems := []endpoint.Middleware{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.10.0 // indirect
Expand Down
88 changes: 88 additions & 0 deletions src/middleware/tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @Time: 2020/12/28 23:23
* @Author: [email protected]
* @File: tracing
* @Software: GoLand
*/

package middleware

import (
"context"
"encoding/json"
"net/http"
"net/url"

"github.com/go-kit/kit/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
stdopentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go"
)

func TracingServerBefore(tracer stdopentracing.Tracer) kithttp.RequestFunc {
return func(ctx context.Context, request *http.Request) context.Context {
if tracer == nil {
return ctx
}
reqPath := ctx.Value(kithttp.ContextKeyRequestURI).(string)
u, _ := url.Parse(reqPath)
span, ctx := stdopentracing.StartSpanFromContextWithTracer(ctx, tracer, u.Path, stdopentracing.Tag{
Key: string(ext.Component),
Value: "ServerBefore",
}, stdopentracing.Tag{
Key: string(ext.HTTPMethod),
Value: ctx.Value(kithttp.ContextKeyRequestMethod),
}, stdopentracing.Tag{
Key: string(ext.HTTPUrl),
Value: ctx.Value(kithttp.ContextKeyRequestURI),
}, stdopentracing.Tag{
Key: "user_agent",
Value: ctx.Value(kithttp.ContextKeyRequestUserAgent),
}, stdopentracing.Tag{
Key: "x-request-id",
Value: ctx.Value(kithttp.ContextKeyRequestXRequestID),
}, stdopentracing.Tag{
Key: "Authorization",
Value: ctx.Value(kithttp.ContextKeyRequestAuthorization),
}, stdopentracing.Tag{
Key: "referer",
Value: ctx.Value(kithttp.ContextKeyRequestReferer),
}, stdopentracing.Tag{
Key: "x-forwarded-for",
Value: ctx.Value(kithttp.ContextKeyRequestXForwardedFor),
})
traceId := span.Context().(jaeger.SpanContext).TraceID().String()
ctx = context.WithValue(ctx, "TraceId", traceId)
ctx = context.WithValue(ctx, "traceId", traceId)
span = span.SetTag("TraceId", span.Context().(jaeger.SpanContext).TraceID().String())

defer func() {
b, _ := json.Marshal(request)
span.LogKV("request", string(b))
span.Finish()
}()
return ctx
}
}

func TracingMiddleware(tracer stdopentracing.Tracer) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
if tracer == nil {
return next(ctx, request)
}
span, ctx := stdopentracing.StartSpanFromContextWithTracer(ctx, tracer, "TracingMiddleware", stdopentracing.Tag{
Key: string(ext.Component),
Value: "Middleware",
})
defer func() {
b, _ := json.Marshal(request)
res, _ := json.Marshal(response)
span.LogKV("request", string(b), "response", string(res), "err", err)
span.Finish()
}()
return next(ctx, request)
}
}
}

0 comments on commit 67ba8a9

Please sign in to comment.