Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson 2023-08-25 17:28:22 +08:00
commit 5d2b7df900
3 changed files with 44 additions and 0 deletions

View 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)
}
}

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"fusenapi/server/resource/internal/config" "fusenapi/server/resource/internal/config"
"fusenapi/server/resource/internal/middleware"
"fusenapi/shared" "fusenapi/shared"
"net/http" "net/http"
@ -14,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"github.com/zeromicro/go-zero/rest"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -26,6 +28,7 @@ type ServiceContext struct {
RabbitMq *initalize.RabbitMqHandle RabbitMq *initalize.RabbitMqHandle
AwsSession *session.Session AwsSession *session.Session
Repositories *initalize.Repositories Repositories *initalize.Repositories
Tracing rest.Middleware
} }
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
@ -43,6 +46,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
BLMServiceUrl: &c.BLMService.Url, BLMServiceUrl: &c.BLMService.Url,
AwsSession: session.Must(session.NewSession(&config)), AwsSession: session.Must(session.NewSession(&config)),
}), }),
Tracing: middleware.NewTracingMiddleware().Handle,
} }
} }

View File

@ -9,6 +9,10 @@ info (
import "basic.api" import "basic.api"
// @server(
// middleware: Tracing // 路由中间件声明
// )
service resource { service resource {
@handler LogoCombineHandler @handler LogoCombineHandler
post /api/resource/logo-combine(LogoCombineReq) returns (response); post /api/resource/logo-combine(LogoCombineReq) returns (response);