Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop
This commit is contained in:
commit
5d2b7df900
36
server/resource/internal/middleware/tracingmiddleware.go
Normal file
36
server/resource/internal/middleware/tracingmiddleware.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type TracingMiddleware struct {
|
||||
}
|
||||
|
||||
func NewTracingMiddleware() *TracingMiddleware {
|
||||
return &TracingMiddleware{}
|
||||
}
|
||||
|
||||
func (m *TracingMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
traceId := r.Header.Get("X-Trace-Id")
|
||||
spanId := r.Header.Get("X-Span-Id")
|
||||
if traceId != "" && spanId != "" {
|
||||
TraceID, _ := trace.TraceIDFromHex(traceId)
|
||||
SpanID, _ := trace.SpanIDFromHex(spanId)
|
||||
ctx := r.Context()
|
||||
sc := trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: TraceID,
|
||||
SpanID: SpanID,
|
||||
Remote: true,
|
||||
})
|
||||
ctx = trace.ContextWithRemoteSpanContext(ctx, sc)
|
||||
//next.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
|
||||
// 处理请求
|
||||
next(w, r)
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/resource/internal/config"
|
||||
"fusenapi/server/resource/internal/middleware"
|
||||
"fusenapi/shared"
|
||||
"net/http"
|
||||
|
||||
|
@ -14,6 +15,7 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -26,6 +28,7 @@ type ServiceContext struct {
|
|||
RabbitMq *initalize.RabbitMqHandle
|
||||
AwsSession *session.Session
|
||||
Repositories *initalize.Repositories
|
||||
Tracing rest.Middleware
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
@ -43,6 +46,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
|||
BLMServiceUrl: &c.BLMService.Url,
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
}),
|
||||
Tracing: middleware.NewTracingMiddleware().Handle,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,10 @@ info (
|
|||
|
||||
import "basic.api"
|
||||
|
||||
// @server(
|
||||
// middleware: Tracing // 路由中间件声明
|
||||
// )
|
||||
|
||||
service resource {
|
||||
@handler LogoCombineHandler
|
||||
post /api/resource/logo-combine(LogoCombineReq) returns (response);
|
||||
|
|
Loading…
Reference in New Issue
Block a user